diff --git a/src/storage.py b/src/storage.py index 71ab6a7..5e26299 100644 --- a/src/storage.py +++ b/src/storage.py @@ -17,35 +17,39 @@ class Storage: def __init__(self, filename="ecourts.db"): self.filename = filename self.conn = sqlite3.connect(self.filename) - self.cursor = self.conn.cursor() - self.cursor.execute("CREATE TABLE IF NOT EXISTS case_types (value JSON)") - self.cursor.execute( + cursor = self.conn.cursor() + cursor.execute("CREATE TABLE IF NOT EXISTS case_types (value JSON)") + cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS idx_case_types ON case_types(json_extract(value, '$.code'), json_extract(value, '$.court_state_code'), json_extract(value, '$.court_court_code'))" ) - self.cursor.execute("CREATE TABLE IF NOT EXISTS act_types (value JSON)") - self.cursor.execute( + cursor.execute("CREATE TABLE IF NOT EXISTS act_types (value JSON)") + cursor.execute( "CREATE UNIQUE INDEX IF NOT EXISTS idx_act_types ON act_types(json_extract(value, '$.code'), json_extract(value, '$.court_state_code'), json_extract(value, '$.court_court_code'))" ) - self.cursor.execute("CREATE TABLE IF NOT EXISTS courts (value JSON)") - self.cursor.execute("CREATE TABLE IF NOT EXISTS cases (state_code, court_code, value JSON)") - self.cursor.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_cases_cnr ON cases(json_extract(value, '$.cnr_number'))") - self.cursor.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_cases_caseno ON cases(state_code, court_code, json_extract(value, '$.case_type'), json_extract(value, '$.registration_number'))") - self.cursor.execute("CREATE INDEX IF NOT EXISTS idx_cases_category ON cases(json_extract(value, '$.category'))") + cursor.execute("CREATE TABLE IF NOT EXISTS courts (value JSON)") + cursor.execute("CREATE TABLE IF NOT EXISTS cases (state_code, court_code, value JSON)") + cursor.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_cases_cnr ON cases(json_extract(value, '$.cnr_number'))") + cursor.execute("CREATE UNIQUE INDEX IF NOT EXISTS idx_cases_caseno ON cases(state_code, court_code, json_extract(value, '$.case_type'), json_extract(value, '$.registration_number'))") + cursor.execute("CREATE INDEX IF NOT EXISTS idx_cases_category ON cases(json_extract(value, '$.category'))") + cursor.close() self.conn.commit() + def close(self): + self.conn.close() + def addCaseTypes(self, records: list[CaseType]): for record in records: - self.cursor.execute( + self.conn .execute( "INSERT OR IGNORE INTO case_types VALUES (?)", (json.dumps(dict(record)),), ) self.conn.commit() def getCaseTypes(self): - r = self.conn.execute("SELECT * FROM case_types") + r = self.conn.execute("SELECT value FROM case_types") for record in r: - j = json.loads(record["value"]) + j = json.loads(record[0]) court = Court( state_code=j["court_state_code"], court_code=j["court_court_code"] ) @@ -54,7 +58,7 @@ def getCaseTypes(self): def addActTypes(self, records: list[ActType]): for record in records: - self.cursor.execute( + self.conn .execute( "INSERT OR IGNORE INTO act_types VALUES (?)", (json.dumps(dict(record)),), ) @@ -63,7 +67,7 @@ def addActTypes(self, records: list[ActType]): def getActTypes(self) -> Iterator[ActType]: r = self.conn.execute("SELECT * FROM act_types") for record in r: - j = json.loads(record["value"]) + j = json.loads(record[0]) court = Court( state_code=j["court_state_code"], court_code=j["court_court_code"] ) @@ -71,7 +75,7 @@ def getActTypes(self) -> Iterator[ActType]: def addCourts(self, records: list[Court]): for record in records: - self.cursor.execute( + self.conn.execute( "INSERT OR IGNORE INTO courts VALUES (?)", (json.dumps(dict(record)),) ) self.conn.commit() @@ -98,6 +102,7 @@ def addCases(self, court: Court, cases: list[Case], extra_fields: dict={}): cursor.execute( "INSERT INTO cases VALUES (?, ?, ?)", (court.state_code, court.court_code or "1", d) ) + cursor.close() self.conn.commit() def getCases(self): diff --git a/test/test_storage.py b/test/test_storage.py index e46ba7f..facd17f 100644 --- a/test/test_storage.py +++ b/test/test_storage.py @@ -26,6 +26,7 @@ def test_storage_init(): assert "courts" in tables[2] assert "cases" in tables[3] + storage.close() os.unlink("/tmp/ecourts.db") @@ -36,8 +37,9 @@ def test_courts_add(): records = storage.conn.execute("SELECT * FROM courts").fetchall() assert len(records) == 39 for record in records: - court = Court(**json.loads(record["value"])) + court = Court(**json.loads(record[0])) assert court in courts + storage.close() os.unlink("/tmp/ecourts.db") @@ -94,17 +96,20 @@ def test_case_types(): storage.addCaseTypes(case_types) for record in storage.getCaseTypes(): assert record in case_types + storage.close() os.unlink("/tmp/ecourts.db") def test_case_storage(case_details): + if os.path.exists("/tmp/ecourts.db"): + os.unlink("/tmp/ecourts.db") storage = Storage("/tmp/ecourts.db") court=Court(state_code="1") storage.addCases(court, [case_details]) - records = storage.conn.execute("SELECT * FROM cases").fetchall() - # assert len(records) == 1 - data = json.loads(records[0]["value"]) + records = storage.conn.execute("SELECT value FROM cases").fetchall() + assert(len(records) == 1) + data = json.loads(records[0][0]) assert data['case_type'] == case_details.case_type assert data['cnr_number'] == case_details.cnr_number - # assert data['petitioners'] == case_details['petitioners'] + storage.close() os.unlink("/tmp/ecourts.db")