Skip to content

Commit 61f9dbd

Browse files
committed
Add test for ChangesHandler
1 parent 31787f3 commit 61f9dbd

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

mergin/client_push.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -126,8 +126,8 @@ def _split_by_type(self, changes: Dict[str, List[dict]]) -> List[Dict[str, List[
126126
1. Blocking: updated/removed and added files that are blocking
127127
2. Non-blocking: added files that are not blocking
128128
"""
129-
blocking_changes = {"added": [], "updated": [], "removed": []}
130-
non_blocking_changes = {"added": [], "updated": [], "removed": []}
129+
blocking_changes = {"added": [], "updated": [], "removed": [], "renamed": []}
130+
non_blocking_changes = {"added": [], "updated": [], "removed": [], "renamed": []}
131131

132132
for f in changes.get("added", []):
133133
if self.is_blocking_file(f):

mergin/test/test_client.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import tempfile
66
import subprocess
77
import shutil
8+
from collections import defaultdict
89
from datetime import datetime, timedelta, date
910
import pytest
1011
import pytz
@@ -2893,3 +2894,38 @@ def test_mc_without_login():
28932894
# without login should not be able to access workspaces
28942895
with pytest.raises(ClientError, match="Authentication information is missing or invalid."):
28952896
mc.workspaces_list()
2897+
2898+
def sort_dict_of_files_by_path(d):
2899+
return {
2900+
k: sorted(v, key=lambda f: f["path"]) for k, v in d.items()
2901+
}
2902+
2903+
def test_changes_handler(mc):
2904+
"""
2905+
Test methods of the ChangesHandler class
2906+
"""
2907+
# test _split_by_type
2908+
test_project = "test_changes_handler"
2909+
project = API_USER + "/" + test_project
2910+
project_dir = os.path.join(TMP_DIR, test_project)
2911+
cleanup(mc, project, [project_dir])
2912+
shutil.copytree(TEST_DATA_DIR, project_dir)
2913+
mc.create_project(project)
2914+
project_info = mc.project_info(project)
2915+
mp = MerginProject(project_dir)
2916+
mp.write_metadata(project_dir, project_info)
2917+
2918+
mixed_changes = mp.get_push_changes()
2919+
changes_handler = ChangesHandler(mc, project_info, mixed_changes)
2920+
split_changes = changes_handler._split_by_type(mixed_changes)
2921+
assert len(split_changes) == 2
2922+
# all blocking files in the first dict and no blocking file in the second dict
2923+
assert all(ChangesHandler.is_blocking_file(f) for files in split_changes[0].values() for f in files)
2924+
assert all(not ChangesHandler.is_blocking_file(f) for files in split_changes[1].values() for f in files)
2925+
# merge the split changes dicts back into a single dict and check files are the same
2926+
merged = defaultdict(list)
2927+
for d in split_changes:
2928+
for k, v in d.items():
2929+
merged[k].extend(v)
2930+
assert sort_dict_of_files_by_path(merged) == sort_dict_of_files_by_path(mixed_changes)
2931+

0 commit comments

Comments
 (0)