drift/moor/test/streams_test.dart

108 lines
3.0 KiB
Dart
Raw Normal View History

import 'package:moor/moor.dart';
import 'package:moor/src/runtime/executor/stream_queries.dart';
2019-02-14 08:25:41 -08:00
import 'package:test_api/test_api.dart';
2019-02-17 04:07:27 -08:00
import 'data/tables/todos.dart';
import 'data/utils/mocks.dart';
2019-02-14 08:25:41 -08:00
void main() {
TodoDb db;
MockExecutor executor;
setUp(() {
executor = MockExecutor();
db = TodoDb(executor);
});
test('streams fetch when the first listener attaches', () {
final stream = db.select(db.users).watch();
verifyNever(executor.runSelect(any, any));
stream.listen((_) {});
verify(executor.runSelect(any, any)).called(1);
});
test('streams fetch when the underlying data changes', () {
db.select(db.users).watch().listen((_) {});
db.markTablesUpdated({db.users});
2019-02-14 08:25:41 -08:00
// twice: Once because the listener attached, once because the data changed
verify(executor.runSelect(any, any)).called(2);
});
test('streams recognize aliased tables', () {
final first = db.alias(db.users, 'one');
final second = db.alias(db.users, 'two');
db.select(first).watch().listen((_) {});
db.markTablesUpdated({second});
verify(executor.runSelect(any, any)).called(2);
});
2019-07-04 22:58:23 -07:00
test('streams emit cached data when a new listener attaches', () async {
when(executor.runSelect(any, any)).thenAnswer((_) => Future.value([]));
2019-07-04 22:58:23 -07:00
final first = (db.select(db.users).watch());
expect(first, emits(isEmpty));
clearInteractions(executor);
final second = (db.select(db.users).watch());
expect(second, emits(isEmpty));
verifyZeroInteractions(executor);
});
group('stream keys', () {
final keyA = StreamKey('SELECT * FROM users;', [], User);
final keyB = StreamKey('SELECT * FROM users;', [], User);
final keyCustom = StreamKey('SELECT * FROM users;', [], QueryRow);
final keyCustomTodos = StreamKey('SELECT * FROM todos;', [], QueryRow);
final keyArgs = StreamKey('SELECT * FROM users;', ['name'], User);
test('are equal for same parameters', () {
expect(keyA, equals(keyB));
expect(keyA.hashCode, keyB.hashCode);
});
test('are not equal for different queries', () {
expect(keyCustomTodos, isNot(keyCustom));
expect(keyCustomTodos.hashCode, isNot(keyCustom.hashCode));
});
test('are not equal for different variables', () {
expect(keyArgs, isNot(keyA));
expect(keyArgs.hashCode, isNot(keyA.hashCode));
});
test('are not equal for different types', () {
expect(keyCustom, isNot(keyA));
expect(keyCustom.hashCode, isNot(keyA.hashCode));
});
});
2019-02-14 08:25:41 -08:00
group("streams don't fetch", () {
test('when no listeners were attached', () {
db.select(db.users).watch();
db.markTablesUpdated({db.users});
2019-02-14 08:25:41 -08:00
verifyNever(executor.runSelect(any, any));
});
test('when the data updates after the listener has detached', () {
final subscription = db.select(db.users).watch().listen((_) {});
clearInteractions(executor);
subscription.cancel();
db.markTablesUpdated({db.users});
2019-02-14 08:25:41 -08:00
verifyNever(executor.runSelect(any, any));
});
});
2019-02-14 08:55:31 -08:00
}