Skip to content

Commit

Permalink
Add query tests for lists of locations
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Sep 1, 2023
1 parent de46224 commit d9c0545
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 0 deletions.
2 changes: 2 additions & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@
"nodoc",
"nullptr",
"posix",
"sublist",
"sublists",
"TRUEPREDICATE",
"unmanaged",
"upsert",
Expand Down
41 changes: 41 additions & 0 deletions test/geospatial_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import 'dart:async';
import 'dart:math';

import 'package:collection/collection.dart';
import 'package:realm_common/realm_common.dart';
import 'package:test/test.dart' hide test, throws;

Expand All @@ -39,6 +40,9 @@ class _Location {
set lat(double value) => coordinates[1] = value;

GeoPoint toGeoPoint() => GeoPoint(lon: lon, lat: lat);

@override
toString() => '($lon, $lat)';
}

@RealmModel()
Expand All @@ -57,6 +61,14 @@ void createRestaurants(Realm realm) {
});
}

@RealmModel()
class _LocationList {
final locations = <_Location>[];

@override
String toString() => '[${locations.join(', ')}]';
}

extension on GeoPoint {
Location toLocation() {
return Location(coordinates: [lon, lat]);
Expand Down Expand Up @@ -323,4 +335,33 @@ Future<void> main([List<String>? args]) async {
expect(1000.meters, 1.kilometers);
expect(1609.344.meters, 1.miles);
});

test('LocationList', () {
final config = Configuration.local([Location.schema, LocationList.schema]);
final realm = getRealm(config);
const max = 3;
final random = Random(42);
final geoPoints = List.generate(max, (index) => GeoPoint(lon: random.nextDouble(), lat: random.nextDouble()));
final sublists = List.generate(max, (index) {
final start = random.nextInt(max);
final end = random.nextInt(max - start) + start;
return geoPoints.sublist(start, end).map((p) => p.toLocation()).toList();
});
realm.write(() {
realm.addAll(sublists.map((l) => LocationList(locations: l)));
});

for (final p in geoPoints) {
final results = realm.query<LocationList>('ANY locations geoWithin \$0', [GeoCircle(p, 0.radians)]);
for (final list in results) {
expect(list.locations.map((l) => l.toGeoPoint()), contains(p));
}
}
final nonEmpty = realm.query<LocationList>('locations.@size > 0');
final bigCircle = realm.query<LocationList>('ALL locations geoWithin \$0', [GeoCircle(GeoPoint(lon: 0, lat: 0), sqrt(2).radians)]);
expect(bigCircle, unorderedEquals(nonEmpty));

final box = GeoBox(GeoPoint(lon: 0, lat: 0), GeoPoint(lon: 1, lat: 1));
expect(realm.query<LocationList>('ALL locations geoWithin \$0', [box]), unorderedEquals(nonEmpty));
});
}
37 changes: 37 additions & 0 deletions test/geospatial_test.g.dart

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

0 comments on commit d9c0545

Please sign in to comment.