Skip to content

Commit

Permalink
Add integration tests for view
Browse files Browse the repository at this point in the history
  • Loading branch information
mqus committed Mar 11, 2020
1 parent 657c3ee commit ee2806f
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 1 deletion.
24 changes: 24 additions & 0 deletions floor/test/integration/dao/name_dao.dart
Original file line number Diff line number Diff line change
@@ -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<List<Name>> findAllNames();

@Query('SELECT * FROM names ORDER BY name ASC')
Stream<List<Name>> findAllNamesAsStream();

@Query('SELECT * FROM names WHERE name = :name')
Future<Name> findExactName(String name);

@Query('SELECT * FROM names WHERE name = :name')
Stream<Name> findExactNameAsStream(int id);

@Query('SELECT * FROM names WHERE name LIKE :suffix ORDER BY name ASC')
Future<List<Name>> findNamesLike(String suffix);

@Query('SELECT * FROM names WHERE name LIKE :suffix')
Stream<List<Name>> findNamesLikeAsStream(String suffix);
}
6 changes: 5 additions & 1 deletion floor/test/integration/database.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
41 changes: 41 additions & 0 deletions floor/test/integration/database_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -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() {
Expand All @@ -19,6 +21,7 @@ void main() {
TestDatabase database;
PersonDao personDao;
DogDao dogDao;
NameDao nameDao;

setUp(() async {
final migration1to2 = Migration(1, 2, (database) {
Expand All @@ -33,6 +36,7 @@ void main() {

personDao = database.personDao;
dogDao = database.dogDao;
nameDao = database.nameDao;
});

tearDown(() async {
Expand Down Expand Up @@ -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));
});
});
});
});
}
Expand Down
23 changes: 23 additions & 0 deletions floor/test/integration/model/name.dart
Original file line number Diff line number Diff line change
@@ -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}';
}
}

0 comments on commit ee2806f

Please sign in to comment.