Repository navigation
Add DynamoDB module #1613
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
Open
stschulte
wants to merge
14
commits into
testcontainers:main
Choose a base branch
from
stschulte:new-module-dynamodb
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add DynamoDB module #1613
Changes from all commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
1180c12
New module DynamoDB
stschulte 72258fe
DynamoDB: Move helper beyond caller
stschulte 7a0dc86
DynamoDB: Remove withInMemory
stschulte ac4984b
DynamoDB: Drop support for client config
stschulte c071c31
DynamoDB: Improve testcase for withDisableTelemetry
stschulte 247ce27
DynamoDB: Do not import globals
stschulte 2119fbf
DynamoDB: Adapt wait strategy
stschulte 11fd887
DynamoDB: Shorten documentation
stschulte 726a7f0
DynamoDB: Simplify helper function
stschulte dafbbd7
DynamoDB: Simplify construction of getEndpoint
stschulte cccf1fa
DynamoDB: Disable dbPath and withTelemetry
stschulte 2071e88
DynamoDB: Move withEnvironment to constructor
stschulte 2ffb516
DynamoDB: Remove comments
stschulte 3a9bb8f
DynamoDB: Return expressions directly and fix typo
cristianrgreco File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| # DynamoDB | ||
|
|
||
| ## Install | ||
|
|
||
| ```bash | ||
| npm install @testcontainers/dynamodb --save-dev | ||
| ``` | ||
|
|
||
| ## Examples | ||
|
|
||
| These examples use the following libraries: | ||
|
|
||
| - [@aws-sdk/client-dynamodb](https://www.npmjs.com/package/@aws-sdk/client-dynamodb) | ||
|
|
||
| npm install @aws-sdk/client-dynamodb | ||
|
|
||
| Choose an image from the [container registry](https://hub.docker.com/r/amazon/dynamodb-local) and substitute `IMAGE`. | ||
|
|
||
| ### Connect to local endpoint | ||
|
|
||
| <!--codeinclude--> | ||
| [](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:connectEndpoint | ||
| <!--/codeinclude--> | ||
|
|
||
| ### Use a shared database | ||
|
|
||
| <!--codeinclude--> | ||
| [](../../packages/modules/dynamodb/src/dynamodb-container.test.ts) inside_block:sharedDB | ||
| <!--/codeinclude--> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| FROM amazon/dynamodb-local:3.3.2 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| { | ||
| "name": "@testcontainers/dynamodb", | ||
| "version": "12.2.1", | ||
| "license": "MIT", | ||
| "keywords": [ | ||
| "dynamodb", | ||
| "testing", | ||
| "docker", | ||
| "testcontainers" | ||
| ], | ||
| "description": "DynamoDB module for Testcontainers", | ||
| "homepage": "https://github.com/testcontainers/testcontainers-node#readme", | ||
| "repository": { | ||
| "type": "git", | ||
| "url": "git+https://github.com/testcontainers/testcontainers-node.git" | ||
| }, | ||
| "bugs": { | ||
| "url": "https://github.com/testcontainers/testcontainers-node/issues" | ||
| }, | ||
| "main": "build/index.js", | ||
| "files": [ | ||
| "build" | ||
| ], | ||
| "publishConfig": { | ||
| "access": "public" | ||
| }, | ||
| "scripts": { | ||
| "prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .", | ||
| "build": "tsc --project tsconfig.build.json" | ||
| }, | ||
| "dependencies": { | ||
| "testcontainers": "^12.2.1" | ||
| }, | ||
| "devDependencies": { | ||
| "@aws-sdk/client-dynamodb": "^3.1168.0" | ||
| } | ||
| } |
157 changes: 157 additions & 0 deletions
157
packages/modules/dynamodb/src/dynamodb-container.test.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,157 @@ | ||
| import { | ||
| CreateTableCommand, | ||
| DynamoDBClient, | ||
| GetItemCommand, | ||
| ListTablesCommand, | ||
| PutItemCommand, | ||
| waitUntilTableExists, | ||
| } from "@aws-sdk/client-dynamodb"; | ||
| import { getImage } from "../../../testcontainers/src/utils/test-helper"; | ||
| import { DynamoDBContainer } from "./dynamodb-container"; | ||
|
|
||
| const IMAGE = getImage(__dirname); | ||
|
|
||
| async function createTestTable(client: DynamoDBClient): Promise<{ | ||
| addUser: (userId: number, name: string) => Promise<void>; | ||
| getUser: (userId: number) => Promise<string | null>; | ||
| }> { | ||
| const command = new CreateTableCommand({ | ||
| TableName: "users", | ||
| BillingMode: "PAY_PER_REQUEST", | ||
| AttributeDefinitions: [ | ||
| { | ||
| AttributeName: "userId", | ||
| AttributeType: "N", | ||
| }, | ||
| ], | ||
| KeySchema: [ | ||
| { | ||
| AttributeName: "userId", | ||
| KeyType: "HASH", | ||
| }, | ||
| ], | ||
| }); | ||
|
|
||
| await client.send(command); | ||
| await waitUntilTableExists({ client, maxWaitTime: 30 }, { TableName: "users" }); | ||
|
|
||
| return { | ||
| addUser: async (userId, name) => { | ||
| const putCommand = new PutItemCommand({ | ||
| TableName: "users", | ||
| Item: { | ||
| userId: { N: userId.toString() }, | ||
| name: { S: name }, | ||
| }, | ||
| }); | ||
| await client.send(putCommand); | ||
| }, | ||
| getUser: async (userId) => { | ||
| const getCommand = new GetItemCommand({ | ||
| TableName: "users", | ||
| Key: { | ||
| userId: { N: userId.toString() }, | ||
| }, | ||
| ConsistentRead: true, | ||
| }); | ||
| const result = await client.send(getCommand); | ||
| return result.Item?.name?.S ?? null; | ||
| }, | ||
| }; | ||
| } | ||
|
|
||
| function createClient(endpoint: string, accessKeyId: string = "AKIAIOSFODNN7EXAMPLE"): DynamoDBClient { | ||
| return new DynamoDBClient({ | ||
| region: "us-east-1", | ||
| credentials: { | ||
| accessKeyId, | ||
| secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", | ||
| }, | ||
| endpoint, | ||
| }); | ||
| } | ||
|
|
||
| describe("DynamoDBContainer", { timeout: 180_000 }, () => { | ||
| it("connects with endpoint", async () => { | ||
| // connectEndpoint { | ||
| await using container = await new DynamoDBContainer(IMAGE).start(); | ||
| const client = new DynamoDBClient({ | ||
| region: "us-east-1", | ||
| credentials: { | ||
| // use any credentials | ||
| accessKeyId: "AKIAIOSFODNN7EXAMPLE", | ||
| secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", | ||
| }, | ||
| endpoint: container.getEndpoint(), | ||
| }); | ||
|
|
||
| const command = new ListTablesCommand({}); | ||
| const result = await client.send(command); | ||
|
|
||
| expect(result.TableNames).toStrictEqual([]); | ||
| // } | ||
| }); | ||
|
|
||
| it("stores data", async () => { | ||
| await using container = await new DynamoDBContainer(IMAGE).start(); | ||
| const client = createClient(container.getEndpoint()); | ||
|
|
||
| const { addUser, getUser } = await createTestTable(client); | ||
| await addUser(1, "Bob"); | ||
| await addUser(2, "Alice"); | ||
|
|
||
| const userA = await getUser(1); | ||
| const userB = await getUser(2); | ||
| expect(userA).toBe("Bob"); | ||
| expect(userB).toBe("Alice"); | ||
| }); | ||
|
|
||
| it("separates data by client by default", async () => { | ||
| await using container = await new DynamoDBContainer(IMAGE).start(); | ||
|
|
||
| const clientA = createClient(container.getEndpoint(), "AKIAIOSFODNN7EXAMPL1"); | ||
| const clientB = createClient(container.getEndpoint(), "AKIAIOSFODNN7EXAMPL2"); | ||
|
|
||
| await createTestTable(clientA); | ||
|
|
||
| const resultA = await clientA.send(new ListTablesCommand()); | ||
| const resultB = await clientB.send(new ListTablesCommand()); | ||
|
|
||
| expect(resultA.TableNames).toStrictEqual(["users"]); | ||
| expect(resultB.TableNames).toStrictEqual([]); | ||
| }); | ||
|
|
||
| it("uses a shared table when requested", async () => { | ||
| // sharedDB { | ||
| await using container = await new DynamoDBContainer(IMAGE).withSharedDB().start(); | ||
|
|
||
| // Both clients will see the same data even though the | ||
| // ACCESS_KEY_ID differ | ||
| const clientA = new DynamoDBClient({ | ||
| region: "us-east-1", | ||
| endpoint: container.getEndpoint(), | ||
| credentials: { | ||
| accessKeyId: "AKIAIOSFODNN7EXAMPLE", | ||
| secretAccessKey: "wJalrXUtnFEMI/K7MDENG/bPxRfiCYEXAMPLEKEY", | ||
| }, | ||
| }); | ||
|
|
||
| const clientB = new DynamoDBClient({ | ||
| region: "us-east-1", | ||
| endpoint: container.getEndpoint(), | ||
| credentials: { | ||
| accessKeyId: "AKIAIOSFODNN7EXAMPL2", | ||
| secretAccessKey: "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", | ||
| }, | ||
| }); | ||
| // } | ||
|
|
||
| await createTestTable(clientA); | ||
|
|
||
| const resultA = await clientA.send(new ListTablesCommand()); | ||
| const resultB = await clientB.send(new ListTablesCommand()); | ||
|
|
||
| expect(resultA.TableNames).toStrictEqual(["users"]); | ||
| expect(resultB.TableNames).toStrictEqual(["users"]); | ||
| }); | ||
| }); | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.