|
|
|
@ -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);
|
|
|
|
|
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'];
|
|
|
|
|
// Filter out system tables like sqlite_sequence
|
|
|
|
|
const userTables = tables.filter(t => !t.startsWith('sqlite_'));
|
|
|
|
|
expect(userTables.sort()).toEqual(expectedTables.sort());
|
|
|
|
|
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);
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|
});
|
|
|
|
|