Sub topic 16
Testing Queries Against an In-Memory Database
This chapter assumes that you know basic Dart tests, async and await, and that your AppDatabase class already contains generated drift code. It also assumes that your constructor accepts an optional QueryExecutor, as in the earlier chapters.
Database tests should check behavior rather than implementation details. For example, test that inserting an item makes it appear in a query. Do not test generated classes directly. Drift generates those classes from your table definitions.
Why use an in-memory database?
An in-memory database stores data only in RAM. It creates no database file, so each test can start with an empty database. This makes tests fast and prevents one test from affecting another.
| Database used in a test | Data survives after the test? | Best use |
|---|---|---|
| `NativeDatabase.memory()` | No | Isolated database logic tests |
| File-backed database | Yes | Migration and persistence tests |
The production database can still use a file. The test supplies a different executor to the same database class.
Create a fresh database for each test
Add database tests in a file such as test/database_test.dart. The test imports NativeDatabase.memory() and passes it to AppDatabase.
import 'package:drift/native.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:your_app/database.dart';
void main() {
late AppDatabase db;
setUp(() {
db = AppDatabase(NativeDatabase.memory());
});
tearDown(() async {
await db.close();
});
test('inserted items can be read back', () async {
await db.addItem('Write tests');
final items = await db.allItems();
expect(items, hasLength(1));
expect(items.single.title, 'Write tests');
expect(items.single.isDone, isFalse);
});
}setUp runs before each test. tearDown runs after each test, even when the test fails. Because a new in-memory executor is created for every test, the second test does not see rows inserted by the first test.
Test queries, filters, and updates
A useful test arranges data, performs one operation, and checks the result. If your database has the methods from earlier chapters, you can test them like this:
test('open-item query excludes completed items', () async {
final openId = await db.addItem('Open item');
final doneId = await db.addItem('Finished item');
final open = await db.select(db.todoItems).get();
final doneItem = open.singleWhere((item) => item.id == doneId);
await db.updateItem(doneItem.copyWith(isDone: true));
final results = await db.watchOpenItems().first;
expect(results.map((item) => item.id), contains(openId));
expect(results.map((item) => item.id), isNot(contains(doneId)));
});The first getter waits for the first value emitted by the query stream. For a one-time query, calling a method such as allItems() is usually simpler. Use stream assertions when you need to verify reactive behavior.
Keep tests independent
Avoid relying on test order or shared mutable database objects. Seed only the rows needed by the current test. Test boundary cases too, such as an empty table, multiple matching rows, and deletion of completed items.
Exercise
Why should each test create its own NativeDatabase.memory() instance?
Show answer
Each test then starts with isolated, predictable data and cannot pass or fail because of rows left by another test.