Skip to content

Commit

Permalink
Add tests
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Jun 12, 2023
1 parent d8216cc commit 3ae85e3
Showing 1 changed file with 38 additions and 0 deletions.
38 changes: 38 additions & 0 deletions test/results_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -741,4 +741,42 @@ Future<void> main([List<String>? args]) async {
final items = realm.query<Product>(ids);
expect(items.length, 0);
});

test('RealmResults.first throws on empty', () async {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);

final empty = Iterable<int>.empty();
expect(() => empty.first, throwsA(isA<StateError>()));
expect(() => realm.all<Dog>().first, throwsA(isA<RealmException>()));
});

test('RealmResults.last throws on empty', () async {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);

final empty = Iterable<int>.empty();
expect(() => empty.last, throwsA(isA<StateError>()));
expect(() => realm.all<Dog>().last, throwsA(isA<RealmException>()));
});

test('RealmResult.single throws on empty', () {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);

final empty = Iterable<int>.empty();
expect(() => empty.single, throwsA(isA<StateError>()));
expect(() => realm.all<Dog>().single, throwsA(isA<RealmStateError>()));
});

test('RealmResult.single throws on two items', () {
final config = Configuration.local([Dog.schema, Person.schema]);
final realm = getRealm(config);

final twoItems = [1, 2];
expect(() => twoItems.single, throwsA(isA<StateError>()));

realm.write(() => realm.addAll([Dog('Fido'), Dog('Spot')]));
expect(() => realm.all<Dog>().single, throwsA(isA<StateError>()));
});
}

0 comments on commit 3ae85e3

Please sign in to comment.