Skip to content

feat: jest integ tests for dynamodb #580

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

Closed
wants to merge 14 commits into from
Closed
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
199 changes: 199 additions & 0 deletions clients/node/client-dynamodb-node/test/integ/index.ispec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
import { DynamoDB } from "../../DynamoDB";

describe("DynamoDB integration tests", () => {
// Move this timeout to jest config for integ tests
jest.setTimeout(500000);

const client = new DynamoDB({});
const tableName = `aws-js-integration-${Math.random()
.toString(36)
.substring(2)}`;
const sleepFor = (ms: number) =>
new Promise(resolve => setTimeout(resolve, ms));

// Replace with waiters once available
const tableExists = async (tableName: string) => {
const params = { TableName: tableName };

// Iterate totalTries times
const totalTries = 25;
for (let i = 0; i < totalTries; i++) {
try {
const response = await client.describeTable(params);
if (response.Table && response.Table.TableStatus === "ACTIVE") {
return true;
} else {
if (i === totalTries - 1) {
throw `Table ${tableName} not in active status`;
}
await sleepFor(20000);
}
} catch (e) {
if (i === totalTries - 1) {
throw e;
}
await sleepFor(20000);
}
}
};

// Replace with waiters once available
const tableNotExists = async (tableName: string) => {
const params = { TableName: tableName };

// Iterate totalTries times
const totalTries = 25;
for (let i = 0; i < totalTries; i++) {
try {
await client.describeTable(params);
if (i === totalTries - 1) {
throw `Table ${tableName} not deleted`;
}
await sleepFor(20000);
} catch (e) {
if (i === totalTries - 1) {
throw e;
} else if (e.name === "ResourceNotFoundException") {
return true;
}
await sleepFor(20000);
}
}
};

describe("Table CRUD operations", () => {
const itemKey = {
Artist: {
S: "Acme Band"
},
SongTitle: {
S: "Happy Day"
}
};

it("create table for testing CRUD operations", async () => {
expect.assertions(1);
const params = {
TableName: tableName,
AttributeDefinitions: [
{
AttributeName: "Artist",
AttributeType: "S"
},
{
AttributeName: "SongTitle",
AttributeType: "S"
}
],
KeySchema: [
{
AttributeName: "Artist",
KeyType: "HASH"
},
{
AttributeName: "SongTitle",
KeyType: "RANGE"
}
],
ProvisionedThroughput: {
ReadCapacityUnits: 5,
WriteCapacityUnits: 5
}
};
await client.createTable(params);

// TODO: replace with waiters once introduced
await tableExists(tableName);

const response = await client.describeTable(params);
expect(response.Table && response.Table.TableName).toBe(tableName);
});

it("adds an item to the table", async () => {
expect.assertions(1);

const albumTitle = "Somewhat Famous";
const params = {
Item: {
...itemKey,
AlbumTitle: {
S: albumTitle
}
},
TableName: tableName
};

await client.putItem(params);
const item = await client.getItem({
Key: itemKey,
TableName: tableName
});
expect(item.Item && item.Item.AlbumTitle.S).toBe(albumTitle);
});

it("updates an item in the table", async () => {
expect.assertions(1);

const item = await client.getItem({
Key: itemKey,
TableName: tableName
});

const newAlbumTitle = `New ${item.Item && item.Item.AlbumTitle.S}`;
var params = {
ExpressionAttributeNames: {
"#AT": "AlbumTitle"
},
ExpressionAttributeValues: {
":t": {
S: newAlbumTitle
}
},
Key: itemKey,
ReturnValues: "ALL_NEW",
TableName: tableName,
UpdateExpression: "SET #AT = :t"
};
await client.updateItem(params);

const updatedItem = await client.getItem({
Key: itemKey,
TableName: tableName
});
expect(updatedItem.Item && updatedItem.Item.AlbumTitle.S).toBe(
newAlbumTitle
);
});

it("deletes an item from the table", async () => {
expect.assertions(1);

await client.deleteItem({
Key: itemKey,
TableName: tableName
});
const item = await client.getItem({
Key: itemKey,
TableName: tableName
});
expect(item.Item).toBeUndefined();
});

it("delete table after testing CRUD operations", async () => {
expect.assertions(1);
const params = {
TableName: tableName
};
await client.deleteTable(params);

// TODO: replace with waiters once introduced
await tableNotExists(tableName);

try {
await client.describeTable(params);
} catch (e) {
expect(e.name).toBe("ResourceNotFoundException");
}
});
});
});