|
3 | 3 | import os |
4 | 4 |
|
5 | 5 | import pytest |
| 6 | +from rule_assessment.test_rule_matcher import ( |
| 7 | + ALL_MODULES, |
| 8 | + MODULE_1, |
| 9 | + MODULE_2, |
| 10 | + MODULE_3, |
| 11 | +) |
6 | 12 |
|
7 | 13 | from integration.interesting_rules_for_tests import ( |
8 | 14 | A, |
|
11 | 17 | single_rule_subject_single_rule_object_error_message_test_cases, |
12 | 18 | ) |
13 | 19 | from pytestarch import EvaluableArchitecture, Rule, get_evaluable_architecture |
| 20 | +from pytestarch.eval_structure.evaluable_graph import EvaluableArchitectureGraph |
| 21 | +from pytestarch.eval_structure.networkxgraph import NetworkxGraph |
| 22 | +from pytestarch.eval_structure_generation.file_import.import_types import AbsoluteImport |
14 | 23 | from resources import nested_root_module_mismatch_project, root_module_mismatch_project |
15 | 24 | from resources.nested_root_module_mismatch_project.dir1.dir2 import nested_app |
16 | 25 | from resources.root_module_mismatch_project import app |
@@ -183,3 +192,267 @@ def test_same_submodule_as_importer_shows_same_behavior( |
183 | 192 | rule_with_submodule.assert_applies(graph_based_on_string_module_names) |
184 | 193 |
|
185 | 194 | assert str(e1.value) == str(e2.value) |
| 195 | + |
| 196 | + |
| 197 | +_SUBMODULE_1 = f"{MODULE_1}.1" |
| 198 | +_SUBMODULE_2 = f"{MODULE_1}.2" |
| 199 | + |
| 200 | +same_importer_importee_test_cases = [ |
| 201 | + pytest.param( |
| 202 | + [ |
| 203 | + AbsoluteImport(_SUBMODULE_1, _SUBMODULE_2), |
| 204 | + ], |
| 205 | + Rule() |
| 206 | + .modules_that() |
| 207 | + .are_sub_modules_of(MODULE_1) |
| 208 | + .should() |
| 209 | + .import_modules_that() |
| 210 | + .are_sub_modules_of(MODULE_1), |
| 211 | + None, |
| 212 | + id="should_import_true", |
| 213 | + ), |
| 214 | + pytest.param( |
| 215 | + [ |
| 216 | + AbsoluteImport(_SUBMODULE_1, MODULE_2), |
| 217 | + ], |
| 218 | + Rule() |
| 219 | + .modules_that() |
| 220 | + .are_sub_modules_of(MODULE_1) |
| 221 | + .should() |
| 222 | + .import_modules_that() |
| 223 | + .are_sub_modules_of(MODULE_1), |
| 224 | + 'Sub modules of "1" do not import a sub module of "1".', |
| 225 | + id="should_import_false", |
| 226 | + ), |
| 227 | + pytest.param( |
| 228 | + [ |
| 229 | + AbsoluteImport(_SUBMODULE_1, MODULE_2), |
| 230 | + ], |
| 231 | + Rule() |
| 232 | + .modules_that() |
| 233 | + .are_sub_modules_of(MODULE_1) |
| 234 | + .should_not() |
| 235 | + .import_modules_that() |
| 236 | + .are_sub_modules_of(MODULE_1), |
| 237 | + None, |
| 238 | + id="should_not_import_true", |
| 239 | + ), |
| 240 | + pytest.param( |
| 241 | + [ |
| 242 | + AbsoluteImport(_SUBMODULE_1, _SUBMODULE_2), |
| 243 | + ], |
| 244 | + Rule() |
| 245 | + .modules_that() |
| 246 | + .are_sub_modules_of(MODULE_1) |
| 247 | + .should_not() |
| 248 | + .import_modules_that() |
| 249 | + .are_sub_modules_of(MODULE_1), |
| 250 | + '"1.1" imports "1.2"', |
| 251 | + id="should_not_import_false", |
| 252 | + ), |
| 253 | + pytest.param( |
| 254 | + [ |
| 255 | + AbsoluteImport(_SUBMODULE_1, _SUBMODULE_2), |
| 256 | + ], |
| 257 | + Rule() |
| 258 | + .modules_that() |
| 259 | + .are_sub_modules_of(MODULE_1) |
| 260 | + .should_only() |
| 261 | + .import_modules_that() |
| 262 | + .are_sub_modules_of(MODULE_1), |
| 263 | + None, |
| 264 | + id="should_only_import_true", |
| 265 | + ), |
| 266 | + pytest.param( |
| 267 | + [ |
| 268 | + AbsoluteImport(_SUBMODULE_1, _SUBMODULE_2), |
| 269 | + AbsoluteImport(_SUBMODULE_1, MODULE_3), |
| 270 | + ], |
| 271 | + Rule() |
| 272 | + .modules_that() |
| 273 | + .are_sub_modules_of(MODULE_1) |
| 274 | + .should_only() |
| 275 | + .import_modules_that() |
| 276 | + .are_sub_modules_of(MODULE_1), |
| 277 | + '"1.1" imports "3"', |
| 278 | + id="should_only_import_false", |
| 279 | + ), |
| 280 | + pytest.param( |
| 281 | + [ |
| 282 | + AbsoluteImport(_SUBMODULE_2, _SUBMODULE_1), |
| 283 | + ], |
| 284 | + Rule() |
| 285 | + .modules_that() |
| 286 | + .are_sub_modules_of(MODULE_1) |
| 287 | + .should() |
| 288 | + .be_imported_by_modules_that() |
| 289 | + .are_sub_modules_of(MODULE_1), |
| 290 | + None, |
| 291 | + id="should_be_imported_true", |
| 292 | + ), |
| 293 | + pytest.param( |
| 294 | + [ |
| 295 | + AbsoluteImport(MODULE_2, _SUBMODULE_1), |
| 296 | + ], |
| 297 | + Rule() |
| 298 | + .modules_that() |
| 299 | + .are_sub_modules_of(MODULE_1) |
| 300 | + .should() |
| 301 | + .be_imported_by_modules_that() |
| 302 | + .are_sub_modules_of(MODULE_1), |
| 303 | + 'Sub modules of "1" are not imported by a sub module of "1"', |
| 304 | + id="should_be_imported_false", |
| 305 | + ), |
| 306 | + pytest.param( |
| 307 | + [ |
| 308 | + AbsoluteImport(MODULE_2, _SUBMODULE_1), |
| 309 | + ], |
| 310 | + Rule() |
| 311 | + .modules_that() |
| 312 | + .are_sub_modules_of(MODULE_1) |
| 313 | + .should_not() |
| 314 | + .be_imported_by_modules_that() |
| 315 | + .are_sub_modules_of(MODULE_1), |
| 316 | + None, |
| 317 | + id="should_not_be_imported_true", |
| 318 | + ), |
| 319 | + pytest.param( |
| 320 | + [ |
| 321 | + AbsoluteImport(_SUBMODULE_2, _SUBMODULE_1), |
| 322 | + ], |
| 323 | + Rule() |
| 324 | + .modules_that() |
| 325 | + .are_sub_modules_of(MODULE_1) |
| 326 | + .should_not() |
| 327 | + .be_imported_by_modules_that() |
| 328 | + .are_sub_modules_of(MODULE_1), |
| 329 | + '"1.1" is imported by "1.2"', |
| 330 | + id="should_not_be_imported_false", |
| 331 | + ), |
| 332 | + pytest.param( |
| 333 | + [ |
| 334 | + AbsoluteImport(_SUBMODULE_1, _SUBMODULE_2), |
| 335 | + ], |
| 336 | + Rule() |
| 337 | + .modules_that() |
| 338 | + .are_sub_modules_of(MODULE_1) |
| 339 | + .should_only() |
| 340 | + .be_imported_by_modules_that() |
| 341 | + .are_sub_modules_of(MODULE_1), |
| 342 | + None, |
| 343 | + id="should_only_be_imported_true", |
| 344 | + ), |
| 345 | + pytest.param( |
| 346 | + [ |
| 347 | + AbsoluteImport(MODULE_3, _SUBMODULE_2), |
| 348 | + AbsoluteImport(_SUBMODULE_2, _SUBMODULE_1), |
| 349 | + ], |
| 350 | + Rule() |
| 351 | + .modules_that() |
| 352 | + .are_sub_modules_of(MODULE_1) |
| 353 | + .should_only() |
| 354 | + .be_imported_by_modules_that() |
| 355 | + .are_sub_modules_of(MODULE_1), |
| 356 | + '"1.2" is imported by "3', |
| 357 | + id="should_only_be_imported_false", |
| 358 | + ), |
| 359 | + pytest.param( |
| 360 | + [ |
| 361 | + AbsoluteImport(MODULE_2, _SUBMODULE_2), |
| 362 | + ], |
| 363 | + Rule() |
| 364 | + .modules_that() |
| 365 | + .are_sub_modules_of(MODULE_1) |
| 366 | + .should() |
| 367 | + .be_imported_by_modules_except_modules_that() |
| 368 | + .are_sub_modules_of(MODULE_1), |
| 369 | + None, |
| 370 | + id="should_be_imported_except_true", |
| 371 | + ), |
| 372 | + pytest.param( |
| 373 | + [ |
| 374 | + AbsoluteImport(_SUBMODULE_1, _SUBMODULE_1), |
| 375 | + ], |
| 376 | + Rule() |
| 377 | + .modules_that() |
| 378 | + .are_sub_modules_of(MODULE_1) |
| 379 | + .should() |
| 380 | + .be_imported_by_modules_except_modules_that() |
| 381 | + .are_sub_modules_of(MODULE_1), |
| 382 | + 'Sub modules of "1" are not imported by any module that is not a sub module of "1".', |
| 383 | + id="should_be_imported_except_false", |
| 384 | + ), |
| 385 | + pytest.param( |
| 386 | + [ |
| 387 | + AbsoluteImport(_SUBMODULE_2, _SUBMODULE_1), |
| 388 | + ], |
| 389 | + Rule() |
| 390 | + .modules_that() |
| 391 | + .are_sub_modules_of(MODULE_1) |
| 392 | + .should_not() |
| 393 | + .be_imported_by_modules_except_modules_that() |
| 394 | + .are_sub_modules_of(MODULE_1), |
| 395 | + None, |
| 396 | + id="should_not_be_imported_except_true", |
| 397 | + ), |
| 398 | + pytest.param( |
| 399 | + [ |
| 400 | + AbsoluteImport(MODULE_2, _SUBMODULE_1), |
| 401 | + ], |
| 402 | + Rule() |
| 403 | + .modules_that() |
| 404 | + .are_sub_modules_of(MODULE_1) |
| 405 | + .should_not() |
| 406 | + .be_imported_by_modules_except_modules_that() |
| 407 | + .are_sub_modules_of(MODULE_1), |
| 408 | + '"1.1" is imported by "2".', |
| 409 | + id="should_not_be_imported_except_false", |
| 410 | + ), |
| 411 | + pytest.param( |
| 412 | + [ |
| 413 | + AbsoluteImport(MODULE_3, _SUBMODULE_2), |
| 414 | + ], |
| 415 | + Rule() |
| 416 | + .modules_that() |
| 417 | + .are_sub_modules_of(MODULE_1) |
| 418 | + .should_only() |
| 419 | + .be_imported_by_modules_except_modules_that() |
| 420 | + .are_sub_modules_of(MODULE_1), |
| 421 | + None, |
| 422 | + id="should_only_be_imported_except_true", |
| 423 | + ), |
| 424 | + pytest.param( |
| 425 | + [ |
| 426 | + AbsoluteImport(_SUBMODULE_2, _SUBMODULE_1), |
| 427 | + AbsoluteImport(MODULE_2, _SUBMODULE_2), |
| 428 | + ], |
| 429 | + Rule() |
| 430 | + .modules_that() |
| 431 | + .are_sub_modules_of(MODULE_1) |
| 432 | + .should_only() |
| 433 | + .be_imported_by_modules_except_modules_that() |
| 434 | + .are_sub_modules_of(MODULE_1), |
| 435 | + '"1.1" is imported by "1.2".', |
| 436 | + id="should_only_be_imported_except_false", |
| 437 | + ), |
| 438 | +] |
| 439 | + |
| 440 | + |
| 441 | +@pytest.mark.parametrize( |
| 442 | + "imports, rule, expected_error", |
| 443 | + same_importer_importee_test_cases, |
| 444 | +) |
| 445 | +def test_same_importer_same_importee_handled_correctly( |
| 446 | + imports: list[AbsoluteImport], |
| 447 | + rule: Rule, |
| 448 | + expected_error: str | None, |
| 449 | +) -> None: |
| 450 | + evaluable = EvaluableArchitectureGraph( |
| 451 | + NetworkxGraph(ALL_MODULES + [_SUBMODULE_1, _SUBMODULE_2], imports) |
| 452 | + ) |
| 453 | + |
| 454 | + if expected_error is None: |
| 455 | + rule.assert_applies(evaluable) |
| 456 | + else: |
| 457 | + with pytest.raises(AssertionError, match=expected_error): |
| 458 | + rule.assert_applies(evaluable) |
0 commit comments