diff --git a/swh/provenance/tests/test_conflict_resolution.py b/swh/provenance/tests/test_conflict_resolution.py new file mode 100644 --- /dev/null +++ b/swh/provenance/tests/test_conflict_resolution.py @@ -0,0 +1,143 @@ +# Copyright (C) 2021 The Software Heritage developers +# See the AUTHORS file at the top-level directory of this distribution +# License: GNU General Public License version 3, or any later version +# See top-level LICENSE file for more information + +from datetime import datetime +from typing import List, Optional, Tuple, Union + +from swh.model.model import Sha1Git +from swh.provenance.api.server import resolve_dates, resolve_relation, resolve_revision +from swh.provenance.interface import RelationData, RevisionData + + +def test_resolve_dates() -> None: + items: List[Union[Tuple[Sha1Git, Optional[datetime]], Tuple[Sha1Git]]] = [ + (b"20329687bb9c1231a7e05afe86160343ad49b494",), + ( + b"20329687bb9c1231a7e05afe86160343ad49b494", + datetime.fromtimestamp(1000000000), + ), + (b"20329687bb9c1231a7e05afe86160343ad49b494", None), + ] + assert resolve_dates(items) == { + b"20329687bb9c1231a7e05afe86160343ad49b494": datetime.fromtimestamp(1000000000) + } + + +def test_resolve_dates_keep_min() -> None: + items: List[Union[Tuple[Sha1Git, Optional[datetime]], Tuple[Sha1Git]]] = [ + ( + b"20329687bb9c1231a7e05afe86160343ad49b494", + datetime.fromtimestamp(1000000001), + ), + ( + b"20329687bb9c1231a7e05afe86160343ad49b494", + datetime.fromtimestamp(1000000000), + ), + ] + assert resolve_dates(items) == { + b"20329687bb9c1231a7e05afe86160343ad49b494": datetime.fromtimestamp(1000000000) + } + + +def test_resolve_revision_without_date() -> None: + items: List[Union[Tuple[Sha1Git, RevisionData], Tuple[Sha1Git]]] = [ + (b"c0d8929936631ecbcf9147be6b8aa13b13b014e4",), + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + RevisionData(date=None, origin=b"3acef14580ea7fd42840ee905c5ce2b0ef9e8175"), + ), + ] + assert resolve_revision(items) == { + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4": RevisionData( + date=None, + origin=b"3acef14580ea7fd42840ee905c5ce2b0ef9e8175", + ) + } + + +def test_resolve_revision_without_origin() -> None: + items: List[Union[Tuple[Sha1Git, RevisionData], Tuple[Sha1Git]]] = [ + (b"c0d8929936631ecbcf9147be6b8aa13b13b014e4",), + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + RevisionData(date=datetime.fromtimestamp(1000000000), origin=None), + ), + ] + assert resolve_revision(items) == { + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4": RevisionData( + date=datetime.fromtimestamp(1000000000), + origin=None, + ) + } + + +def test_resolve_revision_merge() -> None: + items: List[Union[Tuple[Sha1Git, RevisionData], Tuple[Sha1Git]]] = [ + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + RevisionData(date=datetime.fromtimestamp(1000000000), origin=None), + ), + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + RevisionData(date=None, origin=b"3acef14580ea7fd42840ee905c5ce2b0ef9e8175"), + ), + ] + assert resolve_revision(items) == { + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4": RevisionData( + date=datetime.fromtimestamp(1000000000), + origin=b"3acef14580ea7fd42840ee905c5ce2b0ef9e8175", + ) + } + + +def test_resolve_revision_keep_min_date() -> None: + items: List[Union[Tuple[Sha1Git, RevisionData], Tuple[Sha1Git]]] = [ + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + RevisionData( + date=datetime.fromtimestamp(1000000000), + origin=b"3acef14580ea7fd42840ee905c5ce2b0ef9e8174", + ), + ), + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + RevisionData( + date=datetime.fromtimestamp(1000000001), + origin=b"3acef14580ea7fd42840ee905c5ce2b0ef9e8175", + ), + ), + ] + assert resolve_revision(items) == { + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4": RevisionData( + date=datetime.fromtimestamp(1000000000), + origin=b"3acef14580ea7fd42840ee905c5ce2b0ef9e8175", + ) + } + + +def test_resolve_relation() -> None: + items: List[Tuple[Sha1Git, Sha1Git, bytes]] = [ + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + b"3acef14580ea7fd42840ee905c5ce2b0ef9e8174", + b"/path/1", + ), + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + b"3acef14580ea7fd42840ee905c5ce2b0ef9e8174", + b"/path/2", + ), + ( + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4", + b"3acef14580ea7fd42840ee905c5ce2b0ef9e8174", + b"/path/1", + ), + ] + assert resolve_relation(items) == { + b"c0d8929936631ecbcf9147be6b8aa13b13b014e4": { + RelationData(b"3acef14580ea7fd42840ee905c5ce2b0ef9e8174", b"/path/1"), + RelationData(b"3acef14580ea7fd42840ee905c5ce2b0ef9e8174", b"/path/2"), + } + }