API to close databases, integration test that saves data

This commit is contained in:
Simon Binder 2019-07-25 17:04:32 +02:00
parent 7327e1f8b1
commit 4bf58cb83b
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
6 changed files with 40 additions and 0 deletions

View File

@ -121,6 +121,10 @@ class Database extends _$Database {
return (select(users)..where((u) => u.id.equals(id))).watchSingle();
}
Future<int> writeUser(Insertable<User> user) {
return into(users).insert(user);
}
Future<void> makeFriends(User a, User b, {bool goodFriends}) async {
var friendsValue = const Value<bool>.absent();
if (goodFriends != null) {

View File

@ -1,4 +1,5 @@
import 'package:test/test.dart';
import 'package:tests/data/sample_data.dart';
import 'package:tests/database/database.dart';
import 'suite.dart';
@ -11,4 +12,16 @@ void migrationTests(TestExecutor executor) {
final count = await database.userCount();
expect(count.single.cOUNTid, 3);
});
test('saves and restores database', () async {
var database = Database(executor.createExecutor(), schemaVersion: 1);
await database.writeUser(People.florian);
await database.close();
database = Database(executor.createExecutor(), schemaVersion: 2);
// the 3 initial users plus People.florian
final count = await database.userCount();
expect(count.single.cOUNTid, 4);
});
}

View File

@ -331,4 +331,9 @@ abstract class GeneratedDatabase extends DatabaseConnectionUser
});
}
}
/// Closes this database and releases associated resources.
Future<void> close() async {
await executor.close();
}
}

View File

@ -48,6 +48,13 @@ abstract class QueryExecutor {
/// Starts a [TransactionExecutor].
TransactionExecutor beginTransaction();
/// Closes this database connection and releases all resources associated with
/// it. Implementations should also handle [close] calls in a state where the
/// database isn't open.
Future<void> close() async {
// no-op per default for backwards compatibility
}
}
/// A statement that should be executed in a batch. Used internally by moor.

View File

@ -101,6 +101,10 @@ class SqlJsDatabase {
Uint8List export() {
return _obj.callMethod('export') as Uint8List;
}
void close() {
_obj.callMethod('close');
}
}
class PreparedStatement {

View File

@ -87,6 +87,13 @@ class _WebDelegate extends DatabaseDelegate {
return _handlePotentialUpdate();
}
@override
Future<void> close() {
_storeDb();
_db?.close();
return Future.value();
}
/// Saves the database if the last statement changed rows. As a side-effect,
/// saving the database resets the `last_insert_id` counter in sqlite.
Future<int> _handlePotentialUpdate() {