162 lines
4.2 KiB
Swift
162 lines
4.2 KiB
Swift
import Foundation
|
|
import Testing
|
|
|
|
@testable import MusicBrainz
|
|
|
|
@Test func testAreaSearchDecoding() throws {
|
|
let json = """
|
|
{
|
|
"created": "2024-05-20T12:00:00.000Z",
|
|
"count": 1,
|
|
"offset": 0,
|
|
"areas": [
|
|
{
|
|
"id": "f03ed396-3f4d-4ef4-81b0-7c03c46961e2",
|
|
"type": "City",
|
|
"score": 100,
|
|
"name": "London",
|
|
"sort-name": "London",
|
|
"life-span": { "ended": false }
|
|
}
|
|
]
|
|
}
|
|
""".data(using: .utf8)!
|
|
|
|
let response = try JSONDecoder().decode(SearchResponse<Area>.self, from: json)
|
|
|
|
#expect(response.count == 1)
|
|
#expect(response.entities.count == 1)
|
|
#expect(response.entities[0].name == "London")
|
|
#expect(response.entities[0].score == 100)
|
|
}
|
|
|
|
@Test func testReleaseGroupSearchDecoding() throws {
|
|
let json = """
|
|
{
|
|
"created": "2024-05-20T12:00:00.000Z",
|
|
"count": 1,
|
|
"offset": 0,
|
|
"release-groups": [
|
|
{
|
|
"id": "1b022e01-4da6-387b-8658-8678046e4ccc",
|
|
"score": 100,
|
|
"primary-type": "Album",
|
|
"title": "Nevermind",
|
|
"artist-credit": [{ "name": "Nirvana" }]
|
|
}
|
|
]
|
|
}
|
|
""".data(using: .utf8)!
|
|
|
|
let response = try JSONDecoder().decode(SearchResponse<ReleaseGroup>.self, from: json)
|
|
|
|
#expect(response.count == 1)
|
|
#expect(response.entities.count == 1)
|
|
#expect(response.entities[0].title == "Nevermind")
|
|
#expect(response.entities[0].primaryType == "Album")
|
|
#expect(response.entities[0].artistCredit?[0].name == "Nirvana")
|
|
}
|
|
|
|
@Test func testLabelSearchDecoding() throws {
|
|
let json = """
|
|
{
|
|
"created": "2024-05-20T12:00:00.000Z",
|
|
"count": 1,
|
|
"offset": 0,
|
|
"labels": [
|
|
{
|
|
"id": "79239441-bfd5-4981-a70c-55c3f15c1287",
|
|
"type": "Production",
|
|
"score": 100,
|
|
"name": "Ninja Tune",
|
|
"label-code": 12885,
|
|
"country": "GB"
|
|
}
|
|
]
|
|
}
|
|
""".data(using: .utf8)!
|
|
|
|
let response = try JSONDecoder().decode(SearchResponse<Label>.self, from: json)
|
|
|
|
#expect(response.count == 1)
|
|
#expect(response.entities.count == 1)
|
|
#expect(response.entities[0].name == "Ninja Tune")
|
|
#expect(response.entities[0].labelCode == 12885)
|
|
}
|
|
|
|
@Test func testURLSearchDecoding() throws {
|
|
let json = """
|
|
{
|
|
"created": "2024-05-20T12:00:00.000Z",
|
|
"count": 1,
|
|
"offset": 0,
|
|
"urls": [
|
|
{
|
|
"id": "b663423b-9b54-4067-9674-fffaecf68851",
|
|
"resource": "http://www.madonna.com/",
|
|
"score": 100
|
|
}
|
|
]
|
|
}
|
|
""".data(using: .utf8)!
|
|
|
|
let response = try JSONDecoder().decode(SearchResponse<URLReference>.self, from: json)
|
|
|
|
#expect(response.count == 1)
|
|
#expect(response.entities.count == 1)
|
|
#expect(response.entities[0].resource == "http://www.madonna.com/")
|
|
}
|
|
|
|
@Test func testCoverArtDecoding() throws {
|
|
let json = """
|
|
{
|
|
"images": [
|
|
{
|
|
"image": "http://coverartarchive.org/release/a2d12ee8-9aeb-4d91-bfab-5c21f7a577fc/3125412512.jpg",
|
|
"thumbnails": {
|
|
"small": "http://coverartarchive.org/release/a2d12ee8-9aeb-4d91-bfab-5c21f7a577fc/3125412512-250.jpg",
|
|
"large": "http://coverartarchive.org/release/a2d12ee8-9aeb-4d91-bfab-5c21f7a577fc/3125412512-500.jpg"
|
|
},
|
|
"types": ["Front"],
|
|
"front": true,
|
|
"back": false,
|
|
"edit": 12345,
|
|
"comment": "",
|
|
"id": "3125412512"
|
|
}
|
|
],
|
|
"release": "https://musicbrainz.org/release/a2d12ee8-9aeb-4d91-bfab-5c21f7a577fc"
|
|
}
|
|
""".data(using: .utf8)!
|
|
|
|
let response = try JSONDecoder().decode(CoverArtResponse.self, from: json)
|
|
|
|
#expect(response.images.count == 1)
|
|
#expect(response.images[0].front == true)
|
|
#expect(response.frontImage?.image == "http://coverartarchive.org/release/a2d12ee8-9aeb-4d91-bfab-5c21f7a577fc/3125412512.jpg")
|
|
#expect(response.images[0].thumbnails["small"] == "http://coverartarchive.org/release/a2d12ee8-9aeb-4d91-bfab-5c21f7a577fc/3125412512-250.jpg")
|
|
}
|
|
|
|
@Test func testArtistImageExtraction() throws {
|
|
let json = """
|
|
{
|
|
"id": "f27ec8db-af05-4f36-916e-3d57f91ecf5e",
|
|
"name": "Michael Jackson",
|
|
"sort-name": "Jackson, Michael",
|
|
"relations": [
|
|
{
|
|
"type": "image",
|
|
"direction": "forward",
|
|
"url": {
|
|
"id": "123",
|
|
"resource": "https://commons.wikimedia.org/wiki/File:Michael_Jackson_1984.jpg"
|
|
}
|
|
}
|
|
]
|
|
}
|
|
""".data(using: .utf8)!
|
|
|
|
let artist = try JSONDecoder().decode(Artist.self, from: json)
|
|
#expect(artist.imageURL?.absoluteString == "https://commons.wikimedia.org/wiki/File:Michael_Jackson_1984.jpg")
|
|
}
|