Skip to content

Commit

Permalink
fix sqlite closure
Browse files Browse the repository at this point in the history
  • Loading branch information
captn3m0 committed Aug 15, 2024
1 parent 5b0e02e commit 084e8ce
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 21 deletions.
37 changes: 21 additions & 16 deletions src/storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
)
Expand All @@ -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)),),
)
Expand All @@ -63,15 +67,15 @@ 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"]
)
yield ActType(code=j["code"], description=j["description"], court=court)

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()
Expand All @@ -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):
Expand Down
15 changes: 10 additions & 5 deletions test/test_storage.py
Original file line number Diff line number Diff line change
Expand Up @@ -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")


Expand All @@ -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")


Expand Down Expand Up @@ -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")

0 comments on commit 084e8ce

Please sign in to comment.