mirror of
https://github.com/encounter/JSONAPI.git
synced 2026-03-30 11:18:38 -07:00
Add tests to confirm that Entities can safely have computed attributes or relationships.
This commit is contained in:
@@ -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)!
|
||||
Reference in New Issue
Block a user