Skip to content

Commit

Permalink
Query subscription tests (#638)
Browse files Browse the repository at this point in the history
* Return correct error for subscriptions

* Test subscriptions with queries

* Subscription on unqueryable field sould throw
  • Loading branch information
desistefanova authored Jun 8, 2022
1 parent 64b7bbf commit b285f8c
Show file tree
Hide file tree
Showing 4 changed files with 162 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/src/cli/deployapps/baas_client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -184,7 +184,7 @@ class BaasClient {
"flexible_sync": {
"state": "enabled",
"database_name": "$_differentiator-$name",
"queryable_fields_names": ["differentiator"],
"queryable_fields_names": ["differentiator", "stringQueryField", "boolQueryField", "intQueryField"],
"permissions": {
"rules": {},
"defaultRoles": [
Expand Down
78 changes: 77 additions & 1 deletion test/subscription_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,12 @@ void testSubscriptions(String name, FutureOr<void> Function(Realm) tester) async
final app = App(appConfiguration);
final credentials = Credentials.anonymous();
final user = await app.logIn(credentials);
final configuration = Configuration.flexibleSync(user, [Task.schema, Schedule.schema]);
final configuration = Configuration.flexibleSync(user, [
Task.schema,
Schedule.schema,
Event.schema,
])
..sessionStopPolicy = SessionStopPolicy.immediately;
final realm = getRealm(configuration);
await tester(realm);
});
Expand Down Expand Up @@ -498,4 +503,75 @@ Future<void> main([List<String>? args]) async {
final realm = getRealm(config);
expect(() => realm.write(() => realm.add(Task(ObjectId()))), throws<RealmException>("no flexible sync subscription has been created"));
});

testSubscriptions('Subscription on unqueryable field sould throw', (realm) async {
realm.subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.add(realm.all<Event>());
});

realm.write(() {
realm.addAll([
Event(ObjectId(), name: "NPMG Event", isCompleted: true, durationInMinutes: 30, assignedTo: "@me"),
Event(
ObjectId(),
name: "NPMG Meeting",
isCompleted: false,
durationInMinutes: 10,
),
Event(ObjectId(), name: "Some other eveent", isCompleted: true, durationInMinutes: 60),
]);
});

await realm.syncSession.waitForUpload();

realm.subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.removeByQuery(realm.all<Event>());
mutableSubscriptions.add(realm.query<Event>(r'assignedTo BEGINSWITH $0 AND boolQueryField == $1 AND intQueryField > $2', ["@me", true, 20]),
name: "filter");
});

String expectedErrorMessage =
"Client provided query with bad syntax: unsupported query for table \"${(Event).toString()}\": key \"assignedTo\" is not a queryable field";

try {
await realm.subscriptions.waitForSynchronization();
fail("Expected exception not thrown");
} catch (e) {
expect(e is RealmException, isTrue);
expect((e as RealmException).message, expectedErrorMessage);
expect(realm.subscriptions.state, SubscriptionSetState.error);
expect(realm.subscriptions.error, isNotNull);
expect(realm.subscriptions.error is RealmException, isTrue);
expect((realm.subscriptions.error as RealmException).message, expectedErrorMessage);
}
});

testSubscriptions('Filter realm data using query subscription', (realm) async {
realm.subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.add(realm.all<Event>());
});

realm.write(() {
realm.addAll([
Event(ObjectId(), name: "NPMG Event", isCompleted: true, durationInMinutes: 30),
Event(ObjectId(), name: "NPMG Meeting", isCompleted: false, durationInMinutes: 10),
Event(ObjectId(), name: "Some other eveent", isCompleted: true, durationInMinutes: 60),
]);
});

await realm.syncSession.waitForUpload();

realm.subscriptions.update((mutableSubscriptions) {
mutableSubscriptions.removeByQuery(realm.all<Event>());
mutableSubscriptions.add(realm.query<Event>(r'stringQueryField BEGINSWITH $0 AND boolQueryField == $1 AND intQueryField > $2', ["NPMG", true, 20]),
name: "filter");
});

await realm.subscriptions.waitForSynchronization();

var filtered = realm.query<Event>(realm.subscriptions.findByName("filter")!.queryString);
var all = realm.all<Event>();
expect(filtered, isNotEmpty);
expect(filtered.length, all.length);
});
}
14 changes: 14 additions & 0 deletions test/test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,20 @@ class _NullableTypes {
late int? intProp;
}

@RealmModel()
class _Event {
@PrimaryKey()
@MapTo('_id')
late ObjectId id;
@MapTo('stringQueryField')
late String? name;
@MapTo('boolQueryField')
late bool? isCompleted;
@MapTo('intQueryField')
late int? durationInMinutes;
late String? assignedTo;
}

String? testName;
Map<String, String?> arguments = {};
final baasApps = <String, BaasApp>{};
Expand Down
70 changes: 70 additions & 0 deletions test/test.g.dart

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit b285f8c

Please sign in to comment.