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

DynamoDB: Item exists with specific values assertion #26

Merged
merged 6 commits into from
Jan 28, 2022
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
2 changes: 2 additions & 0 deletions src/assertions/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import toHaveEventWithSource from "./toHaveEventWithSource";
import toHaveObjectWithNameEqualTo from "./toHaveObjectWithNameEqualTo";
import toExistInDynamoTable from "./toExistInDynamoTable";
import toHaveCompletedExecutionWithStatus from "./toHaveCompletedExecutionWithStatus";
import toContainItemWithValues from "./toContainItemWithValues";
import toMatchStateMachineOutput from "./toMatchStateMachineOutput";

export default {
Expand All @@ -17,5 +18,6 @@ export default {
...toHaveObjectWithNameEqualTo,
...toExistInDynamoTable,
...toHaveCompletedExecutionWithStatus,
...toContainItemWithValues,
...toMatchStateMachineOutput,
};
39 changes: 39 additions & 0 deletions src/assertions/toContainItemWithValues/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
import { testResult, TestResultOutput } from "utils/testResult";
import { AWSClient } from "helpers/general";
import { region } from "../../helpers/general";

export default {
async toContainItemWithValues(
tableName: string,
values: { [key: string]: unknown }
): Promise<TestResultOutput> {
const docClient = new AWSClient.DynamoDB.DocumentClient({
region: region,
});
const keys: { pk: unknown; sk?: unknown } = { pk: values["PK"] };
if (values["SK"] !== undefined) {
keys.sk = values["SK"];
}
const queryParams = {
Key: keys,
TableName: tableName,
};
try {
const result = await docClient.get(queryParams).promise();
Object.entries(values).forEach(([key, val]) => {
if (result.Item?.[key] !== val) {
return testResult(
`Item was expected to have ${key} value of ${val}, but instead had ${key} value of ${result.Item?.[key]}`,
false
);
}
});

return testResult("Item exists with expected values", true);
} catch (e: any) {
console.log(e);

return testResult("Item with specified keys does not exist.", false);
}
},
};