@@ -24,6 +24,10 @@ class EntityTests: XCTestCase {
2424
2525 XCTAssertEqual ( entity2 ~> \. other, entity1. id)
2626 }
27+
28+ func test_optional_relationship_operator_access( ) {
29+
30+ }
2731
2832 func test_toMany_relationship_operator_access( ) {
2933 let entity1 = TestEntity1 ( )
@@ -33,6 +37,10 @@ class EntityTests: XCTestCase {
3337
3438 XCTAssertEqual ( entity3 ~> \. others, [ entity1. id, entity2. id, entity4. id] )
3539 }
40+
41+ func test_optionalToMany_relationship_opeartor_access( ) {
42+
43+ }
3644
3745 func test_relationshipIds( ) {
3846 let entity1 = TestEntity1 ( )
@@ -63,9 +71,12 @@ class EntityTests: XCTestCase {
6371 let _ = TestEntity6 ( id: . init( rawValue: " 6 " ) , attributes: . init( here: . init( value: " here " ) , maybeHere: nil , maybeNull: . init( value: nil ) ) )
6472 let _ = TestEntity7 ( id: . init( rawValue: " 7 " ) , attributes: . init( here: . init( value: " hello " ) , maybeHereMaybeNull: . init( value: " world " ) ) )
6573 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 ) ) ) )
66- let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: nil ) )
67- let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: . init( entity: nil ) ) )
68- let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: . init( entity: entity1, meta: . none, links: . none) ) )
74+ let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: nil , optionalOne: nil , optionalNullableOne: nil , optionalMany: nil ) )
75+ let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: . init( entity: nil ) , optionalOne: nil , optionalNullableOne: nil , optionalMany: nil ) )
76+ let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: . init( entity: entity1, meta: . none, links: . none) , optionalOne: nil , optionalNullableOne: nil , optionalMany: nil ) )
77+ let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: nil , optionalOne: entity1. pointer, optionalNullableOne: nil , optionalMany: nil ) )
78+ let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: nil , optionalOne: nil , optionalNullableOne: . init( entity: entity1, meta: . none, links: . none) , optionalMany: nil ) )
79+ let _ = TestEntity9 ( id: . init( rawValue: " 9 " ) , relationships: . init( one: entity1. pointer, nullableOne: nil , optionalOne: nil , optionalNullableOne: . init( entity: entity1, meta: . none, links: . none) , optionalMany: . init( entities: [ ] , meta: . none, links: . none) ) )
6980 let e10id1 = TestEntity10 . Identifier ( rawValue: " hello " )
7081 let e10id2 = TestEntity10 . Id ( rawValue: " world " )
7182 let e10id3 : TestEntity10 . Id = " ! "
@@ -275,18 +286,50 @@ extension EntityTests {
275286
276287// MARK: Relationship omission and nullification
277288extension EntityTests {
289+ func test_nullableRelationshipNotNullOrOmitted( ) {
290+ let entity = decoded ( type: TestEntity9 . self,
291+ data: entity_optional_not_omitted_relationship)
292+
293+ XCTAssertEqual ( ( entity ~> \. nullableOne) ? . rawValue, " 3323 " )
294+ XCTAssertEqual ( ( entity ~> \. one) . rawValue, " 4459 " )
295+ XCTAssertNil ( entity ~> \. optionalOne)
296+ XCTAssertEqual ( ( entity ~> \. optionalNullableOne) ? . rawValue, " 1229 " )
297+ XCTAssertNoThrow ( try TestEntity9 . check ( entity) )
298+ }
299+
300+ func test_nullableRelationshipNotNullOrOmitted_encode( ) {
301+ test_DecodeEncodeEquality ( type: TestEntity9 . self,
302+ data: entity_optional_not_omitted_relationship)
303+ }
304+
278305 func test_nullableRelationshipNotNull( ) {
279306 let entity = decoded ( type: TestEntity9 . self,
280- data: entity_omitted_relationship)
307+ data: entity_omitted_relationship)
281308
282309 XCTAssertEqual ( ( entity ~> \. nullableOne) ? . rawValue, " 3323 " )
283310 XCTAssertEqual ( ( entity ~> \. one) . rawValue, " 4459 " )
311+ XCTAssertNil ( entity ~> \. optionalNullableOne)
284312 XCTAssertNoThrow ( try TestEntity9 . check ( entity) )
285313 }
286314
287315 func test_nullableRelationshipNotNull_encode( ) {
288316 test_DecodeEncodeEquality ( type: TestEntity9 . self,
289- data: entity_omitted_relationship)
317+ data: entity_omitted_relationship)
318+ }
319+
320+ func test_optionalNullableRelationshipNulled( ) {
321+ let entity = decoded ( type: TestEntity9 . self,
322+ data: entity_optional_nullable_nulled_relationship)
323+
324+ XCTAssertEqual ( ( entity ~> \. nullableOne) ? . rawValue, " 3323 " )
325+ XCTAssertEqual ( ( entity ~> \. one) . rawValue, " 4459 " )
326+ XCTAssertNil ( entity ~> \. optionalNullableOne)
327+ XCTAssertNoThrow ( try TestEntity9 . check ( entity) )
328+ }
329+
330+ func test_optionalNullableRelationshipNulled_encode( ) {
331+ test_DecodeEncodeEquality ( type: TestEntity9 . self,
332+ data: entity_optional_nullable_nulled_relationship)
290333 }
291334
292335 func test_nullableRelationshipIsNull( ) {
@@ -295,13 +338,30 @@ extension EntityTests {
295338
296339 XCTAssertNil ( entity ~> \. nullableOne)
297340 XCTAssertEqual ( ( entity ~> \. one) . rawValue, " 4452 " )
341+ XCTAssertNil ( entity ~> \. optionalNullableOne)
298342 XCTAssertNoThrow ( try TestEntity9 . check ( entity) )
299343 }
300344
301345 func test_nullableRelationshipIsNull_encode( ) {
302346 test_DecodeEncodeEquality ( type: TestEntity9 . self,
303347 data: entity_nulled_relationship)
304348 }
349+
350+ func test_optionalToManyIsNotOmitted( ) {
351+ let entity = decoded ( type: TestEntity9 . self,
352+ data: entity_optional_to_many_relationship_not_omitted)
353+
354+ XCTAssertEqual ( ( entity ~> \. nullableOne) ? . rawValue, " 3323 " )
355+ XCTAssertEqual ( ( entity ~> \. one) . rawValue, " 4459 " )
356+ XCTAssertEqual ( ( entity ~> \. optionalMany) ? [ 0 ] . rawValue, " 332223 " )
357+ XCTAssertNil ( entity ~> \. optionalNullableOne)
358+ XCTAssertNoThrow ( try TestEntity9 . check ( entity) )
359+ }
360+
361+ func test_optionalToManyIsNotOmitted_encode( ) {
362+ test_DecodeEncodeEquality ( type: TestEntity9 . self,
363+ data: entity_optional_to_many_relationship_not_omitted)
364+ }
305365}
306366
307367// MARK: Relationships of same type as root entity
@@ -581,13 +641,14 @@ extension EntityTests {
581641
582642 let nullableOne : ToOneRelationship < TestEntity1 ? , NoMetadata , NoLinks >
583643
644+ let optionalOne : ToOneRelationship < TestEntity1 , NoMetadata , NoLinks > ?
645+
646+ let optionalNullableOne : ToOneRelationship < TestEntity1 ? , NoMetadata , NoLinks > ?
647+
648+ let optionalMany : ToManyRelationship < TestEntity1 , NoMetadata , NoLinks > ?
649+
584650 // a nullable many is not allowed. it should
585651 // just be an empty array.
586-
587- // omitted relationships are not allowed either,
588- // so ToOneRelationship<TestEntity1>? (with the
589- // question on the relationship, not the entity)
590- // is not a thing.
591652 }
592653 }
593654
0 commit comments