From e262410ab521aaaa36ee90e5adf1d4c11a0f1f1d Mon Sep 17 00:00:00 2001 From: tjroach Date: Wed, 4 Oct 2023 17:04:46 -0400 Subject: [PATCH] Additional tests --- .../aws/AppSyncGraphQLRequestFactoryTest.java | 96 +++++++++++++++++++ .../resources/lazy_create_no_includes.txt | 1 + .../resources/lazy_create_with_includes.txt | 1 + .../resources/lazy_query_no_includes.json | 1 + .../resources/lazy_query_with_includes.json | 1 + 5 files changed, 100 insertions(+) create mode 100644 aws-api/src/test/resources/lazy_create_no_includes.txt create mode 100644 aws-api/src/test/resources/lazy_create_with_includes.txt create mode 100644 aws-api/src/test/resources/lazy_query_no_includes.json create mode 100644 aws-api/src/test/resources/lazy_query_with_includes.json diff --git a/aws-api/src/test/java/com/amplifyframework/api/aws/AppSyncGraphQLRequestFactoryTest.java b/aws-api/src/test/java/com/amplifyframework/api/aws/AppSyncGraphQLRequestFactoryTest.java index 5f4eb65bb8..d567a49693 100644 --- a/aws-api/src/test/java/com/amplifyframework/api/aws/AppSyncGraphQLRequestFactoryTest.java +++ b/aws-api/src/test/java/com/amplifyframework/api/aws/AppSyncGraphQLRequestFactoryTest.java @@ -32,6 +32,9 @@ import com.amplifyframework.datastore.DataStoreException; import com.amplifyframework.testmodels.ecommerce.Item; import com.amplifyframework.testmodels.ecommerce.Status; +import com.amplifyframework.testmodels.lazy.Blog; +import com.amplifyframework.testmodels.lazy.Post; +import com.amplifyframework.testmodels.lazy.PostPath; import com.amplifyframework.testmodels.meeting.Meeting; import com.amplifyframework.testmodels.personcar.MaritalStatus; import com.amplifyframework.testmodels.personcar.Person; @@ -49,6 +52,7 @@ import java.util.Map; import java.util.concurrent.TimeUnit; +import static com.amplifyframework.core.model.ModelPropertyPathKt.includes; import static org.junit.Assert.assertEquals; /** @@ -126,6 +130,98 @@ public void buildQueryFromClassAndPredicate() throws JSONException { ); } + /** + * Validate construction of a GraphQL create mutation for a lazy model. + * @throws JSONException from JSONAssert.assertEquals + */ + @Test + public void buildCreateMutationWithLazyModel() throws JSONException { + // Act: generate query + Blog blog = Blog.builder().name("My Blog").id("b1").build(); + Post post = Post.builder().name("My Post").blog(blog).id("p1").build(); + + GraphQLRequest request = + AppSyncGraphQLRequestFactory.buildMutation( + post, + QueryPredicates.all(), + MutationType.CREATE + ); + + // Validate request is expected request + JSONAssert.assertEquals( + Resources.readAsString("lazy_create_no_includes.txt"), + request.getContent(), + true + ); + } + + /** + * Validate construction of a GraphQL create mutation for a lazy model with includes. + * @throws JSONException from JSONAssert.assertEquals + */ + @Test + public void buildCreateMutationWithLazyModelAndIncludes() throws JSONException { + // Act: generate query + Blog blog = Blog.builder().name("My Blog").id("b1").build(); + Post post = Post.builder().name("My Post").blog(blog).id("p1").build(); + + GraphQLRequest request = + AppSyncGraphQLRequestFactory.buildMutation( + post, + QueryPredicates.all(), + MutationType.CREATE, + ((path) -> includes(path.getBlog(), path.getComments())) + ); + + // Validate request is expected request + JSONAssert.assertEquals( + Resources.readAsString("lazy_create_with_includes.txt"), + request.getContent(), + true + ); + } + + /** + * Validate construction of a GraphQL query for a lazy model. + * @throws JSONException from JSONAssert.assertEquals + */ + @Test + public void buildQueryFromLazyModel() throws JSONException { + // Act: generate query + GraphQLRequest request = + AppSyncGraphQLRequestFactory.buildQuery(Post.class, "p1"); + + // Validate request is expected request + JSONAssert.assertEquals( + Resources.readAsString("lazy_query_no_includes.json"), + request.getContent(), + true + ); + } + + /** + * Validate construction of a GraphQL query for a lazy model with includes. + * @throws JSONException from JSONAssert.assertEquals + */ + @Test + public void buildQueryFromLazyModelWithIncludes() throws JSONException { + // Act: generate query + GraphQLRequest request = + AppSyncGraphQLRequestFactory.buildQuery( + Post.class, + "p1", + ((path) -> includes(path.getBlog(), path.getComments())) + ); + + // Validate request is expected request + JSONAssert.assertEquals( + Resources.readAsString("lazy_query_with_includes.json"), + request.getContent(), + true + ); + } + + /** * Validates construction of a delete mutation query from a Person instance, a predicate. * @throws JSONException from JSONAssert.assertEquals diff --git a/aws-api/src/test/resources/lazy_create_no_includes.txt b/aws-api/src/test/resources/lazy_create_no_includes.txt new file mode 100644 index 0000000000..ca38e45ad5 --- /dev/null +++ b/aws-api/src/test/resources/lazy_create_no_includes.txt @@ -0,0 +1 @@ +{"query": "mutation CreatePost($input: CreatePostInput!) {\n createPost(input: $input) {\n blog {\n id\n }\n createdAt\n id\n name\n updatedAt\n }\n}\n", "variables": {"input":{"blogPostsId":"b1","name":"My Post","id":"p1"}}} \ No newline at end of file diff --git a/aws-api/src/test/resources/lazy_create_with_includes.txt b/aws-api/src/test/resources/lazy_create_with_includes.txt new file mode 100644 index 0000000000..e03262e6c6 --- /dev/null +++ b/aws-api/src/test/resources/lazy_create_with_includes.txt @@ -0,0 +1 @@ +{"query": "mutation CreatePost($input: CreatePostInput!) {\n createPost(input: $input) {\n blog {\n createdAt\n id\n name\n updatedAt\n }\n comments {\n items {\n createdAt\n id\n post {\n id\n }\n text\n updatedAt\n }\n }\n createdAt\n id\n name\n updatedAt\n }\n}\n", "variables": {"input":{"blogPostsId":"b1","name":"My Post","id":"p1"}}} \ No newline at end of file diff --git a/aws-api/src/test/resources/lazy_query_no_includes.json b/aws-api/src/test/resources/lazy_query_no_includes.json new file mode 100644 index 0000000000..1db860ca83 --- /dev/null +++ b/aws-api/src/test/resources/lazy_query_no_includes.json @@ -0,0 +1 @@ +{"query": "query GetPost($id: ID!) {\n getPost(id: $id) {\n blog {\n id\n }\n createdAt\n id\n name\n updatedAt\n }\n}\n", "variables": {"id":"p1"}} \ No newline at end of file diff --git a/aws-api/src/test/resources/lazy_query_with_includes.json b/aws-api/src/test/resources/lazy_query_with_includes.json new file mode 100644 index 0000000000..3f67515c64 --- /dev/null +++ b/aws-api/src/test/resources/lazy_query_with_includes.json @@ -0,0 +1 @@ +{"query": "query GetPost($id: ID!) {\n getPost(id: $id) {\n blog {\n createdAt\n id\n name\n updatedAt\n }\n comments {\n items {\n createdAt\n id\n post {\n id\n }\n text\n updatedAt\n }\n }\n createdAt\n id\n name\n updatedAt\n }\n}\n", "variables": {"id":"p1"}} \ No newline at end of file