From ca7dbc26e385618eec595bd23b3dc23d56a81284 Mon Sep 17 00:00:00 2001 From: rachelmcr Date: Mon, 12 Dec 2022 16:18:01 +0000 Subject: [PATCH 1/4] Compare visitors and views separately when sorting SiteVisitStatsItem --- Networking/Networking/Model/Stats/SiteVisitStatsItem.swift | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/Networking/Networking/Model/Stats/SiteVisitStatsItem.swift b/Networking/Networking/Model/Stats/SiteVisitStatsItem.swift index 70503f976ce..cbda76d0be6 100644 --- a/Networking/Networking/Model/Stats/SiteVisitStatsItem.swift +++ b/Networking/Networking/Model/Stats/SiteVisitStatsItem.swift @@ -23,6 +23,7 @@ public struct SiteVisitStatsItem: Equatable, GeneratedCopiable, GeneratedFakeabl extension SiteVisitStatsItem: Comparable { public static func < (lhs: SiteVisitStatsItem, rhs: SiteVisitStatsItem) -> Bool { return lhs.period < rhs.period || - (lhs.period == rhs.period && lhs.visitors < rhs.visitors && lhs.views < rhs.views) + (lhs.period == rhs.period && lhs.visitors < rhs.visitors) || + (lhs.period == rhs.period && lhs.visitors == rhs.visitors && lhs.views < rhs.views) } } From 309a5edaa7385c72a9ffb7ee8a0e49445ad30047 Mon Sep 17 00:00:00 2001 From: rachelmcr Date: Mon, 12 Dec 2022 15:28:33 +0000 Subject: [PATCH 2/4] Add Core Data Model 79 --- Storage/Storage.xcodeproj/project.pbxproj | 4 +- .../.xccurrentversion | 2 +- .../Model 79.xcdatamodel/contents | 839 ++++++++++++++++++ 3 files changed, 843 insertions(+), 2 deletions(-) create mode 100644 Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 79.xcdatamodel/contents diff --git a/Storage/Storage.xcodeproj/project.pbxproj b/Storage/Storage.xcodeproj/project.pbxproj index fdd68fa87d6..d044b95feb4 100644 --- a/Storage/Storage.xcodeproj/project.pbxproj +++ b/Storage/Storage.xcodeproj/project.pbxproj @@ -433,6 +433,7 @@ CC6A05402877012B002C144E /* Model 71.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Model 71.xcdatamodel"; sourceTree = ""; }; CC6A054128770933002C144E /* OrderMetaData+CoreDataClass.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OrderMetaData+CoreDataClass.swift"; sourceTree = ""; }; CC6A054228770933002C144E /* OrderMetaData+CoreDataProperties.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "OrderMetaData+CoreDataProperties.swift"; sourceTree = ""; }; + CCA1D60B29477F5E00B40560 /* Model 79.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Model 79.xcdatamodel"; sourceTree = ""; }; CCBEBD3F27C68E660010C96F /* FeatureIcon.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = FeatureIcon.swift; sourceTree = ""; }; CCD2E70625DE9AAA00BD975D /* WooCommerceModelV45toV46.xcmappingmodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcmappingmodel; path = WooCommerceModelV45toV46.xcmappingmodel; sourceTree = ""; }; CCF3209E2927EBEE002114B1 /* Model 78.xcdatamodel */ = {isa = PBXFileReference; lastKnownFileType = wrapper.xcdatamodel; path = "Model 78.xcdatamodel"; sourceTree = ""; }; @@ -1816,6 +1817,7 @@ DEC51AA4275B41BE009F3DF4 /* WooCommerce.xcdatamodeld */ = { isa = XCVersionGroup; children = ( + CCA1D60B29477F5E00B40560 /* Model 79.xcdatamodel */, CCF3209E2927EBEE002114B1 /* Model 78.xcdatamodel */, AE7DF9FA2919023100C4D1ED /* Model 77.xcdatamodel */, AEC4481B290853C300BAA299 /* Model 76.xcdatamodel */, @@ -1895,7 +1897,7 @@ DEC51ADE275B41BE009F3DF4 /* Model 47.xcdatamodel */, DEC51ADF275B41BE009F3DF4 /* Model 19.xcdatamodel */, ); - currentVersion = CCF3209E2927EBEE002114B1 /* Model 78.xcdatamodel */; + currentVersion = CCA1D60B29477F5E00B40560 /* Model 79.xcdatamodel */; path = WooCommerce.xcdatamodeld; sourceTree = ""; versionGroupType = wrapper.xcdatamodel; diff --git a/Storage/Storage/Model/WooCommerce.xcdatamodeld/.xccurrentversion b/Storage/Storage/Model/WooCommerce.xcdatamodeld/.xccurrentversion index 8a5c8fddfcf..c0a627929f0 100644 --- a/Storage/Storage/Model/WooCommerce.xcdatamodeld/.xccurrentversion +++ b/Storage/Storage/Model/WooCommerce.xcdatamodeld/.xccurrentversion @@ -3,6 +3,6 @@ _XCCurrentVersionName - Model 78.xcdatamodel + Model 79.xcdatamodel diff --git a/Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 79.xcdatamodel/contents b/Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 79.xcdatamodel/contents new file mode 100644 index 00000000000..aad91aa9eb4 --- /dev/null +++ b/Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 79.xcdatamodel/contents @@ -0,0 +1,839 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 5c6ac58573736ae929d56a36c5b2b4fc1c716850 Mon Sep 17 00:00:00 2001 From: rachelmcr Date: Mon, 12 Dec 2022 15:48:47 +0000 Subject: [PATCH 3/4] Add views attribute to SiteVisitStatsItem in storage --- Networking/NetworkingTests/Responses/site-visits-alt.json | 4 ++-- Storage/Storage/Model/MIGRATIONS.md | 4 ++++ .../Model/SiteVisitStatsItem+CoreDataProperties.swift | 1 + .../Model 79.xcdatamodel/contents | 1 + .../Storage/SiteVisitStatsItem+ReadOnlyConvertible.swift | 3 ++- Yosemite/YosemiteTests/Stores/StatsStoreV4Tests.swift | 8 ++++---- 6 files changed, 14 insertions(+), 7 deletions(-) diff --git a/Networking/NetworkingTests/Responses/site-visits-alt.json b/Networking/NetworkingTests/Responses/site-visits-alt.json index 27ea4b7345b..4bae47337ac 100644 --- a/Networking/NetworkingTests/Responses/site-visits-alt.json +++ b/Networking/NetworkingTests/Responses/site-visits-alt.json @@ -13,7 +13,7 @@ "data": [ [ "2014-01-01", - 12821, + 12831, 1140, 1094, 0, @@ -22,7 +22,7 @@ ], [ "2015-01-01", - 14808, + 14818, 1634, 1492, 0, diff --git a/Storage/Storage/Model/MIGRATIONS.md b/Storage/Storage/Model/MIGRATIONS.md index 054467c54ad..4f72daf1d03 100644 --- a/Storage/Storage/Model/MIGRATIONS.md +++ b/Storage/Storage/Model/MIGRATIONS.md @@ -2,6 +2,10 @@ This file documents changes in the WCiOS Storage data model. Please explain any changes to the data model as well as any custom migrations. +## Model 79 (Release 11.7.0.0) +- @rachelmcr 2022-12-12 + - Added `views` attribute to `SiteVisitStatsItem` entity. + ## Model 78 (Release 11.4.0.0) - @rachelmcr 2022-11-18 - Added `averageOrderValue` attribute to `OrderStatsV4Totals` entity. diff --git a/Storage/Storage/Model/SiteVisitStatsItem+CoreDataProperties.swift b/Storage/Storage/Model/SiteVisitStatsItem+CoreDataProperties.swift index dcd20c3fe99..dd7338c7cc3 100644 --- a/Storage/Storage/Model/SiteVisitStatsItem+CoreDataProperties.swift +++ b/Storage/Storage/Model/SiteVisitStatsItem+CoreDataProperties.swift @@ -10,5 +10,6 @@ extension SiteVisitStatsItem { @NSManaged public var period: String? @NSManaged public var visitors: Int64 + @NSManaged public var views: Int64 @NSManaged public var stats: SiteVisitStats? } diff --git a/Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 79.xcdatamodel/contents b/Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 79.xcdatamodel/contents index aad91aa9eb4..15d22f67521 100644 --- a/Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 79.xcdatamodel/contents +++ b/Storage/Storage/Model/WooCommerce.xcdatamodeld/Model 79.xcdatamodel/contents @@ -756,6 +756,7 @@ + diff --git a/Yosemite/Yosemite/Model/Storage/SiteVisitStatsItem+ReadOnlyConvertible.swift b/Yosemite/Yosemite/Model/Storage/SiteVisitStatsItem+ReadOnlyConvertible.swift index f6ba76fce56..7754db3614a 100644 --- a/Yosemite/Yosemite/Model/Storage/SiteVisitStatsItem+ReadOnlyConvertible.swift +++ b/Yosemite/Yosemite/Model/Storage/SiteVisitStatsItem+ReadOnlyConvertible.swift @@ -11,6 +11,7 @@ extension Storage.SiteVisitStatsItem: ReadOnlyConvertible { public func update(with statsItem: Yosemite.SiteVisitStatsItem) { period = statsItem.period visitors = Int64(statsItem.visitors) + views = Int64(statsItem.views) } /// Returns a ReadOnly version of the receiver. @@ -18,6 +19,6 @@ extension Storage.SiteVisitStatsItem: ReadOnlyConvertible { public func toReadOnly() -> Yosemite.SiteVisitStatsItem { return SiteVisitStatsItem(period: period ?? "", visitors: Int(visitors), - views: 0) // TODO-8363: Update Storage with views count + views: Int(views)) } } diff --git a/Yosemite/YosemiteTests/Stores/StatsStoreV4Tests.swift b/Yosemite/YosemiteTests/Stores/StatsStoreV4Tests.swift index 824fea7cfa6..af91cde7bfb 100644 --- a/Yosemite/YosemiteTests/Stores/StatsStoreV4Tests.swift +++ b/Yosemite/YosemiteTests/Stores/StatsStoreV4Tests.swift @@ -603,11 +603,11 @@ private extension StatsStoreV4Tests { func sampleSiteVisitStatsItem1() -> Networking.SiteVisitStatsItem { - return SiteVisitStatsItem(period: "2014-01-01", visitors: 1135, views: 0) + return SiteVisitStatsItem(period: "2014-01-01", visitors: 1135, views: 12821) } func sampleSiteVisitStatsItem2() -> Networking.SiteVisitStatsItem { - return SiteVisitStatsItem(period: "2015-01-01", visitors: 1629, views: 0) + return SiteVisitStatsItem(period: "2015-01-01", visitors: 1629, views: 14808) } func sampleSiteVisitStatsMutated() -> Networking.SiteVisitStats { @@ -619,11 +619,11 @@ private extension StatsStoreV4Tests { func sampleSiteVisitStatsItem1Mutated() -> Networking.SiteVisitStatsItem { - return SiteVisitStatsItem(period: "2014-01-01", visitors: 1140, views: 0) + return SiteVisitStatsItem(period: "2014-01-01", visitors: 1140, views: 12831) } func sampleSiteVisitStatsItem2Mutated() -> Networking.SiteVisitStatsItem { - return SiteVisitStatsItem(period: "2015-01-01", visitors: 1634, views: 0) + return SiteVisitStatsItem(period: "2015-01-01", visitors: 1634, views: 14818) } // MARK: - Top Earner Stats Sample From 3935f3ce7285bcefd71d3133f77e49ee15c15e88 Mon Sep 17 00:00:00 2001 From: rachelmcr Date: Mon, 12 Dec 2022 16:38:11 +0000 Subject: [PATCH 4/4] Add migration test for Model 78 to 79 --- .../CoreData/MigrationTests.swift | 36 +++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/Storage/StorageTests/CoreData/MigrationTests.swift b/Storage/StorageTests/CoreData/MigrationTests.swift index 8b301228930..7395d4579b6 100644 --- a/Storage/StorageTests/CoreData/MigrationTests.swift +++ b/Storage/StorageTests/CoreData/MigrationTests.swift @@ -1524,6 +1524,34 @@ final class MigrationTests: XCTestCase { let newAverageOrderValue = try XCTUnwrap(migratedOrderStatsV4Totals.value(forKey: "averageOrderValue") as? Double) XCTAssertEqual(newAverageOrderValue, averageOrderValue) } + + func test_migrating_from_78_to_79_adds_views_attribute() throws { + // Given + let sourceContainer = try startPersistentContainer("Model 78") + let sourceContext = sourceContainer.viewContext + + let siteVisitStatsItem = insertSiteVisitStatsItem(to: sourceContainer.viewContext) + try sourceContext.save() + + XCTAssertNil(siteVisitStatsItem.entity.attributesByName["views"]) + + // When + let targetContainer = try migrate(sourceContainer, to: "Model 79") + let targetContext = targetContainer.viewContext + + let migratedSiteVisitStatsItem = try XCTUnwrap(targetContext.first(entityName: "SiteVisitStatsItem")) + let defaultViewsCount = try XCTUnwrap(migratedSiteVisitStatsItem.value(forKey: "views") as? Int) + + let viewsCount = 12 + migratedSiteVisitStatsItem.setValue(viewsCount, forKey: "views") + + // Then + // Default value is 0. + XCTAssertEqual(defaultViewsCount, 0) + + let newViewsCount = try XCTUnwrap(migratedSiteVisitStatsItem.value(forKey: "views") as? Int) + XCTAssertEqual(newViewsCount, viewsCount) + } } // MARK: - Persistent Store Setup and Migrations @@ -1943,6 +1971,14 @@ private extension MigrationTests { ]) } + @discardableResult + func insertSiteVisitStatsItem(to context: NSManagedObjectContext) -> NSManagedObject { + context.insert(entityName: "SiteVisitStatsItem", properties: [ + "period": "day", + "visitors": 3 + ]) + } + @discardableResult func insertTopEarnerStats(to context: NSManagedObjectContext) -> NSManagedObject { context.insert(entityName: "TopEarnerStats", properties: [