Skip to content

Commit

Permalink
fix(datastore): query predicate API and integration tests fixes #487 (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
drochetti authored May 27, 2020
1 parent c7d11a7 commit beec57c
Show file tree
Hide file tree
Showing 16 changed files with 43 additions and 56 deletions.
11 changes: 3 additions & 8 deletions Amplify/Categories/DataStore/DataStoreCategory+Behavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -19,21 +19,16 @@ extension DataStoreCategory: DataStoreBaseBehavior {
}

public func query<M: Model>(_ modelType: M.Type,
where predicate: QueryPredicateFactory? = nil,
where predicate: QueryPredicate? = nil,
paginate paginationInput: QueryPaginationInput? = nil,
completion: DataStoreCallback<[M]>) {
plugin.query(modelType, where: predicate, paginate: paginationInput, completion: completion)
}

public func delete<M: Model>(_ model: M,
where predicate: QueryPredicate? = nil,
completion: @escaping DataStoreCallback<Void>) {
plugin.delete(model, completion: completion)
}

public func delete<M: Model>(_ modelType: M.Type,
where predicate: @escaping QueryPredicateFactory,
completion: @escaping DataStoreCallback<Void>) {
plugin.delete(modelType, where: predicate, completion: completion)
plugin.delete(model, where: predicate, completion: completion)
}

