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