Skip to content

Commit

Permalink
update for JSONAPI v5
Browse files Browse the repository at this point in the history
  • Loading branch information
mattpolzin committed Sep 27, 2020
1 parent b724e1e commit 9d7478b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 27 deletions.
10 changes: 5 additions & 5 deletions Package.resolved
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,16 @@
},
{
"package": "JSONAPI",
"repositoryURL": "https://github.com/mattpolzin/JSONAPI",
"repositoryURL": "https://github.com/mattpolzin/JSONAPI.git",
"state": {
"branch": null,
"revision": "51cb140ebf27949d1185227e3eaef5a54d9a3bee",
"version": "4.0.0"
"revision": "c645c73f51808b85a881e8af6f286086ee65cc35",
"version": "5.0.0"
}
},
{
"package": "Poly",
"repositoryURL": "https://github.com/mattpolzin/Poly",
"repositoryURL": "https://github.com/mattpolzin/Poly.git",
"state": {
"branch": null,
"revision": "36ba3f624bffa34f5f9b9c7648eab3cfdcab4748",
Expand All @@ -48,7 +48,7 @@
},
{
"package": "SwiftCheck",
"repositoryURL": "https://github.com/typelift/SwiftCheck",
"repositoryURL": "https://github.com/typelift/SwiftCheck.git",
"state": {
"branch": null,
"revision": "077c096c3ddfc38db223ac8e525ad16ffb987138",
Expand Down
2 changes: 1 addition & 1 deletion Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ let package = Package(
],
dependencies: [
.package(url: "https://github.com/typelift/SwiftCheck.git", .upToNextMinor(from: "0.12.0")),
.package(url: "https://github.com/mattpolzin/JSONAPI.git", from: "4.0.0"),
.package(url: "https://github.com/mattpolzin/JSONAPI.git", from: "5.0.0"),
],
targets: [
// Targets are the basic building blocks of a package. A target can define a module or a test suite.
Expand Down
61 changes: 40 additions & 21 deletions Sources/JSONAPIArbitrary/Relationship+Arbitrary.swift
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,72 @@
import SwiftCheck
import JSONAPI

extension ToOneRelationship: Arbitrary where Identifiable.ID: Arbitrary, MetaType: Arbitrary, LinksType: Arbitrary {
public static var arbitrary: Gen<ToOneRelationship<Identifiable, MetaType, LinksType>> {
extension ToOneRelationship: Arbitrary where Identifiable.ID: Arbitrary, IdMetaType: Arbitrary, MetaType: Arbitrary, LinksType: Arbitrary {
public static var arbitrary: Gen<ToOneRelationship<Identifiable, IdMetaType, MetaType, LinksType>> {
return Gen.compose { c in
return .init(id: c.generate(),
meta: c.generate(),
links: c.generate())
return .init(
id: (c.generate(), c.generate()),
meta: c.generate(),
links: c.generate()
)
}
}
}

extension ToOneRelationship where MetaType: Arbitrary, LinksType: Arbitrary {
extension ToOneRelationship where IdMetaType: Arbitrary, MetaType: Arbitrary, LinksType: Arbitrary {
/// Create a generator of arbitrary ToOneRelationships that will all
/// point to one of the given entities. This allows you to create
/// arbitrary relationships that make sense in a broader context where
/// the relationship must actually point to another entity.
public static func arbitrary<E: ResourceObjectType>(givenEntities: [E]) -> Gen<ToOneRelationship<Identifiable, MetaType, LinksType>> where E.Id == Identifiable.ID {
public static func arbitrary<E: ResourceObjectType>(givenEntities: [E]) -> Gen<ToOneRelationship<Identifiable, IdMetaType, MetaType, LinksType>> where E.Id == Identifiable.ID {

return Gen.compose { c in
let idGen = Gen.fromElements(of: givenEntities).map { $0.id }
return .init(id: c.generate(using: idGen),
meta: c.generate(),
links: c.generate())
return .init(
id: (c.generate(using: idGen), c.generate()),
meta: c.generate(),
links: c.generate()
)
}
}
}

extension ToManyRelationship: Arbitrary where Relatable.ID: Arbitrary, MetaType: Arbitrary, LinksType: Arbitrary {
public static var arbitrary: Gen<ToManyRelationship<Relatable, MetaType, LinksType>> {
extension ToManyRelationship: Arbitrary where Relatable.ID: Arbitrary, IdMetaType: Arbitrary, MetaType: Arbitrary, LinksType: Arbitrary {
public static var arbitrary: Gen<ToManyRelationship<Relatable, IdMetaType, MetaType, LinksType>> {
let idWithMetaGen: Gen<ID> = Gen.compose { c in
return .init(
id: c.generate(),
meta: c.generate()
)
}
let idWithMetaArrayGen: Gen<[(Relatable.ID, IdMetaType)]> = idWithMetaGen
.map { ($0.id, $0.meta) }
.proliferate
return Gen.compose { c in
return .init(ids: c.generate(),
meta: c.generate(),
links: c.generate())
return .init(
idsWithMetadata: c.generate(using: idWithMetaArrayGen),
meta: c.generate(),
links: c.generate()
)
}
}
}

extension ToManyRelationship where MetaType: Arbitrary, LinksType: Arbitrary {
extension ToManyRelationship where IdMetaType: Arbitrary, MetaType: Arbitrary, LinksType: Arbitrary {
/// Create a generator of arbitrary ToManyRelationships that will all
/// point to some number of the given entities. This allows you to create
/// arbitrary relationships that make sense in a broader context where
/// the relationship must actually point to other existing entities.
public static func arbitrary<E: ResourceObjectType>(givenEntities: [E]) -> Gen<ToManyRelationship<Relatable, MetaType, LinksType>> where E.Id == Relatable.ID {
public static func arbitrary<E: ResourceObjectType>(givenEntities: [E]) -> Gen<ToManyRelationship<Relatable, IdMetaType, MetaType, LinksType>> where E.Id == Relatable.ID {
return Gen.compose { c in
let idsGen = Gen.fromElements(of: givenEntities).map { $0.id }.proliferate
return .init(ids: c.generate(using: idsGen),
meta: c.generate(),
links: c.generate())

let idsGen = Gen.zip(Gen.fromElements(of: givenEntities).map { $0.id }, IdMetaType.arbitrary)
.proliferate
return .init(
idsWithMetadata: c.generate(using: idsGen),
meta: c.generate(),
links: c.generate()
)
}
}
}
Expand Down

0 comments on commit 9d7478b

Please sign in to comment.