|
| 1 | +import pytest |
| 2 | + |
| 3 | +from py2puml.domain.umlitem import UmlItem |
| 4 | +from py2puml.domain.umlrelation import UmlRelation |
| 5 | +from py2puml.export.puml import Filters |
| 6 | +from py2puml.py2puml import py2puml |
| 7 | + |
| 8 | +un_modified_puml = [ |
| 9 | + '@startuml tests.modules.withinheritedconstructor\n!pragma useIntermediatePackages false\n\n', |
| 10 | + 'class tests.modules.withinheritedconstructor.metricorigin.MetricOrigin {\n', |
| 11 | + ' unit: str {static}\n', |
| 12 | + '}\n', |
| 13 | + 'class tests.modules.withinheritedconstructor.point.Origin {\n', |
| 14 | + ' is_origin: bool {static}\n', |
| 15 | + '}\n', |
| 16 | + 'class tests.modules.withinheritedconstructor.point.Point {\n', |
| 17 | + ' x: float\n', |
| 18 | + ' y: float\n', |
| 19 | + '}\n', |
| 20 | + 'tests.modules.withinheritedconstructor.point.Origin <|-- tests.modules.withinheritedconstructor.metricorigin.MetricOrigin\n', |
| 21 | + 'tests.modules.withinheritedconstructor.point.Point <|-- tests.modules.withinheritedconstructor.point.Origin\n', |
| 22 | + 'footer Generated by //py2puml//\n', |
| 23 | + '@enduml\n', |
| 24 | +] |
| 25 | + |
| 26 | +puml_with_origin_class_skipped = [ |
| 27 | + '@startuml tests.modules.withinheritedconstructor\n!pragma useIntermediatePackages false\n\n', |
| 28 | + 'class tests.modules.withinheritedconstructor.metricorigin.MetricOrigin {\n', |
| 29 | + ' unit: str {static}\n', |
| 30 | + '}\n', |
| 31 | + 'class tests.modules.withinheritedconstructor.point.Point {\n', |
| 32 | + ' x: float\n', |
| 33 | + ' y: float\n', |
| 34 | + '}\n', |
| 35 | + 'tests.modules.withinheritedconstructor.point.Origin <|-- tests.modules.withinheritedconstructor.metricorigin.MetricOrigin\n', |
| 36 | + 'tests.modules.withinheritedconstructor.point.Point <|-- tests.modules.withinheritedconstructor.point.Origin\n', |
| 37 | + 'footer Generated by //py2puml//\n', |
| 38 | + '@enduml\n', |
| 39 | +] |
| 40 | + |
| 41 | +puml_with_point_origin_relation_skipped = [ |
| 42 | + '@startuml tests.modules.withinheritedconstructor\n!pragma useIntermediatePackages false\n\n', |
| 43 | + 'class tests.modules.withinheritedconstructor.metricorigin.MetricOrigin {\n', |
| 44 | + ' unit: str {static}\n', |
| 45 | + '}\n', |
| 46 | + 'class tests.modules.withinheritedconstructor.point.Origin {\n', |
| 47 | + ' is_origin: bool {static}\n', |
| 48 | + '}\n', |
| 49 | + 'class tests.modules.withinheritedconstructor.point.Point {\n', |
| 50 | + ' x: float\n', |
| 51 | + ' y: float\n', |
| 52 | + '}\n', |
| 53 | + 'tests.modules.withinheritedconstructor.point.Origin <|-- tests.modules.withinheritedconstructor.metricorigin.MetricOrigin\n', |
| 54 | + 'footer Generated by //py2puml//\n', |
| 55 | + '@enduml\n', |
| 56 | +] |
| 57 | + |
| 58 | +puml_with_point_class_and_point_origin_relation_skipped = [ |
| 59 | + '@startuml tests.modules.withinheritedconstructor\n!pragma useIntermediatePackages false\n\n', |
| 60 | + 'class tests.modules.withinheritedconstructor.metricorigin.MetricOrigin {\n', |
| 61 | + ' unit: str {static}\n', |
| 62 | + '}\n', |
| 63 | + 'class tests.modules.withinheritedconstructor.point.Point {\n', |
| 64 | + ' x: float\n', |
| 65 | + ' y: float\n', |
| 66 | + '}\n', |
| 67 | + 'tests.modules.withinheritedconstructor.point.Origin <|-- tests.modules.withinheritedconstructor.metricorigin.MetricOrigin\n', |
| 68 | + 'footer Generated by //py2puml//\n', |
| 69 | + '@enduml\n', |
| 70 | +] |
| 71 | + |
| 72 | + |
| 73 | +def skip_origin_block(item: UmlItem) -> bool: |
| 74 | + return item.fqn.endswith('.Origin') |
| 75 | + |
| 76 | + |
| 77 | +def skip_point_origin_relation(relation: UmlRelation) -> bool: |
| 78 | + return relation.source_fqn.endswith('.Point') and relation.target_fqn.endswith('.Origin') |
| 79 | + |
| 80 | + |
| 81 | +def get_puml_content(filters: Filters) -> list[str]: |
| 82 | + return list(py2puml('tests/modules/withinheritedconstructor', 'tests.modules.withinheritedconstructor', filters)) |
| 83 | + |
| 84 | + |
| 85 | +def invalid_filter_without_filter_argument(): |
| 86 | + return True |
| 87 | + |
| 88 | + |
| 89 | +def invalid_filter_with_wrong_return_type(item: UmlItem) -> str: |
| 90 | + return 'True' |
| 91 | + |
| 92 | + |
| 93 | +def invalid_filter_with_exception(item: UmlItem) -> bool: |
| 94 | + raise Exception('An error occurred') |
| 95 | + |
| 96 | + |
| 97 | +non_callable_filter = 'not a function' |
| 98 | + |
| 99 | + |
| 100 | +def test_without_giving_filters(): |
| 101 | + generated_puml = list(py2puml('tests/modules/withinheritedconstructor', 'tests.modules.withinheritedconstructor')) |
| 102 | + assert generated_puml == un_modified_puml |
| 103 | + |
| 104 | + |
| 105 | +def test_default_filters(): |
| 106 | + filters = Filters() |
| 107 | + generated_puml = get_puml_content(filters) |
| 108 | + assert generated_puml == un_modified_puml |
| 109 | + |
| 110 | + |
| 111 | +def test_skip_origin_class(): |
| 112 | + filters = Filters(skip_block=skip_origin_block) |
| 113 | + generated_puml = get_puml_content(filters) |
| 114 | + assert generated_puml == puml_with_origin_class_skipped |
| 115 | + |
| 116 | + |
| 117 | +def test_skip_point_origin_relation(): |
| 118 | + filters = Filters(skip_relation=skip_point_origin_relation) |
| 119 | + generated_puml = get_puml_content(filters) |
| 120 | + assert generated_puml == puml_with_point_origin_relation_skipped |
| 121 | + |
| 122 | + |
| 123 | +def test_skip_point_class_and_point_origin_relation(): |
| 124 | + filters = Filters(skip_block=skip_origin_block, skip_relation=skip_point_origin_relation) |
| 125 | + generated_puml = get_puml_content(filters) |
| 126 | + print(''.join(generated_puml)) |
| 127 | + print(len(generated_puml), len(puml_with_point_class_and_point_origin_relation_skipped)) |
| 128 | + assert generated_puml == puml_with_point_class_and_point_origin_relation_skipped |
| 129 | + |
| 130 | + |
| 131 | +@pytest.mark.parametrize( |
| 132 | + 'invalid_filter', |
| 133 | + [ |
| 134 | + invalid_filter_without_filter_argument, |
| 135 | + invalid_filter_with_wrong_return_type, |
| 136 | + invalid_filter_with_exception, |
| 137 | + non_callable_filter, |
| 138 | + ], |
| 139 | +) |
| 140 | +def test_invalid_filters(invalid_filter): |
| 141 | + with pytest.raises(ValueError): |
| 142 | + filters = Filters(skip_block=invalid_filter) |
| 143 | + get_puml_content(filters) |
| 144 | + |
| 145 | + with pytest.raises(ValueError): |
| 146 | + filters = Filters(skip_relation=invalid_filter) |
| 147 | + get_puml_content(filters) |
0 commit comments