Skip to content

Commit 5eee1f5

Browse files
authored
Merge pull request #131 from lutraconsulting/fix-report
Fix report generation
2 parents 54aaf8a + 0f39ade commit 5eee1f5

File tree

5 files changed

+68
-10
lines changed

5 files changed

+68
-10
lines changed

mergin/report.py

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ def __init__(self, changeset_entry, geom_idx, geom, qgs_distance_area=None):
125125
self.area = qgs_distance_area.measureArea(updated_qgs_geom) - self.area
126126

127127

128-
def changeset_report(changeset_reader, schema):
128+
def changeset_report(changeset_reader, schema, mp):
129129
""" Parse Geodiff changeset reader and create report from it.
130130
Aggregate individual entries based on common table, operation and geom type.
131131
If QGIS API is available, then lengths and areas of individual entries are summed.
@@ -156,15 +156,18 @@ def changeset_report(changeset_reader, schema):
156156
# let's iterate through reader and populate entries
157157
for entry in changeset_reader:
158158
schema_table = next((t for t in schema if t["table"] == entry.table.name), None)
159-
# get geometry index in both gpkg schema and diffs values
160-
geom_idx = next((index for (index, col) in enumerate(schema_table["columns"]) if col["type"] == "geometry"),
161-
None)
162-
if geom_idx is None:
163-
continue
159+
if schema_table:
160+
# get geometry index in both gpkg schema and diffs values
161+
geom_idx = next((index for (index, col) in enumerate(schema_table["columns"]) if col["type"] == "geometry"),
162+
None)
163+
if geom_idx is None:
164+
continue
164165

165-
geom_col = schema_table["columns"][geom_idx]["geometry"]
166-
report_entry = ChangesetReportEntry(entry, geom_idx, geom_col, distance_area)
167-
entries.append(report_entry)
166+
geom_col = schema_table["columns"][geom_idx]["geometry"]
167+
report_entry = ChangesetReportEntry(entry, geom_idx, geom_col, distance_area)
168+
entries.append(report_entry)
169+
else:
170+
mp.log.warning(f"Table {entry.table.name} is not present in the changeset.")
168171

169172
# create a map of entries grouped by tables within single .gpkg file
170173
tables = defaultdict(list)
@@ -262,7 +265,7 @@ def create_report(mc, directory, since, to, out_file):
262265
v_diff_file = mp.fpath_cache(f['history'][version]['diff']['path'], version=version)
263266
version_data = versions_map[version]
264267
cr = mp.geodiff.read_changeset(v_diff_file)
265-
report = changeset_report(cr, schema)
268+
report = changeset_report(cr, schema, mp)
266269
# append version info to changeset info
267270
dt = datetime.fromisoformat(version_data["created"].rstrip("Z"))
268271
version_fields = {

mergin/test/test_client.py

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1008,6 +1008,24 @@ def _create_test_table(db_file):
10081008
cursor.execute('COMMIT;')
10091009

10101010

1011+
def _create_spatial_table(db_file):
1012+
""" Creates a spatial table called 'test' in sqlite database. Useful to simulate change of database schema. """
1013+
con = sqlite3.connect(db_file)
1014+
cursor = con.cursor()
1015+
cursor.execute('CREATE TABLE geo_test (fid SERIAL, geometry POINT NOT NULL, txt TEXT);')
1016+
cursor.execute('INSERT INTO gpkg_contents VALUES (\'geo_test\', \'features\',\'description\',\'geo_test\',\'2019-06-18T14:52:50.928Z\',-1.08892,0.0424077,-0.363885,0.562244,4326);')
1017+
cursor.execute('INSERT INTO gpkg_geometry_columns VALUES (\'geo_test\',\'geometry\',\'POINT\',4326, 0, 0 )')
1018+
cursor.execute('COMMIT;')
1019+
1020+
def _delete_spatial_table(db_file):
1021+
""" Drops spatial table called 'test' in sqlite database. Useful to simulate change of database schema. """
1022+
con = sqlite3.connect(db_file)
1023+
cursor = con.cursor()
1024+
cursor.execute('DROP TABLE poi;')
1025+
cursor.execute('DELETE FROM gpkg_geometry_columns WHERE table_name=\'poi\';')
1026+
cursor.execute('DELETE FROM gpkg_contents WHERE table_name=\'poi\';')
1027+
cursor.execute('COMMIT;')
1028+
10111029
def _check_test_table(db_file):
10121030
""" Checks whether the 'test' table exists and has one row - otherwise fails with an exception. """
10131031
#con_verify = sqlite3.connect(db_file)
@@ -1662,6 +1680,7 @@ def test_report(mc):
16621680
with pytest.raises(InvalidProject):
16631681
create_report(mc, directory, since, to, report_file)
16641682

1683+
16651684
def test_project_versions_list(mc, mc2):
16661685
"""
16671686
Test retrieving user permissions
@@ -1703,3 +1722,39 @@ def test_project_versions_list(mc, mc2):
17031722

17041723
# writer should have write access
17051724
assert mc2.has_writing_permissions(test_project_fullname)
1725+
1726+
1727+
def test_report_failure(mc):
1728+
"""Check that report generated without errors when a table was added
1729+
and then deleted.
1730+
"""
1731+
test_project = 'test_report_failure'
1732+
project = API_USER + '/' + test_project
1733+
project_dir = os.path.join(TMP_DIR, test_project) # primary project dir
1734+
test_gpkg = os.path.join(project_dir, 'test.gpkg')
1735+
report_file = os.path.join(TMP_DIR, "report.csv")
1736+
1737+
cleanup(mc, project, [project_dir])
1738+
1739+
os.makedirs(project_dir)
1740+
shutil.copy(os.path.join(TEST_DATA_DIR, 'base.gpkg'), test_gpkg)
1741+
mc.create_project_and_push(test_project, project_dir)
1742+
1743+
shutil.copy(os.path.join(TEST_DATA_DIR, 'inserted_1_A.gpkg'), test_gpkg)
1744+
mc.push_project(project_dir)
1745+
1746+
# add a new table to the geopackage
1747+
shutil.copy(os.path.join(TEST_DATA_DIR, 'two_tables.gpkg'), test_gpkg)
1748+
mc.push_project(project_dir)
1749+
1750+
shutil.copy(os.path.join(TEST_DATA_DIR, 'two_tables_1_A.gpkg'), test_gpkg)
1751+
mc.push_project(project_dir)
1752+
1753+
warnings = create_report(mc, project_dir, "v1", "v4", report_file)
1754+
assert warnings
1755+
1756+
shutil.copy(os.path.join(TEST_DATA_DIR, 'two_tables_drop.gpkg'), test_gpkg)
1757+
mc.push_project(project_dir)
1758+
1759+
warnings = create_report(mc, project_dir, "v1", "v5", report_file)
1760+
assert warnings

mergin/test/test_data/two_tables.gpkg

116 KB
Binary file not shown.
116 KB
Binary file not shown.
116 KB
Binary file not shown.

0 commit comments

Comments
 (0)