Skip to content

Commit

Permalink
Update tests for storefronts
Browse files Browse the repository at this point in the history
  • Loading branch information
rudrankriyam committed Jul 24, 2024
1 parent ff9f995 commit e2fb8a4
Show file tree
Hide file tree
Showing 2 changed files with 174 additions and 58 deletions.
19 changes: 16 additions & 3 deletions Sources/MusadoraKit/Storefronts.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
213 changes: 158 additions & 55 deletions Tests/MusadoraKitTests/Catalog/CatalogStorefrontTests.swift
Original file line number Diff line number Diff line change
Expand Up @@ -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")
Expand All @@ -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")
}
}

0 comments on commit e2fb8a4

Please sign in to comment.