2026-01-13 00:35:53 -03:00
|
|
|
import 'package:drift/native.dart';
|
2026-01-22 00:14:05 -03:00
|
|
|
import 'package:sqlite3/sqlite3.dart';
|
2026-01-13 00:35:53 -03:00
|
|
|
import 'package:test/test.dart';
|
2026-01-22 00:14:05 -03:00
|
|
|
import 'package:test_descriptor/test_descriptor.dart';
|
|
|
|
|
|
|
|
|
|
/// Sets the test executor for the SQLite3 database.
|
|
|
|
|
///
|
|
|
|
|
/// This is a file-based SQLite3 executor that is used for testing. It follows
|
|
|
|
|
/// the same pattern as the `test_descriptor` package to ensure one executor is
|
|
|
|
|
/// created for each test. The executor is closed once test ends.
|
|
|
|
|
///
|
|
|
|
|
/// After setting the test executor, the database can be accessed using the
|
|
|
|
|
/// [testExecutor] getter.
|
|
|
|
|
void setTestFileExecutor([String fileName = 'test.db']) {
|
|
|
|
|
final databaseFile = file(fileName);
|
|
|
|
|
final sqliteDatabase = sqlite3.open(databaseFile.io.path);
|
2026-02-05 22:38:12 -03:00
|
|
|
final executor = NativeDatabase.opened(sqliteDatabase);
|
2026-01-22 00:14:05 -03:00
|
|
|
_setTestExecutor(executor);
|
|
|
|
|
}
|
2026-01-13 00:35:53 -03:00
|
|
|
|
|
|
|
|
/// The test executor for the SQLite3 database.
|
|
|
|
|
///
|
|
|
|
|
/// This is a memory-based SQLite3 executor that is used for testing. It follows
|
|
|
|
|
/// the same pattern as the `test_descriptor` package to ensure one executor is
|
|
|
|
|
/// created for each test. The executor is closed once test ends.
|
|
|
|
|
NativeDatabase get testExecutor {
|
|
|
|
|
if (_testExecutor != null) return _testExecutor!;
|
2026-02-05 22:38:12 -03:00
|
|
|
final executor = NativeDatabase.memory();
|
2026-01-22 00:14:05 -03:00
|
|
|
_setTestExecutor(executor);
|
|
|
|
|
return executor;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
NativeDatabase? _testExecutor;
|
|
|
|
|
|
|
|
|
|
void _setTestExecutor(NativeDatabase executor) {
|
2026-01-13 00:35:53 -03:00
|
|
|
_testExecutor = executor;
|
|
|
|
|
|
|
|
|
|
addTearDown(() async {
|
2026-01-22 00:14:05 -03:00
|
|
|
final executor = _testExecutor;
|
|
|
|
|
if (executor == null) return;
|
2026-01-13 00:35:53 -03:00
|
|
|
_testExecutor = null;
|
|
|
|
|
await executor.close();
|
|
|
|
|
});
|
|
|
|
|
}
|