Files
khb_microservices/packages/khb_common/src/migrations.int.test.ts
Yuri Yaryshev 9e4b039e5e
All checks were successful
ci / test (push) Successful in 25s
ci / deploy (push) Successful in 1m46s
person: дополнительные почты (additional_emails) и уникальность основной почты
Миграция 012: колонка additional_emails (почты через ";") и уникальный индекс по
lower(email); блоки условные, на сервере (таблица принадлежит роли Mathesar) уже
созданное не трогают. Документация person: команда внесена в таблицу.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-15 17:56:01 +03:00

55 lines
3.1 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

import { afterAll, beforeAll, describe, expect, it } from "vitest";
import { createTestDb, testDatabaseUrl, type TestDb } from "./testing/index.js";
const url = testDatabaseUrl();
const d = url ? describe : describe.skip;
d("миграции (временная БД)", () => {
let db: TestDb;
beforeAll(async () => {
db = await createTestDb(url!);
});
afterAll(async () => {
await db?.drop();
});
it("создают все таблицы, двойники _hist и представление", async () => {
const { rows } = await db.pool.query("select table_name from information_schema.tables where table_schema = 'public'");
const names = new Set(rows.map((r) => r.table_name as string));
for (const t of [
"jt_job", "jt_job_hist", "jt_task", "jt_task_hist", "jt_task_dependency_hist", "jt_task_assignment_hist",
"sync_source", "sync_snapshot", "sync_table_status", "purchase_task_ref", "calendar_day", "person",
"construction_manager", "assistant_project", "reminder_history", "sched_run", "ap_input_package",
"ap_input_package_item", "agent_run", "agent_manager_memory", "manager_day_list", "agent_problem_log", "slack_message_log", "v_agent_input",
])
expect(names.has(t), t).toBe(true);
});
it("справочник закупок засеян 9 реальными названиями", async () => {
const { rows } = await db.pool.query("select count(*)::int as n from purchase_task_ref");
expect(rows[0].n).toBe(9);
});
it("у всех таблиц включён RLS", async () => {
const { rows } = await db.pool.query(
"select relname from pg_class c join pg_namespace n on n.oid = c.relnamespace where n.nspname = 'public' and c.relkind = 'r' and not c.relrowsecurity and relname <> 'schema_migrations'",
);
expect(rows).toEqual([]);
});
it("person: основная почта и дополнительные через \";\", почта уникальна без учёта регистра", async () => {
await db.pool.query("insert into person (display_name, email, additional_emails) values ('Yuri', 'Yuri@khbuild.pro', 'yuyaryshev@gmail.com;second@example.com')");
const { rows } = await db.pool.query("select additional_emails from person where lower(email) = 'yuri@khbuild.pro'");
expect(rows[0].additional_emails.split(";")).toEqual(["yuyaryshev@gmail.com", "second@example.com"]);
await expect(db.pool.query("insert into person (display_name, email) values ('Dup', 'YURI@khbuild.pro')")).rejects.toThrow(/person_email_lower_key/);
await db.pool.query("insert into person (display_name) values ('Без почты 1'), ('Без почты 2')"); // null-почта не конфликтует
await db.pool.query("delete from person where display_name in ('Yuri', 'Без почты 1', 'Без почты 2')");
});
it("sched_run: повторный старт в ту же минуту невозможен", async () => {
const q = "insert into sched_run (job_name, scheduled_minute, status) values ('j', '2026-10-11T10:00:00Z', 'started')";
await db.pool.query(q);
await expect(db.pool.query(q)).rejects.toThrow(/duplicate key/);
});
});