From a67a5261fcfbd33d8c66c67157107f6a008d351d Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Fri, 12 May 2023 08:48:17 -0400 Subject: [PATCH 01/21] Initial commit --- .../Responses/OfferingsResponse.swift | 2 + Sources/Purchasing/Offering.swift | 15 +++++- Sources/Purchasing/OfferingsFactory.swift | 1 + Tests/BackendIntegrationTests/Constants.swift | 2 +- .../LoadShedderIntegrationTests.swift | 1 + .../StoreKitIntegrationTests.swift | 1 + .../Fixtures/OfferingsWithMetadata.json | 49 +++++++++++++++++++ .../Fixtures/OfferingsWithNullMetadata.json | 31 ++++++++++++ .../Responses/OfferingsDecodingTests.swift | 2 + 9 files changed, 102 insertions(+), 2 deletions(-) create mode 100644 Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithMetadata.json create mode 100644 Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithNullMetadata.json diff --git a/Sources/Networking/Responses/OfferingsResponse.swift b/Sources/Networking/Responses/OfferingsResponse.swift index 5fd39d7ded..d513b2d5a4 100644 --- a/Sources/Networking/Responses/OfferingsResponse.swift +++ b/Sources/Networking/Responses/OfferingsResponse.swift @@ -29,6 +29,8 @@ struct OfferingsResponse { let identifier: String let description: String let packages: [Package] + @DefaultDecodable.EmptyDictionary + var metadata: [String: AnyDecodable] } diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index fed7c3f09b..a20ecfc71a 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -14,6 +14,12 @@ import Foundation +extension Offering { + struct Metadata { + let data: [String: Any] + } +} + /** * An offering is a collection of ``Package``s, and they let you control which products * are shown to users without requiring an app update. @@ -38,6 +44,12 @@ import Foundation */ @objc public let serverDescription: String + /** + Offering metadata defined in RevenueCat dashboard. + */ + private let _metadata: Metadata + @objc var metadata: [String: Any] { self._metadata.data } + /** Array of ``Package`` objects available for purchase. */ @@ -110,10 +122,11 @@ import Foundation } // swiftlint:disable:next cyclomatic_complexity - init(identifier: String, serverDescription: String, availablePackages: [Package]) { + init(identifier: String, serverDescription: String, metadata: [String: Any], availablePackages: [Package]) { self.identifier = identifier self.serverDescription = serverDescription self.availablePackages = availablePackages + self._metadata = Metadata(data: metadata) var foundPackages: [PackageType: Package] = [:] diff --git a/Sources/Purchasing/OfferingsFactory.swift b/Sources/Purchasing/OfferingsFactory.swift index 203a6d71a8..f929ec744c 100644 --- a/Sources/Purchasing/OfferingsFactory.swift +++ b/Sources/Purchasing/OfferingsFactory.swift @@ -49,6 +49,7 @@ class OfferingsFactory { return Offering(identifier: offering.identifier, serverDescription: offering.description, + metadata: offering.metadata.mapValues(\.asAny), availablePackages: availablePackages) } diff --git a/Tests/BackendIntegrationTests/Constants.swift b/Tests/BackendIntegrationTests/Constants.swift index 392b6a2c97..d1ab823fda 100644 --- a/Tests/BackendIntegrationTests/Constants.swift +++ b/Tests/BackendIntegrationTests/Constants.swift @@ -14,7 +14,7 @@ enum Constants { static let loadShedderApiKey = "REVENUECAT_LOAD_SHEDDER_API_KEY" // Server URL for the tests. If set to empty string, we'll use the default URL. - static let proxyURL = "REVENUECAT_PROXY_URL" + static let proxyURL = "" static let userDefaultsSuiteName = "BackendIntegrationTests" static let storeKitConfigFileName = "RevenueCat_IntegrationPurchaseTesterConfiguration" diff --git a/Tests/BackendIntegrationTests/LoadShedderIntegrationTests.swift b/Tests/BackendIntegrationTests/LoadShedderIntegrationTests.swift index 4daf8ab5b8..1765dd0b1a 100644 --- a/Tests/BackendIntegrationTests/LoadShedderIntegrationTests.swift +++ b/Tests/BackendIntegrationTests/LoadShedderIntegrationTests.swift @@ -40,6 +40,7 @@ class LoadShedderStoreKit1IntegrationTests: BaseStoreKitIntegrationTests { } func testCanGetOfferings() async throws { + // JOSH HERE - add one for meta data let receivedOfferings = try await Purchases.shared.offerings() expect(receivedOfferings.all).toNot(beEmpty()) diff --git a/Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift b/Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift index 61cb45e376..c6ccaebb2a 100644 --- a/Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift +++ b/Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift @@ -39,6 +39,7 @@ class StoreKit1IntegrationTests: BaseStoreKitIntegrationTests { } func testCanGetOfferings() async throws { + // JOSH HERE - add one for meta data let receivedOfferings = try await Purchases.shared.offerings() expect(receivedOfferings.all).toNot(beEmpty()) diff --git a/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithMetadata.json b/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithMetadata.json new file mode 100644 index 0000000000..8d00f3f2ec --- /dev/null +++ b/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithMetadata.json @@ -0,0 +1,49 @@ +{ + "current_offering_id": "default", + "offerings": [ + { + "description": "standard set of packages", + "identifier": "default", + "metadata": { + "int": 5, + "double": 5.5, + "boolean": true, + "string": "five", + "array": ["five"], + "dictionary": { + "string": "five" + } + }, + "packages": [ + { + "identifier": "$rc_monthly", + "platform_product_identifier": "com.revenuecat.monthly_4.99.1_week_intro" + }, + { + "identifier": "$rc_annual", + "platform_product_identifier": "com.revenuecat.yearly_10.99.2_week_intro" + } + ] + }, + { + "description": "alternate offering", + "identifier": "alternate", + "metadata": { + "int": 5, + "double": 5.5, + "boolean": true, + "string": "five", + "array": ["five"], + "dictionary": { + "string": "five" + } + } + "packages": [ + { + "identifier": "$rc_lifetime", + "platform_product_identifier": "com.revenuecat.other_product" + } + ] + } + ] +} diff --git a/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithNullMetadata.json b/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithNullMetadata.json new file mode 100644 index 0000000000..9f51bbac87 --- /dev/null +++ b/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithNullMetadata.json @@ -0,0 +1,31 @@ +{ + "current_offering_id": "default", + "offerings": [ + { + "description": "standard set of packages", + "identifier": "default", + "metadata": null, + "packages": [ + { + "identifier": "$rc_monthly", + "platform_product_identifier": "com.revenuecat.monthly_4.99.1_week_intro" + }, + { + "identifier": "$rc_annual", + "platform_product_identifier": "com.revenuecat.yearly_10.99.2_week_intro" + } + ] + }, + { + "description": "alternate offering", + "identifier": "alternate", + "metadata": null, + "packages": [ + { + "identifier": "$rc_lifetime", + "platform_product_identifier": "com.revenuecat.other_product" + } + ] + } + ] +} diff --git a/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift b/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift index fbbea51267..7b8573f837 100644 --- a/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift +++ b/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift @@ -47,6 +47,8 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { expect(package2.platformProductIdentifier) == "com.revenuecat.yearly_10.99.2_week_intro" } + // + func testDecodesSecondOffering() throws { let offering = try XCTUnwrap(self.response.offerings.last) From 0fdd5b7731d136887e6c95ec8365e11f4d478bc6 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 16 May 2023 22:09:35 -0500 Subject: [PATCH 02/21] Need to make metadata public --- Sources/Purchasing/Offering.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index a20ecfc71a..48b855a80e 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -48,7 +48,7 @@ extension Offering { Offering metadata defined in RevenueCat dashboard. */ private let _metadata: Metadata - @objc var metadata: [String: Any] { self._metadata.data } + @objc public var metadata: [String: Any] { self._metadata.data } /** Array of ``Package`` objects available for purchase. From 0dbe39bb5f3bc8e4425c86f6abd281e5c2a76c1a Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Fri, 19 May 2023 15:57:06 -0500 Subject: [PATCH 03/21] Fixes --- Sources/Purchasing/Offering.swift | 14 ++++++++------ Tests/BackendIntegrationTests/Constants.swift | 2 +- .../LoadShedderIntegrationTests.swift | 1 - .../StoreKitIntegrationTests.swift | 1 - 4 files changed, 9 insertions(+), 9 deletions(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 48b855a80e..8705c90f6f 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -14,12 +14,6 @@ import Foundation -extension Offering { - struct Metadata { - let data: [String: Any] - } -} - /** * An offering is a collection of ``Package``s, and they let you control which products * are shown to users without requiring an app update. @@ -178,6 +172,14 @@ extension Offering { } +extension Offering { + + struct Metadata { + let data: [String: Any] + } + +} + extension Offering: Identifiable { /// The stable identity of the entity associated with this instance. diff --git a/Tests/BackendIntegrationTests/Constants.swift b/Tests/BackendIntegrationTests/Constants.swift index d1ab823fda..392b6a2c97 100644 --- a/Tests/BackendIntegrationTests/Constants.swift +++ b/Tests/BackendIntegrationTests/Constants.swift @@ -14,7 +14,7 @@ enum Constants { static let loadShedderApiKey = "REVENUECAT_LOAD_SHEDDER_API_KEY" // Server URL for the tests. If set to empty string, we'll use the default URL. - static let proxyURL = "" + static let proxyURL = "REVENUECAT_PROXY_URL" static let userDefaultsSuiteName = "BackendIntegrationTests" static let storeKitConfigFileName = "RevenueCat_IntegrationPurchaseTesterConfiguration" diff --git a/Tests/BackendIntegrationTests/LoadShedderIntegrationTests.swift b/Tests/BackendIntegrationTests/LoadShedderIntegrationTests.swift index 1765dd0b1a..4daf8ab5b8 100644 --- a/Tests/BackendIntegrationTests/LoadShedderIntegrationTests.swift +++ b/Tests/BackendIntegrationTests/LoadShedderIntegrationTests.swift @@ -40,7 +40,6 @@ class LoadShedderStoreKit1IntegrationTests: BaseStoreKitIntegrationTests { } func testCanGetOfferings() async throws { - // JOSH HERE - add one for meta data let receivedOfferings = try await Purchases.shared.offerings() expect(receivedOfferings.all).toNot(beEmpty()) diff --git a/Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift b/Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift index c6ccaebb2a..61cb45e376 100644 --- a/Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift +++ b/Tests/BackendIntegrationTests/StoreKitIntegrationTests.swift @@ -39,7 +39,6 @@ class StoreKit1IntegrationTests: BaseStoreKitIntegrationTests { } func testCanGetOfferings() async throws { - // JOSH HERE - add one for meta data let receivedOfferings = try await Purchases.shared.offerings() expect(receivedOfferings.all).toNot(beEmpty()) From 1d41b39e9f63d5c4299964aee4d909ee98dc0d66 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Fri, 19 May 2023 16:09:57 -0500 Subject: [PATCH 04/21] Fixed some lint and compile errors --- Sources/Purchasing/Offering.swift | 3 ++- Tests/UnitTests/Mocks/MockOfferingsFactory.swift | 1 + Tests/UnitTests/Purchasing/OfferingsManagerTests.swift | 1 + 3 files changed, 4 insertions(+), 1 deletion(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 8705c90f6f..a975a484ad 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -38,10 +38,11 @@ import Foundation */ @objc public let serverDescription: String + private let _metadata: Metadata + /** Offering metadata defined in RevenueCat dashboard. */ - private let _metadata: Metadata @objc public var metadata: [String: Any] { self._metadata.data } /** diff --git a/Tests/UnitTests/Mocks/MockOfferingsFactory.swift b/Tests/UnitTests/Mocks/MockOfferingsFactory.swift index c660592280..41bfbadf03 100644 --- a/Tests/UnitTests/Mocks/MockOfferingsFactory.swift +++ b/Tests/UnitTests/Mocks/MockOfferingsFactory.swift @@ -30,6 +30,7 @@ class MockOfferingsFactory: OfferingsFactory { "base": Offering( identifier: "base", serverDescription: "This is the base offering", + metadata: [:], availablePackages: [ Package(identifier: "$rc_monthly", packageType: PackageType.monthly, diff --git a/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift b/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift index 86c76eb3e0..0832af78a7 100644 --- a/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift @@ -495,6 +495,7 @@ private extension OfferingsManagerTests { Offering( identifier: offering.identifier, serverDescription: offering.description, + metadata: [:], availablePackages: offering.packages.map { package in .init( identifier: package.identifier, From 1b6932fb5aeb58e44ec12261152d3a8a46b60dab Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Fri, 19 May 2023 16:41:17 -0500 Subject: [PATCH 05/21] Tests for metadata decoding --- .../Responses/Fixtures/Offerings.json | 31 ++++++++++++ .../Fixtures/OfferingsWithMetadata.json | 49 ------------------- .../Fixtures/OfferingsWithNullMetadata.json | 31 ------------ .../Responses/OfferingsDecodingTests.swift | 45 +++++++++++++++-- 4 files changed, 72 insertions(+), 84 deletions(-) delete mode 100644 Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithMetadata.json delete mode 100644 Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithNullMetadata.json diff --git a/Tests/UnitTests/Networking/Responses/Fixtures/Offerings.json b/Tests/UnitTests/Networking/Responses/Fixtures/Offerings.json index e815753a5b..9b84d11293 100644 --- a/Tests/UnitTests/Networking/Responses/Fixtures/Offerings.json +++ b/Tests/UnitTests/Networking/Responses/Fixtures/Offerings.json @@ -24,6 +24,37 @@ "platform_product_identifier": "com.revenuecat.other_product" } ] + }, + { + "description": "offering with metadata", + "identifier": "metadata", + "metadata": { + "int": 5, + "double": 5.5, + "boolean": true, + "string": "five", + "array": ["five"], + "dictionary": { + "string": "five" + } + }, + "packages": [ + { + "identifier": "$rc_lifetime", + "platform_product_identifier": "com.revenuecat.other_product" + } + ] + }, + { + "description": "offering with null metadata", + "identifier": "nullmetadata", + "metadata": null, + "packages": [ + { + "identifier": "$rc_lifetime", + "platform_product_identifier": "com.revenuecat.other_product" + } + ] } ] } diff --git a/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithMetadata.json b/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithMetadata.json deleted file mode 100644 index 8d00f3f2ec..0000000000 --- a/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithMetadata.json +++ /dev/null @@ -1,49 +0,0 @@ -{ - "current_offering_id": "default", - "offerings": [ - { - "description": "standard set of packages", - "identifier": "default", - "metadata": { - "int": 5, - "double": 5.5, - "boolean": true, - "string": "five", - "array": ["five"], - "dictionary": { - "string": "five" - } - }, - "packages": [ - { - "identifier": "$rc_monthly", - "platform_product_identifier": "com.revenuecat.monthly_4.99.1_week_intro" - }, - { - "identifier": "$rc_annual", - "platform_product_identifier": "com.revenuecat.yearly_10.99.2_week_intro" - } - ] - }, - { - "description": "alternate offering", - "identifier": "alternate", - "metadata": { - "int": 5, - "double": 5.5, - "boolean": true, - "string": "five", - "array": ["five"], - "dictionary": { - "string": "five" - } - } - "packages": [ - { - "identifier": "$rc_lifetime", - "platform_product_identifier": "com.revenuecat.other_product" - } - ] - } - ] -} diff --git a/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithNullMetadata.json b/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithNullMetadata.json deleted file mode 100644 index 9f51bbac87..0000000000 --- a/Tests/UnitTests/Networking/Responses/Fixtures/OfferingsWithNullMetadata.json +++ /dev/null @@ -1,31 +0,0 @@ -{ - "current_offering_id": "default", - "offerings": [ - { - "description": "standard set of packages", - "identifier": "default", - "metadata": null, - "packages": [ - { - "identifier": "$rc_monthly", - "platform_product_identifier": "com.revenuecat.monthly_4.99.1_week_intro" - }, - { - "identifier": "$rc_annual", - "platform_product_identifier": "com.revenuecat.yearly_10.99.2_week_intro" - } - ] - }, - { - "description": "alternate offering", - "identifier": "alternate", - "metadata": null, - "packages": [ - { - "identifier": "$rc_lifetime", - "platform_product_identifier": "com.revenuecat.other_product" - } - ] - } - ] -} diff --git a/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift b/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift index 7b8573f837..d8fd1355a2 100644 --- a/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift +++ b/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift @@ -27,7 +27,7 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { func testDecodesAllOfferings() throws { expect(self.response.currentOfferingId) == "default" - expect(self.response.offerings).to(haveCount(2)) + expect(self.response.offerings).to(haveCount(4)) } func testDecodesFirstOffering() throws { @@ -35,6 +35,7 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { expect(offering.identifier) == "default" expect(offering.description) == "standard set of packages" + expect(offering.metadata) == [:] expect(offering.packages).to(haveCount(2)) let package1 = offering.packages[0] @@ -47,13 +48,49 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { expect(package2.platformProductIdentifier) == "com.revenuecat.yearly_10.99.2_week_intro" } - // - func testDecodesSecondOffering() throws { - let offering = try XCTUnwrap(self.response.offerings.last) + let offering = try XCTUnwrap(self.response.offerings[1]) expect(offering.identifier) == "alternate" expect(offering.description) == "alternate offering" + expect(offering.metadata) == [:] + expect(offering.packages).to(haveCount(1)) + + let package = offering.packages[0] + + expect(package.identifier) == PackageType.lifetime.description + expect(package.platformProductIdentifier) == "com.revenuecat.other_product" + } + + func testDecodesMetadataOffering() throws { + let offering = try XCTUnwrap(self.response.offerings[2]) + + expect(offering.identifier) == "metadata" + expect(offering.description) == "offering with metadata" + expect(offering.metadata) == [ + "int": 5, + "double": 5.5, + "boolean": true, + "string": "five", + "array": ["five"], + "dictionary": [ + "string": "five" + ] + ] + expect(offering.packages).to(haveCount(1)) + + let package = offering.packages[0] + + expect(package.identifier) == PackageType.lifetime.description + expect(package.platformProductIdentifier) == "com.revenuecat.other_product" + } + + func testDecodesNullMetadataOffering() throws { + let offering = try XCTUnwrap(self.response.offerings[3]) + + expect(offering.identifier) == "nullmetadata" + expect(offering.description) == "offering with null metadata" + expect(offering.metadata) == [:] expect(offering.packages).to(haveCount(1)) let package = offering.packages[0] From c8ba355bc07d64813a84a7f1d9a2ebaf9aa90913 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Fri, 19 May 2023 18:11:49 -0500 Subject: [PATCH 06/21] Fixed integration tests --- .../LoadShedderIntegrationTests/testCanGetOfferings.1.json | 3 +++ .../StoreKitIntegrationTests/testCanGetOfferings.1.json | 6 ++++++ 2 files changed, 9 insertions(+) diff --git a/Tests/BackendIntegrationTests/__Snapshots__/LoadShedderIntegrationTests/testCanGetOfferings.1.json b/Tests/BackendIntegrationTests/__Snapshots__/LoadShedderIntegrationTests/testCanGetOfferings.1.json index c990bd9a5e..704bda2d7c 100644 --- a/Tests/BackendIntegrationTests/__Snapshots__/LoadShedderIntegrationTests/testCanGetOfferings.1.json +++ b/Tests/BackendIntegrationTests/__Snapshots__/LoadShedderIntegrationTests/testCanGetOfferings.1.json @@ -4,6 +4,9 @@ { "description" : "standard set of packages", "identifier" : "default", + "metadata" : { + + }, "packages" : [ { "identifier" : "$rc_monthly", diff --git a/Tests/BackendIntegrationTests/__Snapshots__/StoreKitIntegrationTests/testCanGetOfferings.1.json b/Tests/BackendIntegrationTests/__Snapshots__/StoreKitIntegrationTests/testCanGetOfferings.1.json index cfe077fe91..d375436827 100644 --- a/Tests/BackendIntegrationTests/__Snapshots__/StoreKitIntegrationTests/testCanGetOfferings.1.json +++ b/Tests/BackendIntegrationTests/__Snapshots__/StoreKitIntegrationTests/testCanGetOfferings.1.json @@ -4,6 +4,9 @@ { "description" : "standard set of packages", "identifier" : "default", + "metadata" : { + + }, "packages" : [ { "identifier" : "$rc_monthly", @@ -22,6 +25,9 @@ { "description" : "Coins", "identifier" : "coins", + "metadata" : { + + }, "packages" : [ { "identifier" : "10.coins", From 8006a722aa892e08e282fe7ccea5cec6f76a50a1 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Fri, 19 May 2023 19:08:03 -0500 Subject: [PATCH 07/21] Updated integration test --- .../StoreKitIntegrationTests/testCanGetOfferings.1.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/BackendIntegrationTests/__Snapshots__/StoreKitIntegrationTests/testCanGetOfferings.1.json b/Tests/BackendIntegrationTests/__Snapshots__/StoreKitIntegrationTests/testCanGetOfferings.1.json index d375436827..89e9ac31b7 100644 --- a/Tests/BackendIntegrationTests/__Snapshots__/StoreKitIntegrationTests/testCanGetOfferings.1.json +++ b/Tests/BackendIntegrationTests/__Snapshots__/StoreKitIntegrationTests/testCanGetOfferings.1.json @@ -5,7 +5,7 @@ "description" : "standard set of packages", "identifier" : "default", "metadata" : { - + "meta" : "data" }, "packages" : [ { From c443678235c4331460fb53e20cc855da27f24edc Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Sun, 21 May 2023 20:46:36 -0500 Subject: [PATCH 08/21] Updated offerings json for load shedder --- .../LoadShedderIntegrationTests/testCanGetOfferings.1.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/BackendIntegrationTests/__Snapshots__/LoadShedderIntegrationTests/testCanGetOfferings.1.json b/Tests/BackendIntegrationTests/__Snapshots__/LoadShedderIntegrationTests/testCanGetOfferings.1.json index 704bda2d7c..f4fef31596 100644 --- a/Tests/BackendIntegrationTests/__Snapshots__/LoadShedderIntegrationTests/testCanGetOfferings.1.json +++ b/Tests/BackendIntegrationTests/__Snapshots__/LoadShedderIntegrationTests/testCanGetOfferings.1.json @@ -5,7 +5,7 @@ "description" : "standard set of packages", "identifier" : "default", "metadata" : { - + "meta" : "data" }, "packages" : [ { From 8b9ae7ed443d4d9804273f6744341a1dc9cd8b3e Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Sun, 21 May 2023 20:52:43 -0500 Subject: [PATCH 09/21] Prevents crashing in tests --- .../Responses/OfferingsDecodingTests.swift | 25 +++++++++++++------ 1 file changed, 17 insertions(+), 8 deletions(-) diff --git a/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift b/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift index d8fd1355a2..22b7fef1a6 100644 --- a/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift +++ b/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift @@ -38,8 +38,8 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { expect(offering.metadata) == [:] expect(offering.packages).to(haveCount(2)) - let package1 = offering.packages[0] - let package2 = offering.packages[1] + let package1 = try XCTUnwrap(offering.packages[safe: 0]) + let package2 = try XCTUnwrap(offering.packages[safe: 1]) expect(package1.identifier) == PackageType.monthly.description expect(package1.platformProductIdentifier) == "com.revenuecat.monthly_4.99.1_week_intro" @@ -49,21 +49,21 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { } func testDecodesSecondOffering() throws { - let offering = try XCTUnwrap(self.response.offerings[1]) + let offering = try XCTUnwrap(self.response.offerings[safe: 1]) expect(offering.identifier) == "alternate" expect(offering.description) == "alternate offering" expect(offering.metadata) == [:] expect(offering.packages).to(haveCount(1)) - let package = offering.packages[0] + let package = try XCTUnwrap(offering.packages[safe: 0]) expect(package.identifier) == PackageType.lifetime.description expect(package.platformProductIdentifier) == "com.revenuecat.other_product" } func testDecodesMetadataOffering() throws { - let offering = try XCTUnwrap(self.response.offerings[2]) + let offering = try XCTUnwrap(self.response.offerings[safe: 2]) expect(offering.identifier) == "metadata" expect(offering.description) == "offering with metadata" @@ -79,21 +79,21 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { ] expect(offering.packages).to(haveCount(1)) - let package = offering.packages[0] + let package = try XCTUnwrap(offering.packages[safe: 0]) expect(package.identifier) == PackageType.lifetime.description expect(package.platformProductIdentifier) == "com.revenuecat.other_product" } func testDecodesNullMetadataOffering() throws { - let offering = try XCTUnwrap(self.response.offerings[3]) + let offering = try XCTUnwrap(self.response.offerings[safe: 3]) expect(offering.identifier) == "nullmetadata" expect(offering.description) == "offering with null metadata" expect(offering.metadata) == [:] expect(offering.packages).to(haveCount(1)) - let package = offering.packages[0] + let package = try XCTUnwrap(offering.packages[safe: 0]) expect(package.identifier) == PackageType.lifetime.description expect(package.platformProductIdentifier) == "com.revenuecat.other_product" @@ -104,3 +104,12 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { } } + +private extension Collection { + + /// Returns the element at the specified index if it exists, otherwise nil. + subscript (safe index: Index) -> Element? { + return indices.contains(index) ? self[index] : nil + } + +} From e1068bc0654862fa50bb3fa98ce65815f53d8f4b Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Mon, 22 May 2023 15:16:31 -0500 Subject: [PATCH 10/21] Last of test fixes... hopefully :) --- .../Responses/OfferingsDecodingTests.swift | 8 ++++---- .../Purchasing/OfferingsManagerTests.swift | 18 ++++++++++++++++-- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift b/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift index 22b7fef1a6..fd54d95ba7 100644 --- a/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift +++ b/Tests/UnitTests/Networking/Responses/OfferingsDecodingTests.swift @@ -38,7 +38,7 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { expect(offering.metadata) == [:] expect(offering.packages).to(haveCount(2)) - let package1 = try XCTUnwrap(offering.packages[safe: 0]) + let package1 = try XCTUnwrap(offering.packages.first) let package2 = try XCTUnwrap(offering.packages[safe: 1]) expect(package1.identifier) == PackageType.monthly.description @@ -56,7 +56,7 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { expect(offering.metadata) == [:] expect(offering.packages).to(haveCount(1)) - let package = try XCTUnwrap(offering.packages[safe: 0]) + let package = try XCTUnwrap(offering.packages.first) expect(package.identifier) == PackageType.lifetime.description expect(package.platformProductIdentifier) == "com.revenuecat.other_product" @@ -79,7 +79,7 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { ] expect(offering.packages).to(haveCount(1)) - let package = try XCTUnwrap(offering.packages[safe: 0]) + let package = try XCTUnwrap(offering.packages.first) expect(package.identifier) == PackageType.lifetime.description expect(package.platformProductIdentifier) == "com.revenuecat.other_product" @@ -93,7 +93,7 @@ class OfferingsDecodingTests: BaseHTTPResponseTest { expect(offering.metadata) == [:] expect(offering.packages).to(haveCount(1)) - let package = try XCTUnwrap(offering.packages[safe: 0]) + let package = try XCTUnwrap(offering.packages.first) expect(package.identifier) == PackageType.lifetime.description expect(package.platformProductIdentifier) == "com.revenuecat.other_product" diff --git a/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift b/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift index 0832af78a7..ee1643d471 100644 --- a/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift @@ -429,6 +429,7 @@ extension OfferingsManagerTests { expect(result).to(beSuccess()) expect(result?.value?.all).to(haveCount(1)) expect(result?.value?.current?.identifier) == MockData.anyBackendOfferingsResponse.currentOfferingId + expect(result?.value?.current?.metadata as? [String: AnyDecodable]) == MockData.metadata expect(self.mockOfferings.invokedGetOfferingsForAppUserID) == true expect(self.mockDeviceCache.cacheOfferingsCount) == 0 @@ -465,6 +466,18 @@ private extension OfferingsManagerTests { enum MockData { static let anyAppUserID = "" + @DefaultDecodable.EmptyDictionary + static var metadata: [String: AnyDecodable] = [ + "int": 5, + "double": 5.5, + "boolean": true, + "string": "five", + "array": ["five"], + "dictionary": [ + "string": "five" + ] + ] + static let anyBackendOfferingsResponse: OfferingsResponse = .init( currentOfferingId: "base", offerings: [ @@ -472,7 +485,8 @@ private extension OfferingsManagerTests { description: "This is the base offering", packages: [ .init(identifier: "$rc_monthly", platformProductIdentifier: "monthly_freetrial") - ]) + ], + metadata: _metadata) ] ) static let backendOfferingsResponseWithUnknownProducts: OfferingsResponse = .init( @@ -495,7 +509,7 @@ private extension OfferingsManagerTests { Offering( identifier: offering.identifier, serverDescription: offering.description, - metadata: [:], + metadata: offering.metadata, availablePackages: offering.packages.map { package in .init( identifier: package.identifier, From 626109312ab84d90d6f7df9d704e1bf0e8cce30e Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Mon, 22 May 2023 15:44:05 -0500 Subject: [PATCH 11/21] Failing tests from disk cache --- Tests/UnitTests/Purchasing/OfferingsManagerTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift b/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift index ee1643d471..2eb35c1831 100644 --- a/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift @@ -429,7 +429,7 @@ extension OfferingsManagerTests { expect(result).to(beSuccess()) expect(result?.value?.all).to(haveCount(1)) expect(result?.value?.current?.identifier) == MockData.anyBackendOfferingsResponse.currentOfferingId - expect(result?.value?.current?.metadata as? [String: AnyDecodable]) == MockData.metadata + expect(result?.value?.current?.metadata.count) == 6 expect(self.mockOfferings.invokedGetOfferingsForAppUserID) == true expect(self.mockDeviceCache.cacheOfferingsCount) == 0 From f0a0ad629fd93a957ed861eda0906445b74c3001 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 05:53:19 -0500 Subject: [PATCH 12/21] Adding more metadata tests --- .../Purchasing/OfferingsManagerTests.swift | 16 +----- .../UnitTests/Purchasing/OfferingsTests.swift | 49 +++++++++++++++++++ 2 files changed, 50 insertions(+), 15 deletions(-) diff --git a/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift b/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift index 2eb35c1831..e7519a83e7 100644 --- a/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsManagerTests.swift @@ -429,7 +429,6 @@ extension OfferingsManagerTests { expect(result).to(beSuccess()) expect(result?.value?.all).to(haveCount(1)) expect(result?.value?.current?.identifier) == MockData.anyBackendOfferingsResponse.currentOfferingId - expect(result?.value?.current?.metadata.count) == 6 expect(self.mockOfferings.invokedGetOfferingsForAppUserID) == true expect(self.mockDeviceCache.cacheOfferingsCount) == 0 @@ -466,18 +465,6 @@ private extension OfferingsManagerTests { enum MockData { static let anyAppUserID = "" - @DefaultDecodable.EmptyDictionary - static var metadata: [String: AnyDecodable] = [ - "int": 5, - "double": 5.5, - "boolean": true, - "string": "five", - "array": ["five"], - "dictionary": [ - "string": "five" - ] - ] - static let anyBackendOfferingsResponse: OfferingsResponse = .init( currentOfferingId: "base", offerings: [ @@ -485,8 +472,7 @@ private extension OfferingsManagerTests { description: "This is the base offering", packages: [ .init(identifier: "$rc_monthly", platformProductIdentifier: "monthly_freetrial") - ], - metadata: _metadata) + ]) ] ) static let backendOfferingsResponseWithUnknownProducts: OfferingsResponse = .init( diff --git a/Tests/UnitTests/Purchasing/OfferingsTests.swift b/Tests/UnitTests/Purchasing/OfferingsTests.swift index 020750304b..eaded8311b 100644 --- a/Tests/UnitTests/Purchasing/OfferingsTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsTests.swift @@ -153,6 +153,55 @@ class OfferingsTests: TestCase { expect(offerings.current) == offerings["offering_a"] } + func testOfferingsWithMetadataIsCreated() throws { + @DefaultDecodable.EmptyDictionary + var metadata: [String: AnyDecodable] = [ + "int": 5, + "double": 5.5, + "boolean": true, + "string": "five", + "array": ["five"], + "dictionary": [ + "string": "five" + ] + ] + + let annualProduct = MockSK1Product(mockProductIdentifier: "com.myproduct.annual") + let monthlyProduct = MockSK1Product(mockProductIdentifier: "com.myproduct.monthly") + let products = [ + "com.myproduct.annual": StoreProduct(sk1Product: annualProduct), + "com.myproduct.monthly": StoreProduct(sk1Product: monthlyProduct) + ] + let offerings = try XCTUnwrap( + self.offeringsFactory.createOfferings( + from: products, + data: .init( + currentOfferingId: "offering_a", + offerings: [ + .init(identifier: "offering_a", + description: "This is the base offering", + packages: [ + .init(identifier: "$rc_six_month", platformProductIdentifier: "com.myproduct.annual") + ], + metadata: _metadata), + .init(identifier: "offering_b", + description: "This is the base offering b", + packages: [ + .init(identifier: "$rc_monthly", platformProductIdentifier: "com.myproduct.monthly") + ]) + ] + ) + ) + ) + + expect(offerings["offering_a"]).toNot(beNil()) + expect(offerings["offering_b"]).toNot(beNil()) + expect(offerings.current) == offerings["offering_a"] + + let offeringA = try XCTUnwrap(offerings["offering_a"]) + expect(offeringA.metadata.count) == metadata.count + } + func testLifetimePackage() throws { try testPackageType(packageType: PackageType.lifetime) } From d6ec3871068b33bb759bb613e57119e4f8046993 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 14:47:57 -0400 Subject: [PATCH 13/21] Added API tests and new getMetadataValue method --- Sources/Purchasing/Offering.swift | 4 ++++ .../APITesters/ObjCAPITester/ObjCAPITester/RCOfferingAPI.m | 3 ++- .../SwiftAPITester/SwiftAPITester/OfferingAPI.swift | 4 +++- Tests/UnitTests/Purchasing/OfferingsTests.swift | 6 +++++- 4 files changed, 14 insertions(+), 3 deletions(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index a975a484ad..7e494673c3 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -179,6 +179,10 @@ extension Offering { let data: [String: Any] } + func getMetadataValue(for key: String, defaultValue: T) -> T { + return self.metadata[key] as? T ?? defaultValue + } + } extension Offering: Identifiable { diff --git a/Tests/APITesters/ObjCAPITester/ObjCAPITester/RCOfferingAPI.m b/Tests/APITesters/ObjCAPITester/ObjCAPITester/RCOfferingAPI.m index 2f416f8aac..96d63fe9e2 100644 --- a/Tests/APITesters/ObjCAPITester/ObjCAPITester/RCOfferingAPI.m +++ b/Tests/APITesters/ObjCAPITester/ObjCAPITester/RCOfferingAPI.m @@ -32,8 +32,9 @@ + (void)checkAPI { RCPackage *p = [o packageWithIdentifier:nil]; p = [o packageWithIdentifier:@""]; RCPackage *ok = [o objectForKeyedSubscript:@""]; + NSDictionary *md = o.metadata; - NSLog(o, i, sd, a, l, an, s, t, tm, m, w, p, ok); + NSLog(o, i, sd, a, l, an, s, t, tm, m, w, p, ok, md); } @end diff --git a/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift b/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift index a61f6463cf..674f78d62e 100644 --- a/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift +++ b/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift @@ -29,6 +29,8 @@ func checkOfferingAPI() { var pPack: Package? = off.package(identifier: "") pPack = off.package(identifier: nil) let package: Package? = off[""] + let metadata: [String: Any] = off.metadata - print(off!, ident, sDesc, aPacks, lPack!, annPack!, smPack!, thmPack!, twmPack!, mPack!, wPack!, pPack!, package!) + print(off!, ident, sDesc, aPacks, lPack!, annPack!, smPack!, thmPack!, + twmPack!, mPack!, wPack!, pPack!, package!, metadata) } diff --git a/Tests/UnitTests/Purchasing/OfferingsTests.swift b/Tests/UnitTests/Purchasing/OfferingsTests.swift index eaded8311b..841bf7d2c1 100644 --- a/Tests/UnitTests/Purchasing/OfferingsTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsTests.swift @@ -183,7 +183,7 @@ class OfferingsTests: TestCase { packages: [ .init(identifier: "$rc_six_month", platformProductIdentifier: "com.myproduct.annual") ], - metadata: _metadata), + metadata: _metadata), .init(identifier: "offering_b", description: "This is the base offering b", packages: [ @@ -200,6 +200,10 @@ class OfferingsTests: TestCase { let offeringA = try XCTUnwrap(offerings["offering_a"]) expect(offeringA.metadata.count) == metadata.count + expect(offeringA.getMetadataValue(for: "int", defaultValue: 0)) == 5 + expect(offeringA.getMetadataValue(for: "double", defaultValue: 0.0)) == 5.5 + expect(offeringA.getMetadataValue(for: "boolean", defaultValue: false)) == true + expect(offeringA.getMetadataValue(for: "string", defaultValue: "")) == "five" } func testLifetimePackage() throws { From 59ec195a789e2aeb4b5b0e6bc48a27c725049e0c Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 14:49:17 -0400 Subject: [PATCH 14/21] Making the metadata thing private --- Sources/Purchasing/Offering.swift | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 7e494673c3..5c1b1f310e 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -174,11 +174,7 @@ import Foundation } extension Offering { - - struct Metadata { - let data: [String: Any] - } - + func getMetadataValue(for key: String, defaultValue: T) -> T { return self.metadata[key] as? T ?? defaultValue } @@ -196,6 +192,14 @@ extension Offering: Sendable {} // MARK: - Private +private extension Offering { + + struct Metadata { + let data: [String: Any] + } + +} + private extension Offering { static func checkForNilAndLogReplacement(previousPackages: [PackageType: Package], newPackage: Package) { From ae1785763f6814e8931992cf414c0717d2bc1dd3 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 14:56:04 -0400 Subject: [PATCH 15/21] LINT --- Sources/Purchasing/Offering.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 5c1b1f310e..fc61187ca5 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -174,7 +174,7 @@ import Foundation } extension Offering { - + func getMetadataValue(for key: String, defaultValue: T) -> T { return self.metadata[key] as? T ?? defaultValue } From abc407859afcebb851212518af29bbf92e4594d0 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 14:59:50 -0400 Subject: [PATCH 16/21] Docs, public method, and adding to API tester --- Sources/Purchasing/Offering.swift | 5 ++++- .../SwiftAPITester/SwiftAPITester/OfferingAPI.swift | 4 +++- 2 files changed, 7 insertions(+), 2 deletions(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index fc61187ca5..3f89da36c8 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -175,7 +175,10 @@ import Foundation extension Offering { - func getMetadataValue(for key: String, defaultValue: T) -> T { + /** + Gets a value from `metadata` and falls back to a specified default value. + */ + public func getMetadataValue(for key: String, defaultValue: T) -> T { return self.metadata[key] as? T ?? defaultValue } diff --git a/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift b/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift index 674f78d62e..2aff7ea5ff 100644 --- a/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift +++ b/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift @@ -30,7 +30,9 @@ func checkOfferingAPI() { pPack = off.package(identifier: nil) let package: Package? = off[""] let metadata: [String: Any] = off.metadata + let metadataString: String = off.getMetadataValue(for: "", defaultValue: "") + let metadataInt: Int = off.getMetadataValue(for: "", defaultValue: 0) print(off!, ident, sDesc, aPacks, lPack!, annPack!, smPack!, thmPack!, - twmPack!, mPack!, wPack!, pPack!, package!, metadata) + twmPack!, mPack!, wPack!, pPack!, package!, metadata, metadataString, metadataInt) } From 3d6d516c2b9ec2869ab3cd99f9e9b96fb8ec9e6a Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 16:45:08 -0400 Subject: [PATCH 17/21] More tests and fixed bug --- Sources/Purchasing/Offering.swift | 10 +++++++--- .../SwiftAPITester/SwiftAPITester/OfferingAPI.swift | 4 ++-- 2 files changed, 9 insertions(+), 5 deletions(-) diff --git a/Sources/Purchasing/Offering.swift b/Sources/Purchasing/Offering.swift index 3f89da36c8..5638dab1e6 100644 --- a/Sources/Purchasing/Offering.swift +++ b/Sources/Purchasing/Offering.swift @@ -176,10 +176,14 @@ import Foundation extension Offering { /** - Gets a value from `metadata` and falls back to a specified default value. + - Returns: the `metadata` value associated to `key` for the expected type, + or `defaultValue` if not found, or it's not the expected type. */ - public func getMetadataValue(for key: String, defaultValue: T) -> T { - return self.metadata[key] as? T ?? defaultValue + public func getMetadataValue(for key: String, default: T) -> T { + guard let rawValue = self.metadata[key], let value = rawValue as? T else { + return `default` + } + return value } } diff --git a/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift b/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift index 2aff7ea5ff..fbc639f96d 100644 --- a/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift +++ b/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift @@ -30,8 +30,8 @@ func checkOfferingAPI() { pPack = off.package(identifier: nil) let package: Package? = off[""] let metadata: [String: Any] = off.metadata - let metadataString: String = off.getMetadataValue(for: "", defaultValue: "") - let metadataInt: Int = off.getMetadataValue(for: "", defaultValue: 0) + let metadataString: String = off.getMetadataValue(for: "", default: "") + let metadataInt: Int = off.getMetadataValue(for: "", default: 0) print(off!, ident, sDesc, aPacks, lPack!, annPack!, smPack!, thmPack!, twmPack!, mPack!, wPack!, pPack!, package!, metadata, metadataString, metadataInt) From 206f3e6de9a57f11735acdbbd3c896f5c2faa4fb Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 17:13:56 -0400 Subject: [PATCH 18/21] Oops, forgot a file --- Tests/UnitTests/Purchasing/OfferingsTests.swift | 15 ++++++++++----- 1 file changed, 10 insertions(+), 5 deletions(-) diff --git a/Tests/UnitTests/Purchasing/OfferingsTests.swift b/Tests/UnitTests/Purchasing/OfferingsTests.swift index 841bf7d2c1..a41b7989b6 100644 --- a/Tests/UnitTests/Purchasing/OfferingsTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsTests.swift @@ -199,11 +199,16 @@ class OfferingsTests: TestCase { expect(offerings.current) == offerings["offering_a"] let offeringA = try XCTUnwrap(offerings["offering_a"]) - expect(offeringA.metadata.count) == metadata.count - expect(offeringA.getMetadataValue(for: "int", defaultValue: 0)) == 5 - expect(offeringA.getMetadataValue(for: "double", defaultValue: 0.0)) == 5.5 - expect(offeringA.getMetadataValue(for: "boolean", defaultValue: false)) == true - expect(offeringA.getMetadataValue(for: "string", defaultValue: "")) == "five" + expect(offeringA.metadata).to(haveCount(6)) + expect(offeringA.getMetadataValue(for: "int", default: 0)) == 5 + expect(offeringA.getMetadataValue(for: "double", default: 0.0)) == 5.5 + expect(offeringA.getMetadataValue(for: "boolean", default: false)) == true + expect(offeringA.getMetadataValue(for: "string", default: "")) == "five" + + expect(offeringA.getMetadataValue(for: "pizza", default: "no pizza")) == "no pizza" + + let wrongMetadataType = offeringA.getMetadataValue(for: "string", default: 5.5) + expect(wrongMetadataType) == 5.5 } func testLifetimePackage() throws { From 305964fdfbd234e22867d43b020f3ef15da2dcd5 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 17:19:42 -0400 Subject: [PATCH 19/21] Take that wrapped value yo --- Tests/UnitTests/Purchasing/OfferingsTests.swift | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/Tests/UnitTests/Purchasing/OfferingsTests.swift b/Tests/UnitTests/Purchasing/OfferingsTests.swift index a41b7989b6..2db2542e1b 100644 --- a/Tests/UnitTests/Purchasing/OfferingsTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsTests.swift @@ -154,7 +154,6 @@ class OfferingsTests: TestCase { } func testOfferingsWithMetadataIsCreated() throws { - @DefaultDecodable.EmptyDictionary var metadata: [String: AnyDecodable] = [ "int": 5, "double": 5.5, @@ -183,7 +182,9 @@ class OfferingsTests: TestCase { packages: [ .init(identifier: "$rc_six_month", platformProductIdentifier: "com.myproduct.annual") ], - metadata: _metadata), + metadata: .init( + wrappedValue: metadata + )), .init(identifier: "offering_b", description: "This is the base offering b", packages: [ From 62c24d39ccc3ddc6e246db85617b5c52426d36e5 Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Tue, 23 May 2023 17:28:58 -0400 Subject: [PATCH 20/21] this is the one --- Tests/UnitTests/Purchasing/OfferingsTests.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/UnitTests/Purchasing/OfferingsTests.swift b/Tests/UnitTests/Purchasing/OfferingsTests.swift index 2db2542e1b..60640df508 100644 --- a/Tests/UnitTests/Purchasing/OfferingsTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsTests.swift @@ -154,7 +154,7 @@ class OfferingsTests: TestCase { } func testOfferingsWithMetadataIsCreated() throws { - var metadata: [String: AnyDecodable] = [ + let metadata: [String: AnyDecodable] = [ "int": 5, "double": 5.5, "boolean": true, From ac7f8056f5fc74d016875f5ceb8b38234f1147fb Mon Sep 17 00:00:00 2001 From: Josh Holtz Date: Wed, 24 May 2023 08:14:30 -0400 Subject: [PATCH 21/21] Added test for optional default value --- .../SwiftAPITester/SwiftAPITester/OfferingAPI.swift | 5 +++-- Tests/UnitTests/Purchasing/OfferingsTests.swift | 3 +++ 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift b/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift index fbc639f96d..577f1423c0 100644 --- a/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift +++ b/Tests/APITesters/SwiftAPITester/SwiftAPITester/OfferingAPI.swift @@ -32,7 +32,8 @@ func checkOfferingAPI() { let metadata: [String: Any] = off.metadata let metadataString: String = off.getMetadataValue(for: "", default: "") let metadataInt: Int = off.getMetadataValue(for: "", default: 0) + let metadataOptionalInt: Int? = off.getMetadataValue(for: "", default: nil) - print(off!, ident, sDesc, aPacks, lPack!, annPack!, smPack!, thmPack!, - twmPack!, mPack!, wPack!, pPack!, package!, metadata, metadataString, metadataInt) + print(off!, ident, sDesc, aPacks, lPack!, annPack!, smPack!, thmPack!, twmPack!, + mPack!, wPack!, pPack!, package!, metadata, metadataString, metadataInt, metadataOptionalInt!) } diff --git a/Tests/UnitTests/Purchasing/OfferingsTests.swift b/Tests/UnitTests/Purchasing/OfferingsTests.swift index 60640df508..e64ba672b0 100644 --- a/Tests/UnitTests/Purchasing/OfferingsTests.swift +++ b/Tests/UnitTests/Purchasing/OfferingsTests.swift @@ -208,6 +208,9 @@ class OfferingsTests: TestCase { expect(offeringA.getMetadataValue(for: "pizza", default: "no pizza")) == "no pizza" + let optionalInt: Int? = offeringA.getMetadataValue(for: "optionalInt", default: nil) + expect(optionalInt).to(beNil()) + let wrongMetadataType = offeringA.getMetadataValue(for: "string", default: 5.5) expect(wrongMetadataType) == 5.5 }