Adding test coverage for Relationship Meta and Links

This commit is contained in:
Mathew Polzin
2018-12-05 18:12:00 -08:00
parent 4f52cc2fd1
commit 6e6973c170
3 changed files with 210 additions and 0 deletions
@@ -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 = "!"
@@ -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<TestEntity1, NoMetadata, NoLinks>.self,
data: to_many_relationship)
@@ -58,6 +98,46 @@ extension RelationshipTests {
test_DecodeEncodeEquality(type: ToManyRelationship<TestEntity1, NoMetadata, NoLinks>.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<TestEntityType1>
typealias ToOneWithMeta = ToOneRelationship<TestEntity1, TestMeta, NoLinks>
typealias ToOneWithLinks = ToOneRelationship<TestEntity1, NoMetadata, TestLinks>
typealias ToOneWithMetaAndLinks = ToOneRelationship<TestEntity1, TestMeta, TestLinks>
typealias ToManyWithMeta = ToManyRelationship<TestEntity1, TestMeta, NoLinks>
typealias ToManyWithLinks = ToManyRelationship<TestEntity1, NoMetadata, TestLinks>
typealias ToManyWithMetaAndLinks = ToManyRelationship<TestEntity1, TestMeta, TestLinks>
struct TestMeta: JSONAPI.Meta {
let a: String
}
typealias TestLink = JSONAPI.Link<String, NoMetadata>
struct TestLinks: JSONAPI.Links {
let b: TestLink
}
}
@@ -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": [