-- unit_tests: individual test cases extracted from test files
CREATE TABLE unit_tests (
    id           TEXT PRIMARY KEY,
    function_id  TEXT NOT NULL REFERENCES functions(id) ON DELETE CASCADE,
    name         TEXT NOT NULL,
    code         TEXT NOT NULL DEFAULT '',
    file_path    TEXT NOT NULL DEFAULT '',
    lang         TEXT NOT NULL,
    created_at   TEXT NOT NULL,
    updated_at   TEXT NOT NULL
);

CREATE INDEX idx_unit_tests_function ON unit_tests(function_id);
CREATE INDEX idx_unit_tests_lang ON unit_tests(lang);

CREATE VIRTUAL TABLE unit_tests_fts USING fts5(
    id, name, code, function_id, lang,
    content='unit_tests',
    content_rowid='rowid'
);

CREATE TRIGGER unit_tests_ai AFTER INSERT ON unit_tests BEGIN
    INSERT INTO unit_tests_fts(rowid, id, name, code, function_id, lang)
    VALUES (new.rowid, new.id, new.name, new.code, new.function_id, new.lang);
END;

CREATE TRIGGER unit_tests_ad AFTER DELETE ON unit_tests BEGIN
    INSERT INTO unit_tests_fts(unit_tests_fts, rowid, id, name, code, function_id, lang)
    VALUES ('delete', old.rowid, old.id, old.name, old.code, old.function_id, old.lang);
END;

CREATE TRIGGER unit_tests_au AFTER UPDATE ON unit_tests BEGIN
    INSERT INTO unit_tests_fts(unit_tests_fts, rowid, id, name, code, function_id, lang)
    VALUES ('delete', old.rowid, old.id, old.name, old.code, old.function_id, old.lang);
    INSERT INTO unit_tests_fts(rowid, id, name, code, function_id, lang)
    VALUES (new.rowid, new.id, new.name, new.code, new.function_id, new.lang);
END;
