Add tests to confirm that Entities can safely have computed attributes or relationships.

This commit is contained in:
Mathew Polzin
2018-11-28 08:15:57 -08:00
parent 0425e2adcb
commit cf47f88a61
3 changed files with 86 additions and 0 deletions
+1
View File
@@ -100,6 +100,7 @@ To create an Xcode project for JSONAPI, run
- [x] Roll my own `Result` or find an alternative that doesn't use `Foundation`.
- [ ] Create more descriptive errors that are easier to use for troubleshooting.
- [x] Make it easier to construct `Attributes` and `Relationships` values in test cases (literal expressibility).
- [ ] Make `TransformedAttribute` a Monad (or at least a Functor).
## Usage
@@ -0,0 +1,61 @@
//
// ComputedPropertiesTests.swift
// JSONAPITests
//
// Created by Mathew Polzin on 11/28/18.
//
import XCTest
import JSONAPI
import JSONAPITestLib
class ComputedPropertiesTests: XCTestCase {
func test_DecodeIgnoresComputed() {
let entity = decoded(type: TestType.self, data: computed_property_attribute)
XCTAssertEqual(entity.id, "1234")
XCTAssertEqual(entity[\.name], "Sarah")
XCTAssertEqual(entity ~> \.other, "5678")
XCTAssertNoThrow(try TestType.check(entity))
}
func test_EncodeIgnoresComputed() {
test_DecodeEncodeEquality(type: TestType.self, data: computed_property_attribute)
}
func test_ComputedAttributeAccess() {
let entity = decoded(type: TestType.self, data: computed_property_attribute)
XCTAssertEqual(entity[\.computed], "Sarah")
}
func test_ComputedRelationshipAccess() {
let entity = decoded(type: TestType.self, data: computed_property_attribute)
XCTAssertEqual(entity ~> \.computed, "5678")
}
}
// MARK: Test types
extension ComputedPropertiesTests {
public enum TestTypeDescription: EntityDescription {
public static var type: String { return "test" }
public struct Attributes: JSONAPI.Attributes {
public let name: Attribute<String>
public var computed: Attribute<String> {
return name
}
}
public struct Relationships: JSONAPI.Relationships {
public let other: ToOneRelationship<TestType>
public var computed: ToOneRelationship<TestType> {
return other
}
}
}
public typealias TestType = Entity<TestTypeDescription>
}
@@ -0,0 +1,24 @@
//
// ComputedPropertiesStubs.swift
// JSONAPITests
//
// Created by Mathew Polzin on 11/28/18.
//
let computed_property_attribute = """
{
"id": "1234",
"type": "test",
"attributes": {
"name": "Sarah"
},
"relationships": {
"other": {
"data": {
"id": "5678",
"type": "test"
}
}
}
}
""".data(using: .utf8)!