Skip to content

Commit 0e9fc5d

Browse files
Mysql
1 parent d366dbc commit 0e9fc5d

File tree

2 files changed

+30
-22
lines changed

2 files changed

+30
-22
lines changed

docs/modules/mysql.md

Lines changed: 17 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,4 @@
1-
# MySQL Module
2-
3-
[MySQL](https://www.mysql.com/) is the world's most popular open source database. With its proven performance, reliability and ease-of-use, MySQL has become the leading database choice for web-based applications, covering the entire range from personal projects and websites, via e-commerce and information services, all the way to high profile web properties including Facebook, Twitter, YouTube, Yahoo! and many more.
1+
# MySQL
42

53
## Install
64

@@ -10,18 +8,28 @@ npm install @testcontainers/mysql --save-dev
108

119
## Examples
1210

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

1721
<!--codeinclude-->
18-
[Connect and execute query using URI:](../../packages/modules/mysql/src/mysql-container.test.ts) inside_block:uriConnect
22+
[](../../packages/modules/mysql/src/mysql-container.test.ts) inside_block:mysqlConnect
1923
<!--/codeinclude-->
2024

25+
### Execute a query inside the container
26+
2127
<!--codeinclude-->
22-
[Set username:](../../packages/modules/mysql/src/mysql-container.test.ts) inside_block:setUsername
28+
[](../../packages/modules/mysql/src/mysql-container.test.ts) inside_block:mysqlExecuteQuery
2329
<!--/codeinclude-->
2430

31+
### With credentials
32+
2533
<!--codeinclude-->
26-
[Execute a query inside the container:](../../packages/modules/mysql/src/mysql-container.test.ts) inside_block:executeQuery
34+
[](../../packages/modules/mysql/src/mysql-container.test.ts) inside_block:mysqlUriConnect
2735
<!--/codeinclude-->

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

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,8 @@ import { MySqlContainer } from "./mysql-container";
55
const IMAGE = getImage(__dirname);
66

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

1212
const client = await createConnection({
@@ -21,38 +21,38 @@ describe("MySqlContainer", { 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+
// mysqlUriConnect {
2929
const username = "testUser";
3030
const password = "testPassword";
3131
const database = "testDB";
3232

33-
// Test non-root user
3433
await using container = await new MySqlContainer(IMAGE)
3534
.withUsername(username)
3635
.withUserPassword(password)
3736
.withDatabase(database)
3837
.start();
38+
3939
expect(container.getConnectionUri()).toEqual(
4040
`mysql://${username}:${password}@${container.getHost()}:${container.getPort()}/${database}`
4141
);
4242

43-
// Test root user
4443
await using rootContainer = await new MySqlContainer(IMAGE)
4544
.withRootPassword(password)
4645
.withDatabase(database)
4746
.start();
47+
4848
expect(rootContainer.getConnectionUri(true)).toEqual(
4949
`mysql://root:${password}@${rootContainer.getHost()}:${rootContainer.getPort()}/${database}`
5050
);
51+
// }
5152
});
52-
// }
5353

54-
// setDatabase {
5554
it("should set database", async () => {
55+
// mysqlSetDatabase {
5656
await using container = await new MySqlContainer(IMAGE).withDatabase("customDatabase").start();
5757

5858
const client = await createConnection({
@@ -67,10 +67,10 @@ describe("MySqlContainer", { timeout: 240_000 }, () => {
6767
expect(rows).toEqual([{ res: "customDatabase" }]);
6868

6969
await client.end();
70+
// }
7071
});
71-
// }
7272

73-
// setUsername {
73+
// mysqlSetUsername {
7474
it("should set username", async () => {
7575
await using container = await new MySqlContainer(IMAGE).withUsername("customUsername").start();
7676

@@ -89,12 +89,13 @@ describe("MySqlContainer", { timeout: 240_000 }, () => {
8989
});
9090
// }
9191

92-
// executeQuery {
9392
it("should execute a query and return the result", async () => {
93+
// mysqlExecuteQuery {
9494
await using container = await new MySqlContainer(IMAGE).start();
9595

96-
const queryResult = await container.executeQuery("SELECT 1 as res");
97-
expect(queryResult).toEqual(expect.stringContaining("res\n1\n"));
96+
const result = await container.executeQuery("SELECT 1 as res");
97+
expect(result).toEqual(expect.stringContaining("res\n1\n"));
98+
// }
9899
});
99100

100101
it("should execute a query as root user", async () => {
@@ -108,7 +109,6 @@ describe("MySqlContainer", { timeout: 240_000 }, () => {
108109
const rootQueryResult = await container.executeQuery("SELECT CURRENT_USER() as user", [], true);
109110
expect(rootQueryResult).toEqual(expect.stringContaining("user\nroot"));
110111
});
111-
// }
112112

113113
it("should work with restarted container", async () => {
114114
await using container = await new MySqlContainer(IMAGE).start();

0 commit comments

Comments
 (0)