@@ -502,6 +502,146 @@ struct GRDBManagerTests {
502502 }
503503 }
504504 }
505+
506+ struct ResetTests {
507+ let manager : GRDBManager
508+ let sampleSiteID : Int64 = 1
509+
510+ init ( ) throws {
511+ self . manager = try GRDBManager ( )
512+ try manager. databaseConnection. write { db in
513+ let record = TestSite ( id: sampleSiteID)
514+ try record. insert ( db)
515+ }
516+ }
517+
518+ @Test ( " Reset clears all data from database " )
519+ func test_reset_clears_all_data_from_database( ) throws {
520+ // Given - Insert comprehensive test data
521+ try manager. databaseConnection. write { db in
522+ // Insert second site for more comprehensive test
523+ let site2 = TestSite ( id: 2 )
524+ try site2. insert ( db)
525+
526+ // Insert products for both sites
527+ for siteID in [ sampleSiteID, Int64 ( 2 ) ] {
528+ for i in 1 ... 2 {
529+ let product = TestProduct (
530+ siteID: siteID,
531+ id: Int64 ( i + ( siteID == 1 ? 0 : 10 ) ) ,
532+ name: " Product \( i) Site \( siteID) " ,
533+ productTypeKey: " variable " ,
534+ price: " \( i * 10 ) .00 " ,
535+ downloadable: false ,
536+ parentID: 0 ,
537+ manageStock: false ,
538+ stockStatusKey: " "
539+ )
540+ try product. insert ( db)
541+
542+ // Insert variations
543+ let variation = TestProductVariation (
544+ siteID: siteID,
545+ id: Int64 ( 200 + i + ( siteID == 1 ? 0 : 10 ) ) ,
546+ productID: product. id,
547+ price: " \( i * 12 ) .00 " ,
548+ downloadable: false ,
549+ manageStock: false ,
550+ stockStatusKey: " "
551+ )
552+ try variation. insert ( db)
553+
554+ // Insert product attributes
555+ let productAttribute = TestProductAttribute (
556+ productID: product. id,
557+ name: " Color \( i) " ,
558+ position: i,
559+ visible: true ,
560+ variation: true ,
561+ options: [ " Red " , " Blue " , " Green " ]
562+ )
563+ try productAttribute. insert ( db)
564+
565+ // Insert variation attributes
566+ let variationAttribute = TestProductVariationAttribute (
567+ productVariationID: variation. id,
568+ name: " Size \( i) " ,
569+ option: " Large "
570+ )
571+ try variationAttribute. insert ( db)
572+ }
573+ }
574+ }
575+
576+ // Verify data exists before reset
577+ let countsBefore = try manager. databaseConnection. read { db in
578+ return (
579+ sites: try TestSite . fetchCount ( db) ,
580+ products: try TestProduct . fetchCount ( db) ,
581+ variations: try TestProductVariation . fetchCount ( db) ,
582+ productAttributes: try TestProductAttribute . fetchCount ( db) ,
583+ variationAttributes: try TestProductVariationAttribute . fetchCount ( db)
584+ )
585+ }
586+
587+ #expect( countsBefore. sites == 2 ) // Original site + new site
588+ #expect( countsBefore. products == 4 )
589+ #expect( countsBefore. variations == 4 )
590+ #expect( countsBefore. productAttributes == 4 )
591+ #expect( countsBefore. variationAttributes == 4 )
592+
593+ // When - Reset database
594+ try manager. reset ( )
595+
596+ // Then - All data should be cleared
597+ let countsAfter = try manager. databaseConnection. read { db in
598+ return (
599+ sites: try TestSite . fetchCount ( db) ,
600+ products: try TestProduct . fetchCount ( db) ,
601+ variations: try TestProductVariation . fetchCount ( db) ,
602+ productAttributes: try TestProductAttribute . fetchCount ( db) ,
603+ variationAttributes: try TestProductVariationAttribute . fetchCount ( db)
604+ )
605+ }
606+
607+ #expect( countsAfter. sites == 0 )
608+ #expect( countsAfter. products == 0 )
609+ #expect( countsAfter. variations == 0 )
610+ #expect( countsAfter. productAttributes == 0 )
611+ #expect( countsAfter. variationAttributes == 0 )
612+ }
613+
614+ @Test ( " Reset can be called multiple times without error " )
615+ func test_reset_can_be_called_multiple_times( ) throws {
616+ // Given - Some test data
617+ try manager. databaseConnection. write { db in
618+ let product = TestProduct (
619+ siteID: sampleSiteID,
620+ id: 100 ,
621+ name: " Test Product " ,
622+ productTypeKey: " simple " ,
623+ price: " 10.00 " ,
624+ downloadable: false ,
625+ parentID: 0 ,
626+ manageStock: false ,
627+ stockStatusKey: " "
628+ )
629+ try product. insert ( db)
630+ }
631+
632+ // When - Reset multiple times
633+ try manager. reset ( )
634+ try manager. reset ( )
635+ try manager. reset ( )
636+
637+ // Then - Should not throw and database should be empty
638+ let productCount = try manager. databaseConnection. read { db in
639+ try TestProduct . fetchCount ( db)
640+ }
641+
642+ #expect( productCount == 0 )
643+ }
644+ }
505645}
506646
507647// MARK: - Test Models
0 commit comments