drift/moor/test/delete_test.dart

73 lines
2.0 KiB
Dart
Raw Normal View History

import 'dart:async';
2019-03-09 07:37:22 -08:00
import 'package:moor/moor.dart';
2019-09-26 13:52:20 -07:00
import 'package:test/test.dart';
2019-02-14 07:53:52 -08:00
2019-02-17 04:07:27 -08:00
import 'data/tables/todos.dart';
import 'data/utils/mocks.dart';
2019-02-14 07:53:52 -08:00
void main() {
TodoDb db;
MockExecutor executor;
MockStreamQueries streamQueries;
setUp(() {
executor = MockExecutor();
streamQueries = MockStreamQueries();
final connection = createConnection(executor, streamQueries);
db = TodoDb.connect(connection);
2019-02-14 07:53:52 -08:00
});
group('Generates DELETE statements', () {
test('without any constraints', () async {
await db.delete(db.users).go();
verify(executor.runDelete('DELETE FROM users;', argThat(isEmpty)));
});
test('for complex components', () async {
await (db.delete(db.users)
..where((u) => u.isAwesome.not() | u.id.isSmallerThanValue(100)))
2019-02-14 07:53:52 -08:00
.go();
verify(executor.runDelete(
'DELETE FROM users WHERE NOT is_awesome OR id < ?;', [100]));
2019-02-14 07:53:52 -08:00
});
test('to delete an entity via a dataclasss', () async {
await db.delete(db.sharedTodos).delete(SharedTodo(todo: 3, user: 2));
verify(executor.runDelete(
'DELETE FROM shared_todos WHERE todo = ? AND user = ?;', [3, 2]));
});
2019-02-14 07:53:52 -08:00
});
group('executes DELETE statements', () {
test('and reports the correct amount of affected rows', () async {
when(executor.runDelete(any, any)).thenAnswer((_) async => 12);
expect(await db.delete(db.users).go(), 12);
});
});
2019-02-14 07:53:52 -08:00
group('Table updates for delete statements', () {
test('are issued when data was changed', () async {
when(executor.runDelete(any, any)).thenAnswer((_) => Future.value(3));
await db.delete(db.users).go();
verify(streamQueries.handleTableUpdates(
{const TableUpdate('users', kind: UpdateKind.delete)}));
2019-02-14 07:53:52 -08:00
});
2019-02-14 08:25:41 -08:00
test('are not issued when no data was changed', () async {
2019-02-14 07:53:52 -08:00
when(executor.runDelete(any, any)).thenAnswer((_) => Future.value(0));
await db.delete(db.users).go();
verifyNever(streamQueries.handleTableUpdates(any));
});
});
2019-02-14 08:55:31 -08:00
}