From b08cbdb0aef438af0faac1acb9c63138c21bd093 Mon Sep 17 00:00:00 2001 From: Mathew Polzin Date: Sun, 25 Nov 2018 19:12:30 -0800 Subject: [PATCH] Add some tests around documents with links. --- .../JSONAPITests/Document/DocumentTests.swift | 94 +++++++++++++++ .../Document/stubs/DocumentStubs.swift | 113 ++++++++++++++++++ Tests/JSONAPITests/Links/LinksTests.swift | 2 - 3 files changed, 207 insertions(+), 2 deletions(-) diff --git a/Tests/JSONAPITests/Document/DocumentTests.swift b/Tests/JSONAPITests/Document/DocumentTests.swift index 1de6022..5d152bc 100644 --- a/Tests/JSONAPITests/Document/DocumentTests.swift +++ b/Tests/JSONAPITests/Document/DocumentTests.swift @@ -130,6 +130,25 @@ class DocumentTests: XCTestCase { data: metadata_document) } + func test_metaDataDocumentWithLinks() { + let document = decoded(type: JSONAPIDocument.self, + data: metadata_document_with_links) + + XCTAssertFalse(document.body.isError) + XCTAssertEqual(document.body.meta?.total, 100) + XCTAssertEqual(document.body.meta?.limit, 50) + XCTAssertEqual(document.body.meta?.offset, 0) + XCTAssertEqual(document.body.links?.link.url, "https://website.com") + XCTAssertEqual(document.body.links?.link.meta, NoMetadata()) + XCTAssertEqual(document.body.links?.link2.url, "https://othersite.com") + XCTAssertEqual(document.body.links?.link2.meta, TestLinks.TestMetadata(hello: "world")) + } + + func test_metaDataDocumentWithLinks_encode() { + test_DecodeEncodeEquality(type: JSONAPIDocument.self, + data: metadata_document_with_links) + } + func test_metaDocumentMissingMeta() { XCTAssertThrowsError(try JSONDecoder().decode(JSONAPIDocument.self, from: metadata_document_missing_metadata)) @@ -168,6 +187,48 @@ class DocumentTests: XCTestCase { data: single_document_no_includes_with_metadata) } + func test_singleDocumentNoIncludesWithLinks() { + let document = decoded(type: JSONAPIDocument, NoMetadata, TestLinks, NoIncludes, BasicJSONAPIError>.self, + data: single_document_no_includes_with_links) + + XCTAssertFalse(document.body.isError) + XCTAssertNotNil(document.body.primaryData) + XCTAssertEqual(document.body.primaryData?.value?.id.rawValue, "1") + XCTAssertEqual(document.body.includes?.count, 0) + XCTAssertEqual(document.body.meta, NoMetadata()) + XCTAssertEqual(document.body.links?.link.url, "https://website.com") + XCTAssertEqual(document.body.links?.link.meta, NoMetadata()) + XCTAssertEqual(document.body.links?.link2.url, "https://othersite.com") + XCTAssertEqual(document.body.links?.link2.meta, TestLinks.TestMetadata(hello: "world")) + + } + + func test_singleDocumentNoIncludesWithLinks_encode() { + test_DecodeEncodeEquality(type: JSONAPIDocument, NoMetadata, TestLinks, NoIncludes, BasicJSONAPIError>.self, + data: single_document_no_includes_with_links) + } + + func test_singleDocumentNoIncludesWithMetadataWithLinks() { + let document = decoded(type: JSONAPIDocument, TestPageMetadata, TestLinks, NoIncludes, BasicJSONAPIError>.self, + data: single_document_no_includes_with_metadata_with_links) + + XCTAssertFalse(document.body.isError) + XCTAssertNotNil(document.body.primaryData) + XCTAssertEqual(document.body.primaryData?.value?.id.rawValue, "1") + XCTAssertEqual(document.body.includes?.count, 0) + XCTAssertEqual(document.body.meta, TestPageMetadata(total: 70, limit: 40, offset: 10)) + XCTAssertEqual(document.body.links?.link.url, "https://website.com") + XCTAssertEqual(document.body.links?.link.meta, NoMetadata()) + XCTAssertEqual(document.body.links?.link2.url, "https://othersite.com") + XCTAssertEqual(document.body.links?.link2.meta, TestLinks.TestMetadata(hello: "world")) + + } + + func test_singleDocumentNoIncludesWithMetadataWithLinks_encode() { + test_DecodeEncodeEquality(type: JSONAPIDocument, TestPageMetadata, TestLinks, NoIncludes, BasicJSONAPIError>.self, + data: single_document_no_includes_with_metadata_with_links) + } + func test_singleDocumentNoIncludesMissingMetadata() { XCTAssertThrowsError(try JSONDecoder().decode(JSONAPIDocument, TestPageMetadata, NoLinks, NoIncludes, BasicJSONAPIError>.self, from: single_document_no_includes)) } @@ -207,6 +268,28 @@ class DocumentTests: XCTestCase { data: single_document_some_includes_with_metadata) } + func test_singleDocumentNoIncludesWithSomeIncludesWithMetadataWithLinks() { + let document = decoded(type: JSONAPIDocument, TestPageMetadata, TestLinks, Include1, BasicJSONAPIError>.self, + data: single_document_some_includes_with_metadata_with_links) + + XCTAssertFalse(document.body.isError) + XCTAssertNotNil(document.body.primaryData) + XCTAssertEqual(document.body.primaryData?.value?.id.rawValue, "1") + XCTAssertEqual(document.body.meta, TestPageMetadata(total: 70, limit: 40, offset: 10)) + XCTAssertEqual(document.body.links?.link.url, "https://website.com") + XCTAssertEqual(document.body.links?.link.meta, NoMetadata()) + XCTAssertEqual(document.body.links?.link2.url, "https://othersite.com") + XCTAssertEqual(document.body.links?.link2.meta, TestLinks.TestMetadata(hello: "world")) + XCTAssertEqual(document.body.includes?.count, 1) + XCTAssertEqual(document.body.includes?[Author.self].count, 1) + XCTAssertEqual(document.body.includes?[Author.self][0].id.rawValue, "33") + } + + func test_singleDocumentNoIncludesWithSomeIncludesMetadataWithLinks_encode() { + test_DecodeEncodeEquality(type: JSONAPIDocument, TestPageMetadata, TestLinks, Include1, BasicJSONAPIError>.self, + data: single_document_some_includes_with_metadata_with_links) + } + func test_singleDocument_PolyPrimaryResource() { let article = Article(id: Id(rawValue: "1"), relationships: .init(author: ToOneRelationship(id: Id(rawValue: "33")))) let document = decoded(type: JSONAPIDocument>, NoMetadata, NoLinks, NoIncludes, BasicJSONAPIError>.self, data: single_document_no_includes) @@ -289,6 +372,15 @@ extension DocumentTests { let offset: Int } + struct TestLinks: JSONAPI.Links { + let link: Link + let link2: Link + + struct TestMetadata: JSONAPI.Meta { + let hello: String + } + } + enum TestError: JSONAPIError { case unknownError case basic(BasicError) @@ -320,3 +412,5 @@ extension DocumentTests { } } } + +extension String: JSONAPI.URL {} diff --git a/Tests/JSONAPITests/Document/stubs/DocumentStubs.swift b/Tests/JSONAPITests/Document/stubs/DocumentStubs.swift index 4fc3418..8d0026b 100644 --- a/Tests/JSONAPITests/Document/stubs/DocumentStubs.swift +++ b/Tests/JSONAPITests/Document/stubs/DocumentStubs.swift @@ -50,6 +50,63 @@ let single_document_no_includes_with_metadata = """ } """.data(using: .utf8)! +let single_document_no_includes_with_links = """ +{ + "data": { + "id": "1", + "type": "articles", + "relationships": { + "author": { + "data": { + "type": "authors", + "id": "33" + } + } + } + }, + "links": { + "link": "https://website.com", + "link2": { + "href": "https://othersite.com", + "meta": { + "hello": "world" + } + } + } +} +""".data(using: .utf8)! + +let single_document_no_includes_with_metadata_with_links = """ +{ + "data": { + "id": "1", + "type": "articles", + "relationships": { + "author": { + "data": { + "type": "authors", + "id": "33" + } + } + } + }, + "links": { + "link": "https://website.com", + "link2": { + "href": "https://othersite.com", + "meta": { + "hello": "world" + } + } + }, + "meta": { + "total": 70, + "limit": 40, + "offset": 10 + } +} +""".data(using: .utf8)! + let single_document_some_includes = """ { "data": { @@ -73,6 +130,43 @@ let single_document_some_includes = """ } """.data(using: .utf8)! +let single_document_some_includes_with_metadata_with_links = """ +{ + "data": { + "id": "1", + "type": "articles", + "relationships": { + "author": { + "data": { + "type": "authors", + "id": "33" + } + } + } + }, + "included": [ + { + "id": "33", + "type": "authors" + } + ], + "links": { + "link": "https://website.com", + "link2": { + "href": "https://othersite.com", + "meta": { + "hello": "world" + } + } + }, + "meta": { + "total": 70, + "limit": 40, + "offset": 10 + } +} +""".data(using: .utf8)! + let single_document_some_includes_with_metadata = """ { "data": { @@ -238,6 +332,25 @@ let metadata_document = """ } """.data(using: .utf8)! +let metadata_document_with_links = """ +{ + "meta": { + "total": 100, + "limit": 50, + "offset": 0 + }, + "links": { + "link": "https://website.com", + "link2": { + "href": "https://othersite.com", + "meta": { + "hello": "world" + } + } + } +} +""".data(using: .utf8)! + let metadata_document_missing_metadata = """ { } diff --git a/Tests/JSONAPITests/Links/LinksTests.swift b/Tests/JSONAPITests/Links/LinksTests.swift index 2357014..713eeaf 100644 --- a/Tests/JSONAPITests/Links/LinksTests.swift +++ b/Tests/JSONAPITests/Links/LinksTests.swift @@ -66,5 +66,3 @@ extension LinksTests { let hello: String } } - -extension String: JSONAPI.URL {}