|
| 1 | +import { Database } from 'bun:sqlite' |
| 2 | +import { expect, it } from 'bun:test' |
| 3 | +import { mkdtempSync, rmSync } from 'node:fs' |
| 4 | +import { tmpdir } from 'node:os' |
| 5 | +import { join } from 'node:path' |
| 6 | +import { createFixture, FIXTURE_ROWS, resetFixtureLogs } from './fixture' |
| 7 | + |
| 8 | +it('supports real query logging in the isolated benchmark database', async () => { |
| 9 | + const dir = mkdtempSync(join(tmpdir(), 'stacks-routing-fixture-')) |
| 10 | + const file = join(dir, 'bench.sqlite') |
| 11 | + try { |
| 12 | + createFixture(file) |
| 13 | + const child = Bun.spawn([ |
| 14 | + process.execPath, |
| 15 | + `--config=${join(import.meta.dir, 'bunfig.toml')}`, |
| 16 | + join(import.meta.dir, 'fixtures/query-logging.ts'), |
| 17 | + file, |
| 18 | + ], { |
| 19 | + env: { ...process.env, APP_ENV: 'test', DB_CONNECTION: 'sqlite', DB_DATABASE_PATH: file, DB_QUERY_LOGGING_ENABLED: 'true' }, |
| 20 | + stdout: 'pipe', |
| 21 | + stderr: 'pipe', |
| 22 | + }) |
| 23 | + const [exitCode, stdout, stderr] = await Promise.all([ |
| 24 | + child.exited, |
| 25 | + new Response(child.stdout).text(), |
| 26 | + new Response(child.stderr).text(), |
| 27 | + ]) |
| 28 | + expect(exitCode, stderr).toBe(0) |
| 29 | + expect(stdout).toContain('fixture-query-logging-ok') |
| 30 | + |
| 31 | + const db = new Database(file) |
| 32 | + try { |
| 33 | + expect(db.query('SELECT COUNT(*) AS count FROM bench_items').get()).toEqual({ count: FIXTURE_ROWS }) |
| 34 | + expect(db.query('SELECT status FROM query_logs').all()).toEqual([{ status: 'completed' }]) |
| 35 | + } |
| 36 | + finally { |
| 37 | + db.close() |
| 38 | + } |
| 39 | + resetFixtureLogs(file) |
| 40 | + const reset = new Database(file) |
| 41 | + try { |
| 42 | + expect(reset.query('SELECT COUNT(*) AS count FROM query_logs').get()).toEqual({ count: 0 }) |
| 43 | + expect(reset.query('SELECT COUNT(*) AS count FROM bench_items').get()).toEqual({ count: FIXTURE_ROWS }) |
| 44 | + expect(reset.query('SELECT * FROM bench_items WHERE id = 1').get()).toEqual({ id: 1, name: 'item-1' }) |
| 45 | + expect(reset.query("SELECT name FROM sqlite_master WHERE type = 'index' AND tbl_name = 'query_logs' ORDER BY name").all()).toEqual([ |
| 46 | + { name: 'query_logs_duration_index' }, |
| 47 | + { name: 'query_logs_executed_at_index' }, |
| 48 | + { name: 'query_logs_status_index' }, |
| 49 | + ]) |
| 50 | + } |
| 51 | + finally { |
| 52 | + reset.close() |
| 53 | + } |
| 54 | + } |
| 55 | + finally { |
| 56 | + rmSync(dir, { recursive: true, force: true }) |
| 57 | + } |
| 58 | +}) |
0 commit comments