Skip to content

Commit 8274ecb

Browse files
committed
Add tests confirming that initializers work as expected without using any TestLib conveniences
1 parent 853c7e8 commit 8274ecb

2 files changed

Lines changed: 32 additions & 0 deletions

File tree

Sources/JSONAPI/Resource/Entity.swift

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -120,6 +120,14 @@ extension Entity where EntityRawIdType: CreatableRawIdType {
120120
}
121121
}
122122

123+
extension Entity where EntityRawIdType == Unidentified {
124+
public init(attributes: Description.Attributes, relationships: Description.Relationships) {
125+
self.id = .unidentified
126+
self.attributes = attributes
127+
self.relationships = relationships
128+
}
129+
}
130+
123131
extension Entity where Description.Attributes == NoAttributes {
124132
public init(id: Entity.Id, relationships: Description.Relationships) {
125133
self.init(id: id, attributes: NoAttributes(), relationships: relationships)
@@ -144,6 +152,12 @@ extension Entity where Description.Relationships == NoRelationships, EntityRawId
144152
}
145153
}
146154

155+
extension Entity where Description.Relationships == NoRelationships, EntityRawIdType == Unidentified {
156+
public init(attributes: Description.Attributes) {
157+
self.init(attributes: attributes, relationships: NoRelationships())
158+
}
159+
}
160+
147161
extension Entity where Description.Attributes == NoAttributes, Description.Relationships == NoRelationships {
148162
public init(id: Entity.Id) {
149163
self.init(id: id, attributes: NoAttributes(), relationships: NoRelationships())

Tests/JSONAPITests/Entity/EntityTests.swift

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,24 @@ class EntityTests: XCTestCase {
4040

4141
XCTAssertEqual(entity2.relationships.other.id, entity1.id)
4242
}
43+
44+
func test_initialization() {
45+
let entity1 = TestEntity1(id: .init(rawValue: "wow"))
46+
let entity2 = TestEntity2(id: .init(rawValue: "cool"), relationships: .init(other: .init(entity: entity1)))
47+
let _ = TestEntity3(id: .init(rawValue: "3"), relationships: .init(others: .init(ids: [.init(rawValue: "10"), .init(rawValue: "20"), entity1.id])))
48+
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))
49+
let _ = TestEntity5(id: .init(rawValue: "5"), attributes: .init(floater: .init(value: 10.2)))
50+
let _ = TestEntity6(id: .init(rawValue: "6"), attributes: .init(here: .init(value: "here"), maybeHere: nil, maybeNull: .init(value: nil)))
51+
let _ = TestEntity7(id: .init(rawValue: "7"), attributes: .init(here: .init(value: "hello"), maybeHereMaybeNull: .init(value: "world")))
52+
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))))
53+
let _ = TestEntity9(id: .init(rawValue: "9"), relationships: .init(one: entity1.pointer, nullableOne: nil))
54+
let e10id1 = TestEntity10.Identifier(rawValue: "hello")
55+
let e10id2 = TestEntity10.Id(rawValue: "world")
56+
let e10id3: TestEntity10.Id = "!"
57+
let _ = TestEntity10(id: .init(rawValue: "10"), relationships: .init(selfRef: .init(id: e10id1), selfRefs: .init(ids: [e10id2, e10id3])))
58+
XCTAssertNoThrow(try TestEntity11(id: .init(rawValue: "11"), attributes: .init(number: .init(rawValue: 11))))
59+
let _ = UnidentifiedTestEntity(attributes: .init(me: .init(value: "hello")))
60+
}
4361
}
4462

4563
// MARK: - Encode/Decode

0 commit comments

Comments
 (0)