@@ -9,45 +9,93 @@ import XCTest
99import JSONAPI
1010
1111class NonJSONAPIRelatableTests : XCTestCase {
12+ func test_initialization1( ) {
13+ let e1 = NonJSONAPIEntity ( id: . init( rawValue: " hello " ) )
14+ let e2 = NonJSONAPIEntity ( id: . init( rawValue: " world " ) )
1215
16+ let entity = TestEntity ( relationships: . init( one: . init( id: e1. id) , many: . init( ids: [ e1. id, e2. id] ) ) )
17+
18+ XCTAssertEqual ( entity ~> \. one, e1. id)
19+ XCTAssertEqual ( entity ~> \. many, [ e1. id, e2. id] )
20+
21+ XCTAssertNoThrow ( try TestEntity . check ( entity) )
22+ }
23+
24+ func test_initialization2_all_relationships_there( ) {
25+ let e1 = NonJSONAPIEntity ( id: . init( rawValue: " hello " ) )
26+ let e2 = NonJSONAPIEntity ( id: . init( rawValue: " world " ) )
27+
28+ let entity = TestEntity2 ( relationships: . init( nullableOne: . init( id: e1. id) , nullableMaybeOne: . init( id: e2. id) , maybeOne: . init( id: e2. id) , maybeMany: . init( ids: [ e2. id, e1. id] ) ) )
29+
30+ XCTAssertEqual ( ( entity ~> \. nullableOne) ? . rawValue, " hello " )
31+ XCTAssertEqual ( ( entity ~> \. nullableMaybeOne) ? . rawValue, " world " )
32+ XCTAssertEqual ( ( entity ~> \. maybeOne) ? . rawValue, " world " )
33+ XCTAssertEqual ( ( entity ~> \. maybeMany) ? . map { $0. rawValue } , [ " world " , " hello " ] )
34+ }
35+
36+ func test_initialization2_all_relationships_missing( ) {
37+
38+ let entity = TestEntity2 ( relationships: . init( nullableOne: . init( id: nil ) , nullableMaybeOne: . init( id: nil ) , maybeOne: nil , maybeMany: nil ) )
39+ let entity2 = TestEntity2 ( relationships: . init( nullableOne: . init( id: nil ) , nullableMaybeOne: nil , maybeOne: nil , maybeMany: nil ) )
40+
41+ XCTAssertNil ( ( entity ~> \. nullableOne) )
42+ XCTAssertNil ( ( entity ~> \. nullableMaybeOne) )
43+ XCTAssertNil ( ( entity ~> \. maybeOne) )
44+ XCTAssertNil ( ( entity ~> \. maybeMany) )
45+
46+ XCTAssertNil ( ( entity2 ~> \. nullableOne) )
47+ XCTAssertNil ( ( entity2 ~> \. nullableMaybeOne) )
48+ XCTAssertNil ( ( entity2 ~> \. maybeOne) )
49+ XCTAssertNil ( ( entity2 ~> \. maybeMany) )
50+ }
1351}
1452
1553// MARK: - Test Types
1654extension NonJSONAPIRelatableTests {
17- // enum TestEntityDescription: EntityDescription {
18- // static var type: String { return "test" }
19- //
20- // typealias Attributes = NoAttributes
21- //
22- // struct Relationships: JSONAPI.Relationships {
23- // let one: ToOneRelationship<NonJSONAPIEntity, NoMetadata, NoLinks>
24- // let many: ToManyRelationship<NonJSONAPIEntity, NoMetadata, NoLinks>
25- // }
26- // }
27-
28- // enum NonJSONAPIEntityDescription: EntityDescription {
29- // static var type: String { return "other" }
30- //
31- // typealias Attributes = NoAttributes
32- // typealias Relationships = NoRelationships
33- // }
55+ enum TestEntityDescription : EntityDescription {
56+ static var type : String { return " test " }
3457
35- // struct NonJSONAPIEntity: Relatable, OptionalRelatable, JSONTyped {
36- // static var type: String { return "other" }
37- //
38- // typealias Identifier = NonJSONAPIEntity.Id
39- // typealias WrappedIdentifier = NonJSONAPIEntity.Id
40- //
41- // let id: Id
42- //
43- // let attributes: NoAttributes
44- // let relationships: NoRelationships
45- //
46- // struct Id: IdType {
47- // var rawValue: String
48- //
49- // typealias IdentifiableType = NonJSONAPIEntity
50- // typealias RawType = String
51- // }
52- // }
58+ typealias Attributes = NoAttributes
59+
60+ struct Relationships : JSONAPI . Relationships {
61+ let one : ToOneRelationship < NonJSONAPIEntity , NoMetadata , NoLinks >
62+ let many : ToManyRelationship < NonJSONAPIEntity , NoMetadata , NoLinks >
63+ }
64+ }
65+
66+ typealias TestEntity = JSONAPI . Entity < TestEntityDescription , NoMetadata , NoLinks , String >
67+
68+ enum TestEntity2Description : EntityDescription {
69+ static var type : String { return " test " }
70+
71+ typealias Attributes = NoAttributes
72+
73+ struct Relationships : JSONAPI . Relationships {
74+ let nullableOne : ToOneRelationship < NonJSONAPIEntity ? , NoMetadata , NoLinks >
75+ let nullableMaybeOne : ToOneRelationship < NonJSONAPIEntity ? , NoMetadata , NoLinks > ?
76+ let maybeOne : ToOneRelationship < NonJSONAPIEntity , NoMetadata , NoLinks > ?
77+ let maybeMany : ToManyRelationship < NonJSONAPIEntity , NoMetadata , NoLinks > ?
78+ }
79+ }
80+
81+ typealias TestEntity2 = JSONAPI . Entity < TestEntity2Description , NoMetadata , NoLinks , String >
82+
83+ struct NonJSONAPIEntity : Relatable , JSONTyped {
84+ static var type : String { return " other " }
85+
86+ typealias Identifier = NonJSONAPIEntity . Id
87+
88+ let id : Id
89+
90+ struct Id : IdType {
91+ var rawValue : String
92+
93+ typealias IdentifiableType = NonJSONAPIEntity
94+ typealias RawType = String
95+
96+ static func id( from rawValue: String ) -> Id {
97+ return Id ( rawValue: rawValue)
98+ }
99+ }
100+ }
53101}
0 commit comments