Skip to content

Commit cf47f88

Browse files
committed
Add tests to confirm that Entities can safely have computed attributes or relationships.
1 parent 0425e2a commit cf47f88

3 files changed

Lines changed: 86 additions & 0 deletions

File tree

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,7 @@ To create an Xcode project for JSONAPI, run
100100
- [x] Roll my own `Result` or find an alternative that doesn't use `Foundation`.
101101
- [ ] Create more descriptive errors that are easier to use for troubleshooting.
102102
- [x] Make it easier to construct `Attributes` and `Relationships` values in test cases (literal expressibility).
103+
- [ ] Make `TransformedAttribute` a Monad (or at least a Functor).
103104

104105
## Usage
105106

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
//
2+
// ComputedPropertiesTests.swift
3+
// JSONAPITests
4+
//
5+
// Created by Mathew Polzin on 11/28/18.
6+
//
7+
8+
import XCTest
9+
import JSONAPI
10+
import JSONAPITestLib
11+
12+
class ComputedPropertiesTests: XCTestCase {
13+
func test_DecodeIgnoresComputed() {
14+
let entity = decoded(type: TestType.self, data: computed_property_attribute)
15+
16+
XCTAssertEqual(entity.id, "1234")
17+
XCTAssertEqual(entity[\.name], "Sarah")
18+
XCTAssertEqual(entity ~> \.other, "5678")
19+
XCTAssertNoThrow(try TestType.check(entity))
20+
}
21+
22+
func test_EncodeIgnoresComputed() {
23+
test_DecodeEncodeEquality(type: TestType.self, data: computed_property_attribute)
24+
}
25+
26+
func test_ComputedAttributeAccess() {
27+
let entity = decoded(type: TestType.self, data: computed_property_attribute)
28+
29+
XCTAssertEqual(entity[\.computed], "Sarah")
30+
}
31+
32+
func test_ComputedRelationshipAccess() {
33+
let entity = decoded(type: TestType.self, data: computed_property_attribute)
34+
35+
XCTAssertEqual(entity ~> \.computed, "5678")
36+
}
37+
}
38+
39+
// MARK: Test types
40+
extension ComputedPropertiesTests {
41+
public enum TestTypeDescription: EntityDescription {
42+
public static var type: String { return "test" }
43+
44+
public struct Attributes: JSONAPI.Attributes {
45+
public let name: Attribute<String>
46+
public var computed: Attribute<String> {
47+
return name
48+
}
49+
}
50+
51+
public struct Relationships: JSONAPI.Relationships {
52+
public let other: ToOneRelationship<TestType>
53+
54+
public var computed: ToOneRelationship<TestType> {
55+
return other
56+
}
57+
}
58+
}
59+
60+
public typealias TestType = Entity<TestTypeDescription>
61+
}
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
//
2+
// ComputedPropertiesStubs.swift
3+
// JSONAPITests
4+
//
5+
// Created by Mathew Polzin on 11/28/18.
6+
//
7+
8+
let computed_property_attribute = """
9+
{
10+
"id": "1234",
11+
"type": "test",
12+
"attributes": {
13+
"name": "Sarah"
14+
},
15+
"relationships": {
16+
"other": {
17+
"data": {
18+
"id": "5678",
19+
"type": "test"
20+
}
21+
}
22+
}
23+
}
24+
""".data(using: .utf8)!

0 commit comments

Comments
 (0)