|
1 | 1 | from __future__ import annotations |
2 | 2 |
|
| 3 | +import datetime as dt |
| 4 | +import itertools |
3 | 5 | import json |
4 | 6 | import pathlib |
5 | 7 | from collections.abc import Callable |
@@ -831,6 +833,125 @@ def test_instrument_map_resolve_with_date( |
831 | 833 | assert instrument_map.resolve(instrument_id, end_date.date()) is None |
832 | 834 |
|
833 | 835 |
|
| 836 | +def _sep(day: int) -> dt.date: |
| 837 | + return dt.date(2026, 9, day) |
| 838 | + |
| 839 | + |
| 840 | +@pytest.mark.parametrize( |
| 841 | + "intervals, expected", |
| 842 | + [ |
| 843 | + pytest.param( |
| 844 | + [ |
| 845 | + MappingInterval(_sep(14), _sep(20), "ES.c.0"), |
| 846 | + MappingInterval(_sep(14), _sep(16), "ES.v.0"), |
| 847 | + ], |
| 848 | + {13: None, 14: "ES.v.0", 15: "ES.v.0", 16: "ES.c.0", 19: "ES.c.0", 20: None}, |
| 849 | + id="same_start_short_last", |
| 850 | + ), |
| 851 | + pytest.param( |
| 852 | + [ |
| 853 | + MappingInterval(_sep(14), _sep(16), "ES.v.0"), |
| 854 | + MappingInterval(_sep(14), _sep(20), "ES.c.0"), |
| 855 | + ], |
| 856 | + {13: None, 14: "ES.c.0", 15: "ES.c.0", 16: "ES.c.0", 19: "ES.c.0", 20: None}, |
| 857 | + id="same_start_long_last", |
| 858 | + ), |
| 859 | + pytest.param( |
| 860 | + [ |
| 861 | + MappingInterval(_sep(10), _sep(20), "A"), |
| 862 | + MappingInterval(_sep(14), _sep(16), "B"), |
| 863 | + ], |
| 864 | + {9: None, 10: "A", 13: "A", 14: "B", 15: "B", 16: "A", 19: "A", 20: None}, |
| 865 | + id="nested", |
| 866 | + ), |
| 867 | + pytest.param( |
| 868 | + [ |
| 869 | + MappingInterval(_sep(14), _sep(16), "B"), |
| 870 | + MappingInterval(_sep(10), _sep(20), "A"), |
| 871 | + ], |
| 872 | + {9: None, 10: "A", 14: "A", 15: "A", 16: "A", 19: "A", 20: None}, |
| 873 | + id="covered", |
| 874 | + ), |
| 875 | + pytest.param( |
| 876 | + [ |
| 877 | + MappingInterval(_sep(10), _sep(16), "A"), |
| 878 | + MappingInterval(_sep(14), _sep(20), "B"), |
| 879 | + ], |
| 880 | + {9: None, 10: "A", 13: "A", 14: "B", 16: "B", 19: "B", 20: None}, |
| 881 | + id="staggered", |
| 882 | + ), |
| 883 | + pytest.param( |
| 884 | + [ |
| 885 | + MappingInterval(_sep(10), _sep(24), "A"), |
| 886 | + MappingInterval(_sep(12), _sep(20), "B"), |
| 887 | + MappingInterval(_sep(14), _sep(16), "C"), |
| 888 | + ], |
| 889 | + {10: "A", 12: "B", 14: "C", 15: "C", 16: "B", 19: "B", 20: "A", 23: "A", 24: None}, |
| 890 | + id="three_deep", |
| 891 | + ), |
| 892 | + pytest.param( |
| 893 | + [ |
| 894 | + MappingInterval(_sep(10), _sep(20), "A"), |
| 895 | + MappingInterval(_sep(14), _sep(14), "B"), |
| 896 | + ], |
| 897 | + {10: "A", 13: "A", 14: "A", 19: "A", 20: None}, |
| 898 | + id="empty_interval", |
| 899 | + ), |
| 900 | + pytest.param( |
| 901 | + [ |
| 902 | + MappingInterval(_sep(10), _sep(12), "A"), |
| 903 | + MappingInterval(_sep(14), _sep(16), "B"), |
| 904 | + ], |
| 905 | + {9: None, 10: "A", 12: None, 13: None, 14: "B", 16: None}, |
| 906 | + id="gap", |
| 907 | + ), |
| 908 | + ], |
| 909 | +) |
| 910 | +@pytest.mark.parametrize( |
| 911 | + "single_insert", |
| 912 | + [True, False], |
| 913 | +) |
| 914 | +def test_instrument_map_resolve_overlapping_intervals( |
| 915 | + instrument_map: InstrumentMap, |
| 916 | + intervals: list[MappingInterval], |
| 917 | + expected: dict[int, str | None], |
| 918 | + single_insert: bool, |
| 919 | +) -> None: |
| 920 | + """ |
| 921 | + Test that where the intervals of an instrument ID overlap, the later insert |
| 922 | + replaces the earlier one, as it does in DBN. |
| 923 | + """ |
| 924 | + # Arrange |
| 925 | + instrument_id = 42140870 |
| 926 | + other_id = 10252 |
| 927 | + other_interval = MappingInterval(_sep(1), _sep(30), "other") |
| 928 | + |
| 929 | + # Act |
| 930 | + if single_insert: |
| 931 | + instrument_map._insert_intervals( |
| 932 | + [other_id, *[instrument_id] * len(intervals)], |
| 933 | + [other_interval, *intervals], |
| 934 | + ) |
| 935 | + else: |
| 936 | + instrument_map._insert_intervals([other_id], [other_interval]) |
| 937 | + for interval in intervals: |
| 938 | + instrument_map._insert_intervals([instrument_id], [interval]) |
| 939 | + |
| 940 | + dates = [_sep(day) for day in expected] |
| 941 | + resolved_many = instrument_map.resolve_many( |
| 942 | + np.full(len(dates), instrument_id, dtype=np.uint64), |
| 943 | + np.array(dates, dtype="datetime64[D]"), |
| 944 | + ) |
| 945 | + |
| 946 | + # Assert |
| 947 | + assert [instrument_map.resolve(instrument_id, d) for d in dates] == list(expected.values()) |
| 948 | + assert resolved_many.tolist() == list(expected.values()) |
| 949 | + assert instrument_map.resolve(other_id, _sep(15)) == "other" |
| 950 | + for _, mapped in instrument_map.build_symbol_map().items(): |
| 951 | + for (_, end, _), (next_start, _, _) in itertools.pairwise(mapped): |
| 952 | + assert end <= next_start |
| 953 | + |
| 954 | + |
834 | 955 | @pytest.mark.parametrize( |
835 | 956 | "dataset", |
836 | 957 | [ |
|
0 commit comments