|
| 1 | +import pytest |
| 2 | + |
| 3 | +from data_prototype.containers import Desc |
| 4 | + |
| 5 | + |
| 6 | +@pytest.mark.parametrize( |
| 7 | + "spec,actual", |
| 8 | + [ |
| 9 | + ([()], [()]), |
| 10 | + ([(3,)], [(3,)]), |
| 11 | + ([("N",)], [(3,)]), |
| 12 | + ([("N",)], [("X",)]), |
| 13 | + ([("N+1",)], [(3,)]), |
| 14 | + ([("N", "N+1")], [(3, 4)]), |
| 15 | + ([("N", "N-1")], [(3, 2)]), |
| 16 | + ([("N", "N+10")], [(3, 13)]), |
| 17 | + ([("N", "N+1")], [("X", "X+1")]), |
| 18 | + ([("N", "N+9")], [("X", "X+9")]), |
| 19 | + ([("N",), ("N",)], [("X",), ("X",)]), |
| 20 | + ([("N",), ("N",)], [(3,), (3,)]), |
| 21 | + ([("N",), ("N+1",)], [(3,), (4,)]), |
| 22 | + ([("N", "M")], [(3, 4)]), |
| 23 | + ([("N", "M")], [("X", "Y")]), |
| 24 | + ([("N", "M")], [("X", "X")]), |
| 25 | + ([("N", "M", 3)], [(3, 4, 3)]), |
| 26 | + ([("N",), ("M",), ("N", "M")], [(3,), (4,), (3, 4)]), |
| 27 | + ([("N",), ("M",), ("N", "M")], [("X",), ("Y",), ("X", "Y")]), |
| 28 | + ], |
| 29 | +) |
| 30 | +def test_passing_no_broadcast( |
| 31 | + spec: list[tuple[int | str, ...]], actual: list[tuple[int | str, ...]] |
| 32 | +): |
| 33 | + spec = {var: shape for var, shape in zip("abcdefg", spec)} |
| 34 | + actual = {var: shape for var, shape in zip("abcdefg", actual)} |
| 35 | + Desc.validate_shapes(spec, actual) |
| 36 | + |
| 37 | + |
| 38 | +@pytest.mark.parametrize( |
| 39 | + "spec,actual", |
| 40 | + [ |
| 41 | + ([(2,)], [()]), |
| 42 | + ([(3,)], [(4,)]), |
| 43 | + ([(3,)], [(1,)]), |
| 44 | + ([("N",)], [(3, 4)]), |
| 45 | + ([("N", "N+1")], [(4, 4)]), |
| 46 | + ([("N", "N-1")], [(4, 4)]), |
| 47 | + ([("N", "N+1")], [("X", "Y")]), |
| 48 | + ([("N", "N+1")], [("X", 3)]), |
| 49 | + ([("N",), ("N",)], [(3,), (4,)]), |
| 50 | + ([("N", "N")], [("X", "Y")]), |
| 51 | + ([("N", "M", 3)], [(3, 4, 4)]), |
| 52 | + ([("N",), ("M",), ("N", "M")], [(3,), (4,), (3, 5)]), |
| 53 | + ], |
| 54 | +) |
| 55 | +def test_failing_no_broadcast( |
| 56 | + spec: list[tuple[int | str, ...]], actual: list[tuple[int | str, ...]] |
| 57 | +): |
| 58 | + spec = {var: shape for var, shape in zip("abcdefg", spec)} |
| 59 | + actual = {var: shape for var, shape in zip("abcdefg", actual)} |
| 60 | + with pytest.raises(ValueError): |
| 61 | + Desc.validate_shapes(spec, actual) |
| 62 | + |
| 63 | + |
| 64 | +@pytest.mark.parametrize( |
| 65 | + "spec,actual", |
| 66 | + [ |
| 67 | + ([()], [()]), |
| 68 | + ([(2,)], [()]), |
| 69 | + ([(3,)], [(3,)]), |
| 70 | + ([(3,)], [(1,)]), |
| 71 | + ([("N",)], [(3,)]), |
| 72 | + ([("N",)], [("X",)]), |
| 73 | + ([("N", 4)], [(3, 1)]), |
| 74 | + ([("N+1",)], [(3,)]), |
| 75 | + ([("N", "N+1")], [(3, 4)]), |
| 76 | + ([("N", "N+1")], [("X", "X+1")]), |
| 77 | + ([("N", "N+1")], [("X", 1)]), |
| 78 | + ([("N",), ("N",)], [("X",), ("X",)]), |
| 79 | + ([("N",), ("N+1",)], [("X",), (1,)]), |
| 80 | + ([("N",), ("N+1",)], [(3,), (4,)]), |
| 81 | + ([("N",), ("N+1",)], [(1,), (4,)]), |
| 82 | + ([("N", "M")], [(3, 4)]), |
| 83 | + ([("N", "M")], [("X", "Y")]), |
| 84 | + ([("N", "M")], [("X", "X")]), |
| 85 | + ([("N", "M", 3)], [(3, 4, 3)]), |
| 86 | + ([("N",), ("M",), ("N", "M")], [(3,), (4,), (3, 4)]), |
| 87 | + ([("N",), ("M",), ("N", "M")], [(3,), (4,), (3, 1)]), |
| 88 | + ([("N",), ("M",), ("N", "M")], [("X",), ("Y",), ("X", "Y")]), |
| 89 | + ], |
| 90 | +) |
| 91 | +def test_passing_broadcast( |
| 92 | + spec: list[tuple[int | str, ...]], actual: list[tuple[int | str, ...]] |
| 93 | +): |
| 94 | + spec = {var: shape for var, shape in zip("abcdefg", spec)} |
| 95 | + actual = {var: shape for var, shape in zip("abcdefg", actual)} |
| 96 | + Desc.validate_shapes(spec, actual, broadcast=True) |
| 97 | + |
| 98 | + |
| 99 | +@pytest.mark.parametrize( |
| 100 | + "spec,actual", |
| 101 | + [ |
| 102 | + ([(1,)], [(3,)]), |
| 103 | + ([(3,)], [(4,)]), |
| 104 | + ([("N",)], [(3, 4)]), |
| 105 | + ([("N", "N+1")], [(4, 4)]), |
| 106 | + ([("N", "N+1")], [("X", "Y")]), |
| 107 | + ([("N", "N+1")], [("X", 3)]), |
| 108 | + ([("N",), ("N",)], [(3,), (4,)]), |
| 109 | + ([("N", "N")], [("X", "Y")]), |
| 110 | + ([("N", "M", 3)], [(3, 4, 4)]), |
| 111 | + ([("N",), ("M",), ("N", "M")], [(3,), (4,), (3, 5)]), |
| 112 | + ], |
| 113 | +) |
| 114 | +def test_failing_broadcast( |
| 115 | + spec: list[tuple[int | str, ...]], actual: list[tuple[int | str, ...]] |
| 116 | +): |
| 117 | + spec = {var: shape for var, shape in zip("abcdefg", spec)} |
| 118 | + actual = {var: shape for var, shape in zip("abcdefg", actual)} |
| 119 | + with pytest.raises(ValueError): |
| 120 | + Desc.validate_shapes(spec, actual, broadcast=True) |
| 121 | + |
| 122 | + |
| 123 | +def test_desc_object(): |
| 124 | + spec = {"a": Desc(("N",), float), "b": Desc(("N+1",), float)} |
| 125 | + actual = {"a": Desc((3,), float), "b": Desc((4,), float)} |
| 126 | + Desc.validate_shapes(spec, actual) |
| 127 | + |
| 128 | + |
| 129 | +def test_missing_key(): |
| 130 | + spec = {"a": Desc(("N",), float), "b": Desc(("N+1",), float)} |
| 131 | + actual = {"a": Desc((3,), float)} |
| 132 | + with pytest.raises(KeyError): |
| 133 | + Desc.validate_shapes(spec, actual) |
0 commit comments