Initial release 0.1.0
This commit is contained in:
commit
9f97d8407a
52 files changed
+6774
No files matched your search
@@ -0,0 +1,77 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { execFileSync } from 'node:child_process';
|
||||
import { mkdtempSync, rmSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import path from 'node:path';
|
||||
import { afterEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const temporaryDirectories: string[] = [];
|
||||
|
||||
afterEach(async () => {
|
||||
const { closeDatabase } = await import('./database.js');
|
||||
closeDatabase();
|
||||
delete process.env.SENDEWERK_DB_PATH;
|
||||
delete process.env.SENDEWERK_UPLOAD_DIR;
|
||||
for (const directory of temporaryDirectories.splice(0)) {
|
||||
rmSync(directory, { recursive: true, force: true });
|
||||
}
|
||||
vi.resetModules();
|
||||
});
|
||||
|
||||
describe('database setup', () => {
|
||||
it('runs migrations and the seed repeatedly in an isolated database', () => {
|
||||
const directory = mkdtempSync(path.join(tmpdir(), 'sendewerk-test-'));
|
||||
temporaryDirectories.push(directory);
|
||||
const databasePath = path.join(directory, 'test.sqlite');
|
||||
const environment = {
|
||||
...process.env,
|
||||
SENDEWERK_DB_PATH: databasePath,
|
||||
SENDEWERK_UPLOAD_DIR: path.join(directory, 'uploads')
|
||||
};
|
||||
|
||||
for (let run = 0; run < 2; run += 1) {
|
||||
execFileSync(process.execPath, ['scripts/migrate.mjs'], {
|
||||
cwd: process.cwd(),
|
||||
env: environment
|
||||
});
|
||||
execFileSync(process.execPath, ['scripts/seed.mjs'], {
|
||||
cwd: process.cwd(),
|
||||
env: environment
|
||||
});
|
||||
}
|
||||
|
||||
const database = new Database(databasePath, { readonly: true });
|
||||
const counts = database
|
||||
.prepare(
|
||||
`SELECT
|
||||
(SELECT count(*) FROM schema_migrations) AS migrations,
|
||||
(SELECT count(*) FROM pieces) AS pieces,
|
||||
(SELECT count(*) FROM shows) AS shows,
|
||||
(SELECT count(*) FROM piece_usages) AS usages`
|
||||
)
|
||||
.get() as { migrations: number; pieces: number; shows: number; usages: number };
|
||||
database.close();
|
||||
|
||||
expect(counts).toEqual({ migrations: 1, pieces: 4, shows: 1, usages: 3 });
|
||||
expect(databasePath).not.toBe(path.resolve('.data/sendewerk.sqlite'));
|
||||
});
|
||||
|
||||
it('does not count reserve pieces or explicit sections twice', async () => {
|
||||
const directory = mkdtempSync(path.join(tmpdir(), 'sendewerk-duration-test-'));
|
||||
temporaryDirectories.push(directory);
|
||||
process.env.SENDEWERK_DB_PATH = path.join(directory, 'test.sqlite');
|
||||
process.env.SENDEWERK_UPLOAD_DIR = path.join(directory, 'uploads');
|
||||
|
||||
execFileSync(process.execPath, ['scripts/migrate.mjs'], {
|
||||
cwd: process.cwd(),
|
||||
env: process.env
|
||||
});
|
||||
execFileSync(process.execPath, ['scripts/seed.mjs'], { cwd: process.cwd(), env: process.env });
|
||||
const { getShow } = await import('./previews.js');
|
||||
const show = getShow();
|
||||
|
||||
expect(show.sections.map((section) => section.durationSeconds)).toEqual([300, 1200, 3600]);
|
||||
expect(show.totalDurationSeconds).toBe(5100);
|
||||
expect(show.sections[1].usages.find((usage) => usage.isReserve)?.durationSeconds).toBe(540);
|
||||
});
|
||||
});
|
||||
Reference in new issue
Block a user