Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(amplify-e2e-tests): add tests for graphql schemas in doc #4092

Merged
merged 7 commits into from
Jul 15, 2020

Conversation

UnleashedMind
Copy link
Contributor

@UnleashedMind UnleashedMind commented Apr 27, 2020

Add tests for graphql schemas in the document Amplify CLI/API(GraphQL)/Directives

Issue #, if available:

Description of changes:

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.

@UnleashedMind UnleashedMind force-pushed the graphqlschemae2e branch 2 times, most recently from 6efb4c3 to 6ecc678 Compare May 4, 2020 21:14
Copy link
Contributor

@yuth yuth left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The main goal with tests should be verification (which this change does) and readability of the test code so one knows what is going on (and make it easier to figure out why tests are failing) rather then reducing code duplication.

Could you make the schema, query and mutation inline and use the runQuery, runMutation in the test itself. Also use the proper asserting for tests instead of returning true and expecting tests to return true as it makes it harder to figure out why a test failed

@codecov
Copy link

codecov bot commented May 29, 2020

Codecov Report

Merging #4092 into master will increase coverage by 0.42%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #4092      +/-   ##
==========================================
+ Coverage   60.51%   60.93%   +0.42%     
==========================================
  Files         350      347       -3     
  Lines       15199    15099     -100     
  Branches     2878     2860      -18     
==========================================
+ Hits         9197     9200       +3     
+ Misses       5514     5411     -103     
  Partials      488      488              
Impacted Files Coverage Δ
...es/graphql-connection-transformer/src/resources.ts 78.20% <ø> (ø)
...tion-transformer/src/ModelConnectionTransformer.ts 89.52% <100.00%> (+0.10%) ⬆️
packages/amplify-util-mock/src/func/index.ts
packages/amplify-util-mock/src/api/index.ts
...ages/amplify-util-mock/src/utils/lambda/execute.ts

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 827c7b8...e7a4524. Read the comment docs.

@lgtm-com
Copy link

lgtm-com bot commented May 29, 2020

This pull request introduces 3 alerts when merging f0cf6970bb23a3f40943fa7938d2e0e9dc118a74 into 15eac84 - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class
  • 1 for Duplicate character in character class

@lgtm-com
Copy link

lgtm-com bot commented May 29, 2020

This pull request introduces 3 alerts when merging 0ff23d47f2f58a07ebfad4d7677e3f38619f2b61 into 15eac84 - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class
  • 1 for Duplicate character in character class

@lgtm-com
Copy link

lgtm-com bot commented May 29, 2020

This pull request introduces 3 alerts when merging 23a9907 into 709491f - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class
  • 1 for Duplicate character in character class

@UnleashedMind
Copy link
Contributor Author

UnleashedMind commented Jun 4, 2020

@yuth

In response to the following comments:

"The main goal with tests should be verification (which this change does) and readability of the test code so one knows what is going on (and make it easier to figure out why tests are failing) rather then reducing code duplication.

Could you make the schema, query and mutation inline and use the runQuery, runMutation in the test itself. Also use the proper asserting for tests instead of returning true and expecting tests to return true as it makes it harder to figure out why a test failed"

The schema being tested, the mutations, queries and subscriptions to test the schema, and their expected responses are consolidated into one file for better readability.
If the test for a schema does not fall into one of the common test patterns, the test method itself (runTest) is also in the same file as the schema.

The test compares the actual mutation, query and subscription responses with the expected results. As the responses can be objects of depth, the test goes to a max depth of 50 (configurable constant) trying to identify mismatches. I can't figure out a simple assertion for that. But if there is a mismatch, the cause of mismatch is clearly printed out, so there shouldn't be any problem identifying the failed test, and the part that failed it.

@yuth
Copy link
Contributor

yuth commented Jun 8, 2020

The schema being tested, the mutations, queries and subscriptions to test the schema, and their expected responses are consolidated into one file for better readability.
If the test for a schema does not fall into one of the common test patterns, the test method itself (runTest) is also in the same file as the schema.

