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

feat: auto increment support #2883

Open
wants to merge 18 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
134 changes: 72 additions & 62 deletions codebuild_specs/e2e_workflow.yml

Large diffs are not rendered by default.

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,
);
p5quared marked this conversation as resolved.
Show resolved Hide resolved
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
import * as path from 'path';
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';

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 amplifyGraphqlSchema = `
type CoffeeQueue @model @refersTo(name: "${getRDSTableNamePrefix()}coffee_queue") {
orderNumber: Int! @primaryKey @default
Copy link
Member

Choose a reason for hiding this comment

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

It's worth nothing in a comment here the important characteristics of this tests:

  1. It's an Int field, backed by a SERIAL type in PG
  2. It's a required field, which would normally mean that customers would have to supply a value in the GraphQL mutation input
  3. It has a @default annotation with no arguments, meaning that the only behavior our transformer should do is to suppress the input requirement.

Copy link
Member Author

Choose a reason for hiding this comment

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

Noted in comment

order: String!
customer: String
}
`;

const { projFolderName, region, connectionConfigName, dbController } = options;
const templatePath = path.resolve(path.join(__dirname, '..', '__tests__', 'backends', 'sql-models'));
Copy link
Member

Choose a reason for hiding this comment

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

Now or in the next PR: Can we migrate this test to use the configurable stack so we can (eventually) remove the sql-models stack? Eventually I'd like us to be using just one stack to reduce the number of test fixtures we have to maintain.

Copy link
Member Author

Choose a reason for hiding this comment

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

If it's okay I'd like to defer this migration to a separate PR and migrate a number of tests at once.


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, amplifyGraphqlSchema);
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', ['orderNumber', 'order', 'customer']);
});

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);
}
});
Copy link
Member

Choose a reason for hiding this comment

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

Does PG allow someone to specify a value for a SERIAL field? If so, we need a test for that case. If not, we need a test that a customer cannot supply a value for it (which might just be a unit test that asserts the serial default field is not present in the input shape.

Copy link
Member Author

@p5quared p5quared Oct 1, 2024

Choose a reason for hiding this comment

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

PG does allow users to specify a value for SERIAL fields (but does not stop users' sequences from reproducing that value later). I modified the test to use a custom value for one of the inserts.

});
};
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,11 @@ export const testGraphQLAPI = (
engine: ImportedRDSType,
): void => {
describe(`${testBlockDescription} - ${engine}`, () => {
const defaultTodoDescription = 'Lorem Ipsum yadda yadda';
const amplifyGraphqlSchema = `
type Todo @model @refersTo(name: "${getRDSTableNamePrefix()}todos") {
id: ID! @primaryKey
description: String!
description: String! @default("${defaultTodoDescription}")
}
type Student @model @refersTo(name: "${getRDSTableNamePrefix()}students") {
studentId: Int! @primaryKey(sortKeyFields: ["classId"])
Expand Down Expand Up @@ -72,6 +73,32 @@ export const testGraphQLAPI = (
deleteProjectDir(projRoot);
});

test(`check default value on todo table - ${engine}`, async () => {
// Create Todo Mutation
const createTodo1 = await toDoTableCRUDLHelper.create({});

expect(createTodo1).toBeDefined();
expect(createTodo1.id).toBeDefined();
expect(createTodo1.description).toEqual(defaultTodoDescription);

// Get Todo Query
const getTodo1 = await toDoTableCRUDLHelper.getById(createTodo1.id);
expect(getTodo1.id).toEqual(createTodo1.id);
expect(getTodo1.description).toEqual(createTodo1.description);

// Update Todo Mutation
const updateTodo1 = await toDoTableCRUDLHelper.update({ id: createTodo1.id, description: 'Updated Todo #1' });

expect(updateTodo1.id).toEqual(createTodo1.id);
expect(updateTodo1.description).toEqual('Updated Todo #1');

// Get Todo Query after update
const getUpdatedTodo1 = await toDoTableCRUDLHelper.getById(createTodo1.id);

expect(getUpdatedTodo1.id).toEqual(createTodo1.id);
expect(getUpdatedTodo1.description).toEqual('Updated Todo #1');
});

test(`check CRUDL on todo table with default primary key - ${engine}`, async () => {
// Create Todo Mutation
const createTodo1 = await toDoTableCRUDLHelper.create({ description: 'Todo #1' });
Expand Down
Loading
Loading