42 lines
1.9 KiB
Python
42 lines
1.9 KiB
Python
import csv
|
|
import sqlite3
|
|
import tempfile
|
|
import unittest
|
|
from pathlib import Path
|
|
|
|
from marketgrid.nrdata import build_gpkg
|
|
|
|
|
|
class NrDataTest(unittest.TestCase):
|
|
def test_build_gpkg_drops_failed_rows_and_creates_spatial_index(self):
|
|
with tempfile.TemporaryDirectory() as directory:
|
|
root = Path(directory)
|
|
polygon = root / "NRBuildPolygon.csv"
|
|
data = root / "NRBuildingsData.csv"
|
|
output = root / "NRBuildingsData.gpkg"
|
|
polygon.write_text(
|
|
"WKT,building_id\n\"POLYGON ((112 22,113 22,113 23,112 23,112 22))\",A\n",
|
|
encoding="utf-8",
|
|
)
|
|
with data.open("w", encoding="gbk", newline="") as stream:
|
|
writer = csv.writer(stream)
|
|
writer.writerow(["输入行号", "building_id", "查询状态", "错误信息", "building_name"])
|
|
writer.writerow(["1", "A", "成功", "", "甲楼"])
|
|
writer.writerow(["2", "A", "失败", "超时", ""])
|
|
result = build_gpkg(data, polygon, output)
|
|
self.assertEqual(result["feature_count"], 1)
|
|
with data.open(encoding="gbk", newline="") as stream:
|
|
cleaned = csv.DictReader(stream)
|
|
self.assertEqual(cleaned.fieldnames, ["building_id", "building_name", "WKT"])
|
|
self.assertEqual(len(list(cleaned)), 1)
|
|
conn = sqlite3.connect(output)
|
|
try:
|
|
columns = [row[1] for row in conn.execute('pragma table_info("NRBuildingsData")')]
|
|
self.assertNotIn("输入行号", columns)
|
|
self.assertNotIn("查询状态", columns)
|
|
self.assertIn("WKT", columns)
|
|
self.assertEqual(conn.execute('select count(*) from "NRBuildingsData"').fetchone()[0], 1)
|
|
self.assertEqual(conn.execute("select count(*) from rtree_NRBuildingsData_geom").fetchone()[0], 1)
|
|
finally:
|
|
conn.close()
|