import sqlite3 import tempfile import unittest from pathlib import Path from openpyxl import load_workbook from marketgrid.result_xlsx import create_result_xlsx class ResultXlsxTests(unittest.TestCase): def test_building_headers_and_percent_format(self): with tempfile.TemporaryDirectory() as temp: root = Path(temp) building = root / "buildings.gpkg" aoi = root / "aoi.gpkg" conn = sqlite3.connect(building) try: conn.execute( """CREATE TABLE NRBuildingsData ( building_id TEXT, longitude REAL, latitude REAL, mro_ratio_yd REAL, mro_ratio_dx REAL, mro_ratio_lt REAL, mro_ratio_gd REAL, mro_ratio_dxlt REAL )""" ) conn.execute( "INSERT INTO NRBuildingsData VALUES ('JM1', 112.509795, 22.059436, 99.65, 0, 0.35, 12.61, 27.75)" ) conn.commit() finally: conn.close() conn = sqlite3.connect(aoi) try: conn.execute( """CREATE TABLE "AOI市场网格匹配" ( "所属市场网格" TEXT, "AOI唯一标签" TEXT, "AOI名称" TEXT, "移动C端采样点占比" REAL, "联通C端采样点占比" REAL, "电信C端采样点占比" REAL, "广电C端采样点占比" REAL, "电联C端采样点占比" REAL )""" ) conn.execute( 'INSERT INTO "AOI市场网格匹配" VALUES (?, ?, ?, ?, ?, ?, ?, ?)', ("网格", "标签", "名称", 80.5, 1.25, 0, 3, 10), ) conn.commit() finally: conn.close() output = root / "Result.xlsx" create_result_xlsx(output, building, aoi) workbook = load_workbook(output) self.addCleanup(workbook.close) building_sheet = workbook["移动C端采样点比例_楼宇"] self.assertEqual( [cell.value for cell in building_sheet[1]], ["楼宇ID", "经度", "纬度", "移动采样点占比", "电信采样点占比", "联通采样点占比", "广电采样点占比", "电联采样点占比"], ) self.assertEqual(building_sheet["D2"].value, "99.65%") self.assertEqual(building_sheet["E2"].value, "0.00%") self.assertEqual(building_sheet["F2"].value, "0.35%") self.assertEqual(building_sheet["A2"].value, "JM1") aoi_sheet = workbook["移动C端采样点比例_AOI"] self.assertEqual(aoi_sheet["D2"].value, "80.50%") self.assertEqual(aoi_sheet["E2"].value, "1.25%") self.assertEqual(aoi_sheet["H2"].value, "10.00%") self.assertEqual(aoi_sheet["A2"].value, "网格") if __name__ == "__main__": unittest.main()