-
Notifications
You must be signed in to change notification settings - Fork 21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
chore: allow absent data
in ToManyRelationship
#110
base: main
Are you sure you want to change the base?
Changes from 3 commits
9b6392a
33ee3fa
fd514b1
93efa69
cae14e9
74c9230
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,7 +6,7 @@ | |
// | ||
|
||
import XCTest | ||
import JSONAPI | ||
@testable import JSONAPI | ||
|
||
class RelationshipTests: XCTestCase { | ||
|
||
|
@@ -235,6 +235,37 @@ extension RelationshipTests { | |
test_DecodeEncodeEquality(type: ToManyWithMetaAndLinks.self, | ||
data: to_many_relationship_with_meta_and_links) | ||
} | ||
|
||
func test_ToManyRelationshipWithMetaNoDataOmittable() { | ||
TestEntityType1.canHaveNoDataInRelationships = true | ||
|
||
let relationship = decoded(type: ToManyWithMeta.self, | ||
data: to_many_relationship_with_meta_no_data) | ||
|
||
XCTAssertEqual(relationship.ids, []) | ||
XCTAssertEqual(relationship.meta.a, "hello") | ||
|
||
TestEntityType1.canHaveNoDataInRelationships = false | ||
} | ||
|
||
func test_ToManyRelationshipWithMetaNoDataNotOmittable() { | ||
TestEntityType1.canHaveNoDataInRelationships = false | ||
|
||
do { | ||
_ = try decodedThrows(type: ToManyWithMeta.self, | ||
data: to_many_relationship_with_meta_no_data) | ||
XCTFail("Expected decoding to fail.") | ||
} catch let error as DecodingError { | ||
switch error { | ||
case .keyNotFound(ResourceLinkageCodingKeys.data, _): | ||
break | ||
default: | ||
XCTFail("Expected error to be DecodingError.keyNotFound(.data), but got \(error)") | ||
} | ||
} catch { | ||
XCTFail("Expected to have DecodingError.keyNotFound(.data), but got \(error)") | ||
} | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I would prefer to keep the coding keys private, not do an The test could use the XCTAssertThrowsError(
try decodedThrows(type: ToManyWithMeta.self,
data: to_many_relationship_with_meta_no_data)
) { error in
XCTAssertEqual(error.localizedDescription, "The data couldn’t be read because it is missing.")
} |
||
} | ||
} | ||
|
||
// MARK: Nullable | ||
|
@@ -277,12 +308,14 @@ extension RelationshipTests { | |
|
||
// MARK: - Test types | ||
extension RelationshipTests { | ||
enum TestEntityType1: ResourceObjectDescription { | ||
enum TestEntityType1: ResourceObjectDescription, ResourceObjectWithOptionalDataInRelationships { | ||
typealias Attributes = NoAttributes | ||
|
||
typealias Relationships = NoRelationships | ||
|
||
public static var jsonType: String { return "test_entity1" } | ||
|
||
static var canHaveNoDataInRelationships: Bool = false | ||
} | ||
|
||
typealias TestEntity1 = BasicEntity<TestEntityType1> | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is for
test_ToManyRelationshipWithMetaNoDataNotOmittable
to check the exact thrown error.