diff --git a/src/e2e.test.ts b/src/e2e.test.ts index fd5fc06..91892d2 100644 --- a/src/e2e.test.ts +++ b/src/e2e.test.ts @@ -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(); }); diff --git a/src/serverlessMock.test.ts b/src/serverlessMock.test.ts index de8c26e..2a32ae3 100644 --- a/src/serverlessMock.test.ts +++ b/src/serverlessMock.test.ts @@ -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: () => { }, diff --git a/test-resources/buildings.rawsources.json b/test-resources/buildings.rawsources.json new file mode 100644 index 0000000..a684fa4 --- /dev/null +++ b/test-resources/buildings.rawsources.json @@ -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 } + } +] diff --git a/test-resources/persons.sources.json b/test-resources/persons.sources.json new file mode 100644 index 0000000..cf73458 --- /dev/null +++ b/test-resources/persons.sources.json @@ -0,0 +1,10 @@ +[ + { + "id": "per_123", + "name": "Adam Jones" + }, + { + "id": "per_456", + "name": "Jane Doe" + } +] \ No newline at end of file