public func delete<M: Model>(_ modelType: M.Type,
Expand Down
9 changes: 2 additions & 7 deletions Amplify/Categories/DataStore/DataStoreCategoryBehavior.swift
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,6 @@

import Combine

public typealias QueryPredicateFactory = () -> QueryPredicate

public typealias DataStoreCategoryBehavior = DataStoreBaseBehavior & DataStoreSubscribeBehavior

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

func query<M: Model>(_ modelType: M.Type,
where predicate: QueryPredicateFactory?,
where predicate: QueryPredicate?,
paginate paginationInput: QueryPaginationInput?,
completion: DataStoreCallback<[M]>)

func delete<M: Model>(_ model: M,
completion: @escaping DataStoreCallback<Void>)

func delete<M: Model>(_ modelType: M.Type,
where predicate: @escaping QueryPredicateFactory,
where predicate: QueryPredicate?,
completion: @escaping DataStoreCallback<Void>)

func delete<M: Model>(_ modelType: M.Type,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ extension List {
name = targetName ?? name
}

let predicate: QueryPredicateFactory = { field(name) == associatedId }
let predicate: QueryPredicate = field(name) == associatedId
Amplify.DataStore.query(Element.self, where: predicate) {
switch $0 {
case .success(let elements):
Expand Down
2 changes: 1 addition & 1 deletion Amplify/Core/Category/CategoryType.swift
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ public extension CategoryType {
return "Storage"
}
}

var category: Category {
switch self {
case .analytics:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class GraphQLCreateMutationTests: XCTestCase {
func testCreateGraphQLMutationFromSimpleModel() {
let post = Post(title: "title",
content: "content",
createdAt: Date(),
createdAt: .now(),
status: .private)
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
Expand Down Expand Up @@ -84,8 +84,8 @@ class GraphQLCreateMutationTests: XCTestCase {
/// - it contains an `input` of type `CreateCommentInput`
/// - it has a list of fields with a `postId`
func testCreateGraphQLMutationFromModelWithAssociation() {
let post = Post(title: "title", content: "content", createdAt: Date())
let comment = Comment(content: "comment", createdAt: Date(), post: post)
let post = Post(title: "title", content: "content", createdAt: .now())
let comment = Comment(content: "comment", createdAt: .now(), post: post)
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Comment.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
documentBuilder.add(decorator: ModelDecorator(model: comment))
Expand Down Expand Up @@ -136,7 +136,7 @@ class GraphQLCreateMutationTests: XCTestCase {
/// - it contains an `input` of type `CreatePostInput`
/// - it has a list of fields with no nested models
func testCreateGraphQLMutationFromSimpleModelWithSyncEnabled() {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
documentBuilder.add(decorator: ModelDecorator(model: post))
Expand Down Expand Up @@ -187,8 +187,8 @@ class GraphQLCreateMutationTests: XCTestCase {
/// - it contains an `input` of type `CreateCommentInput`
/// - it has a list of fields with a `postId`
func testCreateGraphQLMutationFromModelWithAssociationWithSyncEnabled() {
let post = Post(title: "title", content: "content", createdAt: Date())
let comment = Comment(content: "comment", createdAt: Date(), post: post)
let post = Post(title: "title", content: "content", createdAt: .now())
let comment = Comment(content: "comment", createdAt: .now(), post: post)

var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Comment.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GraphQLDeleteMutationTests: XCTestCase {
/// - it contains an `input` of type `ID!`
/// - it has a list of fields with no nested models
func testDeleteGraphQLMutationFromSimpleModel() {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
documentBuilder.add(decorator: ModelIdDecorator(id: post.id))
Expand Down Expand Up @@ -79,7 +79,7 @@ class GraphQLDeleteMutationTests: XCTestCase {
/// - it contains an `input` of type `ID!`
/// - it has a list of fields with no nested models
func testDeleteGraphQLMutationFromSimpleModelWithVersion() {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
documentBuilder.add(decorator: ModelIdDecorator(id: post.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ class GraphQLUpdateMutationTests: XCTestCase {
/// - it contains an `input` of type `UpdatePostInput`
/// - it has a list of fields with no nested models
func testUpdateGraphQLMutationFromSimpleModel() {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .update))
documentBuilder.add(decorator: ModelDecorator(model: post))
Expand Down Expand Up @@ -80,7 +80,7 @@ class GraphQLUpdateMutationTests: XCTestCase {
/// - it contains an `input` of type `UpdatePostInput`
/// - it has a list of fields with no nested models
func testUpdateGraphQLMutationFromSimpleModelWithVersion() {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .update))
documentBuilder.add(decorator: ModelDecorator(model: post))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
}

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

func testCreateMutationGraphQLRequest() throws {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: post.modelName,
operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
Expand Down Expand Up @@ -103,7 +103,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
}

func testUpdateMutationGraphQLRequest() throws {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: post.modelName,
operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .update))
Expand Down Expand Up @@ -146,7 +146,7 @@ class GraphQLRequestAnyModelWithSyncTests: XCTestCase {
}

func testDeleteMutationGraphQLRequest() throws {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: post.modelName,
operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class GraphQLRequestAuthRuleTests: XCTestCase {
}

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

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

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

func testDeleteMutationGraphQLRequest() throws {
let blog = Blog(content: "content", createdAt: Date(), owner: nil, authorNotes: nil)
let blog = Blog(content: "content", createdAt: .now(), owner: nil, authorNotes: nil)
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelName: blog.modelName, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
documentBuilder.add(decorator: ModelIdDecorator(id: blog.id))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class GraphQLRequestModelTests: XCTestCase {
/// - the `responseType` is correct
/// - the `variables` is non-nil
func testCreateMutationGraphQLRequest() {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .create))
documentBuilder.add(decorator: ModelDecorator(model: post))
Expand All @@ -46,7 +46,7 @@ class GraphQLRequestModelTests: XCTestCase {
}

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

func testDeleteMutationGraphQLRequest() {
let post = Post(title: "title", content: "content", createdAt: Date())
let post = Post(title: "title", content: "content", createdAt: .now())
var documentBuilder = ModelBasedGraphQLDocumentBuilder(modelType: Post.self, operationType: .mutation)
documentBuilder.add(decorator: DirectiveNameDecorator(type: .delete))
documentBuilder.add(decorator: ModelDecorator(model: post))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ class QueryPredicateGraphQLTests: XCTestCase {

func testPredicateToGraphQLValues() throws {
let post = Post.keys
guard let date = "2019-11-23T02:06:50.689Z".iso8601Date else {
guard let date = try? Temporal.DateTime(iso8601String: "2019-11-23T02:06:50.689Z") else {
XCTFail("Failed to set up date")
return
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
byId id: String,
completion: DataStoreCallback<M?>) {
reinitStorageEngineIfNeeded()
let predicate: QueryPredicateFactory = { field("id") == id }
let predicate: QueryPredicate = field("id") == id
query(modelType, where: predicate, paginate: .firstResult) {
switch $0 {
case .success(let models):
Expand All @@ -78,12 +78,12 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
}

public func query<M: Model>(_ modelType: M.Type,
where predicateFactory: QueryPredicateFactory? = nil,
where predicate: QueryPredicate? = nil,
paginate paginationInput: QueryPaginationInput? = nil,
completion: DataStoreCallback<[M]>) {
reinitStorageEngineIfNeeded()
storageEngine.query(modelType,
predicate: predicateFactory?(),
predicate: predicate,
paginationInput: paginationInput,
completion: completion)
}
Expand All @@ -98,15 +98,17 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
}

public func delete<M: Model>(_ model: M,
where predicate: QueryPredicate? = nil,
completion: @escaping DataStoreCallback<Void>) {
reinitStorageEngineIfNeeded()
// TODO: handle query predicate like in the update flow
storageEngine.delete(type(of: model), withId: model.id) { result in
self.onDeleteCompletion(result: result, completion: completion)
}
}

public func delete<M: Model>(_ modelType: M.Type,
where predicate: @escaping QueryPredicateFactory,
where predicate: QueryPredicate,
completion: @escaping DataStoreCallback<Void>) {
reinitStorageEngineIfNeeded()
let onCompletion: DataStoreCallback<[M]> = { result in
Expand All @@ -121,7 +123,7 @@ extension AWSDataStorePlugin: DataStoreBaseBehavior {
}
}
storageEngine.delete(modelType,
predicate: predicate(),
predicate: predicate,
completion: onCompletion)
}

Expand Down
11 changes: 3 additions & 8 deletions AmplifyTestCommon/Mocks/MockDataStoreCategoryPlugin.swift
Original file line number Diff line number Diff line change
Expand Up @@ -36,25 +36,20 @@ class MockDataStoreCategoryPlugin: MessageReporter, DataStoreCategoryPlugin {
}

func query<M: Model>(_ modelType: M.Type,
where predicate: QueryPredicateFactory?,
where predicate: QueryPredicate?,
paginate paginationInput: QueryPaginationInput?,
completion: (DataStoreResult<[M]>) -> Void) {
notify("queryByPredicate")
}

func delete<M: Model>(_ model: M,
completion: (DataStoreResult<Void>) -> Void) {
notify("deleteByModel")
}

func delete<M: Model>(_ modelType: M.Type,
withId id: String,
completion: (DataStoreResult<Void>) -> Void) {
notify("deleteById")
}

func delete<M: Model>(_ model: M.Type,
where predicate: @escaping QueryPredicateFactory,
func delete<M: Model>(_ model: M,
where predicate: QueryPredicate? = nil,
completion: @escaping DataStoreCallback<Void>) {
notify("deleteByPredicate")
}
Expand Down
4 changes: 2 additions & 2 deletions AmplifyTestCommon/Models/Blog.swift
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ import Foundation
public struct Blog: Model {
public let id: String
public var content: String
public var createdAt: Date
public var createdAt: Temporal.DateTime
public var owner: String?
public var authorNotes: String?

public init(id: String = UUID().uuidString,
content: String,
createdAt: Date,
createdAt: Temporal.DateTime,
owner: String?,
authorNotes: String?) {
self.id = id
Expand Down
2 changes: 1 addition & 1 deletion Podfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ DEPENDENCIES:
- SwiftLint

SPEC REPOS:
trunk:
https://cdn.cocoapods.org/:
- AWSAuthCore
- AWSCognitoIdentityProvider
- AWSCognitoIdentityProviderASF
Expand Down
2 changes: 1 addition & 1 deletion Pods/Manifest.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit beec57c

Please sign in to comment.