Skip to content

Commit

Permalink
Add MariaDB module (#851)
Browse files Browse the repository at this point in the history
  • Loading branch information
stscoundrel authored Nov 2, 2024
1 parent 08da47b commit 1930c36
Show file tree
Hide file tree
Showing 10 changed files with 395 additions and 0 deletions.
29 changes: 29 additions & 0 deletions docs/modules/mariadb.md
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-->
1 change: 1 addition & 0 deletions mkdocs.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ nav:
- HiveMQ: modules/hivemq.md
- Kafka: modules/kafka.md
- Localstack: modules/localstack.md
- MariaDB: modules/mariadb.md
- MongoDB: modules/mongodb.md
- MSSQLServer: modules/mssqlserver.md
- MySQL: modules/mysql.md
Expand Down
58 changes: 58 additions & 0 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

11 changes: 11 additions & 0 deletions packages/modules/mariadb/jest.config.ts
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;
37 changes: 37 additions & 0 deletions packages/modules/mariadb/package.json
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"
}
}
1 change: 1 addition & 0 deletions packages/modules/mariadb/src/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export { MariaDbContainer, StartedMariaDbContainer } from "./mariadb-container";
128 changes: 128 additions & 0 deletions packages/modules/mariadb/src/mariadb-container.test.ts
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();
});
// }
});
Loading

0 comments on commit 1930c36

Please sign in to comment.