Skip to content

Commit 7959404

Browse files
alexbruywonder-sk
authored andcommitted
correctly handle case when requested changesets were cached and
download_file_diffs() call returns empty list of downloaded files
1 parent 576c4cb commit 7959404

File tree

2 files changed

+65
-0
lines changed

2 files changed

+65
-0
lines changed

mergin/client.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212
from datetime import datetime, timezone
1313
import dateutil.parser
1414
import ssl
15+
import re
1516

1617
from .common import ClientError, LoginError, InvalidProject
1718
from .merginproject import MerginProject
@@ -779,6 +780,23 @@ def get_file_diff(self, project_dir, file_path, output_diff, version_from, versi
779780
self, project_path, file_path, version_from=version_from, version_to=version_to, file_history=file_history
780781
)
781782
diffs = self.download_file_diffs(project_dir, file_path, versions_to_fetch[1:])
783+
# if some changesets were cached before, they will be missed from the
784+
# list of downloaded changesets and should be added before any attempt
785+
# to concatenate them into single file
786+
sort = False
787+
if len(diffs) < len(versions_to_fetch[1:]):
788+
sort = True
789+
for v in versions_to_fetch[1:]:
790+
file_name = mp.fpath_cache(file_history["history"][v]["diff"]["path"], v)
791+
if file_name not in diffs:
792+
diffs.append(file_name)
793+
794+
# at this point diffs in the list might be in the wrong order
795+
# we need to sort them by version in ascending order
796+
if sort:
797+
ver_regex = re.compile("\/v[0-9]+\/")
798+
diffs.sort(key=lambda x: int(ver_regex.search(x)[0].strip("/").replace("v", "")))
799+
782800
# concatenate diffs, if needed
783801
output_dir = os.path.dirname(output_diff)
784802
if len(diffs) >= 1:

mergin/test/test_client.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1764,3 +1764,50 @@ def test_report_failure(mc):
17641764

17651765
warnings = create_report(mc, project_dir, "v1", "v5", report_file)
17661766
assert warnings
1767+
1768+
1769+
def test_changesets_download(mc):
1770+
"""Check that downloading diffs works correctly, including case when
1771+
changesets are cached.
1772+
"""
1773+
test_project = 'test_changesets_download'
1774+
project = API_USER + '/' + test_project
1775+
project_dir = os.path.join(TMP_DIR, test_project) # primary project dir
1776+
test_gpkg = 'test.gpkg'
1777+
file_path = os.path.join(project_dir, 'test.gpkg')
1778+
download_dir = os.path.join(TMP_DIR, "changesets")
1779+
1780+
cleanup(mc, project, [project_dir])
1781+
1782+
os.makedirs(project_dir, exist_ok=True)
1783+
shutil.copy(os.path.join(TEST_DATA_DIR, 'base.gpkg'), file_path)
1784+
mc.create_project_and_push(test_project, project_dir)
1785+
1786+
shutil.copy(os.path.join(TEST_DATA_DIR, 'inserted_1_A.gpkg'), file_path)
1787+
mc.push_project(project_dir)
1788+
1789+
shutil.copy(os.path.join(TEST_DATA_DIR, 'inserted_1_A_mod.gpkg'), file_path)
1790+
mc.push_project(project_dir)
1791+
1792+
mp = MerginProject(project_dir)
1793+
1794+
os.makedirs(download_dir, exist_ok=True)
1795+
diff_file = os.path.join(download_dir, "base-v1-2.diff")
1796+
mc.get_file_diff(project_dir, test_gpkg, diff_file, "v1", "v2")
1797+
assert os.path.exists(diff_file)
1798+
assert mp.geodiff.has_changes(diff_file)
1799+
assert mp.geodiff.changes_count(diff_file) == 1
1800+
1801+
diff_file = os.path.join(download_dir, "base-v2-3.diff")
1802+
mc.get_file_diff(project_dir, test_gpkg, diff_file, "v2", "v3")
1803+
assert os.path.exists(diff_file)
1804+
assert mp.geodiff.has_changes(diff_file)
1805+
assert mp.geodiff.changes_count(diff_file) == 2
1806+
1807+
# even if changesets were cached and were not downloaded destination
1808+
# file should be created and contain a valid changeset
1809+
diff_file = os.path.join(download_dir, "base-all.diff")
1810+
mc.get_file_diff(project_dir, test_gpkg, diff_file, "v1", "v3")
1811+
assert os.path.exists(diff_file)
1812+
assert mp.geodiff.has_changes(diff_file)
1813+
assert mp.geodiff.changes_count(diff_file) == 3

0 commit comments

Comments
 (0)