Skip to content

Commit 1930c36

Browse files
authored
Add MariaDB module (#851)
1 parent 08da47b commit 1930c36

File tree

10 files changed

+395
-0
lines changed

10 files changed

+395
-0
lines changed

docs/modules/mariadb.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
# MariaDB Module
2+
3+
[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.
4+
5+
6+
7+
## Install
8+
9+
```bash
10+
npm install @testcontainers/mariadb --save-dev
11+
```
12+
13+
## Examples
14+
15+
<!--codeinclude-->
16+
[Connect and execute query:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:connect
17+
<!--/codeinclude-->
18+
19+
<!--codeinclude-->
20+
[Connect and execute query using URI:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:uriConnect
21+
<!--/codeinclude-->
22+
23+
<!--codeinclude-->
24+
[Set username:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:setUsername
25+
<!--/codeinclude-->
26+
27+
<!--codeinclude-->
28+
[Insert & fetch data:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:insertAndFetchData
29+
<!--/codeinclude-->

mkdocs.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,7 @@ nav:
5050
- HiveMQ: modules/hivemq.md
5151
- Kafka: modules/kafka.md
5252
- Localstack: modules/localstack.md
53+
- MariaDB: modules/mariadb.md
5354
- MongoDB: modules/mongodb.md
5455
- MSSQLServer: modules/mssqlserver.md
5556
- MySQL: modules/mysql.md

package-lock.json

Lines changed: 58 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import type { Config } from "jest";
2+
import * as path from "path";
3+
4+
const config: Config = {
5+
preset: "ts-jest",
6+
moduleNameMapper: {
7+
"^testcontainers$": path.resolve(__dirname, "../../testcontainers/src"),
8+
},
9+
};
10+
11+
export default config;
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
{
2+
"name": "@testcontainers/mariadb",
3+
"version": "10.13.2",
4+
"license": "MIT",
5+
"keywords": [
6+
"mariadb",
7+
"testing",
8+
"docker",
9+
"testcontainers"
10+
],
11+
"description": "MariaDB module for Testcontainers",
12+
"homepage": "https://github.com/testcontainers/testcontainers-node#readme",
13+
"repository": {
14+
"type": "git",
15+
"url": "https://github.com/testcontainers/testcontainers-node"
16+
},
17+
"bugs": {
18+
"url": "https://github.com/testcontainers/testcontainers-node/issues"
19+
},
20+
"main": "build/index.js",
21+
"files": [
22+
"build"
23+
],
24+
"publishConfig": {
25+
"access": "public"
26+
},
27+
"scripts": {
28+
"prepack": "shx cp ../../../README.md . && shx cp ../../../LICENSE .",
29+
"build": "tsc --project tsconfig.build.json"
30+
},
31+
"dependencies": {
32+
"testcontainers": "^10.13.2"
33+
},
34+
"devDependencies": {
35+
"mariadb": "^3.4.0"
36+
}
37+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export { MariaDbContainer, StartedMariaDbContainer } from "./mariadb-container";
Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,128 @@
1+
import mariadb from "mariadb";
2+
import { MariaDbContainer } from "./mariadb-container";
3+
4+
describe("MariaDb", () => {
5+
jest.setTimeout(240_000);
6+
7+
// connect {
8+
it("should connect and execute query", async () => {
9+
const container = await new MariaDbContainer().start();
10+
11+
const client = await mariadb.createConnection({
12+
host: container.getHost(),
13+
port: container.getPort(),
14+
database: container.getDatabase(),
15+
user: container.getUsername(),
16+
password: container.getUserPassword(),
17+
});
18+
19+
const rows = await client.query("SELECT 1 as res");
20+
expect(rows).toEqual([{ res: 1 }]);
21+
22+
await client.end();
23+
await container.stop();
24+
});
25+
// }
26+
27+
// uriConnect {
28+
it("should work with database URI", async () => {
29+
const username = "testUser";
30+
const password = "testPassword";
31+
const database = "testDB";
32+
33+
// Test non-root user
34+
const container = await new MariaDbContainer()
35+
.withUsername(username)
36+
.withUserPassword(password)
37+
.withDatabase(database)
38+
.start();
39+
expect(container.getConnectionUri()).toEqual(
40+
`mariadb://${username}:${password}@${container.getHost()}:${container.getPort()}/${database}`
41+
);
42+
await container.stop();
43+
44+
// Test root user
45+
const rootContainer = await new MariaDbContainer().withRootPassword(password).withDatabase(database).start();
46+
expect(rootContainer.getConnectionUri(true)).toEqual(
47+
`mariadb://root:${password}@${rootContainer.getHost()}:${rootContainer.getPort()}/${database}`
48+
);
49+
await rootContainer.stop();
50+
});
51+
// }
52+
53+
// setDatabase {
54+
it("should set database", async () => {
55+
const container = await new MariaDbContainer().withDatabase("customDatabase").start();
56+
57+
const client = await mariadb.createConnection({
58+
host: container.getHost(),
59+
port: container.getPort(),
60+
database: container.getDatabase(),
61+
user: container.getUsername(),
62+
password: container.getUserPassword(),
63+
});
64+
65+
const rows = await client.query("SELECT DATABASE() as res");
66+
expect(rows).toEqual([{ res: "customDatabase" }]);
67+
68+
await client.end();
69+
await container.stop();
70+
});
71+
// }
72+
73+
// setUsername {
74+
it("should set username", async () => {
75+
const container = await new MariaDbContainer().withUsername("customUsername").start();
76+
77+
const client = await mariadb.createConnection({
78+
host: container.getHost(),
79+
port: container.getPort(),
80+
database: container.getDatabase(),
81+
user: container.getUsername(),
82+
password: container.getUserPassword(),
83+
});
84+
85+
const rows = await client.query("SELECT CURRENT_USER() as res");
86+
expect(rows).toEqual([{ res: "customUsername@%" }]);
87+
88+
await client.end();
89+
await container.stop();
90+
});
91+
// }
92+
93+
// insertAndFetchData {
94+
it("should create a table, insert a row, and fetch that row", async () => {
95+
const container = await new MariaDbContainer().start();
96+
97+
const client = await mariadb.createConnection({
98+
host: container.getHost(),
99+
port: container.getPort(),
100+
database: container.getDatabase(),
101+
user: container.getUsername(),
102+
password: container.getUserPassword(),
103+
});
104+
105+
// Create table
106+
await client.query(`
107+
CREATE TABLE users (
108+
id INT AUTO_INCREMENT PRIMARY KEY,
109+
name VARCHAR(255) NOT NULL,
110+
email VARCHAR(255) NOT NULL UNIQUE
111+
);
112+
`);
113+
114+
// Insert a row
115+
const name = "John Doe";
116+
const email = "[email protected]";
117+
const insertResult = await client.query("INSERT INTO users (name, email) VALUES (?, ?)", [name, email]);
118+
expect(insertResult.affectedRows).toBe(1);
119+
120+
// Fetch the row
121+
const [user] = await client.query("SELECT id, name, email FROM users WHERE email = ?", [email]);
122+
expect(user).toEqual({ id: expect.any(Number), name, email });
123+
124+
await client.end();
125+
await container.stop();
126+
});
127+
// }
128+
});

0 commit comments

Comments
 (0)