|
| 1 | +from pathlib import Path |
| 2 | + |
| 3 | +import pytest |
| 4 | + |
| 5 | +import ymmsl |
| 6 | +from ymmsl.v0_2 import ConduitFilter, Configuration, Timeline |
| 7 | +from ymmsl.v0_2 import Reference as Ref |
| 8 | +from ymmsl.v0_2.timeline_resolver import ( |
| 9 | + ROOT_TIMELINE, |
| 10 | + ConduitTimelineError, |
| 11 | + CyclicDependency, |
| 12 | + InconsistentTimelines, |
| 13 | + TooManyReducerFilters, |
| 14 | + resolve_timelines, |
| 15 | +) |
| 16 | + |
| 17 | + |
| 18 | +@pytest.fixture() |
| 19 | +def timelines_configuration() -> Configuration: |
| 20 | + return ymmsl.load_as( |
| 21 | + Configuration, Path(__file__).parent / "ymmsl1/timelines.ymmsl" |
| 22 | + ) |
| 23 | + |
| 24 | + |
| 25 | +def test_consistent_configuration(timelines_configuration: Configuration) -> None: |
| 26 | + timelines_configuration.check_consistent() |
| 27 | + |
| 28 | + |
| 29 | +def test_dispatch(timelines_configuration: Configuration) -> None: |
| 30 | + model = timelines_configuration.models[Ref("dispatch")] |
| 31 | + resolve_timelines(model) |
| 32 | + assert model.components[Ref("first")].timeline == ROOT_TIMELINE |
| 33 | + assert model.components[Ref("second")].timeline == ROOT_TIMELINE |
| 34 | + |
| 35 | + |
| 36 | +def test_macromicro(timelines_configuration: Configuration) -> None: |
| 37 | + model = timelines_configuration.models[Ref("macromicro")] |
| 38 | + resolve_timelines(model) |
| 39 | + assert model.components[Ref("macro")].timeline == ROOT_TIMELINE |
| 40 | + assert model.components[Ref("micro")].timeline == Timeline(":macro") |
| 41 | + |
| 42 | + # Check that ports have the correct relative timelines: |
| 43 | + macro = model.components[Ref("macro")] |
| 44 | + assert macro.ports["init"].timeline == Timeline("") |
| 45 | + assert macro.ports["out"].timeline == Timeline("macro") |
| 46 | + assert macro.ports["in"].timeline == Timeline("macro") |
| 47 | + |
| 48 | + for micro_port in model.components[Ref("micro")].ports.values(): |
| 49 | + assert micro_port.timeline == Timeline("") |
| 50 | + |
| 51 | + |
| 52 | +def test_cycle(timelines_configuration: Configuration) -> None: |
| 53 | + with pytest.raises(CyclicDependency, match="cycle in model"): |
| 54 | + resolve_timelines(timelines_configuration.models[Ref("cycle")]) |
| 55 | + |
| 56 | + |
| 57 | +def test_reducer(timelines_configuration: Configuration) -> None: |
| 58 | + model = timelines_configuration.models[Ref("reducer")] |
| 59 | + resolve_timelines(model) |
| 60 | + assert model.components[Ref("first")].timeline == ROOT_TIMELINE |
| 61 | + assert model.components[Ref("second")].timeline == ROOT_TIMELINE |
| 62 | + |
| 63 | + |
| 64 | +def test_only_reducer(timelines_configuration: Configuration) -> None: |
| 65 | + model = timelines_configuration.models[Ref("reducer")] |
| 66 | + # Remove the conduit first.final -> second.init1 and keep only the reduced conduit |
| 67 | + del model.conduits[0] |
| 68 | + assert model.conduits[0].filters == [ConduitFilter("last")] |
| 69 | + resolve_timelines(model) |
| 70 | + assert model.components[Ref("first")].timeline == ROOT_TIMELINE |
| 71 | + assert model.components[Ref("second")].timeline == ROOT_TIMELINE |
| 72 | + |
| 73 | + |
| 74 | +def test_too_many_reducers(timelines_configuration: Configuration) -> None: |
| 75 | + model = timelines_configuration.models[Ref("reducer")] |
| 76 | + model.conduits[-1].filters.append(model.conduits[-1].filters[0]) |
| 77 | + with pytest.raises(TooManyReducerFilters, match="too many reducer filters"): |
| 78 | + resolve_timelines(model) |
| 79 | + |
| 80 | + |
| 81 | +def test_inconsistent_timelines(timelines_configuration: Configuration) -> None: |
| 82 | + with pytest.raises(InconsistentTimelines): |
| 83 | + resolve_timelines(timelines_configuration.models[Ref("inconsistent")]) |
| 84 | + |
| 85 | + |
| 86 | +def test_repeaters(timelines_configuration: Configuration) -> None: |
| 87 | + model = timelines_configuration.models[Ref("repeaters")] |
| 88 | + resolve_timelines(model) |
| 89 | + assert model.components[Ref("macro")].timeline == ROOT_TIMELINE |
| 90 | + assert model.components[Ref("meso")].timeline == Timeline(":macro") |
| 91 | + assert model.components[Ref("micro")].timeline == Timeline(":macro:meso") |
| 92 | + |
| 93 | + |
| 94 | +def test_too_many_repeaters(timelines_configuration: Configuration) -> None: |
| 95 | + model = timelines_configuration.models[Ref("repeaters")] |
| 96 | + model.conduits[-2].filters.append(ConduitFilter.REPEAT) |
| 97 | + with pytest.raises(ConduitTimelineError, match="remove a repeater"): |
| 98 | + resolve_timelines(model) |
| 99 | + |
| 100 | + |
| 101 | +def test_too_few_repeaters(timelines_configuration: Configuration) -> None: |
| 102 | + model = timelines_configuration.models[Ref("repeaters")] |
| 103 | + model.conduits[-1].filters.pop() |
| 104 | + with pytest.raises(ConduitTimelineError, match="add a repeater"): |
| 105 | + resolve_timelines(model) |
| 106 | + |
| 107 | + |
| 108 | +def test_repeater_and_too_many_reducers(timelines_configuration: Configuration) -> None: |
| 109 | + model = timelines_configuration.models[Ref("repeaters")] |
| 110 | + model.conduits[-1].filters.insert(0, ConduitFilter.LAST) |
| 111 | + with pytest.raises(TooManyReducerFilters, match="too many reducer filters"): |
| 112 | + resolve_timelines(model) |
| 113 | + |
| 114 | + |
| 115 | +def test_repeater_after_reducer(timelines_configuration: Configuration) -> None: |
| 116 | + model = timelines_configuration.models[Ref("repeater_reducer")] |
| 117 | + resolve_timelines(model) |
| 118 | + assert model.components[Ref("macro1")].timeline == ROOT_TIMELINE |
| 119 | + assert model.components[Ref("macro2")].timeline == ROOT_TIMELINE |
| 120 | + assert model.components[Ref("micro1")].timeline == Timeline(":macro1") |
| 121 | + assert model.components[Ref("micro2")].timeline == Timeline(":macro2") |
| 122 | + |
| 123 | + # Remove filters on the last conduit to make the incoming timelines inconsistent |
| 124 | + model.conduits[-1].filters = [] |
| 125 | + with pytest.raises(InconsistentTimelines, match="different timelines"): |
| 126 | + resolve_timelines(model) |
| 127 | + |
| 128 | + |
| 129 | +def test_repeater_after_reducer_error(timelines_configuration: Configuration) -> None: |
| 130 | + model = timelines_configuration.models[Ref("repeater_reducer_error")] |
| 131 | + with pytest.raises(ConduitTimelineError, match="remove a repeater and reducer"): |
| 132 | + resolve_timelines(model) |
| 133 | + model.conduits[-1].filters = [] |
| 134 | + resolve_timelines(model) |
| 135 | + assert model.components[Ref("macro")].timeline == ROOT_TIMELINE |
| 136 | + assert model.components[Ref("micro1")].timeline == Timeline(":macro") |
| 137 | + assert model.components[Ref("micro2")].timeline == Timeline(":macro") |
| 138 | + |
| 139 | + |
| 140 | +def test_inconsistent_interact(timelines_configuration: Configuration) -> None: |
| 141 | + model = timelines_configuration.models[Ref("inconsistent_interact")] |
| 142 | + with pytest.raises(ConduitTimelineError, match="missing timeline annotations"): |
| 143 | + resolve_timelines(model) |
| 144 | + model.components[Ref("B")].ports["out"].timeline = Timeline("A") |
| 145 | + model.components[Ref("B")].ports["in"].timeline = Timeline("A") |
| 146 | + resolve_timelines(model) |
| 147 | + |
| 148 | + |
| 149 | +def test_model_ports(timelines_configuration: Configuration) -> None: |
| 150 | + model = timelines_configuration.models[Ref("model_ports")] |
| 151 | + resolve_timelines(model) |
0 commit comments