@@ -2244,3 +2244,93 @@ describe('ErrsoleSQLite - deleteExpiredNotificationItems', () => {
2244
2244
expect ( errsoleSQLite . deleteExpiredNotificationItemsRunning ) . toBe ( false ) ;
2245
2245
} ) ;
2246
2246
} ) ;
2247
+
2248
+ describe ( 'ErrsoleSQLite - deleteAllLogs' , ( ) => {
2249
+ let errsoleSQLite ;
2250
+
2251
+ beforeEach ( ( ) => {
2252
+ errsoleSQLite = new ErrsoleSQLite ( ':memory:' ) ;
2253
+
2254
+ jest . spyOn ( errsoleSQLite . db , 'run' ) . mockImplementation ( ( query , callback ) => {
2255
+ callback ( null ) ;
2256
+ } ) ;
2257
+ errsoleSQLite . logsTable = 'errsole_logs_v2' ;
2258
+ errsoleSQLite . createLogsTableQuery = 'CREATE TABLE IF NOT EXISTS errsole_logs_v2 (id INTEGER PRIMARY KEY AUTOINCREMENT, message TEXT)' ;
2259
+ } ) ;
2260
+
2261
+ afterEach ( ( ) => {
2262
+ jest . clearAllMocks ( ) ;
2263
+ } ) ;
2264
+
2265
+ it ( 'should delete all logs successfully' , async ( ) => {
2266
+ await expect ( errsoleSQLite . deleteAllLogs ( ) ) . resolves . toEqual ( { } ) ;
2267
+
2268
+ expect ( errsoleSQLite . db . run ) . toHaveBeenCalledTimes ( 4 ) ;
2269
+
2270
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 1 , 'BEGIN TRANSACTION;' , expect . any ( Function ) ) ;
2271
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 2 , `DROP TABLE IF EXISTS ${ errsoleSQLite . logsTable } ;` , expect . any ( Function ) ) ;
2272
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 3 , errsoleSQLite . createLogsTableQuery , expect . any ( Function ) ) ;
2273
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 4 , 'COMMIT;' , expect . any ( Function ) ) ;
2274
+ } ) ;
2275
+
2276
+ it ( 'should handle errors when starting the transaction' , async ( ) => {
2277
+ const mockError = new Error ( 'Transaction error' ) ;
2278
+
2279
+ errsoleSQLite . db . run . mockImplementationOnce ( ( query , callback ) => {
2280
+ callback ( mockError ) ;
2281
+ } ) ;
2282
+
2283
+ await expect ( errsoleSQLite . deleteAllLogs ( ) ) . rejects . toThrow ( 'Transaction error' ) ;
2284
+
2285
+ expect ( errsoleSQLite . db . run ) . toHaveBeenCalledTimes ( 1 ) ;
2286
+ expect ( errsoleSQLite . db . run ) . toHaveBeenCalledWith ( 'BEGIN TRANSACTION;' , expect . any ( Function ) ) ;
2287
+ } ) ;
2288
+
2289
+ it ( 'should handle errors when dropping the logs table' , async ( ) => {
2290
+ const mockError = new Error ( 'Drop table error' ) ;
2291
+
2292
+ errsoleSQLite . db . run
2293
+ . mockImplementationOnce ( ( query , callback ) => callback ( null ) )
2294
+ . mockImplementationOnce ( ( query , callback ) => callback ( mockError ) ) ;
2295
+
2296
+ await expect ( errsoleSQLite . deleteAllLogs ( ) ) . rejects . toThrow ( 'Drop table error' ) ;
2297
+
2298
+ expect ( errsoleSQLite . db . run ) . toHaveBeenCalledTimes ( 2 ) ;
2299
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 1 , 'BEGIN TRANSACTION;' , expect . any ( Function ) ) ;
2300
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 2 , `DROP TABLE IF EXISTS ${ errsoleSQLite . logsTable } ;` , expect . any ( Function ) ) ;
2301
+ } ) ;
2302
+
2303
+ it ( 'should handle errors when recreating the logs table' , async ( ) => {
2304
+ const mockError = new Error ( 'Create table error' ) ;
2305
+
2306
+ errsoleSQLite . db . run
2307
+ . mockImplementationOnce ( ( query , callback ) => callback ( null ) )
2308
+ . mockImplementationOnce ( ( query , callback ) => callback ( null ) )
2309
+ . mockImplementationOnce ( ( query , callback ) => callback ( mockError ) ) ;
2310
+
2311
+ await expect ( errsoleSQLite . deleteAllLogs ( ) ) . rejects . toThrow ( 'Create table error' ) ;
2312
+
2313
+ expect ( errsoleSQLite . db . run ) . toHaveBeenCalledTimes ( 3 ) ;
2314
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 1 , 'BEGIN TRANSACTION;' , expect . any ( Function ) ) ;
2315
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 2 , `DROP TABLE IF EXISTS ${ errsoleSQLite . logsTable } ;` , expect . any ( Function ) ) ;
2316
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 3 , errsoleSQLite . createLogsTableQuery , expect . any ( Function ) ) ;
2317
+ } ) ;
2318
+
2319
+ it ( 'should handle errors when committing the transaction' , async ( ) => {
2320
+ const mockError = new Error ( 'Commit error' ) ;
2321
+
2322
+ errsoleSQLite . db . run
2323
+ . mockImplementationOnce ( ( query , callback ) => callback ( null ) )
2324
+ . mockImplementationOnce ( ( query , callback ) => callback ( null ) )
2325
+ . mockImplementationOnce ( ( query , callback ) => callback ( null ) )
2326
+ . mockImplementationOnce ( ( query , callback ) => callback ( mockError ) ) ;
2327
+
2328
+ await expect ( errsoleSQLite . deleteAllLogs ( ) ) . rejects . toThrow ( 'Commit error' ) ;
2329
+
2330
+ expect ( errsoleSQLite . db . run ) . toHaveBeenCalledTimes ( 4 ) ;
2331
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 1 , 'BEGIN TRANSACTION;' , expect . any ( Function ) ) ;
2332
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 2 , `DROP TABLE IF EXISTS ${ errsoleSQLite . logsTable } ;` , expect . any ( Function ) ) ;
2333
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 3 , errsoleSQLite . createLogsTableQuery , expect . any ( Function ) ) ;
2334
+ expect ( errsoleSQLite . db . run ) . toHaveBeenNthCalledWith ( 4 , 'COMMIT;' , expect . any ( Function ) ) ;
2335
+ } ) ;
2336
+ } ) ;
0 commit comments