70 lines
2.2 KiB
Dart
70 lines
2.2 KiB
Dart
import 'package:drift/native.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
import 'package:timetrack/core/database/app_database.dart';
|
|
import '../../helpers/sqlite_test_helper.dart';
|
|
|
|
AppDatabase createTestDb() => AppDatabase(NativeDatabase.memory());
|
|
|
|
void main() {
|
|
setUpAll(configureSqliteForTests);
|
|
late AppDatabase db;
|
|
|
|
setUp(() => db = createTestDb());
|
|
tearDown(() => db.close());
|
|
|
|
group('AppDatabase', () {
|
|
test('schema creates without error', () async {
|
|
// Simply opening the DB triggers onCreate migration
|
|
final projects = await db.projectsDao.watchAll().first;
|
|
expect(projects, isEmpty);
|
|
});
|
|
});
|
|
|
|
group('ProjectsDao', () {
|
|
test('insert and retrieve a project', () async {
|
|
await db.projectsDao.insertProject(
|
|
ProjectsCompanion.insert(name: 'Test Project', colorValue: 0xFF2563EB),
|
|
);
|
|
|
|
final all = await db.projectsDao.watchAll().first;
|
|
expect(all, hasLength(1));
|
|
expect(all.first.name, equals('Test Project'));
|
|
expect(all.first.colorValue, equals(0xFF2563EB));
|
|
});
|
|
|
|
test('watchActive excludes archived projects', () async {
|
|
await db.projectsDao.insertProject(
|
|
ProjectsCompanion.insert(name: 'Active', colorValue: 0xFF00FF00),
|
|
);
|
|
final id = await db.projectsDao.insertProject(
|
|
ProjectsCompanion.insert(name: 'Archived', colorValue: 0xFFFF0000),
|
|
);
|
|
await db.projectsDao.archiveProject(id);
|
|
|
|
final active = await db.projectsDao.watchActive().first;
|
|
expect(active, hasLength(1));
|
|
expect(active.first.name, equals('Active'));
|
|
});
|
|
|
|
test('archiveProject sets archivedAt', () async {
|
|
final id = await db.projectsDao.insertProject(
|
|
ProjectsCompanion.insert(name: 'ToArchive', colorValue: 0xFF000000),
|
|
);
|
|
await db.projectsDao.archiveProject(id);
|
|
|
|
final project = await db.projectsDao.getById(id);
|
|
expect(project?.archivedAt, isNotNull);
|
|
});
|
|
|
|
test('deleteProject removes the row', () async {
|
|
final id = await db.projectsDao.insertProject(
|
|
ProjectsCompanion.insert(name: 'ToDelete', colorValue: 0xFF000000),
|
|
);
|
|
await db.projectsDao.deleteProject(id);
|
|
|
|
final all = await db.projectsDao.watchAll().first;
|
|
expect(all, isEmpty);
|
|
});
|
|
});
|
|
}
|