Skip to content

Commit

Permalink
Additional tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tylerjroach committed Oct 4, 2023
1 parent 24b8a4d commit e262410
Show file tree
Hide file tree
Showing 5 changed files with 100 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

/**
Expand Down Expand Up @@ -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<Post> 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<Post> request =
AppSyncGraphQLRequestFactory.<Post, Post, PostPath>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<Post> 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<Post> request =
AppSyncGraphQLRequestFactory.<Post, Post, PostPath>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
Expand Down
1 change: 1 addition & 0 deletions aws-api/src/test/resources/lazy_create_no_includes.txt
Original file line number Diff line number Diff line change
@@ -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"}}}
1 change: 1 addition & 0 deletions aws-api/src/test/resources/lazy_create_with_includes.txt
Original file line number Diff line number Diff line change
@@ -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"}}}
1 change: 1 addition & 0 deletions aws-api/src/test/resources/lazy_query_no_includes.json
Original file line number Diff line number Diff line change
@@ -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"}}
1 change: 1 addition & 0 deletions aws-api/src/test/resources/lazy_query_with_includes.json
Original file line number Diff line number Diff line change
@@ -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"}}

0 comments on commit e262410

Please sign in to comment.