generated from amazon-archives/__template_Apache-2.0
-
Notifications
You must be signed in to change notification settings - Fork 89
feat: auto increment support #2883
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
Merged
Merged
Changes from all commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
30b3d61
chore(graphql-default-value-transformer): tidy tests
p5quared 316029f
test(graphql-default-value-transformer): add unit tests for auto incr…
p5quared eda0b0e
feat: 🎸 utils to detect Postgres datasource
p5quared e6c56f5
feat: 🎸 support auto increment
p5quared 4a1d413
feat: support auto increment
p5quared 026767b
test(graphql-default-value-transformer): pk can be auto increment
p5quared 56e93b9
test(graphql-default-value-transformer): auto-increment crud e2e
p5quared cc39a40
chore: describe test purpose
p5quared 634f6e7
chore: removing logging
p5quared a9cb7c4
chore: describe why invalid cases are invalid
p5quared 4fabed6
chore: remove unecessary e2e test case
p5quared f5d03f6
chore: test messaging clarity
p5quared dca85b4
chore: type safety
p5quared d95e101
chore: alphabetize list
p5quared e8ad58b
chore: type of return value asserts against string
p5quared dacb206
chore: test ensures customers can insert to serial fields with custom…
p5quared a9db3a6
chore: verify that @default(value) works on mysql
p5quared ebdae2e
chore: remove unecessary ssm test case
p5quared f78fd03
chore: update branch from main
p5quared cb3156b
test: value cannot be null on ddb
p5quared File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
55 changes: 55 additions & 0 deletions
55
packages/amplify-graphql-api-construct-tests/src/__tests__/sql-pg-auto-increment.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,55 @@ | ||
| import generator from 'generate-password'; | ||
| import { getResourceNamesForStrategyName, ImportedRDSType } from '@aws-amplify/graphql-transformer-core'; | ||
| import { getRDSTableNamePrefix } from 'amplify-category-api-e2e-core'; | ||
| import { SqlDatatabaseController } from '../sql-datatabase-controller'; | ||
| import { DURATION_1_HOUR } from '../utils/duration-constants'; | ||
| import { testGraphQLAPIAutoIncrement } from '../sql-tests-common/sql-models-auto-increment'; | ||
|
|
||
| jest.setTimeout(DURATION_1_HOUR); | ||
|
|
||
| describe('CDK GraphQL Transformer deployments with Postgres SQL datasources', () => { | ||
| const projFolderName = 'pgmodels'; | ||
|
|
||
| // sufficient password length that meets the requirements for RDS cluster/instance | ||
| const [username, password, identifier] = generator.generateMultiple(3, { length: 11 }); | ||
| const region = process.env.CLI_REGION ?? 'us-west-2'; | ||
| const engine = 'postgres'; | ||
|
|
||
| const databaseController: SqlDatatabaseController = new SqlDatatabaseController( | ||
| [ | ||
| `CREATE TABLE "${getRDSTableNamePrefix()}coffee_queue" ("orderNumber" SERIAL PRIMARY KEY, "order" VARCHAR(256) NOT NULL, "customer" VARCHAR(256))`, | ||
| ], | ||
| { | ||
| identifier, | ||
| engine, | ||
| username, | ||
| password, | ||
| region, | ||
| }, | ||
| ); | ||
|
|
||
| const strategyName = `${engine}DBStrategy`; | ||
| const resourceNames = getResourceNamesForStrategyName(strategyName); | ||
|
|
||
| beforeAll(async () => { | ||
| await databaseController.setupDatabase(); | ||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| await databaseController.cleanupDatabase(); | ||
| }); | ||
|
|
||
| const constructTestOptions = (connectionConfigName: string) => ({ | ||
| projFolderName, | ||
| region, | ||
| connectionConfigName, | ||
| dbController: databaseController, | ||
| resourceNames, | ||
| }); | ||
|
|
||
| testGraphQLAPIAutoIncrement( | ||
| constructTestOptions('connectionUri'), | ||
| 'creates a GraphQL API from SQL-based models using Connection String SSM parameter', | ||
| ImportedRDSType.POSTGRESQL, | ||
| ); | ||
| }); | ||
7 changes: 7 additions & 0 deletions
7
...-graphql-api-construct-tests/src/sql-tests-common/schemas/sql-auto-increment/field-map.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,7 @@ | ||
| import { FieldMap } from '../../../utils/sql-crudl-helper'; | ||
|
|
||
| export const coffeeQueueFieldMap: FieldMap = { | ||
| orderNumber: true, | ||
| order: true, | ||
| customer: true, | ||
| }; |
5 changes: 5 additions & 0 deletions
5
...raphql-api-construct-tests/src/sql-tests-common/schemas/sql-auto-increment/schema.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| type CoffeeQueue @model @refersTo(name: "e2e_test_coffee_queue") { | ||
| orderNumber: Int! @primaryKey @default | ||
| order: String! | ||
| customer: String | ||
| } |
2 changes: 1 addition & 1 deletion
2
...mplify-graphql-api-construct-tests/src/sql-tests-common/schemas/sql-models/schema.graphql
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
174 changes: 174 additions & 0 deletions
174
...ges/amplify-graphql-api-construct-tests/src/sql-tests-common/sql-models-auto-increment.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,174 @@ | ||
| import * as path from 'path'; | ||
| import * as fs from 'fs-extra'; | ||
| import { ImportedRDSType } from '@aws-amplify/graphql-transformer-core'; | ||
| import AWSAppSyncClient, { AUTH_TYPE } from 'aws-appsync'; | ||
| import { createNewProjectDir, deleteProjectDir, getRDSTableNamePrefix } from 'amplify-category-api-e2e-core'; | ||
| import { initCDKProject, cdkDeploy, cdkDestroy } from '../commands'; | ||
| import { SqlDatatabaseController } from '../sql-datatabase-controller'; | ||
| import { CRUDLHelper } from '../utils/sql-crudl-helper'; | ||
| import { ONE_MINUTE } from '../utils/duration-constants'; | ||
| import { coffeeQueueFieldMap } from './schemas/sql-auto-increment/field-map'; | ||
|
|
||
| export const testGraphQLAPIAutoIncrement = ( | ||
| options: { | ||
| projFolderName: string; | ||
| region: string; | ||
| connectionConfigName: string; | ||
| dbController: SqlDatatabaseController; | ||
| resourceNames: { sqlLambdaAliasName: string }; | ||
| }, | ||
| testBlockDescription: string, | ||
| engine: ImportedRDSType, | ||
| ): void => { | ||
| describe(`${testBlockDescription} - ${engine}`, () => { | ||
| // In particular, we want to verify that the new CREATE operation | ||
| // is allowed to omit the primary key field, and that the primary key | ||
| // we get back is the correct, db generated value. | ||
| // NOTE: Expects underlying orderNumber column to be a serial primary key in Postgres table | ||
| const schemaPath = path.resolve(path.join(__dirname, '..', 'sql-tests-common', 'schemas', 'sql-auto-increment', 'schema.graphql')); | ||
| const schemaConfigString = fs.readFileSync(schemaPath).toString(); | ||
| const { projFolderName, region, connectionConfigName, dbController } = options; | ||
| const templatePath = path.resolve(path.join(__dirname, '..', '__tests__', 'backends', 'sql-models')); | ||
palpatim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| let projRoot: string; | ||
| let name: string; | ||
| let outputs: Promise<any>; | ||
| let coffeeQueueTableCRUDLHelper: CRUDLHelper; | ||
|
|
||
| beforeAll(async () => { | ||
| projRoot = await createNewProjectDir(projFolderName); | ||
| name = await initCDKProject(projRoot, templatePath); | ||
| dbController.writeDbDetails(projRoot, connectionConfigName, schemaConfigString); | ||
| outputs = await cdkDeploy(projRoot, '--all', { postDeployWaitMs: ONE_MINUTE }); | ||
| const { awsAppsyncApiEndpoint: apiEndpoint, awsAppsyncApiKey: apiKey } = outputs[name]; | ||
|
|
||
| const appSyncClient = new AWSAppSyncClient({ | ||
| url: apiEndpoint, | ||
| region, | ||
| disableOffline: true, | ||
| auth: { | ||
| type: AUTH_TYPE.API_KEY, | ||
| apiKey, | ||
| }, | ||
| }); | ||
|
|
||
| coffeeQueueTableCRUDLHelper = new CRUDLHelper(appSyncClient, 'CoffeeQueue', 'CoffeeQueues', coffeeQueueFieldMap); | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Since previous approval, we now use a |
||
| }); | ||
|
|
||
| afterAll(async () => { | ||
| try { | ||
| await cdkDestroy(projRoot, '--all'); | ||
| await dbController.clearDatabase(); | ||
| } catch (err) { | ||
| console.log(`Error invoking 'cdk destroy': ${err}`); | ||
| } | ||
|
|
||
| deleteProjectDir(projRoot); | ||
| }); | ||
|
|
||
| test(`check CRUDL on coffee queue table with auto increment primary key - ${engine}`, async () => { | ||
| // Order Coffee Mutation | ||
| const createCoffeeOrder1 = await coffeeQueueTableCRUDLHelper.create({ customer: 'petesv', order: 'cold brew' }); | ||
|
|
||
| expect(createCoffeeOrder1).toBeDefined(); | ||
| expect(createCoffeeOrder1.orderNumber).toBeDefined(); | ||
| expect(createCoffeeOrder1.customer).toEqual('petesv'); | ||
| expect(createCoffeeOrder1.order).toEqual('cold brew'); | ||
|
|
||
| // Get Todo Query | ||
| const getCoffeeOrder1 = await coffeeQueueTableCRUDLHelper.get({ orderNumber: createCoffeeOrder1.orderNumber }); | ||
|
|
||
| expect(getCoffeeOrder1.orderNumber).toEqual(createCoffeeOrder1.orderNumber); | ||
| expect(getCoffeeOrder1.customer).toEqual(createCoffeeOrder1.customer); | ||
|
|
||
| // Update Todo Mutation | ||
| const updateCoffeeOrder1 = await coffeeQueueTableCRUDLHelper.update({ | ||
| orderNumber: createCoffeeOrder1.orderNumber, | ||
| customer: 'petesv', | ||
| order: 'hot brew', | ||
| }); | ||
|
|
||
| expect(updateCoffeeOrder1.orderNumber).toEqual(createCoffeeOrder1.orderNumber); | ||
| expect(updateCoffeeOrder1.order).toEqual('hot brew'); | ||
|
|
||
| // Get Todo Query after update | ||
| const getUpdatedCoffeeOrder1 = await coffeeQueueTableCRUDLHelper.get({ orderNumber: createCoffeeOrder1.orderNumber }); | ||
|
|
||
| expect(getUpdatedCoffeeOrder1.orderNumber).toEqual(createCoffeeOrder1.orderNumber); | ||
| expect(getUpdatedCoffeeOrder1.order).toEqual('hot brew'); | ||
|
|
||
| // List Todo Query & Create with custom SERIAL field value | ||
| const customOrderNumber = 42; | ||
| const createCofffeeOrder2 = await coffeeQueueTableCRUDLHelper.create({ orderNumber: customOrderNumber, order: 'latte' }); | ||
| expect(createCofffeeOrder2.orderNumber).toEqual(customOrderNumber); | ||
|
|
||
| const listTodo = await coffeeQueueTableCRUDLHelper.list(); | ||
| expect(listTodo.items.length).toEqual(2); | ||
| expect(listTodo.items).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| orderNumber: getUpdatedCoffeeOrder1.orderNumber, | ||
| order: 'hot brew', | ||
| }), | ||
| expect.objectContaining({ | ||
| orderNumber: createCofffeeOrder2.orderNumber, | ||
| order: 'latte', | ||
| }), | ||
| ]), | ||
| ); | ||
|
|
||
| // Delete Todo Mutation | ||
| const deleteCoffeeOrder1 = await coffeeQueueTableCRUDLHelper.delete({ orderNumber: createCoffeeOrder1.orderNumber }); | ||
|
|
||
| expect(deleteCoffeeOrder1.orderNumber).toEqual(createCoffeeOrder1.orderNumber); | ||
| expect(deleteCoffeeOrder1.order).toEqual('hot brew'); | ||
|
|
||
| const getDeletedCoffeeOrder1 = await coffeeQueueTableCRUDLHelper.get({ orderNumber: createCoffeeOrder1.orderNumber }); | ||
|
|
||
| expect(getDeletedCoffeeOrder1).toBeNull(); | ||
|
|
||
| // List Todo Query after delete | ||
| const listCoffeeOrdersAfterDelete = await coffeeQueueTableCRUDLHelper.list(); | ||
|
|
||
| expect(listCoffeeOrdersAfterDelete.items.length).toEqual(1); | ||
| expect(listCoffeeOrdersAfterDelete.items).toEqual( | ||
| expect.arrayContaining([ | ||
| expect.objectContaining({ | ||
| orderNumber: createCofffeeOrder2.orderNumber, | ||
| order: 'latte', | ||
| }), | ||
| ]), | ||
| ); | ||
|
|
||
| // Check invalid CRUD operation returns generic error message | ||
| const createTodo6 = await coffeeQueueTableCRUDLHelper.create({ order: 'mocha' }); | ||
|
|
||
| try { | ||
| // Invalid because the pk (orderNumber) already exists | ||
| await coffeeQueueTableCRUDLHelper.create({ orderNumber: createTodo6.orderNumber, order: 'americano' }); | ||
| } catch (error) { | ||
| coffeeQueueTableCRUDLHelper.checkGenericError(error?.message); | ||
| } | ||
|
|
||
| const biggerThanAnyExistingOrderNumber = 99999999; | ||
|
|
||
| try { | ||
| await coffeeQueueTableCRUDLHelper.get({ orderNumber: biggerThanAnyExistingOrderNumber }); | ||
| } catch (error) { | ||
| coffeeQueueTableCRUDLHelper.checkGenericError(error?.message); | ||
| } | ||
|
|
||
| try { | ||
| await coffeeQueueTableCRUDLHelper.update({ orderNumber: biggerThanAnyExistingOrderNumber, order: 'cortado' }); | ||
| } catch (error) { | ||
| coffeeQueueTableCRUDLHelper.checkGenericError(error?.message); | ||
| } | ||
|
|
||
| try { | ||
| await coffeeQueueTableCRUDLHelper.delete({ orderNumber: biggerThanAnyExistingOrderNumber }); | ||
| } catch (error) { | ||
| coffeeQueueTableCRUDLHelper.checkGenericError(error?.message); | ||
| } | ||
| }); | ||
palpatim marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }); | ||
| }; | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.