|
6 | 6 | // |
7 | 7 |
|
8 | 8 | import XCTest |
9 | | -import JSONAPI |
| 9 | +@testable import JSONAPI |
10 | 10 |
|
11 | 11 | class RelationshipTests: XCTestCase { |
12 | 12 |
|
@@ -236,12 +236,35 @@ extension RelationshipTests { |
236 | 236 | data: to_many_relationship_with_meta_and_links) |
237 | 237 | } |
238 | 238 |
|
239 | | - func test_ToManyRelationshipWithMetaNoData() { |
| 239 | + func test_ToManyRelationshipWithMetaNoDataOmittable() { |
| 240 | + TestEntityType1.canHaveNoDataInRelationships = true |
| 241 | + |
240 | 242 | let relationship = decoded(type: ToManyWithMeta.self, |
241 | 243 | data: to_many_relationship_with_meta_no_data) |
242 | 244 |
|
243 | 245 | XCTAssertEqual(relationship.ids, []) |
244 | 246 | XCTAssertEqual(relationship.meta.a, "hello") |
| 247 | + |
| 248 | + TestEntityType1.canHaveNoDataInRelationships = false |
| 249 | + } |
| 250 | + |
| 251 | + func test_ToManyRelationshipWithMetaNoDataNotOmittable() { |
| 252 | + TestEntityType1.canHaveNoDataInRelationships = false |
| 253 | + |
| 254 | + do { |
| 255 | + _ = try decodedThrows(type: ToManyWithMeta.self, |
| 256 | + data: to_many_relationship_with_meta_no_data) |
| 257 | + XCTFail("Expected decoding to fail.") |
| 258 | + } catch let error as DecodingError { |
| 259 | + switch error { |
| 260 | + case .keyNotFound(ResourceLinkageCodingKeys.data, _): |
| 261 | + break |
| 262 | + default: |
| 263 | + XCTFail("Expected error to be DecodingError.keyNotFound(.data), but got \(error)") |
| 264 | + } |
| 265 | + } catch { |
| 266 | + XCTFail("Expected to have DecodingError.keyNotFound(.data), but got \(error)") |
| 267 | + } |
245 | 268 | } |
246 | 269 | } |
247 | 270 |
|
@@ -285,12 +308,14 @@ extension RelationshipTests { |
285 | 308 |
|
286 | 309 | // MARK: - Test types |
287 | 310 | extension RelationshipTests { |
288 | | - enum TestEntityType1: ResourceObjectDescription { |
| 311 | + enum TestEntityType1: ResourceObjectDescription, ResourceObjectWithOptionalDataInRelationships { |
289 | 312 | typealias Attributes = NoAttributes |
290 | 313 |
|
291 | 314 | typealias Relationships = NoRelationships |
292 | 315 |
|
293 | 316 | public static var jsonType: String { return "test_entity1" } |
| 317 | + |
| 318 | + static var canHaveNoDataInRelationships: Bool = false |
294 | 319 | } |
295 | 320 |
|
296 | 321 | typealias TestEntity1 = BasicEntity<TestEntityType1> |
|
0 commit comments