Skip to content

Commit beec57c

Browse files
authored
fix(datastore): query predicate API and integration tests fixes #487 (#491)
1 parent c7d11a7 commit beec57c

16 files changed

+43
-56
lines changed

Amplify/Categories/DataStore/DataStoreCategory+Behavior.swift

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,21 +19,16 @@ extension DataStoreCategory: DataStoreBaseBehavior {
1919
}
2020

2121
public func query<M: Model>(_ modelType: M.Type,
22-
where predicate: QueryPredicateFactory? = nil,
22+
where predicate: QueryPredicate? = nil,
2323
paginate paginationInput: QueryPaginationInput? = nil,
2424
completion: DataStoreCallback<[M]>) {
2525
plugin.query(modelType, where: predicate, paginate: paginationInput, completion: completion)
2626
}
2727

2828
public func delete<M: Model>(_ model: M,
29+
where predicate: QueryPredicate? = nil,
2930
completion: @escaping DataStoreCallback<Void>) {
30-
plugin.delete(model, completion: completion)
31-
}
32-
33-
public func delete<M: Model>(_ modelType: M.Type,
34-
where predicate: @escaping QueryPredicateFactory,
35-
completion: @escaping DataStoreCallback<Void>) {
36-
plugin.delete(modelType, where: predicate, completion: completion)
31+
plugin.delete(model, where: predicate, completion: completion)
3732
}
3833

