diff --git a/Tests/JSONAPITests/Entity/EntityTests.swift b/Tests/JSONAPITests/Entity/EntityTests.swift index 01d37b7..e08111d 100644 --- a/Tests/JSONAPITests/Entity/EntityTests.swift +++ b/Tests/JSONAPITests/Entity/EntityTests.swift @@ -45,12 +45,15 @@ class EntityTests: XCTestCase { let entity1 = TestEntity1(id: .init(rawValue: "wow")) let entity2 = TestEntity2(id: .init(rawValue: "cool"), relationships: .init(other: .init(entity: entity1))) let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .init(ids: [.init(rawValue: "10"), .init(rawValue: "20"), entity1.id]))) + let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .none)) let _ = TestEntity4(id: .init(rawValue: "4"), attributes: .init(word: .init(value: "hello"), number: .init(value: 10), array: .init(value: [10.2, 10.3])), relationships: .init(other: entity2.pointer)) let _ = TestEntity5(id: .init(rawValue: "5"), attributes: .init(floater: .init(value: 10.2))) let _ = TestEntity6(id: .init(rawValue: "6"), attributes: .init(here: .init(value: "here"), maybeHere: nil, maybeNull: .init(value: nil))) let _ = TestEntity7(id: .init(rawValue: "7"), attributes: .init(here: .init(value: "hello"), maybeHereMaybeNull: .init(value: "world"))) XCTAssertNoThrow(try TestEntity8(id: .init(rawValue: "8"), attributes: .init(string: .init(value: "hello"), int: .init(value: 10), stringFromInt: .init(rawValue: 20), plus: .init(rawValue: 30), doubleFromInt: .init(rawValue: 32), omitted: nil, nullToString: .init(rawValue: nil)))) let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: nil)) + let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: .init(entity: nil))) + let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: .init(entity: entity1, meta: .none, links: .none))) let e10id1 = TestEntity10.Identifier(rawValue: "hello") let e10id2 = TestEntity10.Id(rawValue: "world") let e10id3: TestEntity10.Id = "!" diff --git a/Tests/JSONAPITests/Relationships/RelationshipTests.swift b/Tests/JSONAPITests/Relationships/RelationshipTests.swift index 1216f0c..6d3f176 100644 --- a/Tests/JSONAPITests/Relationships/RelationshipTests.swift +++ b/Tests/JSONAPITests/Relationships/RelationshipTests.swift @@ -47,6 +47,46 @@ extension RelationshipTests { data: to_one_relationship) } + func test_ToOneRelationshipWithMeta() { + let relationship = decoded(type: ToOneWithMeta.self, + data: to_one_relationship_with_meta) + + XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF") + XCTAssertEqual(relationship.meta.a, "hello") + } + + func test_ToOneRelationshipWithMeta_encode() { + test_DecodeEncodeEquality(type: ToOneWithMeta.self, + data: to_one_relationship_with_meta) + } + + func test_ToOneRelationshipWithLinks() { + let relationship = decoded(type: ToOneWithLinks.self, + data: to_one_relationship_with_links) + + XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF") + XCTAssertEqual(relationship.links.b, .init(url: "world")) + } + + func test_ToOneRelationshipWithLinks_encode() { + test_DecodeEncodeEquality(type: ToOneWithLinks.self, + data: to_one_relationship_with_links) + } + + func test_ToOneRelationshipWithMetaAndLinks() { + let relationship = decoded(type: ToOneWithMetaAndLinks.self, + data: to_one_relationship_with_meta_and_links) + + XCTAssertEqual(relationship.id.rawValue, "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF") + XCTAssertEqual(relationship.meta.a, "hello") + XCTAssertEqual(relationship.links.b, .init(url: "world")) + } + + func test_ToOneRelationshipWithMetaAndLinks_encode() { + test_DecodeEncodeEquality(type: ToOneWithMetaAndLinks.self, + data: to_one_relationship_with_meta_and_links) + } + func test_ToManyRelationship() { let relationship = decoded(type: ToManyRelationship.self, data: to_many_relationship) @@ -58,6 +98,46 @@ extension RelationshipTests { test_DecodeEncodeEquality(type: ToManyRelationship.self, data: to_many_relationship) } + + func test_ToManyRelationshipWithMeta() { + let relationship = decoded(type: ToManyWithMeta.self, + data: to_many_relationship_with_meta) + + XCTAssertEqual(relationship.ids.map { $0.rawValue }, ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"]) + XCTAssertEqual(relationship.meta.a, "hello") + } + + func test_ToManyRelationshipWithMeta_encode() { + test_DecodeEncodeEquality(type: ToManyWithMeta.self, + data: to_many_relationship_with_meta) + } + + func test_ToManyRelationshipWithLinks() { + let relationship = decoded(type: ToManyWithLinks.self, + data: to_many_relationship_with_links) + + XCTAssertEqual(relationship.ids.map { $0.rawValue }, ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"]) + XCTAssertEqual(relationship.links.b, .init(url: "world")) + } + + func test_ToManyRelationshipWithLinks_encode() { + test_DecodeEncodeEquality(type: ToManyWithLinks.self, + data: to_many_relationship_with_links) + } + + func test_ToManyRelationshipWithMetaAndLinks() { + let relationship = decoded(type: ToManyWithMetaAndLinks.self, + data: to_many_relationship_with_meta_and_links) + + XCTAssertEqual(relationship.ids.map { $0.rawValue }, ["2DF03B69-4B0A-467F-B52E-B0C9E44FCECF", "90F03B69-4DF1-467F-B52E-B0C9E44FC333", "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF"]) + XCTAssertEqual(relationship.meta.a, "hello") + XCTAssertEqual(relationship.links.b, .init(url: "world")) + } + + func test_ToManyRelationshipWithMetaAndLinks_encode() { + test_DecodeEncodeEquality(type: ToManyWithMetaAndLinks.self, + data: to_many_relationship_with_meta_and_links) + } } // MARK: Failure tests @@ -82,4 +162,23 @@ extension RelationshipTests { } typealias TestEntity1 = BasicEntity + + typealias ToOneWithMeta = ToOneRelationship + + typealias ToOneWithLinks = ToOneRelationship + typealias ToOneWithMetaAndLinks = ToOneRelationship + + typealias ToManyWithMeta = ToManyRelationship + typealias ToManyWithLinks = ToManyRelationship + typealias ToManyWithMetaAndLinks = ToManyRelationship + + struct TestMeta: JSONAPI.Meta { + let a: String + } + + typealias TestLink = JSONAPI.Link + + struct TestLinks: JSONAPI.Links { + let b: TestLink + } } diff --git a/Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift b/Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift index 6be0d90..2f5d6d5 100644 --- a/Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift +++ b/Tests/JSONAPITests/Relationships/stubs/RelationshipStubs.swift @@ -23,6 +23,45 @@ let to_one_relationship_type_mismatch = """ } """.data(using: .utf8)! +let to_one_relationship_with_meta = """ +{ + "data": { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + }, + "meta": { + "a": "hello" + } +} +""".data(using: .utf8)! + +let to_one_relationship_with_links = """ +{ + "data": { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + }, + "links": { + "b": "world" + } +} +""".data(using: .utf8)! + +let to_one_relationship_with_meta_and_links = """ +{ + "data": { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + }, + "meta": { + "a": "hello" + }, + "links": { + "b": "world" + } +} +""".data(using: .utf8)! + let to_many_relationship = """ { "data": [ @@ -42,6 +81,75 @@ let to_many_relationship = """ } """.data(using: .utf8)! +let to_many_relationship_with_meta = """ +{ + "data": [ + { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + }, + { + "type": "test_entity1", + "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333" + }, + { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } + ], + "meta": { + "a": "hello" + } +} +""".data(using: .utf8)! + +let to_many_relationship_with_links = """ +{ + "data": [ + { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + }, + { + "type": "test_entity1", + "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333" + }, + { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } + ], + "links": { + "b": "world" + } +} +""".data(using: .utf8)! + +let to_many_relationship_with_meta_and_links = """ +{ + "data": [ + { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + }, + { + "type": "test_entity1", + "id": "90F03B69-4DF1-467F-B52E-B0C9E44FC333" + }, + { + "type": "test_entity1", + "id": "2DF03B69-4B0A-467F-B52E-B0C9E44FCECF" + } + ], + "meta": { + "a": "hello" + }, + "links": { + "b": "world" + } +} +""".data(using: .utf8)! + let to_many_relationship_type_mismatch = """ { "data": [