2019-02-20 08:54:00 -08:00
|
|
|
import 'dart:async';
|
|
|
|
|
2019-03-09 07:37:22 -08:00
|
|
|
import 'package:moor/moor.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;
|
|
|
|
MockStreamQueries streamQueries;
|
|
|
|
|
|
|
|
setUp(() {
|
|
|
|
executor = MockExecutor();
|
|
|
|
streamQueries = MockStreamQueries();
|
|
|
|
db = TodoDb(executor)..streamQueries = streamQueries;
|
|
|
|
});
|
|
|
|
|
|
|
|
group('generates update statements', () {
|
|
|
|
test('for entire table', () async {
|
|
|
|
await db
|
|
|
|
.update(db.todosTable)
|
|
|
|
.write(TodoEntry(title: 'Updated title', category: 3));
|
|
|
|
|
|
|
|
verify(executor.runUpdate(
|
2019-03-05 11:59:10 -08:00
|
|
|
'UPDATE todos SET title = ?, category = ?;', ['Updated title', 3]));
|
2019-02-14 08:25:41 -08:00
|
|
|
});
|
|
|
|
|
2019-02-19 01:42:30 -08:00
|
|
|
test('with a WHERE clause', () async {
|
2019-02-14 08:25:41 -08:00
|
|
|
await (db.update(db.todosTable)
|
2019-02-19 01:42:30 -08:00
|
|
|
..where((t) => t.id.isSmallerThanValue(50)))
|
2019-02-14 08:25:41 -08:00
|
|
|
.write(TodoEntry(title: 'Changed title'));
|
|
|
|
|
|
|
|
verify(executor.runUpdate(
|
2019-02-19 02:19:58 -08:00
|
|
|
'UPDATE todos SET title = ? WHERE id < ?;', ['Changed title', 50]));
|
2019-02-14 08:25:41 -08:00
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2019-03-09 07:06:39 -08:00
|
|
|
test('generates replace statements', () async {
|
|
|
|
await db.update(db.todosTable).replace(TodoEntry(
|
|
|
|
id: 3,
|
|
|
|
title: 'Title',
|
|
|
|
content: 'Updated content',
|
|
|
|
// category and targetDate are null
|
|
|
|
));
|
|
|
|
|
|
|
|
verify(executor.runUpdate(
|
|
|
|
'UPDATE todos SET title = ?, content = ?, '
|
|
|
|
'target_date = NULL, category = NULL WHERE id = ?;',
|
|
|
|
['Title', 'Updated content', 3]));
|
|
|
|
});
|
|
|
|
|
2019-02-14 08:25:41 -08:00
|
|
|
test('does not update with invalid data', () {
|
|
|
|
// The length of a title must be between 4 and 16 chars
|
|
|
|
|
|
|
|
expect(() async {
|
2019-03-15 04:56:22 -07:00
|
|
|
await db.update(db.todosTable).write(TodoEntry(title: 'lol'));
|
2019-02-14 08:25:41 -08:00
|
|
|
}, throwsA(const TypeMatcher<InvalidDataException>()));
|
|
|
|
});
|
|
|
|
|
2019-03-09 07:06:39 -08:00
|
|
|
group('Table updates for update statements', () {
|
2019-02-14 08:25:41 -08:00
|
|
|
test('are issued when data was changed', () async {
|
|
|
|
when(executor.runUpdate(any, any)).thenAnswer((_) => Future.value(3));
|
|
|
|
|
2019-03-05 11:59:10 -08:00
|
|
|
await db.update(db.todosTable).write(TodoEntry(
|
|
|
|
content: 'Updated content',
|
|
|
|
));
|
2019-02-14 08:25:41 -08:00
|
|
|
|
2019-03-10 04:06:02 -07:00
|
|
|
verify(streamQueries.handleTableUpdates({'todos'}));
|
2019-02-14 08:25:41 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('are not issued when no data was changed', () async {
|
|
|
|
when(executor.runDelete(any, any)).thenAnswer((_) => Future.value(0));
|
|
|
|
|
|
|
|
await db.update(db.todosTable).write(TodoEntry());
|
|
|
|
|
|
|
|
verifyNever(streamQueries.handleTableUpdates(any));
|
|
|
|
});
|
|
|
|
});
|
2019-02-27 12:19:34 -08:00
|
|
|
|
|
|
|
group('custom updates', () {
|
|
|
|
test('execute the correct sql', () async {
|
2019-02-28 13:06:05 -08:00
|
|
|
await db.customUpdate('DELETE FROM users');
|
2019-02-27 12:19:34 -08:00
|
|
|
|
|
|
|
verify(executor.runUpdate('DELETE FROM users', []));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('map the variables correctly', () async {
|
2019-02-28 13:06:05 -08:00
|
|
|
await db.customUpdate(
|
2019-02-27 12:19:34 -08:00
|
|
|
'DELETE FROM users WHERE name = ? AND birthdate < ?',
|
|
|
|
variables: [
|
|
|
|
Variable.withString('Name'),
|
|
|
|
Variable.withDateTime(
|
|
|
|
DateTime.fromMillisecondsSinceEpoch(1551297563000))
|
|
|
|
]);
|
|
|
|
|
|
|
|
verify(executor.runUpdate(
|
|
|
|
'DELETE FROM users WHERE name = ? AND birthdate < ?',
|
|
|
|
['Name', 1551297563]));
|
|
|
|
});
|
|
|
|
|
|
|
|
test('returns information from executor', () async {
|
|
|
|
when(executor.runUpdate(any, any)).thenAnswer((_) => Future.value(10));
|
|
|
|
|
2019-02-28 13:06:05 -08:00
|
|
|
expect(await db.customUpdate(''), 10);
|
2019-02-27 12:19:34 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test('informs about updated tables', () async {
|
2019-03-01 11:23:14 -08:00
|
|
|
await db.customUpdate('', updates: {db.users, db.todosTable});
|
2019-02-27 12:19:34 -08:00
|
|
|
|
2019-03-10 04:06:02 -07:00
|
|
|
verify(streamQueries.handleTableUpdates({'users', 'todos'}));
|
2019-02-27 12:19:34 -08:00
|
|
|
});
|
|
|
|
});
|
2019-02-14 08:25:41 -08:00
|
|
|
}
|