From b64e89f324df3df9246452d08fa71fcd097a419c Mon Sep 17 00:00:00 2001 From: Vitus Date: Fri, 21 Feb 2020 21:37:16 +0100 Subject: [PATCH] Fix failing stream query test (#265) --- floor/test/integration/database_test.dart | 102 +------------- floor/test/integration/stream_query_test.dart | 132 ++++++++++++++++++ floor/test/test_util/extensions.dart | 3 + 3 files changed, 136 insertions(+), 101 deletions(-) create mode 100644 floor/test/integration/stream_query_test.dart create mode 100644 floor/test/test_util/extensions.dart diff --git a/floor/test/integration/database_test.dart b/floor/test/integration/database_test.dart index 90000c95..e2afdc93 100644 --- a/floor/test/integration/database_test.dart +++ b/floor/test/integration/database_test.dart @@ -4,13 +4,13 @@ import 'package:matcher/matcher.dart'; import 'package:sqflite/sqflite.dart'; import 'package:sqflite_ffi_test/sqflite_ffi_test.dart'; +import '../test_util/extensions.dart'; import 'dao/dog_dao.dart'; import 'dao/person_dao.dart'; import 'database.dart'; import 'model/dog.dart'; import 'model/person.dart'; -// trigger generator with 'flutter packages pub run build_runner build' void main() { TestWidgetsFlutterBinding.ensureInitialized(); sqfliteFfiTestInit(); @@ -253,102 +253,6 @@ void main() { }); }); - group('stream queries', () { - test('initially emit persistent data', () async { - final person = Person(1, 'Simon'); - await personDao.insertPerson(person); - - final actual = personDao.findAllPersonsAsStream(); - - expect(actual, emits([person])); - }); - - group('insert change', () { - test('find person by id as stream', () async { - final person = Person(1, 'Simon'); - - final actual = personDao.findPersonByIdAsStream(person.id); - - await personDao.insertPerson(person); - expect(actual, emits(person)); - }); - - test('find all persons as stream', () async { - final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; - - final actual = personDao.findAllPersonsAsStream(); - expect(actual, emits(>[])); - - await personDao.insertPersons(persons); - expect(actual, emits(persons)); - }); - - test('initially emits persistent data then new', () async { - final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; - final persons2 = [Person(3, 'Paul'), Person(4, 'George')]; - await personDao.insertPersons(persons); - - final actual = personDao.findAllPersonsAsStream(); - expect(actual, emits(persons)); - - await personDao.insertPersons(persons2); - expect(actual, emits(persons + persons2)); - }); - }); - - group('update change', () { - test('update item', () async { - final person = Person(1, 'Simon'); - await personDao.insertPerson(person); - - final actual = personDao.findAllPersonsAsStream(); - expect(actual, emits([person])); - - final updatedPerson = Person(person.id, 'Frank'); - await personDao.updatePerson(updatedPerson); - expect(actual, emits([updatedPerson])); - }); - - test('update items', () async { - final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; - final updatedPersons = persons - .map((person) => Person(person.id, person.name.reversed())) - .toList(); - await personDao.insertPersons(persons); - - final actual = personDao.findAllPersonsAsStream(); - expect(actual, emits(persons)); - - await personDao.updatePersons(updatedPersons); - expect(actual, emits(updatedPersons)); - }); - }); - - group('deletion change', () { - test('delete item', () async { - final person = Person(1, 'Simon'); - await personDao.insertPerson(person); - - final actual = personDao.findAllPersonsAsStream(); - expect(actual, emits([person])); - - await personDao.deletePerson(person); - expect(actual, emits([])); - }); - - test('delete items', () async { - final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; - await personDao.insertPersons(persons); - - final actual = personDao.findAllPersonsAsStream(); - expect(actual, emits(persons)); - - await personDao.deletePersons(persons); - expect(actual, emits([])); - }); - }); - }); - group('IN clause', () { test('Find persons with IDs', () async { final person1 = Person(1, 'Simon'); @@ -396,7 +300,3 @@ void main() { final _throwsDatabaseException = throwsA(const TypeMatcher()); - -extension on String { - String reversed() => split('').reversed.join(); -} diff --git a/floor/test/integration/stream_query_test.dart b/floor/test/integration/stream_query_test.dart new file mode 100644 index 00000000..cf38e321 --- /dev/null +++ b/floor/test/integration/stream_query_test.dart @@ -0,0 +1,132 @@ +import 'package:flutter_test/flutter_test.dart'; +import 'package:sqflite_ffi_test/sqflite_ffi_test.dart'; + +import '../test_util/extensions.dart'; +import 'dao/person_dao.dart'; +import 'database.dart'; +import 'model/person.dart'; + +void main() { + TestWidgetsFlutterBinding.ensureInitialized(); + sqfliteFfiTestInit(); + + group('stream query tests', () { + TestDatabase database; + PersonDao personDao; + + setUp(() async { + database = await $FloorTestDatabase.inMemoryDatabaseBuilder().build(); + personDao = database.personDao; + }); + + tearDown(() async { + await database.close(); + }); + + test('initially emit persistent data', () async { + final person = Person(1, 'Simon'); + await personDao.insertPerson(person); + + final actual = personDao.findAllPersonsAsStream(); + + expect(actual, emits([person])); + }); + + group('insert change', () { + test('find person by id as stream', () async { + final person = Person(1, 'Simon'); + await personDao.insertPerson(person); + + final actual = personDao.findPersonByIdAsStream(person.id); + + expect(actual, emits(person)); + }); + + test('find all persons as stream', () async { + final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; + + final actual = personDao.findAllPersonsAsStream(); + expect(actual, emits(>[])); + + await personDao.insertPersons(persons); + expect(actual, emits(persons)); + }); + + test('initially emits persistent data then new', () async { + final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; + final persons2 = [Person(3, 'Paul'), Person(4, 'George')]; + await personDao.insertPersons(persons); + + final actual = personDao.findAllPersonsAsStream(); + expect(actual, emits(persons)); + + await personDao.insertPersons(persons2); + expect(actual, emits(persons + persons2)); + }); + }); + + group('update change', () { + test('update item when querying single item', () async { + final person = Person(1, 'Simon'); + final updatedPerson = Person(person.id, 'Frank'); + await personDao.insertPerson(person); + + final actual = personDao.findPersonByIdAsStream(person.id); + expect(actual, emits(person)); + await personDao.updatePerson(updatedPerson); + + expect(actual, emits(updatedPerson)); + }); + + test('update item when querying list of items', () async { + final person = Person(1, 'Simon'); + await personDao.insertPerson(person); + + final actual = personDao.findAllPersonsAsStream(); + expect(actual, emits([person])); + + final updatedPerson = Person(person.id, 'Frank'); + await personDao.updatePerson(updatedPerson); + expect(actual, emits([updatedPerson])); + }); + + test('update items when querying list of items', () async { + final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; + final updatedPersons = persons + .map((person) => Person(person.id, person.name.reversed())) + .toList(); + await personDao.insertPersons(persons); + + final actual = personDao.findAllPersonsAsStream(); + expect(actual, emits(persons)); + + await personDao.updatePersons(updatedPersons); + expect(actual, emits(updatedPersons)); + }); + }); + + group('deletion change', () { + test('delete item', () async { + final person = Person(1, 'Simon'); + await personDao.insertPerson(person); + + final actual = personDao.findAllPersonsAsStream(); + expect(actual, emits([person])); + + await personDao.deletePerson(person); + expect(actual, emits([])); + }); + + test('delete items', () async { + final persons = [Person(1, 'Simon'), Person(2, 'Frank')]; + await personDao.insertPersons(persons); + + final actual = personDao.findAllPersonsAsStream(); + expect(actual, emits(persons)); + + await personDao.deletePersons(persons); + expect(actual, emits([])); + }); + }); + }); +} diff --git a/floor/test/test_util/extensions.dart b/floor/test/test_util/extensions.dart new file mode 100644 index 00000000..5412a623 --- /dev/null +++ b/floor/test/test_util/extensions.dart @@ -0,0 +1,3 @@ +extension StringExtension on String { + String reversed() => split('').reversed.join(); +}