From e2fb8a40bc87a069647c45d0bce812fadbc84000 Mon Sep 17 00:00:00 2001 From: Rudrank Riyam Date: Wed, 24 Jul 2024 13:52:30 +0530 Subject: [PATCH] Update tests for storefronts --- Sources/MusadoraKit/Storefronts.swift | 19 +- .../Catalog/CatalogStorefrontTests.swift | 213 +++++++++++++----- 2 files changed, 174 insertions(+), 58 deletions(-) diff --git a/Sources/MusadoraKit/Storefronts.swift b/Sources/MusadoraKit/Storefronts.swift index 7e4e5cd75..57717522a 100644 --- a/Sources/MusadoraKit/Storefronts.swift +++ b/Sources/MusadoraKit/Storefronts.swift @@ -46,15 +46,28 @@ public struct MStorefront: Codable { /// The default language tag for the storefront. public let defaultLanguageTag: String + /// The Apple iTunes storefront ID. + /// + /// This property represents the unique identifier for the storefront in Apple's iTunes system. + /// It is populated during initialisation by mapping the country code (stored in `id`) + /// to its corresponding iTunes storefront ID. The property is optional because not all country codes + /// may have a matching iTunes storefront ID in the mapping data. + /// + /// - Note: This ID is particularly useful for making API calls to iTunes or Apple Music services + /// that require a specific storefront identifier. public let storefrontId: Int? enum CodingKeys: String, CodingKey { - case id, type, attributes + case id + case type + case attributes } enum AttributesCodingKeys: String, CodingKey { - case explicitContentPolicy, name - case supportedLanguageTags, defaultLanguageTag + case explicitContentPolicy + case name + case supportedLanguageTags + case defaultLanguageTag } public init(from decoder: Decoder) throws { diff --git a/Tests/MusadoraKitTests/Catalog/CatalogStorefrontTests.swift b/Tests/MusadoraKitTests/Catalog/CatalogStorefrontTests.swift index c33d40031..4f82a1d20 100644 --- a/Tests/MusadoraKitTests/Catalog/CatalogStorefrontTests.swift +++ b/Tests/MusadoraKitTests/Catalog/CatalogStorefrontTests.swift @@ -5,82 +5,81 @@ // Created by Rudrank Riyam on 25/03/23. // -import Foundation - +import XCTest @testable import MusadoraKit import MusicKit -import XCTest final class CatalogStorefrontTests: XCTestCase { func testDecodingForUSStorefront() throws { let jsonData = """ - { - "data": [ - { - "id": "us", - "type": "storefronts", - "href": "/v1/storefronts/us", - "attributes": { - "supportedLanguageTags": [ - "en-US", - "es-MX", - "ar", - "ru", - "zh-Hans-CN", - "fr-FR", - "ko", - "pt-BR", - "vi", - "zh-Hant-TW" - ], - "explicitContentPolicy": "allowed", - "name": "United States", - "defaultLanguageTag": "en-US" - } - } - ] - } - """.data(using: .utf8)! - + { + "data": [ + { + "id": "us", + "type": "storefronts", + "href": "/v1/storefronts/us", + "attributes": { + "supportedLanguageTags": [ + "en-US", + "es-MX", + "ar", + "ru", + "zh-Hans-CN", + "fr-FR", + "ko", + "pt-BR", + "vi", + "zh-Hant-TW" + ], + "explicitContentPolicy": "allowed", + "name": "United States", + "defaultLanguageTag": "en-US" + } + } + ] + } + """.data(using: .utf8)! + let decoder = JSONDecoder() let result = try decoder.decode(StorefrontsData.self, from: jsonData) - + XCTAssertEqual(result.data.count, 1) let storefront = result.data[0] - + XCTAssertEqual(storefront.id, "us") XCTAssertEqual(storefront.type, .storefronts) XCTAssertEqual(storefront.supportedLanguageTags, ["en-US", "es-MX", "ar", "ru", "zh-Hans-CN", "fr-FR", "ko", "pt-BR", "vi", "zh-Hant-TW"]) XCTAssertEqual(storefront.explicitContentPolicy, .allowed) XCTAssertEqual(storefront.name, "United States") XCTAssertEqual(storefront.defaultLanguageTag, "en-US") + XCTAssertEqual(storefront.storefrontId, 143441, "US storefront ID should be 143441") } - + func testDecodingForINStorefront() throws { let jsonData = """ - { - "data": [ - { - "id": "in", - "type": "storefronts", - "href": "/v1/storefronts/in", - "attributes": { - "supportedLanguageTags": [ - "en-GB", - "hi" - ], - "explicitContentPolicy": "opt-in", - "name": "India", - "defaultLanguageTag": "en-GB" - } - } - ] - } - """.data(using: .utf8)! - + { + "data": [ + { + "id": "in", + "type": "storefronts", + "href": "/v1/storefronts/in", + "attributes": { + "supportedLanguageTags": [ + "en-GB", + "hi" + ], + "explicitContentPolicy": "opt-in", + "name": "India", + "defaultLanguageTag": "en-GB" + } + } + ] + } + """.data(using: .utf8)! + let decoder = JSONDecoder() let result = try decoder.decode(StorefrontsData.self, from: jsonData) - + XCTAssertEqual(result.data.count, 1) let storefront = result.data[0] XCTAssertEqual(storefront.id, "in") @@ -89,5 +88,109 @@ final class CatalogStorefrontTests: XCTestCase { XCTAssertEqual(storefront.explicitContentPolicy, .optIn) XCTAssertEqual(storefront.name, "India") XCTAssertEqual(storefront.defaultLanguageTag, "en-GB") + XCTAssertEqual(storefront.storefrontId, 143467, "India storefront ID should be 143467") + } + + func testDecodingForJPStorefront() throws { + let jsonData = """ + { + "data": [ + { + "id": "jp", + "type": "storefronts", + "href": "/v1/storefronts/jp", + "attributes": { + "supportedLanguageTags": [ + "ja", + "en-US" + ], + "explicitContentPolicy": "allowed", + "name": "Japan", + "defaultLanguageTag": "ja" + } + } + ] + } + """.data(using: .utf8)! + + let decoder = JSONDecoder() + let result = try decoder.decode(StorefrontsData.self, from: jsonData) + + XCTAssertEqual(result.data.count, 1) + let storefront = result.data[0] + XCTAssertEqual(storefront.id, "jp") + XCTAssertEqual(storefront.type, .storefronts) + XCTAssertEqual(storefront.supportedLanguageTags, ["ja", "en-US"]) + XCTAssertEqual(storefront.explicitContentPolicy, .allowed) + XCTAssertEqual(storefront.name, "Japan") + XCTAssertEqual(storefront.defaultLanguageTag, "ja") + XCTAssertEqual(storefront.storefrontId, 143462, "Japan storefront ID should be 143462") + } + + func testDecodingForCNStorefront() throws { + let jsonData = """ + { + "data": [ + { + "id": "cn", + "type": "storefronts", + "href": "/v1/storefronts/cn", + "attributes": { + "supportedLanguageTags": [ + "zh-Hans-CN", + "en-GB" + ], + "explicitContentPolicy": "prohibited", + "name": "China", + "defaultLanguageTag": "zh-Hans-CN" + } + } + ] + } + """.data(using: .utf8)! + + let decoder = JSONDecoder() + let result = try decoder.decode(StorefrontsData.self, from: jsonData) + + XCTAssertEqual(result.data.count, 1) + let storefront = result.data[0] + XCTAssertEqual(storefront.id, "cn") + XCTAssertEqual(storefront.type, .storefronts) + XCTAssertEqual(storefront.supportedLanguageTags, ["zh-Hans-CN", "en-GB"]) + XCTAssertEqual(storefront.explicitContentPolicy, .prohibited) + XCTAssertEqual(storefront.name, "China") + XCTAssertEqual(storefront.defaultLanguageTag, "zh-Hans-CN") + XCTAssertEqual(storefront.storefrontId, 143465, "China storefront ID should be 143465") + } + + func testDecodingForNonExistentStorefront() throws { + let jsonData = """ + { + "data": [ + { + "id": "xx", + "type": "storefronts", + "href": "/v1/storefronts/xx", + "attributes": { + "supportedLanguageTags": [ + "en-US" + ], + "explicitContentPolicy": "allowed", + "name": "Non-existent Country", + "defaultLanguageTag": "en-US" + } + } + ] + } + """.data(using: .utf8)! + + let decoder = JSONDecoder() + let result = try decoder.decode(StorefrontsData.self, from: jsonData) + + XCTAssertEqual(result.data.count, 1) + let storefront = result.data[0] + XCTAssertEqual(storefront.id, "xx") + XCTAssertEqual(storefront.type, .storefronts) + XCTAssertNil(storefront.storefrontId, "Non-existent storefront should have nil storefrontId") } }