Skip to content

Commit 05d7048

Browse files
authored
Overwrite row with identical job id as pending (#142)
1 parent 10dc84b commit 05d7048

3 files changed

Lines changed: 68 additions & 10 deletions

File tree

printer/modules/sqlite_helpers.py

Lines changed: 20 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,14 +35,27 @@ def maybe_create_table(sqlite_file: str) -> bool:
3535

3636
def insert_print_job(sqlite_file: str, job_id: str):
3737
try:
38-
with sqlite3.connect(sqlite_file, timeout=10.0) as db:
39-
cursor = db.cursor()
40-
timestamp = datetime.datetime.now()
41-
db = sqlite3.connect(sqlite_file)
42-
sql = "INSERT INTO logs (job_id) VALUES (?)"
38+
with sqlite3.connect(sqlite_file, timeout=10.0) as conn:
39+
cursor = conn.cursor()
40+
41+
"""
42+
yes i know we could do
43+
44+
INSERT INTO logs (job_id, status, date)
45+
VALUES (?, 'created', CURRENT_TIMESTAMP)
46+
ON CONFLICT(job_id) DO UPDATE SET
47+
status = excluded.status,
48+
date = excluded.date;
49+
50+
but i dont care i dont feel like updating the schema on the real server
51+
"""
52+
cursor.execute("DELETE FROM logs WHERE job_id = ?", (job_id,))
53+
54+
sql = "INSERT INTO logs (job_id, status, date) VALUES (?, 'created', CURRENT_TIMESTAMP)"
4355
cursor.execute(sql, (job_id,))
44-
db.commit()
45-
return timestamp
56+
57+
conn.commit()
58+
return datetime.datetime.now()
4659
except sqlite3.IntegrityError:
4760
return None
4861
except Exception:

printer/test/test_lpstat.py

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,15 @@ def test_poll_lpstat(
8787
self.assertEqual(str(cm.exception), "stop loop")
8888

8989
# Check correct jobs marked as completed and acknowledged
90-
mock_mark_completed.assert_called_once_with(
91-
"dummy.db", ["HP_LaserJet_p2015dn_Right-53", "HP_LaserJet_p2015dn_Right-54"],
92-
)
90+
mock_mark_completed.assert_called_once()
91+
92+
# Extract the arguments from the call
93+
# call_args is a tuple of (args, kwargs)
94+
args, _ = mock_mark_completed.call_args
95+
db_name, job_list = args
96+
97+
self.assertEqual(db_name, "dummy.db")
98+
self.assertCountEqual(job_list, ["HP_LaserJet_p2015dn_Right-53", "HP_LaserJet_p2015dn_Right-54"])
9399

94100

95101
if __name__ == "__main__":

printer/test/test_sqlite.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,45 @@ def test_insert_log(self):
5757
self.assertEqual(job_id, self.EXAMPLE_JOB_ID)
5858
self.assertEqual(status, "created")
5959

60+
def test_insert_log_overwrites_existing(self):
61+
tmp = tempfile.NamedTemporaryFile(delete=False)
62+
db_path = tmp.name
63+
tmp.close()
64+
sqlite_helpers.maybe_create_table(db_path)
65+
66+
date_in_2023 = '2023-12-24 23:50:00'
67+
68+
with sqlite3.connect(db_path) as db:
69+
cursor = db.cursor()
70+
cursor.execute(
71+
"INSERT INTO logs (job_id, status, date) VALUES (?, ?, '2023-12-24 23:50:00')",
72+
(self.EXAMPLE_JOB_ID, "completed")
73+
)
74+
db.commit()
75+
76+
new_timestamp = sqlite_helpers.insert_print_job(db_path, self.EXAMPLE_JOB_ID)
77+
self.assertIsNotNone(new_timestamp)
78+
79+
with sqlite3.connect(db_path) as db:
80+
cursor = db.cursor()
81+
82+
cursor.execute("SELECT date, job_id, status FROM logs WHERE job_id = ?", (self.EXAMPLE_JOB_ID,))
83+
rows = cursor.fetchall()
84+
85+
self.assertEqual(len(rows), 1, "Database should not have duplicate job_ids")
86+
87+
(db_date_str, _, db_status) = rows[0]
88+
89+
self.assertEqual(db_status, "created", "Status has been reset to 'created'")
90+
91+
self.assertNotEqual(db_date_str, date_in_2023, "The date should have been updated to now")
92+
93+
current_year = str(datetime.datetime.now().year)
94+
self.assertIn(current_year, db_date_str, f"Expected timestamp to contain {current_year}")
95+
96+
# Cleanup
97+
os.unlink(db_path)
98+
6099
def test_mark_jobs_acknowledged(self):
61100
tmp = tempfile.NamedTemporaryFile(delete=False)
62101
db_path = tmp.name

0 commit comments

Comments
 (0)