-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathtest_e2e.py
More file actions
213 lines (166 loc) · 6.66 KB
/
Copy pathtest_e2e.py
File metadata and controls
213 lines (166 loc) · 6.66 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
"""End-to-end integration tests including self-testing."""
import os
import pytest
from archunitpython import (
assert_passes,
clear_graph_cache,
format_violations,
metrics,
project_files,
project_graph,
project_layers,
)
from archunitpython.files.assertion.cycle_free import ViolatingCycle
FIXTURES_DIR = os.path.join(
os.path.dirname(os.path.dirname(__file__)), "fixtures", "sample_project"
)
SRC_DIR = os.path.join(
os.path.dirname(os.path.dirname(os.path.dirname(__file__))), "src", "archunitpython"
)
class TestTopLevelImports:
"""Verify all top-level imports work."""
def test_import_project_files(self):
from archunitpython import project_files
assert callable(project_files)
def test_import_project_slices(self):
from archunitpython import project_slices
assert callable(project_slices)
def test_import_project_graph(self):
assert callable(project_graph)
def test_import_project_layers(self):
assert callable(project_layers)
def test_import_rules_from_config(self):
from archunitpython import rules_from_config
assert callable(rules_from_config)
def test_import_metrics(self):
from archunitpython import metrics
assert callable(metrics)
def test_import_assert_passes(self):
from archunitpython import assert_passes
assert callable(assert_passes)
def test_import_format_violations(self):
from archunitpython import format_violations
assert callable(format_violations)
def test_version(self):
import archunitpython
assert archunitpython.__version__
class TestAssertPasses:
def setup_method(self):
clear_graph_cache()
def test_passing_rule(self):
rule = project_files(FIXTURES_DIR).should().have_no_cycles()
assert_passes(rule)
def test_failing_rule_raises(self):
# controllers should not depend on services (but they do)
rule = (
project_files(FIXTURES_DIR)
.in_folder("**/controllers*")
.should_not()
.depend_on_files()
.in_folder("**/services*")
)
with pytest.raises(AssertionError, match="architecture violation"):
assert_passes(rule)
class TestFormatViolations:
def test_no_violations(self):
result = format_violations([])
assert "No violations" in result
def test_with_violations(self):
from archunitpython.common.projection.types import ProjectedEdge
violation = ViolatingCycle(
cycle=[
ProjectedEdge(source_label="a.py", target_label="b.py"),
ProjectedEdge(source_label="b.py", target_label="a.py"),
]
)
result = format_violations([violation])
assert "1 architecture violation" in result
assert "Circular dependency" in result
class TestSelfTesting:
"""ArchUnitPython tests its own architecture."""
def setup_method(self):
clear_graph_cache()
def test_no_cycles_in_library(self):
"""The library itself should have no circular dependencies."""
if not os.path.isdir(SRC_DIR):
pytest.skip("Source directory not found")
rule = project_files(SRC_DIR).should().have_no_cycles()
violations = rule.check()
cycle_violations = [v for v in violations if isinstance(v, ViolatingCycle)]
assert len(cycle_violations) == 0, format_violations(cycle_violations)
def test_common_does_not_depend_on_files(self):
"""common/ should not import from files/."""
if not os.path.isdir(SRC_DIR):
pytest.skip("Source directory not found")
rule = (
project_files(SRC_DIR)
.in_folder("**/archunitpython/common*")
.should_not()
.depend_on_files()
.in_folder("**/archunitpython/files*")
)
violations = rule.check()
from archunitpython.files.assertion.depend_on_files import ViolatingFileDependency
dep_v = [v for v in violations if isinstance(v, ViolatingFileDependency)]
assert len(dep_v) == 0, format_violations(dep_v)
def test_common_does_not_depend_on_slices(self):
"""common/ should not import from slices/."""
if not os.path.isdir(SRC_DIR):
pytest.skip("Source directory not found")
rule = (
project_files(SRC_DIR)
.in_folder("**/archunitpython/common*")
.should_not()
.depend_on_files()
.in_folder("**/archunitpython/slices*")
)
violations = rule.check()
from archunitpython.files.assertion.depend_on_files import ViolatingFileDependency
dep_v = [v for v in violations if isinstance(v, ViolatingFileDependency)]
assert len(dep_v) == 0, format_violations(dep_v)
def test_common_does_not_depend_on_testing(self):
"""common/ should not import from testing/."""
if not os.path.isdir(SRC_DIR):
pytest.skip("Source directory not found")
rule = (
project_files(SRC_DIR)
.in_folder("**/archunitpython/common*")
.should_not()
.depend_on_files()
.in_folder("**/archunitpython/testing*")
)
violations = rule.check()
from archunitpython.files.assertion.depend_on_files import ViolatingFileDependency
dep_v = [v for v in violations if isinstance(v, ViolatingFileDependency)]
assert len(dep_v) == 0, format_violations(dep_v)
class TestFullWorkflow:
"""Complete workflow tests combining multiple features."""
def setup_method(self):
clear_graph_cache()
def test_layered_architecture_check(self):
"""Verify controllers → services → models pattern."""
# controllers should NOT depend on models directly
rule = (
project_files(FIXTURES_DIR)
.in_folder("**/controllers*")
.should_not()
.depend_on_files()
.in_folder("**/models*")
)
violations = rule.check()
from archunitpython.files.assertion.depend_on_files import ViolatingFileDependency
dep_v = [v for v in violations if isinstance(v, ViolatingFileDependency)]
assert len(dep_v) == 0
def test_metrics_on_sample(self):
rule = metrics(FIXTURES_DIR).count().method_count().should_be_below(100)
assert_passes(rule)
def test_custom_condition(self):
rule = (
project_files(FIXTURES_DIR)
.should()
.adhere_to(
lambda f: f.lines_of_code < 5000,
"Files should be under 5000 lines",
)
)
assert_passes(rule)