Skip to content

Commit d911ef4

Browse files
test: simplify test
1 parent 32a2b36 commit d911ef4

1 file changed

Lines changed: 18 additions & 41 deletions

File tree

tests/validate/test_facades.py

Lines changed: 18 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,13 @@
22
# Licensed under the Apache V2, see LICENCE file for details.
33

44
import importlib
5-
import re
5+
from collections import defaultdict
66
from pathlib import Path
7-
from typing import Dict, List, TypedDict, cast
7+
from typing import Dict, List, TypedDict
88

99
import pytest
1010

11-
from juju.client import connection, _definitions
11+
from juju.client.connection import client_facades, excluded_facades
1212

1313

1414
class Versions(TypedDict, total=True):
@@ -23,52 +23,29 @@ def project_root(pytestconfig: pytest.Config) -> Path:
2323
return pytestconfig.rootpath
2424

2525

26-
def test_facade_version_matches_filename(project_root: Path) -> None:
27-
ignore_names = dir(_definitions)
26+
@pytest.fixture
27+
def generated_code_facades(project_root: Path) -> ClientFacades:
28+
"""Return a client_facades dictionary from generated code under project_root.
29+
"""
30+
facades: Dict[str, List[int]] = defaultdict(list)
2831
for file in (project_root / 'juju' / 'client').glob('_client[0-9]*.py'):
29-
match = re.search('_client([0-9]+).py', file.name)
30-
assert match
31-
version = int(match.group(1))
3232
module = importlib.import_module(f'juju.client.{file.stem}')
3333
for cls_name in dir(module):
34-
if cls_name.startswith('_') or cls_name in ignore_names:
34+
cls = getattr(module, cls_name)
35+
try:
36+
cls.name
37+
cls.version
38+
except AttributeError:
3539
continue
36-
assert getattr(module, cls_name).version == version
37-
38-
39-
def test_class_name_matches_facade_name(project_root: Path) -> None:
40-
ignore_names = dir(_definitions)
41-
for file in (project_root / 'juju' / 'client').glob('_client[0-9]*.py'):
42-
module = importlib.import_module(f'juju.client.{file.stem}')
43-
for cls_name in dir(module):
44-
if cls_name.startswith('_') or cls_name in ignore_names:
40+
if cls.version in excluded_facades.get(cls.name, []):
4541
continue
46-
assert getattr(module, cls_name).name == cls_name.removesuffix('Facade')
42+
facades[cls.name].append(cls.version)
43+
return {name: {'versions': sorted(facades[name])} for name in sorted(facades)}
4744

4845

49-
def test_client_facades_matches_generated_code(project_root: Path) -> None:
50-
client_facades = cast(ClientFacades, connection.client_facades)
51-
expected_facades = make_client_facades_from_generated_code(project_root)
46+
def test_client_facades(project_root: Path, generated_code_facades: ClientFacades) -> None:
5247
assert {
5348
k: v['versions'] for k, v in client_facades.items()
5449
} == {
55-
k: v['versions'] for k, v in expected_facades.items()
50+
k: v['versions'] for k, v in generated_code_facades.items()
5651
}
57-
58-
59-
def make_client_facades_from_generated_code(project_root: Path) -> ClientFacades:
60-
"""Return a client_facades dictionary from generated code under project_root.
61-
"""
62-
ignore_names = dir(_definitions)
63-
excluded_facades = connection.excluded_facades
64-
facades: Dict[str, List[int]] = {}
65-
for file in (project_root / 'juju' / 'client').glob('_client[0-9]*.py'):
66-
module = importlib.import_module(f'juju.client.{file.stem}')
67-
for cls_name in dir(module):
68-
if cls_name.startswith('_') or cls_name in ignore_names:
69-
continue
70-
cls = getattr(module, cls_name)
71-
if cls.version in excluded_facades.get(cls.name, []):
72-
continue
73-
facades.setdefault(cls.name, []).append(cls.version)
74-
return {name: {'versions': sorted(facades[name])} for name in sorted(facades)}

0 commit comments

Comments
 (0)