Files
convert/test/db.test.js
T
2026-07-31 01:11:35 +02:00

45 lines
1.4 KiB
JavaScript

import { describe, it, expect, afterAll } from 'vitest';
import { getPool, closePool, buildDatabaseUrl } from '../src/db.js';
import { loadConfig } from '../src/config.js';
describe('buildDatabaseUrl', () => {
it('builds a mysql connection string from db config', () => {
const url = buildDatabaseUrl({
db: { host: '127.0.0.1', user: 'convert_user', password: 'change_me', database: 'file_converter' },
});
expect(url).toBe('mysql://convert_user:change_me@127.0.0.1:3306/file_converter?connection_limit=10');
});
it('percent-encodes special characters in user and password', () => {
const url = buildDatabaseUrl({
db: { host: '127.0.0.1', user: 'a@b', password: 'p@ss:word', database: 'file_converter' },
});
expect(url).toBe('mysql://a%40b:p%40ss%3Aword@127.0.0.1:3306/file_converter?connection_limit=10');
});
});
describe('getPool', () => {
afterAll(async () => {
await closePool();
});
it('returns a working pool that can run a query', async () => {
const config = loadConfig();
const pool = getPool(config);
const rows = await pool.query('SELECT 1 AS value');
expect(Number(rows[0].value)).toBe(1);
});
it('returns the same pool instance on repeated calls', () => {
const config = loadConfig();
const poolA = getPool(config);
const poolB = getPool(config);
expect(poolA).toBe(poolB);
});
});