@@ -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