Skip to content

Commit

Permalink
Add migration and seeding tests (#43)
Browse files Browse the repository at this point in the history
  • Loading branch information
domdomegg authored Feb 13, 2024
1 parent 6a16b74 commit a207e9a
Show file tree
Hide file tree
Showing 4 changed files with 95 additions and 0 deletions.
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"
}
]

0 comments on commit a207e9a

Please sign in to comment.