diff --git a/floor/test/integration/dao/name_dao.dart b/floor/test/integration/dao/name_dao.dart new file mode 100644 index 00000000..42f35bf0 --- /dev/null +++ b/floor/test/integration/dao/name_dao.dart @@ -0,0 +1,24 @@ +import 'package:floor/floor.dart'; + +import '../model/name.dart'; + +@dao +abstract class NameDao { + @Query('SELECT * FROM names ORDER BY name ASC') + Future> findAllNames(); + + @Query('SELECT * FROM names ORDER BY name ASC') + Stream> findAllNamesAsStream(); + + @Query('SELECT * FROM names WHERE name = :name') + Future findExactName(String name); + + @Query('SELECT * FROM names WHERE name = :name') + Stream findExactNameAsStream(int id); + + @Query('SELECT * FROM names WHERE name LIKE :suffix ORDER BY name ASC') + Future> findNamesLike(String suffix); + + @Query('SELECT * FROM names WHERE name LIKE :suffix') + Stream> findNamesLikeAsStream(String suffix); +} diff --git a/floor/test/integration/database.dart b/floor/test/integration/database.dart index 5efe1988..dac60a40 100644 --- a/floor/test/integration/database.dart +++ b/floor/test/integration/database.dart @@ -5,15 +5,19 @@ import 'package:path/path.dart'; import 'package:sqflite/sqflite.dart' as sqflite; import 'dao/dog_dao.dart'; +import 'dao/name_dao.dart'; import 'dao/person_dao.dart'; import 'model/dog.dart'; +import 'model/name.dart'; import 'model/person.dart'; part 'database.g.dart'; -@Database(version: 2, entities: [Person, Dog]) +@Database(version: 2, entities: [Person, Dog], views: [Name]) abstract class TestDatabase extends FloorDatabase { PersonDao get personDao; DogDao get dogDao; + + NameDao get nameDao; } diff --git a/floor/test/integration/database_test.dart b/floor/test/integration/database_test.dart index 0d132257..b15677d9 100644 --- a/floor/test/integration/database_test.dart +++ b/floor/test/integration/database_test.dart @@ -6,9 +6,11 @@ import 'package:sqflite_ffi_test/sqflite_ffi_test.dart'; import '../test_util/extensions.dart'; import 'dao/dog_dao.dart'; +import 'dao/name_dao.dart'; import 'dao/person_dao.dart'; import 'database.dart'; import 'model/dog.dart'; +import 'model/name.dart'; import 'model/person.dart'; void main() { @@ -19,6 +21,7 @@ void main() { TestDatabase database; PersonDao personDao; DogDao dogDao; + NameDao nameDao; setUp(() async { final migration1to2 = Migration(1, 2, (database) { @@ -33,6 +36,7 @@ void main() { personDao = database.personDao; dogDao = database.dogDao; + nameDao = database.nameDao; }); tearDown(() async { @@ -295,6 +299,43 @@ void main() { expect(actual, equals(expectedPersons)); }); }); + + group('Query View', () { + test('query view with exact value', () async { + final person = Person(1, 'Frank'); + await personDao.insertPerson(person); + final expectedName = Name('Frank'); + final actual = await nameDao.findExactName('Frank'); + + expect(actual, equals(expectedName)); + }); + + test('query view with LIKE', () async { + final person = Person(1, 'Leo'); + final dog = Dog(1, 'Romeo', 'Rome', 1); + await personDao.insertPerson(person); + await personDao.insertPerson(Person(2, 'Frank')); + await dogDao.insertDog(dog); + + final expectedNames = [Name('Leo'), Name('Romeo')]; + final actual = await nameDao.findNamesLike('%eo'); + + expect(actual, equals(expectedNames)); + }); + + test('query view with all values', () async { + final person = Person(1, 'Leo'); + final dog = Dog(1, 'Romeo', 'Rome', 1); + await personDao.insertPerson(person); + await personDao.insertPerson(Person(2, 'Frank')); + await dogDao.insertDog(dog); + + final expectedNames = [Name('Frank'), Name('Leo'), Name('Romeo')]; + final actual = await nameDao.findAllNames(); + + expect(actual, equals(expectedNames)); + }); + }); }); }); } diff --git a/floor/test/integration/model/name.dart b/floor/test/integration/model/name.dart new file mode 100644 index 00000000..a465cb40 --- /dev/null +++ b/floor/test/integration/model/name.dart @@ -0,0 +1,23 @@ +import 'package:floor/floor.dart'; + +@DatabaseView( + 'SELECT custom_name as name FROM person UNION SELECT name from dog', + viewName: 'names') +class Name { + final String name; + + Name(this.name); + + @override + bool operator ==(Object other) => + identical(this, other) || + other is Name && runtimeType == other.runtimeType && name == other.name; + + @override + int get hashCode => name.hashCode; + + @override + String toString() { + return 'Name{name: $name}'; + } +}