Skip to content

Commit 972b45f

Browse files
Mariadb
1 parent 6fe9d69 commit 972b45f

File tree

2 files changed

+32
-53
lines changed

2 files changed

+32
-53
lines changed

docs/modules/mariadb.md

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
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-
1+
# MariaDB
62

73
## Install
84

@@ -12,18 +8,34 @@ npm install @testcontainers/mariadb --save-dev
128

139
## Examples
1410

11+
These examples use the following libraries:
12+
13+
- [mariadb](https://www.npmjs.com/package/mariadb)
14+
15+
npm install mariadb
16+
17+
Choose an image from the [container registry](https://hub.docker.com/_/mariadb) and substitute `IMAGE`.
18+
19+
### Execute a query
20+
1521
<!--codeinclude-->
16-
[Connect and execute query:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:connect
22+
[](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:mariaDbConnect
1723
<!--/codeinclude-->
1824

25+
### Connect via URI
26+
1927
<!--codeinclude-->
20-
[Connect and execute query using URI:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:uriConnect
28+
[](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:mariaDbUriConnect
2129
<!--/codeinclude-->
2230

31+
### With user
32+
2333
<!--codeinclude-->
24-
[Set username:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:setUsername
34+
[](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:mariaDbSetUsername
2535
<!--/codeinclude-->
2636

37+
### With database
38+
2739
<!--codeinclude-->
28-
[Insert & fetch data:](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:insertAndFetchData
40+
[](../../packages/modules/mariadb/src/mariadb-container.test.ts) inside_block:mariaDbSetDatabase
2941
<!--/codeinclude-->

packages/modules/mariadb/src/mariadb-container.test.ts

Lines changed: 11 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@ import { MariaDbContainer } from "./mariadb-container";
44

55
const IMAGE = getImage(__dirname);
66

7-
describe("MariaDb", { timeout: 240_000 }, () => {
8-
// connect {
7+
describe("MariaDbContainer", { timeout: 240_000 }, () => {
98
it("should connect and execute query", async () => {
9+
// mariaDbConnect {
1010
await using container = await new MariaDbContainer(IMAGE).start();
1111

1212
const client = await mariadb.createConnection({
@@ -21,11 +21,11 @@ describe("MariaDb", { timeout: 240_000 }, () => {
2121
expect(rows).toEqual([{ res: 1 }]);
2222

2323
await client.end();
24+
// }
2425
});
25-
// }
2626

27-
// uriConnect {
2827
it("should work with database URI", async () => {
28+
// mariaDbUriConnect {
2929
const username = "testUser";
3030
const password = "testPassword";
3131
const database = "testDB";
@@ -36,6 +36,7 @@ describe("MariaDb", { timeout: 240_000 }, () => {
3636
.withUserPassword(password)
3737
.withDatabase(database)
3838
.start();
39+
3940
expect(container.getConnectionUri()).toEqual(
4041
`mariadb://${username}:${password}@${container.getHost()}:${container.getPort()}/${database}`
4142
);
@@ -45,14 +46,15 @@ describe("MariaDb", { timeout: 240_000 }, () => {
4546
.withRootPassword(password)
4647
.withDatabase(database)
4748
.start();
49+
4850
expect(rootContainer.getConnectionUri(true)).toEqual(
4951
`mariadb://root:${password}@${rootContainer.getHost()}:${rootContainer.getPort()}/${database}`
5052
);
53+
// }
5154
});
52-
// }
5355

54-
// setDatabase {
5556
it("should set database", async () => {
57+
// mariaDbSetDatabase {
5658
await using container = await new MariaDbContainer(IMAGE).withDatabase("customDatabase").start();
5759

5860
const client = await mariadb.createConnection({
@@ -67,11 +69,11 @@ describe("MariaDb", { timeout: 240_000 }, () => {
6769
expect(rows).toEqual([{ res: "customDatabase" }]);
6870

6971
await client.end();
72+
// }
7073
});
71-
// }
7274

73-
// setUsername {
7475
it("should set username", async () => {
76+
// mariaDbSetUsername {
7577
await using container = await new MariaDbContainer(IMAGE).withUsername("customUsername").start();
7678

7779
const client = await mariadb.createConnection({
@@ -86,43 +88,8 @@ describe("MariaDb", { timeout: 240_000 }, () => {
8688
expect(rows).toEqual([{ res: "customUsername@%" }]);
8789

8890
await client.end();
91+
// }
8992
});
90-
// }
91-
92-
// insertAndFetchData {
93-
it("should create a table, insert a row, and fetch that row", async () => {
94-
await using container = await new MariaDbContainer(IMAGE).start();
95-
96-
const client = await mariadb.createConnection({
97-
host: container.getHost(),
98-
port: container.getPort(),
99-
database: container.getDatabase(),
100-
user: container.getUsername(),
101-
password: container.getUserPassword(),
102-
});
103-
104-
// Create table
105-
await client.query(`
106-
CREATE TABLE users (
107-
id INT AUTO_INCREMENT PRIMARY KEY,
108-
name VARCHAR(255) NOT NULL,
109-
email VARCHAR(255) NOT NULL UNIQUE
110-
);
111-
`);
112-
113-
// Insert a row
114-
const name = "John Doe";
115-
const email = "[email protected]";
116-
const insertResult = await client.query("INSERT INTO users (name, email) VALUES (?, ?)", [name, email]);
117-
expect(insertResult.affectedRows).toBe(1);
118-
119-
// Fetch the row
120-
const [user] = await client.query("SELECT id, name, email FROM users WHERE email = ?", [email]);
121-
expect(user).toEqual({ id: expect.any(Number), name, email });
122-
123-
await client.end();
124-
});
125-
// }
12693

12794
it("should work with restarted container", async () => {
12895
await using container = await new MariaDbContainer(IMAGE).start();

0 commit comments

Comments
 (0)