-
-
Notifications
You must be signed in to change notification settings - Fork 202
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
08da47b
commit 1930c36
Showing
10 changed files
with
395 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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 @@ | ||
# MariaDB Module | ||
|
||
[MariaDB](https://mariadb.org/) is one of the most popular open source relational databases. It’s made by the original developers of MySQL and guaranteed to stay open source. It is part of most cloud offerings and the default in most Linux distributions. | ||
|
||
|
||
|
||
## Install | ||
|
||
```bash | ||
npm install @testcontainers/mariadb --save-dev | ||
``` | ||
|
||
## Examples | ||
|
||
<!--codeinclude--> | ||
[Connect and execute query:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:connect | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Connect and execute query using URI:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:uriConnect | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Set username:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:setUsername | ||
<!--/codeinclude--> | ||
|
||
<!--codeinclude--> | ||
[Insert & fetch data:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:insertAndFetchData | ||
<!--/codeinclude--> |
This file contains 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 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,11 @@ | ||
import type { Config } from "jest"; | ||
import * as path from "path"; | ||
|
||
const config: Config = { | ||
preset: "ts-jest", | ||
moduleNameMapper: { | ||
"^testcontainers$": path.resolve(__dirname, "../../testcontainers/src"), | ||
}, | ||
}; | ||
|
||
export default config; |
This file contains 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/mariadb", | ||
"version": "10.13.2", | ||
"license": "MIT", | ||
"keywords": [ | ||
"mariadb", | ||
"testing", | ||
"docker", | ||
"testcontainers" | ||
], | ||
"description": "MariaDB module for Testcontainers", | ||
"homepage": "https://github.com/testcontainers/testcontainers-node#readme", | ||
"repository": { | ||
"type": "git", | ||
"url": "https://github.com/testcontainers/testcontainers-node" | ||
}, | ||
"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": "^10.13.2" | ||
}, | ||
"devDependencies": { | ||
"mariadb": "^3.4.0" | ||
} | ||
} |
This file contains 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 @@ | ||
export { MariaDbContainer, StartedMariaDbContainer } from "./mariadb-container"; |
This file contains 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,128 @@ | ||
import mariadb from "mariadb"; | ||
import { MariaDbContainer } from "./mariadb-container"; | ||
|
||
describe("MariaDb", () => { | ||
jest.setTimeout(240_000); | ||
|
||
// connect { | ||
it("should connect and execute query", async () => { | ||
const container = await new MariaDbContainer().start(); | ||
|
||
const client = await mariadb.createConnection({ | ||
host: container.getHost(), | ||
port: container.getPort(), | ||
database: container.getDatabase(), | ||
user: container.getUsername(), | ||
password: container.getUserPassword(), | ||
}); | ||
|
||
const rows = await client.query("SELECT 1 as res"); | ||
expect(rows).toEqual([{ res: 1 }]); | ||
|
||
await client.end(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// uriConnect { | ||
it("should work with database URI", async () => { | ||
const username = "testUser"; | ||
const password = "testPassword"; | ||
const database = "testDB"; | ||
|
||
// Test non-root user | ||
const container = await new MariaDbContainer() | ||
.withUsername(username) | ||
.withUserPassword(password) | ||
.withDatabase(database) | ||
.start(); | ||
expect(container.getConnectionUri()).toEqual( | ||
`mariadb://${username}:${password}@${container.getHost()}:${container.getPort()}/${database}` | ||
); | ||
await container.stop(); | ||
|
||
// Test root user | ||
const rootContainer = await new MariaDbContainer().withRootPassword(password).withDatabase(database).start(); | ||
expect(rootContainer.getConnectionUri(true)).toEqual( | ||
`mariadb://root:${password}@${rootContainer.getHost()}:${rootContainer.getPort()}/${database}` | ||
); | ||
await rootContainer.stop(); | ||
}); | ||
// } | ||
|
||
// setDatabase { | ||
it("should set database", async () => { | ||
const container = await new MariaDbContainer().withDatabase("customDatabase").start(); | ||
|
||
const client = await mariadb.createConnection({ | ||
host: container.getHost(), | ||
port: container.getPort(), | ||
database: container.getDatabase(), | ||
user: container.getUsername(), | ||
password: container.getUserPassword(), | ||
}); | ||
|
||
const rows = await client.query("SELECT DATABASE() as res"); | ||
expect(rows).toEqual([{ res: "customDatabase" }]); | ||
|
||
await client.end(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// setUsername { | ||
it("should set username", async () => { | ||
const container = await new MariaDbContainer().withUsername("customUsername").start(); | ||
|
||
const client = await mariadb.createConnection({ | ||
host: container.getHost(), | ||
port: container.getPort(), | ||
database: container.getDatabase(), | ||
user: container.getUsername(), | ||
password: container.getUserPassword(), | ||
}); | ||
|
||
const rows = await client.query("SELECT CURRENT_USER() as res"); | ||
expect(rows).toEqual([{ res: "customUsername@%" }]); | ||
|
||
await client.end(); | ||
await container.stop(); | ||
}); | ||
// } | ||
|
||
// insertAndFetchData { | ||
it("should create a table, insert a row, and fetch that row", async () => { | ||
const container = await new MariaDbContainer().start(); | ||
|
||
const client = await mariadb.createConnection({ | ||
host: container.getHost(), | ||
port: container.getPort(), | ||
database: container.getDatabase(), | ||
user: container.getUsername(), | ||
password: container.getUserPassword(), | ||
}); | ||
|
||
// Create table | ||
await client.query(` | ||
CREATE TABLE users ( | ||
id INT AUTO_INCREMENT PRIMARY KEY, | ||
name VARCHAR(255) NOT NULL, | ||
email VARCHAR(255) NOT NULL UNIQUE | ||
); | ||
`); | ||
|
||
// Insert a row | ||
const name = "John Doe"; | ||
const email = "[email protected]"; | ||
const insertResult = await client.query("INSERT INTO users (name, email) VALUES (?, ?)", [name, email]); | ||
expect(insertResult.affectedRows).toBe(1); | ||
|
||
// Fetch the row | ||
const [user] = await client.query("SELECT id, name, email FROM users WHERE email = ?", [email]); | ||
expect(user).toEqual({ id: expect.any(Number), name, email }); | ||
|
||
await client.end(); | ||
await container.stop(); | ||
}); | ||
// } | ||
}); |
Oops, something went wrong.