drift/sally/test/select_test.dart

111 lines
2.9 KiB
Dart
Raw Normal View History

import 'dart:async';
2019-02-14 07:53:52 -08:00
import 'package:sally/sally.dart';
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 07:53:52 -08:00
void main() {
TodoDb db;
MockExecutor executor;
setUp(() {
executor = MockExecutor();
db = TodoDb(executor);
});
group('SELECT statements are generated', () {
test('for simple statements', () {
db.select(db.users).get();
verify(executor.runSelect('SELECT * FROM users;', argThat(isEmpty)));
});
test('with limit statements', () {
(db.select(db.users)..limit(10)).get();
verify(executor.runSelect(
'SELECT * FROM users LIMIT 10;', argThat(isEmpty)));
});
test('with like expressions', () {
(db.select(db.users)..where((u) => u.name.like('Dash%'))).get();
verify(executor
.runSelect('SELECT * FROM users WHERE name LIKE ?;', ['Dash%']));
});
test('with order-by clauses', () async {
await (db.select(db.users)
2019-02-19 02:19:58 -08:00
..orderBy([
(u) => OrderingTerm(
expression: u.isAwesome, mode: OrderingMode.desc),
(u) => OrderingTerm(expression: u.id)
]))
.get();
2019-02-19 02:19:58 -08:00
verify(executor.runSelect(
'SELECT * FROM users ORDER BY '
'(is_awesome = 1) DESC, id ASC;',
argThat(isEmpty)));
});
2019-02-14 07:53:52 -08:00
test('with complex predicates', () {
(db.select(db.users)
..where((u) =>
2019-02-19 01:32:59 -08:00
and(not(u.name.equals('Dash')), (u.id.isBiggerThanValue(12)))))
2019-02-14 07:53:52 -08:00
.get();
verify(executor.runSelect(
'SELECT * FROM users WHERE (NOT name = ?) AND (id > ?);',
['Dash', 12]));
});
test('with expressions from boolean columns', () {
(db.select(db.users)..where((u) => u.isAwesome)).get();
verify(executor.runSelect(
'SELECT * FROM users WHERE (is_awesome = 1);', argThat(isEmpty)));
});
});
group('SELECT results are parsed', () {
test('when all fields are non-null', () {
final data = [
2019-02-14 08:55:31 -08:00
{'id': 10, 'title': 'A todo title', 'content': 'Content', 'category': 3}
2019-02-14 07:53:52 -08:00
];
final resolved = TodoEntry(
id: 10,
title: 'A todo title',
content: 'Content',
category: 3,
);
when(executor.runSelect('SELECT * FROM todos;', any))
.thenAnswer((_) => Future.value(data));
expect(db.select(db.todosTable).get(), completion([resolved]));
});
test('when some fields are null', () {
final data = [
{
'id': 10,
'title': null,
'content': 'Content',
'category': null,
}
];
final resolved = TodoEntry(
id: 10,
title: null,
content: 'Content',
category: null,
);
when(executor.runSelect('SELECT * FROM todos;', any))
.thenAnswer((_) => Future.value(data));
expect(db.select(db.todosTable).get(), completion([resolved]));
});
});
}