Upload backend/scripts/tests/test_etl.py with huggingface_hub
Browse files
backend/scripts/tests/test_etl.py
ADDED
|
@@ -0,0 +1,68 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
import importlib
|
| 2 |
+
import io
|
| 3 |
+
import os
|
| 4 |
+
from pathlib import Path
|
| 5 |
+
from tempfile import TemporaryDirectory
|
| 6 |
+
|
| 7 |
+
from django.test import TestCase
|
| 8 |
+
|
| 9 |
+
from hue_portal.core.models import Office, Fine
|
| 10 |
+
|
| 11 |
+
|
| 12 |
+
class EtlLoaderTestCase(TestCase):
|
| 13 |
+
def setUp(self):
|
| 14 |
+
self.tempdir = TemporaryDirectory()
|
| 15 |
+
self.data_dir = Path(self.tempdir.name)
|
| 16 |
+
self._write_office_csv()
|
| 17 |
+
self._write_fine_csv()
|
| 18 |
+
|
| 19 |
+
def tearDown(self):
|
| 20 |
+
self.tempdir.cleanup()
|
| 21 |
+
os.environ.pop("ETL_DATA_DIR", None)
|
| 22 |
+
|
| 23 |
+
def _write_office_csv(self):
|
| 24 |
+
path = self.data_dir / "danh_ba_diem_tiep_dan.csv"
|
| 25 |
+
path.write_text(
|
| 26 |
+
"unit_name,address,district,working_hours,phone,email,latitude,longitude,service_scope,updated_at\n"
|
| 27 |
+
"Công an phường A,123 Đường B,Quận 1,08:00-17:00,0123456789,ca@example.com,16.0,108.0,Tiếp dân,2025-01-01\n",
|
| 28 |
+
encoding="utf-8"
|
| 29 |
+
)
|
| 30 |
+
|
| 31 |
+
def _write_fine_csv(self):
|
| 32 |
+
path = self.data_dir / "muc_phat_theo_hanh_vi.csv"
|
| 33 |
+
path.write_text(
|
| 34 |
+
"violation_code,violation_name,article,decree,min_fine,max_fine,license_points,remedial_measures,source_url,updated_at\n"
|
| 35 |
+
"V001,Vượt đèn đỏ,5,100/2019/NĐ-CP,1000000,3000000,2,Phạt bổ sung,http://example.com,2025-01-01\n",
|
| 36 |
+
encoding="utf-8"
|
| 37 |
+
)
|
| 38 |
+
|
| 39 |
+
def _load_module(self):
|
| 40 |
+
os.environ["ETL_DATA_DIR"] = str(self.data_dir)
|
| 41 |
+
module = importlib.import_module("scripts.etl_load")
|
| 42 |
+
return importlib.reload(module)
|
| 43 |
+
|
| 44 |
+
def test_load_offices_creates_records(self):
|
| 45 |
+
etl = self._load_module()
|
| 46 |
+
log_buffer = io.StringIO()
|
| 47 |
+
processed = etl.load_offices(since=None, dry_run=False, log_file=log_buffer)
|
| 48 |
+
|
| 49 |
+
self.assertEqual(processed, 1)
|
| 50 |
+
self.assertEqual(Office.objects.count(), 1)
|
| 51 |
+
office = Office.objects.first()
|
| 52 |
+
self.assertEqual(office.unit_name, "Công an phường A")
|
| 53 |
+
|
| 54 |
+
def test_load_fines_creates_records(self):
|
| 55 |
+
etl = self._load_module()
|
| 56 |
+
log_buffer = io.StringIO()
|
| 57 |
+
processed = etl.load_fines(since=None, dry_run=False, log_file=log_buffer)
|
| 58 |
+
|
| 59 |
+
self.assertEqual(processed, 1)
|
| 60 |
+
self.assertEqual(Fine.objects.count(), 1)
|
| 61 |
+
fine = Fine.objects.first()
|
| 62 |
+
self.assertEqual(fine.code, "V001")
|
| 63 |
+
|
| 64 |
+
|
| 65 |
+
if __name__ == "__main__":
|
| 66 |
+
import unittest
|
| 67 |
+
|
| 68 |
+
unittest.main()
|