Skip to content

Commit a1a273f

Browse files
committed
Test the content of the GeoPackage that's generated by the example marimo notebook
1 parent 5df98bc commit a1a273f

File tree

1 file changed

+69
-10
lines changed

1 file changed

+69
-10
lines changed

tests/test_examples/test_hk_kaitak_ags3_to_brgi_geodb.py

Lines changed: 69 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,24 @@
1-
import hashlib
21
import os
32
import shutil
3+
import sqlite3
44
import subprocess
55
from pathlib import Path
66
from tempfile import TemporaryDirectory
77

8+
import geopandas as gpd
9+
810

911
def test_kaitak_ags3_notebook_runs_and_creates_gpkg(examples_dir):
12+
"""Tests the Kai Tak, Hong Kong AGS 3 example marimo notebook.
13+
14+
Tests that the `hk_kaitak_ags3_to_brgi_geodb.py` marimo notebook:
15+
- Runs successfully as a script using `uvx uv run` with the Python version and
16+
dependencies specified in the PEP 723 inline script metadata.
17+
- Creates a valid GeoPackage
18+
- That the GeoPackage contains the expected tables
19+
- That the Project, Location, Sample, InSitu_GEOL, InSitu_ISPT and InSitu_WETH
20+
tables have the expected number of rows.
21+
"""
1022
notebook_dir = examples_dir / "hk_kaitak_ags3"
1123
notebook_path = notebook_dir / "hk_kaitak_ags3_to_brgi_geodb.py"
1224
gpkg_output_path = notebook_dir / "kaitak_gi.gpkg"
@@ -51,17 +63,64 @@ def test_kaitak_ags3_notebook_runs_and_creates_gpkg(examples_dir):
5163
f"The expected GeoPackage {gpkg_output_path} was not created."
5264
)
5365

54-
# TODO: write some logic to compare the original and new GeoPackages.
55-
# with gpkg_output_path.open("rb") as f:
56-
# gpkg_output_hash = hashlib.sha256(f.read()).hexdigest()
66+
# Compare the original and new GeoPackages and check the number of rows
67+
# in the important tables.
68+
conn_original = sqlite3.connect(temp_original_gpkg_path)
69+
conn_output = sqlite3.connect(gpkg_output_path)
5770

58-
# with temp_original_gpkg_path.open("rb") as f:
59-
# temp_original_gpkg_hash = hashlib.sha256(f.read()).hexdigest()
71+
tables_original = conn_original.execute(
72+
"SELECT name FROM sqlite_master WHERE type='table';"
73+
).fetchall()
74+
conn_original.close()
75+
tables_output = conn_output.execute(
76+
"SELECT name FROM sqlite_master WHERE type='table';"
77+
).fetchall()
78+
conn_output.close()
79+
80+
assert tables_original == tables_output, (
81+
f"The original GeoPackage {temp_original_gpkg_path.name} and the output "
82+
"GeoPackage {gpkg_output_path.name} have different tables."
83+
)
6084

61-
# assert gpkg_output_hash == temp_original_gpkg_hash, (
62-
# f"The original GeoPackage {temp_original_gpkg_path} and the output GeoPackage {gpkg_output_path} have different hex hashes."
63-
# )
64-
# TODO: write some logic to compare the original and new GeoPackages.
85+
important_tables = [
86+
{
87+
"table_name": "Project",
88+
"no_rows": 88,
89+
},
90+
{
91+
"table_name": "Location",
92+
"no_rows": 754,
93+
},
94+
{
95+
"table_name": "Sample",
96+
"no_rows": 17_774,
97+
},
98+
{
99+
"table_name": "InSitu_GEOL",
100+
"no_rows": 7_764,
101+
},
102+
{
103+
"table_name": "InSitu_ISPT",
104+
"no_rows": 3_986,
105+
},
106+
{
107+
"table_name": "InSitu_WETH",
108+
"no_rows": 3_928,
109+
},
110+
]
111+
for table in important_tables:
112+
df_output = gpd.read_file(gpkg_output_path, layer=table["table_name"])
113+
assert len(df_output) == table["no_rows"], (
114+
f"The output GeoPackage {gpkg_output_path.name} table {table['table_name']} "
115+
"has {len(df_output)} rows instead of {table['no_rows']}."
116+
)
117+
df_original = gpd.read_file(
118+
temp_original_gpkg_path, layer=table["table_name"]
119+
)
120+
assert df_original.equals(df_output), (
121+
f"The original GeoPackage {temp_original_gpkg_path.name} and the output "
122+
"GeoPackage {gpkg_output_path.name} have a different {table['table_name']} table."
123+
)
65124

66125
# Remove the newly generated kaitak_gi.gpkg
67126
os.remove(gpkg_output_path)

0 commit comments

Comments
 (0)