Consolidating them to one file is counter productive. The most important thing with tests is

  1. Testing of functionality
  2. Readability as tests are living documentation of behaviors.

We have similar tests in our GraphQL E2E tests. In those tests, the schema and mutation is all contained in the same file and makes it easy to understand what is going on.

Schema

beforeAll(async () => {
const validSchema = /* GraphQL */ `
type Post @model {
id: ID!
title: String!
createdAt: AWSDateTime
updatedAt: AWSDateTime
metadata: PostMetadata
entityMetadata: EntityMetadata
appearsIn: [Episode!]
episode: Episode
}
type Author @model {
id: ID!
name: String!
postMetadata: PostMetadata
entityMetadata: EntityMetadata
}
type EntityMetadata {
isActive: Boolean
}
type PostMetadata {
tags: Tag
}
type Tag {
published: Boolean
metadata: PostMetadata
}
enum Episode {
NEWHOPE
EMPIRE
JEDI
}
type Comment @model(timestamps: { createdAt: "createdOn", updatedAt: "updatedOn" }) {
id: ID!
title: String!
content: String
updatedOn: Int # No automatic generation of timestamp if its not AWSDateTime
}

Test

test('Test createAuthor mutation', async () => {
try {
const response = await GRAPHQL_CLIENT.query(
`mutation($input: CreateAuthorInput!) {
createAuthor(input: $input) {
id
name
entityMetadata {
isActive
}
createdAt
updatedAt
}
}`,
{
input: {
name: 'Jeff B',
entityMetadata: {
isActive: true,
},
},
},
);
expect(response.data.createAuthor.id).toBeDefined();
expect(response.data.createAuthor.name).toEqual('Jeff B');
expect(response.data.createAuthor.createdAt).toBeDefined();
expect(response.data.createAuthor.updatedAt).toBeDefined();
expect(response.data.createAuthor.entityMetadata).toBeDefined();
expect(response.data.createAuthor.entityMetadata.isActive).toEqual(true);
} catch (e) {
console.log(e);
// fail
expect(e).toBeUndefined();
}
});

The test compares the actual mutation, query and subscription responses with the expected results. As the responses can be objects of depth, the test goes to a max depth of 50 (configurable constant) trying to identify mismatches. I can't figure out a simple assertion for that. But if there is a mismatch, the cause of mismatch is clearly printed out, so there shouldn't be any problem identifying the failed test, and the part that failed it.

I have added an example test in #4092 (comment)

Could you explain what you mean by max depth of 50? Jest has the assertion expect(result).toEqual({}) which does compare objects. The other option is to use expect(result).toMatchInlineSnapshot() which keeps the exact result.

packages/amplify-e2e-core/src/categories/api.ts Outdated Show resolved Hide resolved
packages/amplify-e2e-core/src/categories/api.ts Outdated Show resolved Hide resolved
packages/amplify-e2e-core/src/categories/auth.ts Outdated Show resolved Hide resolved
packages/amplify-e2e-core/src/categories/auth.ts Outdated Show resolved Hide resolved
packages/amplify-e2e-core/src/init/initProjectHelper.ts Outdated Show resolved Hide resolved
}

const MAX_DEPTH = 50;
function runCompare(queue: { received: any; expected: any; depth: number }[]): boolean {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we use .toMatchObject(object) instead of rolling our own solution

Copy link
Contributor Author

@UnleashedMind UnleashedMind Jun 25, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are some technical difficulties.
In the test, the actual received mutation, query and subscription results are not directly compared with the expected results specified in the test case file (e.g. using the jest .toMatchObject(object) method).

  • id, createdAt and updatedAt fields are generated (if not specified in the mutation), their values are not determined when the test starts to run. Those fields are generally given a value “” in the expected result specification, and the test only checks that they are defined in the actual received result.
  • If error is expected, the test only checks for the error type, but the actual error response contains a lot of fields.
  • The actual response can contain the "__typename" field, even if the schema does not contain union of interface, and even if the mutation or query does not specify it. The expected result in the test case files do not include this extra field.

To compare the expected and the actual result object, the test uses a BFS method to find mismatched field, if no mismatch is found after MAX_DEPTH (set at 50 in the test), the test passes.

packages/amplify-e2e-core/src/categories/storage.ts Outdated Show resolved Hide resolved
packages/amplify-e2e-core/src/categories/storage.ts Outdated Show resolved Hide resolved
packages/amplify-e2e-core/src/categories/storage.ts Outdated Show resolved Hide resolved
packages/amplify-e2e-core/src/categories/function.ts Outdated Show resolved Hide resolved
packages/amplify-e2e-core/src/categories/function.ts Outdated Show resolved Hide resolved
content
}
}`;
export const expected_result_mutation1 = {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please dont use the variable naming magic. Like i suggested in an earlier comment, move this inside a test and then wrap it inside an object, so both mutation and results are wrapped in the same object

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is related to the test layout that we discussed in the meeting.

@UnleashedMind UnleashedMind force-pushed the graphqlschemae2e branch 2 times, most recently from 1b522df to da29899 Compare June 25, 2020 19:23
Add tests for graphql schemas in the document Amplify CLI/API(GraphQL)/Directives
@lgtm-com
Copy link

lgtm-com bot commented Jul 14, 2020

This pull request introduces 1 alert when merging 1cbee0c into 827c7b8 - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented Jul 14, 2020

This pull request introduces 1 alert when merging 8b0521f into 0a6a7ea - view on LGTM.com

new alerts:

  • 1 for Unused variable, import, function or class

@lgtm-com
Copy link

lgtm-com bot commented Jul 14, 2020

This pull request introduces 2 alerts when merging 12f493d into 0a6a7ea - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class

@UnleashedMind UnleashedMind merged commit c9d1ff8 into aws-amplify:master Jul 15, 2020
@lgtm-com
Copy link

lgtm-com bot commented Jul 15, 2020

This pull request introduces 2 alerts when merging e7a4524 into 454fdc0 - view on LGTM.com

new alerts:

  • 2 for Unused variable, import, function or class

nikhname pushed a commit to nikhname/amplify-cli that referenced this pull request Jul 27, 2020
…plify#4092)

* test(amplify-e2e-tests): add e2e test for schema in doc

Add tests for graphql schemas in the document Amplify CLI/API(GraphQL)/Directives

* minor fix

* fix searchable test

* split long tests

* minor fix

* minor fix

Co-authored-by: UnleashedMind <root@186590ce137f.ant.amazon.com>
Co-authored-by: UnleashedMind <zhoweimi@186590ce137f.ant.amazon.com>
sebastiancrossa pushed a commit to MLH-Fellowship/amplify-cli that referenced this pull request Aug 4, 2020
…plify#4092)

* test(amplify-e2e-tests): add e2e test for schema in doc

Add tests for graphql schemas in the document Amplify CLI/API(GraphQL)/Directives

* minor fix

* fix searchable test

* split long tests

* minor fix

* minor fix

Co-authored-by: UnleashedMind <root@186590ce137f.ant.amazon.com>
Co-authored-by: UnleashedMind <zhoweimi@186590ce137f.ant.amazon.com>
sebastiancrossa pushed a commit to MLH-Fellowship/amplify-cli that referenced this pull request Aug 25, 2020
…plify#4092)

* test(amplify-e2e-tests): add e2e test for schema in doc

Add tests for graphql schemas in the document Amplify CLI/API(GraphQL)/Directives

* minor fix

* fix searchable test

* split long tests

* minor fix

* minor fix

Co-authored-by: UnleashedMind <root@186590ce137f.ant.amazon.com>
Co-authored-by: UnleashedMind <zhoweimi@186590ce137f.ant.amazon.com>
@github-actions
Copy link

This pull request has been automatically locked since there hasn't been any recent activity after it was closed. Please open a new issue for related bugs.

Looking for a help forum? We recommend joining the Amplify Community Discord server *-help channels for those types of questions.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 16, 2021
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants