Skip to content

Commit

Permalink
Integration tests for stream events of transactions
Browse files Browse the repository at this point in the history
  • Loading branch information
mqus committed Apr 22, 2021
1 parent c00215f commit f9801cf
Show file tree
Hide file tree
Showing 2 changed files with 63 additions and 0 deletions.
16 changes: 16 additions & 0 deletions floor/test/integration/dao/person_dao.dart
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,22 @@ abstract class PersonDao {
await insertPersons(persons);
}

@transaction
Future<void> fillDatabase(String prefix) async {
await deleteAllPersons();
for (var i in [0, 1, 2, 3]) {
await insertPerson(Person(i, '$prefix P$i'));
}
}

@transaction
Future<void> failingTransaction() async {
await insertPerson(Person(1, 'Name'));
await insertPerson(Person(2, 'Name2'));
//the following should fail (id is not unique)
await insertPersons([Person(1, 'Name')]);
}

@transaction
Future<List<Person>> replacePersonsAndReturn(List<Person> persons) async {
await replacePersons(persons);
Expand Down
47 changes: 47 additions & 0 deletions floor/test/integration/stream_query_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -155,5 +155,52 @@ void main() {

await database.personDao.deletePerson(person1);
});
group('transaction triggers', () {
test('transaction should only trigger once per completed transaction',
() async {
final actual = personDao.findAllPersonsAsStream();
expect(
actual,
emitsInOrder(<dynamic>[
<Person>[], // initial state,
[
Person(0, ' P0'),
Person(1, ' P1'),
Person(2, ' P2'),
Person(3, ' P3'),
], // after first fill
[
Person(0, 'x P0'),
Person(1, 'x P1'),
Person(2, 'x P2'),
Person(3, 'x P3'),
], // after second fill,
emitsDone
]));

await personDao.fillDatabase('');
await personDao.fillDatabase('x');
// avoid that closing happens before the re-execution of
// the select query for the stream
await Future<void>.delayed(const Duration(milliseconds: 100));
await database.close();
});
test('failing transaction should not trigger stream', () async {
final actual = personDao.findAllPersonsAsStream();
expect(
actual,
emitsInOrder(<dynamic>[
<Person>[], // initial state,
emitsDone // do no emit anything else, since statements within
// transaction should not trigger new stream events
]));
try {
await personDao.failingTransaction();
} catch (_) {}
// close database to avoid deadlock (expect emitsDone waits for closing of Database
// and database will be closed in tearDown after expect finishes
await database.close();
});
});
});
}

0 comments on commit f9801cf

Please sign in to comment.