|
| 1 | +"""Tests that generated datapoint IDs stay unique when datapoints are created in a batch.""" |
| 2 | + |
| 3 | +import re |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from proteobench.datapoint.denovo_datapoint import DenovoDatapoint |
| 8 | +from proteobench.datapoint.entrapment_datapoint import EntrapmentDatapoint |
| 9 | +from proteobench.datapoint.quant_datapoint import QuantDatapointHYE |
| 10 | + |
| 11 | +DATAPOINT_CLASSES = [QuantDatapointHYE, DenovoDatapoint, EntrapmentDatapoint] |
| 12 | + |
| 13 | +# software name, then date_time_microseconds |
| 14 | +ID_PATTERN = re.compile(r"^(?P<software>.+)_\d{8}_\d{6}_\d{6}$") |
| 15 | + |
| 16 | + |
| 17 | +@pytest.mark.parametrize("datapoint_class", DATAPOINT_CLASSES) |
| 18 | +def test_generated_id_starts_with_the_software_name(datapoint_class): |
| 19 | + datapoint = datapoint_class(software_name="TestTool") |
| 20 | + datapoint.generate_id() |
| 21 | + assert datapoint.id.startswith("TestTool_") |
| 22 | + |
| 23 | + |
| 24 | +@pytest.mark.parametrize("datapoint_class", DATAPOINT_CLASSES) |
| 25 | +def test_generated_id_carries_sub_second_resolution(datapoint_class): |
| 26 | + datapoint = datapoint_class(software_name="TestTool") |
| 27 | + datapoint.generate_id() |
| 28 | + match = ID_PATTERN.match(datapoint.id) |
| 29 | + assert match is not None, f"unexpected ID format: {datapoint.id}" |
| 30 | + assert match.group("software") == "TestTool" |
| 31 | + |
| 32 | + |
| 33 | +@pytest.mark.parametrize("datapoint_class", DATAPOINT_CLASSES) |
| 34 | +def test_ids_generated_in_a_tight_loop_are_unique(datapoint_class): |
| 35 | + """A batch (e.g. the resubmission script) must not hand several runs the same ID.""" |
| 36 | + ids = [] |
| 37 | + for _ in range(50): |
| 38 | + datapoint = datapoint_class(software_name="TestTool") |
| 39 | + datapoint.generate_id() |
| 40 | + ids.append(datapoint.id) |
| 41 | + assert len(set(ids)) == len(ids) |
| 42 | + |
| 43 | + |
| 44 | +def test_ids_stay_distinct_across_datapoint_types(): |
| 45 | + ids = [] |
| 46 | + for datapoint_class in DATAPOINT_CLASSES: |
| 47 | + datapoint = datapoint_class(software_name="TestTool") |
| 48 | + datapoint.generate_id() |
| 49 | + ids.append(datapoint.id) |
| 50 | + assert len(set(ids)) == len(ids) |
0 commit comments