From 9860fd1dd9a468e659ecf64cf766bdd5637dc09a Mon Sep 17 00:00:00 2001 From: Michael law <1365977+lawmicha@users.noreply.github.com> Date: Tue, 2 Jun 2020 09:07:55 -0700 Subject: [PATCH] fix(api): support query predicate Temporal.DateTime, fix integ tests --- .../AnyModelIntegrationTests.swift | 19 +++++++-------- .../GraphQLModelBasedTests.swift | 24 ++++++++++--------- .../GraphQLModelBased/README.md | 7 ++++++ .../GraphQLSyncBasedTests.swift | 24 ++++++++++--------- .../GraphQLSyncBased/README.md | 7 ++++++ .../Support/QueryPredicate+GraphQL.swift | 4 ++++ 6 files changed, 53 insertions(+), 32 deletions(-) diff --git a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/AnyModelIntegrationTests.swift b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/AnyModelIntegrationTests.swift index 3fd5fb1630..f87a676c40 100644 --- a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/AnyModelIntegrationTests.swift +++ b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/AnyModelIntegrationTests.swift @@ -42,10 +42,9 @@ class AnyModelIntegrationTests: XCTestCase { } func testCreateAsAnyModel() throws { - - let originalPost = Post(title: "Post title", - content: "Original post content as of \(Date())", - createdAt: Date()) + let originalPost = AmplifyTestCommon.Post(title: "Post title", + content: "Original post content as of \(Date())", + createdAt: Temporal.DateTime(Date())) let anyPost = try originalPost.eraseToAnyModel() let callbackInvoked = expectation(description: "Callback invoked") @@ -93,9 +92,9 @@ class AnyModelIntegrationTests: XCTestCase { } func testUpdateAsAnyModel() throws { - let originalPost = Post(title: "Post title", - content: "Original post content as of \(Date())", - createdAt: Date()) + let originalPost = AmplifyTestCommon.Post(title: "Post title", + content: "Original post content as of \(Date())", + createdAt: Temporal.DateTime(Date())) let originalAnyPost = try originalPost.eraseToAnyModel() let createCallbackInvoked = expectation(description: "Create callback invoked") @@ -154,9 +153,9 @@ class AnyModelIntegrationTests: XCTestCase { } func testDeleteAsAnyModel() throws { - let originalPost = Post(title: "Post title", - content: "Original post content as of \(Date())", - createdAt: Date()) + let originalPost = AmplifyTestCommon.Post(title: "Post title", + content: "Original post content as of \(Date())", + createdAt: Temporal.DateTime(Date())) let originalAnyPost = try originalPost.eraseToAnyModel() let createCallbackInvoked = expectation(description: "Create callback invoked") diff --git a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLModelBased/GraphQLModelBasedTests.swift b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLModelBased/GraphQLModelBasedTests.swift index a6bdb8cbb1..438610fb21 100644 --- a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLModelBased/GraphQLModelBasedTests.swift +++ b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLModelBased/GraphQLModelBasedTests.swift @@ -106,12 +106,12 @@ class GraphQLModelBasedTests: XCTestCase { let uuid = UUID().uuidString let testMethodName = String("\(#function)".dropLast(2)) let uniqueTitle = testMethodName + uuid + "Title" - let createdPost = Post(id: uuid, - title: uniqueTitle, - content: "content", - createdAt: Date(), - draft: true, - rating: 12.3) + let createdPost = AmplifyTestCommon.Post(id: uuid, + title: uniqueTitle, + content: "content", + createdAt: Temporal.DateTime.now(), + draft: true, + rating: 12.3) guard createPost(post: createdPost) != nil else { XCTFail("Failed to ensure at least one Post to be retrieved on the listQuery") return @@ -156,7 +156,7 @@ class GraphQLModelBasedTests: XCTestCase { func testCreatPostWithModel() { let completeInvoked = expectation(description: "request completed") - let post = Post(title: "title", content: "content", createdAt: Date()) + let post = AmplifyTestCommon.Post(title: "title", content: "content", createdAt: Temporal.DateTime.now()) _ = Amplify.API.mutate(request: .create(post)) { event in switch event { case .success(let data): @@ -184,7 +184,9 @@ class GraphQLModelBasedTests: XCTestCase { } let completeInvoked = expectation(description: "request completed") - let comment = Comment(content: "commentContent", createdAt: Date(), post: createdPost) + let comment = AmplifyTestCommon.Comment(content: "commentContent", + createdAt: Temporal.DateTime.now(), + post: createdPost) _ = Amplify.API.mutate(request: .create(comment)) { event in switch event { case .success(let data): @@ -497,12 +499,12 @@ class GraphQLModelBasedTests: XCTestCase { // MARK: Helpers func createPost(id: String, title: String) -> AmplifyTestCommon.Post? { - let post = Post(id: id, title: title, content: "content", createdAt: Date()) + let post = Post(id: id, title: title, content: "content", createdAt: Temporal.DateTime.now()) return createPost(post: post) } func createComment(content: String, post: AmplifyTestCommon.Post) -> AmplifyTestCommon.Comment? { - let comment = Comment(content: content, createdAt: Date(), post: post) + let comment = Comment(content: content, createdAt: Temporal.DateTime.now(), post: post) return createComment(comment: comment) } @@ -554,7 +556,7 @@ class GraphQLModelBasedTests: XCTestCase { var result: AmplifyTestCommon.Post? let completeInvoked = expectation(description: "request completed") - let post = Post(id: id, title: title, content: "content", createdAt: Date()) + let post = Post(id: id, title: title, content: "content", createdAt: Temporal.DateTime.now()) _ = Amplify.API.mutate(request: .update(post)) { event in switch event { case .success(let data): diff --git a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLModelBased/README.md b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLModelBased/README.md index 68665f2017..4c3fac80dd 100644 --- a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLModelBased/README.md +++ b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLModelBased/README.md @@ -22,6 +22,12 @@ The following steps demonstrate how to set up a GraphQL endpoint with AppSync. T ``` When asked to provide the schema, create the `schema.graphql` file ``` +enum PostStatus { + PRIVATE + DRAFT + PUBLISHED +} + type Post @model { id: ID! title: String! @@ -30,6 +36,7 @@ type Post @model { updatedAt: AWSDateTime draft: Boolean rating: Float + status: PostStatus comments: [Comment] @connection(name: "PostComment") } diff --git a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/GraphQLSyncBasedTests.swift b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/GraphQLSyncBasedTests.swift index 184c10c995..0f1f5999ba 100644 --- a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/GraphQLSyncBasedTests.swift +++ b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/GraphQLSyncBasedTests.swift @@ -167,10 +167,10 @@ class GraphQLSyncBasedTests: XCTestCase { let updatedTitle = title + "Updated" - let modifiedPost = Post(id: createdPost.model["id"] as? String ?? "", - title: updatedTitle, - content: createdPost.model["content"] as? String ?? "", - createdAt: Date()) + let modifiedPost = AmplifyTestCommon.Post(id: createdPost.model["id"] as? String ?? "", + title: updatedTitle, + content: createdPost.model["content"] as? String ?? "", + createdAt: Temporal.DateTime.now()) let completeInvoked = expectation(description: "request completed") var responseFromOperation: GraphQLResponse>? @@ -235,10 +235,10 @@ class GraphQLSyncBasedTests: XCTestCase { let updatedTitle = title + "Updated" - let modifiedPost = Post(id: createdPost.model["id"] as? String ?? "", + let modifiedPost = AmplifyTestCommon.Post(id: createdPost.model["id"] as? String ?? "", title: updatedTitle, content: createdPost.model["content"] as? String ?? "", - createdAt: Date()) + createdAt: Temporal.DateTime(Date())) let completeInvoked = expectation(description: "request completed") var responseFromOperation: GraphQLResponse>? @@ -305,10 +305,10 @@ class GraphQLSyncBasedTests: XCTestCase { return } let updatedTitle = title + "Updated" - let modifiedPost = Post(id: createdPost.model["id"] as? String ?? "", - title: updatedTitle, - content: createdPost.model["content"] as? String ?? "", - createdAt: Date()) + let modifiedPost = AmplifyTestCommon.Post(id: createdPost.model["id"] as? String ?? "", + title: updatedTitle, + content: createdPost.model["content"] as? String ?? "", + createdAt: Temporal.DateTime.now()) let firstUpdateSuccess = expectation(description: "first update mutation should be successful") let request = GraphQLRequest.updateMutation(of: modifiedPost, @@ -523,7 +523,9 @@ class GraphQLSyncBasedTests: XCTestCase { // MARK: Helpers func createPost(id: String, title: String) -> MutationSyncResult? { - let post = Post(id: id, title: title, content: "content", createdAt: Date()) + let post = AmplifyTestCommon.Post(id: id, title: title, + content: "content", + createdAt: Temporal.DateTime.now()) return createPost(post: post) } diff --git a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/README.md b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/README.md index b016fed331..2ac426d3d9 100644 --- a/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/README.md +++ b/AmplifyPlugins/API/AWSAPICategoryPluginFunctionalTests/GraphQLSyncBased/README.md @@ -26,6 +26,12 @@ The following steps demonstrate how to set up an GraphQL endpoint with AppSync t ``` When asked to provide the schema, create the `schema.graphql` file ``` +enum PostStatus { + PRIVATE + DRAFT + PUBLISHED +} + type Post @model { id: ID! title: String! @@ -34,6 +40,7 @@ type Post @model { updatedAt: AWSDateTime draft: Boolean rating: Float + status: PostStatus comments: [Comment] @connection(name: "PostComment") } diff --git a/AmplifyPlugins/Core/AWSPluginsCore/Model/Support/QueryPredicate+GraphQL.swift b/AmplifyPlugins/Core/AWSPluginsCore/Model/Support/QueryPredicate+GraphQL.swift index 7b03879f60..9e81154d46 100644 --- a/AmplifyPlugins/Core/AWSPluginsCore/Model/Support/QueryPredicate+GraphQL.swift +++ b/AmplifyPlugins/Core/AWSPluginsCore/Model/Support/QueryPredicate+GraphQL.swift @@ -144,6 +144,10 @@ extension Persistable { return value.iso8601String } + if let value = value as? Temporal.DateTime { + return value.iso8601String + } + if let value = value as? Double { return Decimal(value) }