From 56e3486a8450a250bc040e22f3cc7d32b5516ff3 Mon Sep 17 00:00:00 2001 From: "borja (aider)" Date: Mon, 31 Mar 2025 14:08:10 +0200 Subject: [PATCH] refactor: reorganize database tests into logical groups with descriptive names --- tests/unit/db.test.ts | 147 +++++++++++++++++++++++------------------- 1 file changed, 81 insertions(+), 66 deletions(-) diff --git a/tests/unit/db.test.ts b/tests/unit/db.test.ts index 8d16565..9412a60 100644 --- a/tests/unit/db.test.ts +++ b/tests/unit/db.test.ts @@ -11,79 +11,94 @@ describe('Database', () => { initializeDatabase(); }); - test('should create all required tables with correct schema', () => { - // Verify tables exist - const tables = db - .query("SELECT name FROM sqlite_master WHERE type='table'") - .all() - .map((t: any) => t.name); - - const expectedTables = ['groups', 'task_assignments', 'tasks', 'users']; - // Filter out system tables like sqlite_sequence - const userTables = tables.filter(t => !t.startsWith('sqlite_')); - expect(userTables.sort()).toEqual(expectedTables.sort()); + describe('Table Creation', () => { + test('should create all required tables', () => { + const tables = db + .query("SELECT name FROM sqlite_master WHERE type='table'") + .all() + .map((t: any) => t.name); + + const expectedTables = ['groups', 'task_assignments', 'tasks', 'users']; + const userTables = tables.filter(t => !t.startsWith('sqlite_')); + expect(userTables.sort()).toEqual(expectedTables.sort()); + }); + }); - // Verify users table schema - const usersColumns = db - .query("PRAGMA table_info(users)") - .all() - .map((c: any) => c.name); - expect(usersColumns).toEqual([ - 'id', - 'first_seen', - 'last_seen' - ]); + describe('Table Schemas', () => { + test('users table should have correct columns', () => { + const columns = db + .query("PRAGMA table_info(users)") + .all() + .map((c: any) => c.name); + expect(columns).toEqual(['id', 'first_seen', 'last_seen']); + }); - // Verify tasks table schema - const tasksColumns = db - .query("PRAGMA table_info(tasks)") - .all() - .map((c: any) => c.name); - expect(tasksColumns).toContain('description'); - expect(tasksColumns).toContain('due_date'); - expect(tasksColumns).toContain('group_id'); - expect(tasksColumns).toContain('created_by'); + test('tasks table should have required columns', () => { + const columns = db + .query("PRAGMA table_info(tasks)") + .all() + .map((c: any) => c.name); + expect(columns).toContain('description'); + expect(columns).toContain('due_date'); + expect(columns).toContain('group_id'); + expect(columns).toContain('created_by'); + }); - // Verify foreign key constraints - const taskAssignmentsFk = db - .query("PRAGMA foreign_key_list(task_assignments)") - .all(); - expect(taskAssignmentsFk.length).toBe(1); - expect(taskAssignmentsFk[0].from).toBe('task_id'); - expect(taskAssignmentsFk[0].to).toBe('id'); - expect(taskAssignmentsFk[0].table).toBe('tasks'); + test('groups table should have active flag default to true', () => { + db.exec(` + INSERT INTO groups (id, community_id, name) + VALUES ('test-group', 'test-community', 'Test Group') + `); + const group = db.query("SELECT active FROM groups WHERE id = 'test-group'").get(); + expect(group.active).toBe(1); + }); + }); - const tasksFk = db - .query("PRAGMA foreign_key_list(tasks)") - .all(); - expect(tasksFk.length).toBe(1); - expect(tasksFk[0].from).toBe('created_by'); - expect(tasksFk[0].to).toBe('id'); - expect(tasksFk[0].table).toBe('users'); + describe('Foreign Keys', () => { + test('task_assignments should reference tasks', () => { + const fkInfo = db + .query("PRAGMA foreign_key_list(task_assignments)") + .all(); + expect(fkInfo.length).toBe(1); + expect(fkInfo[0].from).toBe('task_id'); + expect(fkInfo[0].to).toBe('id'); + expect(fkInfo[0].table).toBe('tasks'); + }); - // Test data operations - db.exec(` - INSERT INTO users (id) VALUES ('34650112233'); - INSERT INTO groups (id, community_id, name) - VALUES ('test-group', 'test-community', 'Test Group') - `); + test('tasks should reference users via created_by', () => { + const fkInfo = db + .query("PRAGMA foreign_key_list(tasks)") + .all(); + expect(fkInfo.length).toBe(1); + expect(fkInfo[0].from).toBe('created_by'); + expect(fkInfo[0].to).toBe('id'); + expect(fkInfo[0].table).toBe('users'); + }); + }); - // Verify group defaults - const group = db.query("SELECT active FROM groups WHERE id = 'test-group'").get(); - expect(group.active).toBe(1); + describe('Data Operations', () => { + beforeEach(() => { + db.exec(` + INSERT INTO users (id) VALUES ('34650112233'); + INSERT INTO groups (id, community_id, name) + VALUES ('test-group', 'test-community', 'Test Group') + `); + }); - // Verify task insertion with proper user reference - const taskResult = db.prepare(` - INSERT INTO tasks (description, created_by, group_id) - VALUES ('Test task', '34650112233', 'test-group') - `).run(); - expect(taskResult.changes).toBe(1); + test('should allow inserting group tasks', () => { + const result = db.prepare(` + INSERT INTO tasks (description, created_by, group_id) + VALUES ('Test task', '34650112233', 'test-group') + `).run(); + expect(result.changes).toBe(1); + }); - // Verify private task insertion (no group_id) - const privateTaskResult = db.prepare(` - INSERT INTO tasks (description, created_by) - VALUES ('Private task', '34650112233') - `).run(); - expect(privateTaskResult.changes).toBe(1); + test('should allow inserting private tasks', () => { + const result = db.prepare(` + INSERT INTO tasks (description, created_by) + VALUES ('Private task', '34650112233') + `).run(); + expect(result.changes).toBe(1); + }); }); });