|
1 | | -import hashlib |
2 | 1 | import os |
3 | 2 | import shutil |
| 3 | +import sqlite3 |
4 | 4 | import subprocess |
5 | 5 | from pathlib import Path |
6 | 6 | from tempfile import TemporaryDirectory |
7 | 7 |
|
| 8 | +import geopandas as gpd |
| 9 | + |
8 | 10 |
|
9 | 11 | 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 | + """ |
10 | 22 | notebook_dir = examples_dir / "hk_kaitak_ags3" |
11 | 23 | notebook_path = notebook_dir / "hk_kaitak_ags3_to_brgi_geodb.py" |
12 | 24 | gpkg_output_path = notebook_dir / "kaitak_gi.gpkg" |
@@ -51,17 +63,64 @@ def test_kaitak_ags3_notebook_runs_and_creates_gpkg(examples_dir): |
51 | 63 | f"The expected GeoPackage {gpkg_output_path} was not created." |
52 | 64 | ) |
53 | 65 |
|
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) |
57 | 70 |
|
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 | + ) |
60 | 84 |
|
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 | + ) |
65 | 124 |
|
66 | 125 | # Remove the newly generated kaitak_gi.gpkg |
67 | 126 | os.remove(gpkg_output_path) |
|
0 commit comments