refactor: reorganize database tests into logical groups with descriptive names

main
borja (aider) 3 months ago
parent 284e35b0eb
commit 56e3486a84

@ -11,79 +11,94 @@ describe('Database', () => {
initializeDatabase(); initializeDatabase();
}); });
test('should create all required tables with correct schema', () => { describe('Table Creation', () => {
// Verify tables exist test('should create all required tables', () => {
const tables = db const tables = db
.query("SELECT name FROM sqlite_master WHERE type='table'") .query("SELECT name FROM sqlite_master WHERE type='table'")
.all() .all()
.map((t: any) => t.name); .map((t: any) => t.name);
const expectedTables = ['groups', 'task_assignments', 'tasks', 'users']; const expectedTables = ['groups', 'task_assignments', 'tasks', 'users'];
// Filter out system tables like sqlite_sequence
const userTables = tables.filter(t => !t.startsWith('sqlite_')); const userTables = tables.filter(t => !t.startsWith('sqlite_'));
expect(userTables.sort()).toEqual(expectedTables.sort()); expect(userTables.sort()).toEqual(expectedTables.sort());
});
});
// Verify users table schema describe('Table Schemas', () => {
const usersColumns = db test('users table should have correct columns', () => {
const columns = db
.query("PRAGMA table_info(users)") .query("PRAGMA table_info(users)")
.all() .all()
.map((c: any) => c.name); .map((c: any) => c.name);
expect(usersColumns).toEqual([ expect(columns).toEqual(['id', 'first_seen', 'last_seen']);
'id', });
'first_seen',
'last_seen'
]);
// Verify tasks table schema test('tasks table should have required columns', () => {
const tasksColumns = db const columns = db
.query("PRAGMA table_info(tasks)") .query("PRAGMA table_info(tasks)")
.all() .all()
.map((c: any) => c.name); .map((c: any) => c.name);
expect(tasksColumns).toContain('description'); expect(columns).toContain('description');
expect(tasksColumns).toContain('due_date'); expect(columns).toContain('due_date');
expect(tasksColumns).toContain('group_id'); expect(columns).toContain('group_id');
expect(tasksColumns).toContain('created_by'); expect(columns).toContain('created_by');
});
// Verify foreign key constraints test('groups table should have active flag default to true', () => {
const taskAssignmentsFk = db 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);
});
});
describe('Foreign Keys', () => {
test('task_assignments should reference tasks', () => {
const fkInfo = db
.query("PRAGMA foreign_key_list(task_assignments)") .query("PRAGMA foreign_key_list(task_assignments)")
.all(); .all();
expect(taskAssignmentsFk.length).toBe(1); expect(fkInfo.length).toBe(1);
expect(taskAssignmentsFk[0].from).toBe('task_id'); expect(fkInfo[0].from).toBe('task_id');
expect(taskAssignmentsFk[0].to).toBe('id'); expect(fkInfo[0].to).toBe('id');
expect(taskAssignmentsFk[0].table).toBe('tasks'); expect(fkInfo[0].table).toBe('tasks');
});
const tasksFk = db test('tasks should reference users via created_by', () => {
const fkInfo = db
.query("PRAGMA foreign_key_list(tasks)") .query("PRAGMA foreign_key_list(tasks)")
.all(); .all();
expect(tasksFk.length).toBe(1); expect(fkInfo.length).toBe(1);
expect(tasksFk[0].from).toBe('created_by'); expect(fkInfo[0].from).toBe('created_by');
expect(tasksFk[0].to).toBe('id'); expect(fkInfo[0].to).toBe('id');
expect(tasksFk[0].table).toBe('users'); expect(fkInfo[0].table).toBe('users');
});
});
// Test data operations describe('Data Operations', () => {
beforeEach(() => {
db.exec(` db.exec(`
INSERT INTO users (id) VALUES ('34650112233'); INSERT INTO users (id) VALUES ('34650112233');
INSERT INTO groups (id, community_id, name) INSERT INTO groups (id, community_id, name)
VALUES ('test-group', 'test-community', 'Test Group') VALUES ('test-group', 'test-community', 'Test Group')
`); `);
});
// Verify group defaults test('should allow inserting group tasks', () => {
const group = db.query("SELECT active FROM groups WHERE id = 'test-group'").get(); const result = db.prepare(`
expect(group.active).toBe(1);
// Verify task insertion with proper user reference
const taskResult = db.prepare(`
INSERT INTO tasks (description, created_by, group_id) INSERT INTO tasks (description, created_by, group_id)
VALUES ('Test task', '34650112233', 'test-group') VALUES ('Test task', '34650112233', 'test-group')
`).run(); `).run();
expect(taskResult.changes).toBe(1); expect(result.changes).toBe(1);
});
// Verify private task insertion (no group_id) test('should allow inserting private tasks', () => {
const privateTaskResult = db.prepare(` const result = db.prepare(`
INSERT INTO tasks (description, created_by) INSERT INTO tasks (description, created_by)
VALUES ('Private task', '34650112233') VALUES ('Private task', '34650112233')
`).run(); `).run();
expect(privateTaskResult.changes).toBe(1); expect(result.changes).toBe(1);
});
}); });
}); });

Loading…
Cancel
Save