3934
public func delete<M: Model>(_ modelType: M.Type,

Amplify/Categories/DataStore/DataStoreCategoryBehavior.swift

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@
77

88
import Combine
99

10-
public typealias QueryPredicateFactory = () -> QueryPredicate
11-
1210
public typealias DataStoreCategoryBehavior = DataStoreBaseBehavior & DataStoreSubscribeBehavior
1311

1412
public protocol DataStoreBaseBehavior {
@@ -23,15 +21,12 @@ public protocol DataStoreBaseBehavior {
2321
completion: DataStoreCallback<M?>)
2422

2523
func query<M: Model>(_ modelType: M.Type,
26-
where predicate: QueryPredicateFactory?,
24+
where predicate: QueryPredicate?,
2725
paginate paginationInput: QueryPaginationInput?,
2826
completion: DataStoreCallback<[M]>)
2927

3028
func delete<M: Model>(_ model: M,
31-
completion: @escaping DataStoreCallback<Void>)
32-
33-
func delete<M: Model>(_ modelType: M.Type,
34-
where predicate: @escaping QueryPredicateFactory,
29+
where predicate: QueryPredicate?,
3530
completion: @escaping DataStoreCallback<Void>)
3631

3732
func delete<M: Model>(_ modelType: M.Type,

Amplify/Categories/DataStore/Model/Collection/List+LazyLoad.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ extension List {
4545
name = targetName ?? name
4646
}
4747

48-
let predicate: QueryPredicateFactory = { field(name) == associatedId }
48+
let predicate: QueryPredicate = field(name) == associatedId
4949
Amplify.DataStore.query(Element.self, where: predicate) {
5050
switch $0 {
5151
case .success(let elements):

Amplify/Core/Category/CategoryType.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ public extension CategoryType {
6161
return "Storage"
6262
}
6363
}
64-
64+
6565
var category: Category {
6666
switch self {
6767
case .analytics:

AmplifyPlugins/Core/AWSPluginsCoreTests/Model/GraphQLDocument/GraphQLCreateMutationTests.swift

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ class GraphQLCreateMutationTests: XCTestCase {
3535
func testCreateGraphQLMutationFromSimpleModel() {
3636
let post = Post(title: "title",
3737
content: "content",
38-
createdAt: Date(),
38+
createdAt: .now(),
3939
status: .private)
4040
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
4141
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
@@ -84,8 +84,8 @@ class GraphQLCreateMutationTests: XCTestCase {
8484
/// - it contains an `input` of type `CreateCommentInput`
8585
/// - it has a list of fields with a `postId`
8686
func testCreateGraphQLMutationFromModelWithAssociation() {
87-
let post = Post(title: "title", content: "content", createdAt: Date())
88-
let comment = Comment(content: "comment", createdAt: Date(), post: post)
87+
let post = Post(title: "title", content: "content", createdAt: .now())
88+
let comment = Comment(content: "comment", createdAt: .now(), post: post)
8989
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Comment.self, operationType: .mutation)
9090
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
9191
documentBuilder.add(decorator: ModelDecorator(model: comment))
@@ -136,7 +136,7 @@ class GraphQLCreateMutationTests: XCTestCase {
136136
/// - it contains an `input` of type `CreatePostInput`
137137
/// - it has a list of fields with no nested models
138138
func testCreateGraphQLMutationFromSimpleModelWithSyncEnabled() {
139-
let post = Post(title: "title", content: "content", createdAt: Date())
139+
let post = Post(title: "title", content: "content", createdAt: .now())
140140
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
141141
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
142142
documentBuilder.add(decorator: ModelDecorator(model: post))
@@ -187,8 +187,8 @@ class GraphQLCreateMutationTests: XCTestCase {
187187
/// - it contains an `input` of type `CreateCommentInput`
188188
/// - it has a list of fields with a `postId`
189189
func testCreateGraphQLMutationFromModelWithAssociationWithSyncEnabled() {
190-
let post = Post(title: "title", content: "content", createdAt: Date())
191-
let comment = Comment(content: "comment", createdAt: Date(), post: post)
190+
let post = Post(title: "title", content: "content", createdAt: .now())
191+
let comment = Comment(content: "comment", createdAt: .now(), post: post)
192192

193193
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Comment.self, operationType: .mutation)
194194
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))

AmplifyPlugins/Core/AWSPluginsCoreTests/Model/GraphQLDocument/GraphQLDeleteMutationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class GraphQLDeleteMutationTests: XCTestCase {
3333
/// - it contains an `input` of type `ID!`
3434
/// - it has a list of fields with no nested models
3535
func testDeleteGraphQLMutationFromSimpleModel() {
36-
let post = Post(title: "title", content: "content", createdAt: Date())
36+
let post = Post(title: "title", content: "content", createdAt: .now())
3737
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
3838
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
3939
documentBuilder.add(decorator: ModelIdDecorator(id: post.id))
@@ -79,7 +79,7 @@ class GraphQLDeleteMutationTests: XCTestCase {
7979
/// - it contains an `input` of type `ID!`
8080
/// - it has a list of fields with no nested models
8181
func testDeleteGraphQLMutationFromSimpleModelWithVersion() {
82-
let post = Post(title: "title", content: "content", createdAt: Date())
82+
let post = Post(title: "title", content: "content", createdAt: .now())
8383
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
8484
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
8585
documentBuilder.add(decorator: ModelIdDecorator(id: post.id))

AmplifyPlugins/Core/AWSPluginsCoreTests/Model/GraphQLDocument/GraphQLUpdateMutationTests.swift

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ class GraphQLUpdateMutationTests: XCTestCase {
3333
/// - it contains an `input` of type `UpdatePostInput`
3434
/// - it has a list of fields with no nested models
3535
func testUpdateGraphQLMutationFromSimpleModel() {
36-
let post = Post(title: "title", content: "content", createdAt: Date())
36+
let post = Post(title: "title", content: "content", createdAt: .now())
3737
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
3838
documentBuilder.add(decorator: DirectiveNameDecorator(type: .update))
3939
documentBuilder.add(decorator: ModelDecorator(model: post))
@@ -80,7 +80,7 @@ class GraphQLUpdateMutationTests: XCTestCase {
8080
/// - it contains an `input` of type `UpdatePostInput`
8181
/// - it has a list of fields with no nested models
8282
func testUpdateGraphQLMutationFromSimpleModelWithVersion() {
83-
let post = Post(title: "title", content: "content", createdAt: Date())
83+
let post = Post(title: "title", content: "content", createdAt: .now())
8484
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
8585
documentBuilder.add(decorator: DirectiveNameDecorator(type: .update))
8686
documentBuilder.add(decorator: ModelDecorator(model: post))

AmplifyPlugins/Core/AWSPluginsCoreTests/Model/GraphQLRequest/GraphQLRequestAnyModelWithSyncTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
2222
}
2323

2424
func testQueryGraphQLRequest() throws {
25-
let post = Post(title: "title", content: "content", createdAt: Date())
25+
let post = Post(title: "title", content: "content", createdAt: .now())
2626
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: post.modelName, operationType: .query)
2727
documentBuilder.add(decorator: DirectiveNameDecorator(type: .get))
2828
documentBuilder.add(decorator: ModelIdDecorator(id: post.id))
@@ -60,7 +60,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
6060
}
6161

6262
func testCreateMutationGraphQLRequest() throws {
63-
let post = Post(title: "title", content: "content", createdAt: Date())
63+
let post = Post(title: "title", content: "content", createdAt: .now())
6464
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: post.modelName,
6565
operationType: .mutation)
6666
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
@@ -103,7 +103,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
103103
}
104104

105105
func testUpdateMutationGraphQLRequest() throws {
106-
let post = Post(title: "title", content: "content", createdAt: Date())
106+
let post = Post(title: "title", content: "content", createdAt: .now())
107107
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: post.modelName,
108108
operationType: .mutation)
109109
documentBuilder.add(decorator: DirectiveNameDecorator(type: .update))
@@ -146,7 +146,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
146146
}
147147

148148
func testDeleteMutationGraphQLRequest() throws {
149-
let post = Post(title: "title", content: "content", createdAt: Date())
149+
let post = Post(title: "title", content: "content", createdAt: .now())
150150
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: post.modelName,
151151
operationType: .mutation)
152152
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))

AmplifyPlugins/Core/AWSPluginsCoreTests/Model/GraphQLRequest/GraphQLRequestAuthRuleTests.swift

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class GraphQLRequestAuthRuleTests: XCTestCase {
2121
}
2222

2323
func testQueryGraphQLRequest() throws {
24-
let blog = Blog(content: "content", createdAt: Date(), owner: nil, authorNotes: nil)
24+
let blog = Blog(content: "content", createdAt: .now(), owner: nil, authorNotes: nil)
2525
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: blog.modelName, operationType: .query)
2626
documentBuilder.add(decorator: DirectiveNameDecorator(type: .get))
2727
documentBuilder.add(decorator: ModelIdDecorator(id: blog.id))
@@ -57,7 +57,7 @@ class GraphQLRequestAuthRuleTests: XCTestCase {
5757
}
5858

5959
func testCreateMutationGraphQLRequest() throws {
60-
let blog = Blog(content: "content", createdAt: Date(), owner: nil, authorNotes: nil)
60+
let blog = Blog(content: "content", createdAt: .now(), owner: nil, authorNotes: nil)
6161
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: blog.modelName, operationType: .mutation)
6262
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
6363
documentBuilder.add(decorator: ModelDecorator(model: blog))
@@ -98,7 +98,7 @@ class GraphQLRequestAuthRuleTests: XCTestCase {
9898
}
9999

100100
func testUpdateMutationGraphQLRequest() throws {
101-
let blog = Blog(content: "content", createdAt: Date(), owner: nil, authorNotes: nil)
101+
let blog = Blog(content: "content", createdAt: .now(), owner: nil, authorNotes: nil)
102102
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: blog.modelName, operationType: .mutation)
103103
documentBuilder.add(decorator: DirectiveNameDecorator(type: .update))
104104
documentBuilder.add(decorator: ModelDecorator(model: blog))
@@ -138,7 +138,7 @@ class GraphQLRequestAuthRuleTests: XCTestCase {
138138
}
139139

140140
func testDeleteMutationGraphQLRequest() throws {
141-
let blog = Blog(content: "content", createdAt: Date(), owner: nil, authorNotes: nil)
141+
let blog = Blog(content: "content", createdAt: .now(), owner: nil, authorNotes: nil)
142142
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: blog.modelName, operationType: .mutation)
143143
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
144144
documentBuilder.add(decorator: ModelIdDecorator(id: blog.id))

AmplifyPlugins/Core/AWSPluginsCoreTests/Model/GraphQLRequest/GraphQLRequestModelTests.swift

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class GraphQLRequestModelTests: XCTestCase {
3232
/// - the `responseType` is correct
3333
/// - the `variables` is non-nil
3434
func testCreateMutationGraphQLRequest() {
35-
let post = Post(title: "title", content: "content", createdAt: Date())
35+
let post = Post(title: "title", content: "content", createdAt: .now())
3636
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
3737
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
3838
documentBuilder.add(decorator: ModelDecorator(model: post))
@@ -46,7 +46,7 @@ class GraphQLRequestModelTests: XCTestCase {
4646
}
4747

4848
func testUpdateMutationGraphQLRequest() {
49-
let post = Post(title: "title", content: "content", createdAt: Date())
49+
let post = Post(title: "title", content: "content", createdAt: .now())
5050
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
5151
documentBuilder.add(decorator: DirectiveNameDecorator(type: .update))
5252
documentBuilder.add(decorator: ModelDecorator(model: post))
@@ -60,7 +60,7 @@ class GraphQLRequestModelTests: XCTestCase {
6060
}
6161

6262
func testDeleteMutationGraphQLRequest() {
63-
let post = Post(title: "title", content: "content", createdAt: Date())
63+
let post = Post(title: "title", content: "content", createdAt: .now())
6464
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
6565
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
6666
documentBuilder.add(decorator: ModelDecorator(model: post))

0 commit comments

Comments
 (0)