@@ -518,7 +518,8 @@ final class StatsStoreV4Tests: XCTestCase {
518518 let action = StatsActionV4 . retrieveSiteSummaryStats ( siteID: self . sampleSiteID,
519519 period: . day,
520520 quantity: 1 ,
521- latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-09T17:06:55 " ) ) { result in
521+ latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-09T17:06:55 " ) ,
522+ saveInStorage: false ) { result in
522523 promise ( result)
523524 }
524525 store. onAction ( action)
@@ -541,7 +542,8 @@ final class StatsStoreV4Tests: XCTestCase {
541542 let action = StatsActionV4 . retrieveSiteSummaryStats ( siteID: self . sampleSiteID,
542543 period: . month,
543544 quantity: 3 ,
544- latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-31T17:06:55 " ) ) { _ in
545+ latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-31T17:06:55 " ) ,
546+ saveInStorage: false ) { _ in
545547 promise ( ( ) )
546548 }
547549 store. onAction ( action)
@@ -567,7 +569,8 @@ final class StatsStoreV4Tests: XCTestCase {
567569 let action = StatsActionV4 . retrieveSiteSummaryStats ( siteID: self . sampleSiteID,
568570 period: . month,
569571 quantity: 3 ,
570- latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-31T17:06:55 " ) ) { result in
572+ latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-31T17:06:55 " ) ,
573+ saveInStorage: false ) { result in
571574 promise ( result)
572575 }
573576 store. onAction ( action)
@@ -591,7 +594,8 @@ final class StatsStoreV4Tests: XCTestCase {
591594 let action = StatsActionV4 . retrieveSiteSummaryStats ( siteID: self . sampleSiteID,
592595 period: . day,
593596 quantity: 1 ,
594- latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-09T17:06:55 " ) ) { result in
597+ latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-09T17:06:55 " ) ,
598+ saveInStorage: false ) { result in
595599 promise ( result)
596600 }
597601 store. onAction ( action)
@@ -612,7 +616,8 @@ final class StatsStoreV4Tests: XCTestCase {
612616 let action = StatsActionV4 . retrieveSiteSummaryStats ( siteID: self . sampleSiteID,
613617 period: . day,
614618 quantity: 1 ,
615- latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-09T17:06:55 " ) ) { result in
619+ latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-09T17:06:55 " ) ,
620+ saveInStorage: false ) { result in
616621 promise ( result)
617622 }
618623 store. onAction ( action)
@@ -621,6 +626,50 @@ final class StatsStoreV4Tests: XCTestCase {
621626 // Then
622627 XCTAssertTrue ( result. isFailure)
623628 }
629+
630+ /// Verifies that `StatsActionV4.retrieveSiteSummaryStats` effectively persists any retrieved SiteSummaryStats.
631+ ///
632+ func test_retrieveSiteSummaryStats_effectively_persists_retrieved_stats( ) {
633+ // Given
634+ let store = StatsStoreV4 ( dispatcher: dispatcher, storageManager: storageManager, network: network)
635+ network. simulateResponse ( requestUrlSuffix: " sites/ \( sampleSiteID) /stats/summary/ " , filename: " site-summary-stats " )
636+ XCTAssertEqual ( viewStorage. countObjects ( ofType: Storage . SiteSummaryStats. self) , 0 )
637+
638+ // When
639+ let result : Result < Networking . SiteSummaryStats , Error > = waitFor { promise in
640+ let action = StatsActionV4 . retrieveSiteSummaryStats ( siteID: self . sampleSiteID,
641+ period: . day,
642+ quantity: 1 ,
643+ latestDateToInclude: DateFormatter . dateFromString ( with: " 2022-12-09T17:06:55 " ) ,
644+ saveInStorage: true ) { result in
645+ promise ( result)
646+ }
647+ store. onAction ( action)
648+ }
649+
650+ // Then
651+ XCTAssertTrue ( result. isSuccess)
652+ XCTAssertEqual ( viewStorage. countObjects ( ofType: Storage . SiteSummaryStats. self) , 1 )
653+
654+ let readOnlySiteSummaryStats = viewStorage. firstObject ( ofType: Storage . SiteSummaryStats. self) ? . toReadOnly ( )
655+ XCTAssertEqual ( readOnlySiteSummaryStats, sampleSiteSummaryStats ( ) )
656+ }
657+
658+ /// Verifies that `upsertStoredSiteSummaryStats` does not produce duplicate entries.
659+ ///
660+ func test_upsertStoredSiteSummaryStats_effectively_updates_preexistant_SiteSummaryStats( ) {
661+ let statsStore = StatsStoreV4 ( dispatcher: dispatcher, storageManager: storageManager, network: network)
662+
663+ XCTAssertNil ( viewStorage. loadSiteSummaryStats ( date: " 2022-12-09 " , period: StatGranularity . day. rawValue) )
664+ statsStore. upsertStoredSiteSummaryStats ( readOnlyStats: sampleSiteSummaryStats ( ) )
665+ XCTAssertEqual ( viewStorage. countObjects ( ofType: Storage . SiteSummaryStats. self) , 1 )
666+ statsStore. upsertStoredSiteSummaryStats ( readOnlyStats: sampleSiteSummaryStatsMutated ( ) )
667+ XCTAssertEqual ( viewStorage. countObjects ( ofType: Storage . SiteSummaryStats. self) , 1 )
668+
669+ let expectedSiteSummaryStats = sampleSiteSummaryStatsMutated ( )
670+ let storageSiteSummaryStats = viewStorage. loadSiteSummaryStats ( date: " 2022-12-09 " , period: StatGranularity . day. rawValue)
671+ XCTAssertEqual ( storageSiteSummaryStats? . toReadOnly ( ) , expectedSiteSummaryStats)
672+ }
624673}
625674
626675
@@ -812,6 +861,14 @@ private extension StatsStoreV4Tests {
812861 views: 123 )
813862 }
814863
864+ func sampleSiteSummaryStatsMutated( ) -> Networking . SiteSummaryStats {
865+ return SiteSummaryStats ( siteID: sampleSiteID,
866+ date: " 2022-12-09 " ,
867+ period: . day,
868+ visitors: 15 ,
869+ views: 127 )
870+ }
871+
815872 func sampleSiteSummaryStatsQuarter( ) -> Networking . SiteSummaryStats {
816873 return SiteSummaryStats ( siteID: sampleSiteID,
817874 date: " 2022-12-09 " ,
0 commit comments