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

Add migration and seeding tests #43

Merged
merged 1 commit into from
Feb 13, 2024
Merged
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
27 changes: 27 additions & 0 deletions src/e2e.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,33 @@ test('create and list tables, add and scan items', async () => {
expect(items.Items![0]).toEqual(testItem);
});

test('can migrate and seed tables', async () => {
const expectedTables = ['person-table', 'building-table'];

// The tables do not exist at the beginning
const tablesBeforeMigration = await client.send(new ListTablesCommand({}));
expect(expectedTables.some((t) => tablesBeforeMigration.TableNames?.includes(t))).toBeFalsy();

await plugin.migrateHandler();

// After migration, the tables exist and are empty
const tablesAfterMigration = await client.send(new ListTablesCommand({}));
expect(expectedTables.every((t) => tablesAfterMigration.TableNames?.includes(t))).toBeTruthy();
await Promise.all(expectedTables.map(async (tableName) => {
const items = await client.send(new ScanCommand({ TableName: tableName }));
expect(items.Items?.length, tableName).toBe(0);
}));

await plugin.seedHandler();

// After seeding, the tables are not empty
// await new Promise((resolve) => setTimeout(resolve, 1000));
await Promise.all(expectedTables.map(async (tableName) => {
const items = await client.send(new ScanCommand({ TableName: tableName }));
expect(items.Items?.length, tableName).not.toBe(0);
}));
});

afterAll(async () => {
await plugin.endHandler();
});
40 changes: 40 additions & 0 deletions src/serverlessMock.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,48 @@ const serverlessMock = {
dynamodb: {
port: 8000,
stages: ['test'],
seed: {
test: {
sources: [{
table: 'person-table',
sources: ['./test-resources/persons.sources.json'],
}, {
table: 'building-table',
rawsources: ['./test-resources/buildings.rawsources.json'],
}],
},
},
},
},
resources: {
Resources: {
personTable: {
Type: 'AWS::DynamoDB::Table',
Properties: {
TableName: 'person-table',
AttributeDefinitions: [{ AttributeName: 'id', AttributeType: 'S' }],
KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
},
},
buildingTable: {
Type: 'AWS::DynamoDB::Table',
Properties: {
TableName: 'building-table',
AttributeDefinitions: [{ AttributeName: 'id', AttributeType: 'S' }],
KeySchema: [{ AttributeName: 'id', KeyType: 'HASH' }],
ProvisionedThroughput: {
ReadCapacityUnits: 1,
WriteCapacityUnits: 1,
},
},
},
},
},

},
cli: {
log: () => { },
Expand Down
18 changes: 18 additions & 0 deletions test-resources/buildings.rawsources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
[
{
"id": { "S": "bld_123" },
"name": { "S": "Tower A" },
"address": { "S": "123 Main Street" },
"height": { "N": "150" },
"constructedOn": { "S": "2024-02-15" },
"isCommercial": { "BOOL": true }
},
{
"id": { "S": "bld_456" },
"name": { "S": "Plaza Center" },
"address": { "S": "456 Elm Street" },
"height": { "N": "100" },
"constructedOn": { "S": "2024-06-21" },
"isCommercial": { "BOOL": false }
}
]
10 changes: 10 additions & 0 deletions test-resources/persons.sources.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[
{
"id": "per_123",
"name": "Adam Jones"
},
{
"id": "per_456",
"name": "Jane Doe"
}
]
Loading