Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions constants/paths.ts
Original file line number Diff line number Diff line change
@@ -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";
15 changes: 10 additions & 5 deletions handlers/view_journal_entries.ts
Original file line number Diff line number Diff line change
@@ -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");
}
const _otherCtx = await conversation.wait();
await ctx.reply("Test");
}
8 changes: 7 additions & 1 deletion tests/dbutils_test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
8 changes: 7 additions & 1 deletion tests/entry_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
9 changes: 8 additions & 1 deletion tests/gad7_score_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down
8 changes: 7 additions & 1 deletion tests/journal_entry_photo_test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = {
Expand Down
8 changes: 7 additions & 1 deletion tests/journal_test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = {
Expand Down
56 changes: 31 additions & 25 deletions tests/migration_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
7 changes: 7 additions & 0 deletions tests/misc_test.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
8 changes: 7 additions & 1 deletion tests/phq9_score_test.ts
Original file line number Diff line number Diff line change
@@ -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 = {
Expand Down
8 changes: 7 additions & 1 deletion tests/settings_test.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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 = {
Expand Down
9 changes: 8 additions & 1 deletion utils/dbUtils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { PathLike } from "node:fs";
import { existsSync, PathLike } from "node:fs";
import {
createEntryTable,
createGadScoreTable,
Expand All @@ -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);
Expand Down