Skip to content

Commit

Permalink
Better signature for merge_conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
yohanboniface committed Feb 28, 2023
1 parent be5faee commit f261af2
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 14 deletions.
14 changes: 10 additions & 4 deletions umap/tests/test_merge_conflicts.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from umap.utils import merge_conflicts


Expand Down Expand Up @@ -48,14 +50,18 @@ def test_removing_same_element():


def test_removing_changed_element():
assert merge_conflicts(["A", "B"], ["A", "C"], ["A"]) is False
with pytest.raises(ValueError):
merge_conflicts(["A", "B"], ["A", "C"], ["A"])


def test_changing_removed_element():
assert merge_conflicts(["A", "B"], ["A"], ["A", "C"]) is False
with pytest.raises(ValueError):
merge_conflicts(["A", "B"], ["A"], ["A", "C"])


def test_changing_same_element():
assert merge_conflicts(["A", "B"], ["A", "D"], ["A", "C"]) is False
with pytest.raises(ValueError):
merge_conflicts(["A", "B"], ["A", "D"], ["A", "C"])
# Order does not count
assert merge_conflicts(["A", "B", "C"], ["B", "D", "A"], ["A", "E", "B"]) is False
with pytest.raises(ValueError):
merge_conflicts(["A", "B", "C"], ["B", "D", "A"], ["A", "E", "B"])
6 changes: 2 additions & 4 deletions umap/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -135,16 +135,14 @@ def merge_conflicts(reference, latest, entrant):

# Now make sure remaining features are still in latest version
for feature in reference:
found = False
for other in latest:
if other == feature:
found = True
break
if not found:
else:
# We cannot distinguish the case where both deleted the same
# element and added others, or where both modified the same
# element, so let's raise a conflict by caution.
return False
raise ValueError

# We can merge.
for feature in reference:
Expand Down
12 changes: 6 additions & 6 deletions umap/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -804,12 +804,12 @@ def merge(self):
with open(self.path) as f:
latest = json.loads(f.read())

merge = merge_conflicts(
reference["features"], latest["features"], entrant["features"]
)
if merge is False:
return merge
latest["features"] = merge
try:
merge_conflicts(
reference["features"], latest["features"], entrant["features"]
)
except ValueError:
return False

# Update request data.
self.request.FILES["geojson"].file = BytesIO(json.dumps(latest).encode("utf-8"))
Expand Down

0 comments on commit f261af2

Please sign in to comment.