diff --git a/constants/paths.ts b/constants/paths.ts index 5a1b9b6..1939309 100644 --- a/constants/paths.ts +++ b/constants/paths.ts @@ -1,4 +1,6 @@ -export const dbFile: string = `db/prod_db/jotbot.db`; -export const testDbFile: string = `db/test_db/jotbot_test.db`; +export const dbFileBasePath = `db/prod_db`; +export const dbFile: string = `${dbFileBasePath}/jotbot.db`; +export const testDbFileBasePath = `db/test_db`; +export const testDbFile: string = `${testDbFileBasePath}/jotbot_test.db`; export const selfieDirPath: string = `${Deno.cwd()}/assets/selfies`; export const sqlFilePath = "db/sql"; diff --git a/handlers/view_journal_entries.ts b/handlers/view_journal_entries.ts index 6e4289c..3b92fec 100644 --- a/handlers/view_journal_entries.ts +++ b/handlers/view_journal_entries.ts @@ -1,9 +1,14 @@ import { Conversation } from "@grammyjs/conversations"; import { Context, InlineKeyboard } from "grammy"; -export async function view_journal_entries(conversation: Conversation, ctx: Context) { - await ctx.reply('Buttons!', {reply_markup: new InlineKeyboard().text("Add beans")}); +export async function view_journal_entries( + conversation: Conversation, + ctx: Context, +) { + await ctx.reply("Buttons!", { + reply_markup: new InlineKeyboard().text("Add beans"), + }); - const otherCtx = await conversation.wait(); - await ctx.reply("Tits"); -} \ No newline at end of file + const _otherCtx = await conversation.wait(); + await ctx.reply("Test"); +} diff --git a/tests/dbutils_test.ts b/tests/dbutils_test.ts index 7e117e2..b741d50 100644 --- a/tests/dbutils_test.ts +++ b/tests/dbutils_test.ts @@ -1,9 +1,15 @@ import { assertEquals } from "@std/assert/equals"; -import { testDbFile } from "../constants/paths.ts"; +import { testDbFile, testDbFileBasePath } from "../constants/paths.ts"; import { createUserTable } from "../db/migration.ts"; import { insertUser } from "../models/user.ts"; import { User } from "../types/types.ts"; import { getLatestId } from "../utils/dbUtils.ts"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} Deno.test("Test getLatestId()", () => { const testUser: User = { diff --git a/tests/entry_test.ts b/tests/entry_test.ts index b08234f..88aaca3 100644 --- a/tests/entry_test.ts +++ b/tests/entry_test.ts @@ -9,8 +9,14 @@ import { insertEntry, updateEntry, } from "../models/entry.ts"; -import { testDbFile } from "../constants/paths.ts"; +import { testDbFile, testDbFileBasePath } from "../constants/paths.ts"; import { assertObjectMatch } from "@std/assert/object-match"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} // Create test entry const testEntry: Entry = { diff --git a/tests/gad7_score_test.ts b/tests/gad7_score_test.ts index 4a17564..a0621ae 100644 --- a/tests/gad7_score_test.ts +++ b/tests/gad7_score_test.ts @@ -3,7 +3,14 @@ import { getGadScoreById, insertGadScore } from "../models/gad7_score.ts"; import { AnxietySeverity, GAD7Score, User } from "../types/types.ts"; import { createGadScoreTable, createUserTable } from "../db/migration.ts"; import { insertUser } from "../models/user.ts"; -import { testDbFile } from "../constants/paths.ts"; +import { testDbFile, testDbFileBasePath } from "../constants/paths.ts"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} + // Create test user const testUser: User = { telegramId: 12345, diff --git a/tests/journal_entry_photo_test.ts b/tests/journal_entry_photo_test.ts index e5298a8..8b22d0d 100644 --- a/tests/journal_entry_photo_test.ts +++ b/tests/journal_entry_photo_test.ts @@ -1,5 +1,5 @@ import { assertEquals } from "@std/assert/equals"; -import { testDbFile } from "../constants/paths.ts"; +import { testDbFile, testDbFileBasePath } from "../constants/paths.ts"; import { createJournalEntryPhotosTable, createJournalTable, @@ -9,6 +9,12 @@ import { insertJournalEntry } from "../models/journal.ts"; import { insertJournalEntryPhoto } from "../models/journal_entry_photo.ts"; import { insertUser } from "../models/user.ts"; import { JournalEntry, JournalEntryPhoto, User } from "../types/types.ts"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} // Create test user const testUser: User = { diff --git a/tests/journal_test.ts b/tests/journal_test.ts index 0389af1..d8b64c3 100644 --- a/tests/journal_test.ts +++ b/tests/journal_test.ts @@ -1,5 +1,5 @@ import { assertEquals } from "@std/assert/equals"; -import { testDbFile } from "../constants/paths.ts"; +import { testDbFile, testDbFileBasePath } from "../constants/paths.ts"; import { createJournalTable, createUserTable } from "../db/migration.ts"; import { deleteJournalEntryById, @@ -11,6 +11,12 @@ import { import { insertUser } from "../models/user.ts"; import { JournalEntry, User } from "../types/types.ts"; import { assertObjectMatch } from "@std/assert/object-match"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} // Create test journal entry const testJournalEntry: JournalEntry = { diff --git a/tests/migration_test.ts b/tests/migration_test.ts index daf668f..287290e 100644 --- a/tests/migration_test.ts +++ b/tests/migration_test.ts @@ -11,123 +11,129 @@ import { } from "../db/migration.ts"; import { assertNotEquals } from "@std/assert/not-equals"; import { assertEquals } from "@std/assert/equals"; +import { testDbFile, testDbFileBasePath } from "../constants/paths.ts"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} -const testDbPath = "db/test_db/jotbot_test.db"; Deno.test("Test createEntryTable()", () => { // Create test entry db - createEntryTable(testDbPath); + createEntryTable(testDbFile); // Get table - const db = new DatabaseSync(testDbPath); + const db = new DatabaseSync(testDbFile); const table = db.prepare( `SELECT name FROM sqlite_master WHERE type ='table' AND name = 'entry_db';`, ).get(); assertNotEquals(table, undefined); assertEquals(table?.name, "entry_db"); - Deno.removeSync(testDbPath); + Deno.removeSync(testDbFile); }); Deno.test("Test createGadScoreTable()", () => { // Create test gad score table - createGadScoreTable(testDbPath); + createGadScoreTable(testDbFile); // Get the table info from the table - const db = new DatabaseSync(testDbPath); + const db = new DatabaseSync(testDbFile); const table = db.prepare( `SELECT name FROM sqlite_master WHERE type ='table' AND name = 'gad_score_db';`, ).get(); assertNotEquals(table, undefined); assertEquals(table?.name, "gad_score_db"); - Deno.removeSync(testDbPath); + Deno.removeSync(testDbFile); }); Deno.test("Test createPhqScoreTable()", () => { // Create test gad score table - createPhqScoreTable(testDbPath); + createPhqScoreTable(testDbFile); // Get the table info from the table - const db = new DatabaseSync(testDbPath); + const db = new DatabaseSync(testDbFile); const table = db.prepare( `SELECT name FROM sqlite_master WHERE type ='table' AND name = 'phq_score_db';`, ).get(); assertNotEquals(table, undefined); assertEquals(table?.name, "phq_score_db"); - Deno.removeSync(testDbPath); + Deno.removeSync(testDbFile); }); Deno.test("Test createUserTable()", () => { // Create test gad score table - createUserTable(testDbPath); + createUserTable(testDbFile); // Get the table info from the table - const db = new DatabaseSync(testDbPath); + const db = new DatabaseSync(testDbFile); const table = db.prepare( `SELECT name FROM sqlite_master WHERE type ='table' AND name = 'user_db';`, ).get(); assertNotEquals(table, undefined); assertEquals(table?.name, "user_db"); - Deno.removeSync(testDbPath); + Deno.removeSync(testDbFile); }); Deno.test("Test createSettingsTable()", () => { // Create test gad score table - createSettingsTable(testDbPath); + createSettingsTable(testDbFile); // Get the table info from the table - const db = new DatabaseSync(testDbPath); + const db = new DatabaseSync(testDbFile); const table = db.prepare( `SELECT name FROM sqlite_master WHERE type ='table' AND name = 'settings_db';`, ).get(); assertNotEquals(table, undefined); assertEquals(table?.name, "settings_db"); - Deno.removeSync(testDbPath); + Deno.removeSync(testDbFile); }); Deno.test("Test createJournalTable()", () => { // Create test gad score table - createJournalTable(testDbPath); + createJournalTable(testDbFile); // Get the table info from the table - const db = new DatabaseSync(testDbPath); + const db = new DatabaseSync(testDbFile); const table = db.prepare( `SELECT name FROM sqlite_master WHERE type ='table' AND name = 'journal_db';`, ).get(); assertNotEquals(table, undefined); assertEquals(table?.name, "journal_db"); - Deno.removeSync(testDbPath); + Deno.removeSync(testDbFile); }); Deno.test("Test createJournalEntryPhotosTable()", () => { // Create test gad score table - createJournalEntryPhotosTable(testDbPath); + createJournalEntryPhotosTable(testDbFile); // Get the table info from the table - const db = new DatabaseSync(testDbPath); + const db = new DatabaseSync(testDbFile); const table = db.prepare( `SELECT name FROM sqlite_master WHERE type ='table' AND name = 'photo_db';`, ).get(); assertNotEquals(table, undefined); assertEquals(table?.name, "photo_db"); - Deno.removeSync(testDbPath); + Deno.removeSync(testDbFile); }); Deno.test("Test createVoiceRecordingTable()", () => { - createVoiceRecordingTable(testDbPath); + createVoiceRecordingTable(testDbFile); // Get the table info from the table - const db = new DatabaseSync(testDbPath); + const db = new DatabaseSync(testDbFile); const table = db.prepare( `SELECT name FROM sqlite_master WHERE type ='table' AND name = 'voice_recording_db';`, ).get(); assertNotEquals(table, undefined); assertEquals(table?.name, "voice_recording_db"); - Deno.removeSync(testDbPath); + Deno.removeSync(testDbFile); }); diff --git a/tests/misc_test.ts b/tests/misc_test.ts index 62f8bc6..a307ed2 100644 --- a/tests/misc_test.ts +++ b/tests/misc_test.ts @@ -1,6 +1,13 @@ import { assertEquals, assertObjectMatch } from "@std/assert"; import { entryFromString, entryToString } from "../utils/misc.ts"; import { Entry } from "../types/types.ts"; +import { testDbFileBasePath } from "../constants/paths.ts"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} Deno.test("Test entryFromString()", () => { const testEntryString = `Page 1 of 15 diff --git a/tests/phq9_score_test.ts b/tests/phq9_score_test.ts index 93dcf64..73dfad2 100644 --- a/tests/phq9_score_test.ts +++ b/tests/phq9_score_test.ts @@ -1,10 +1,16 @@ import { assertEquals } from "@std/assert/equals"; -import { testDbFile } from "../constants/paths.ts"; +import { testDbFile, testDbFileBasePath } from "../constants/paths.ts"; import { createPhqScoreTable, createUserTable } from "../db/migration.ts"; import { getPhqScoreByUserId, insertPhqScore } from "../models/phq9_score.ts"; import { insertUser } from "../models/user.ts"; import { DepressionSeverity, PHQ9Score, User } from "../types/types.ts"; import { assertObjectMatch } from "@std/assert/object-match"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} // Create test user const testUser: User = { diff --git a/tests/settings_test.ts b/tests/settings_test.ts index 82abc79..75c969e 100644 --- a/tests/settings_test.ts +++ b/tests/settings_test.ts @@ -1,5 +1,5 @@ import { assertEquals } from "@std/assert/equals"; -import { testDbFile } from "../constants/paths.ts"; +import { testDbFile, testDbFileBasePath } from "../constants/paths.ts"; import { createSettingsTable, createUserTable } from "../db/migration.ts"; import { getSettingsById, @@ -10,6 +10,12 @@ import { insertUser } from "../models/user.ts"; import { User } from "../types/types.ts"; import { Settings } from "../types/types.ts"; import { assertObjectMatch } from "@std/assert/object-match"; +import { existsSync } from "node:fs"; + +// Create test db directory structure +if (!existsSync(testDbFileBasePath)) { + Deno.mkdirSync(testDbFileBasePath, { recursive: true }); +} // Create test user const testUser: User = { diff --git a/utils/dbUtils.ts b/utils/dbUtils.ts index 1553f82..11aaa42 100644 --- a/utils/dbUtils.ts +++ b/utils/dbUtils.ts @@ -1,4 +1,4 @@ -import { PathLike } from "node:fs"; +import { existsSync, PathLike } from "node:fs"; import { createEntryTable, createGadScoreTable, @@ -7,12 +7,19 @@ import { createUserTable, } from "../db/migration.ts"; import { DatabaseSync } from "node:sqlite"; +import { dbFileBasePath } from "../constants/paths.ts"; /** * @param dbFile */ export function createDatabase(dbFile: PathLike) { try { + // Create db directory structure + if (!existsSync(dbFileBasePath)) { + Deno.mkdirSync(dbFileBasePath, { recursive: true }); + } + + // Create db createUserTable(dbFile); createGadScoreTable(dbFile); createPhqScoreTable(dbFile);