From 3f8406cde2c0d42ca2968b751c334b4d691b3f80 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Fri, 1 Sep 2023 17:11:58 +0200 Subject: [PATCH] Add query tests for lists of locations --- .vscode/settings.json | 2 ++ test/geospatial_test.dart | 41 +++++++++++++++++++++++++++++++++++++ test/geospatial_test.g.dart | 37 +++++++++++++++++++++++++++++++++ 3 files changed, 80 insertions(+) diff --git a/.vscode/settings.json b/.vscode/settings.json index 68e78e84de..aeafc22375 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -30,6 +30,8 @@ "nodoc", "nullptr", "posix", + "sublist", + "sublists", "TRUEPREDICATE", "unmanaged", "upsert", diff --git a/test/geospatial_test.dart b/test/geospatial_test.dart index fa61566b98..bb8bbd271e 100644 --- a/test/geospatial_test.dart +++ b/test/geospatial_test.dart @@ -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; @@ -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() @@ -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]); @@ -323,4 +335,33 @@ Future main([List? 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('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('locations.@size > 0'); + final bigCircle = realm.query('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('ALL locations geoWithin \$0', [box]), unorderedEquals(nonEmpty)); + }); } diff --git a/test/geospatial_test.g.dart b/test/geospatial_test.g.dart index 2e63709fdc..5159165d09 100644 --- a/test/geospatial_test.g.dart +++ b/test/geospatial_test.g.dart @@ -98,3 +98,40 @@ class Restaurant extends _Restaurant ]); } } + +class LocationList extends _LocationList + with RealmEntity, RealmObjectBase, RealmObject { + LocationList({ + Iterable locations = const [], + }) { + RealmObjectBase.set>( + this, 'locations', RealmList(locations)); + } + + LocationList._(); + + @override + RealmList get locations => + RealmObjectBase.get(this, 'locations') as RealmList; + @override + set locations(covariant RealmList value) => + throw RealmUnsupportedSetError(); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + LocationList freeze() => RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(LocationList._); + return const SchemaObject( + ObjectType.realmObject, LocationList, 'LocationList', [ + SchemaProperty('locations', RealmPropertyType.object, + linkTarget: 'Location', collectionType: RealmCollectionType.list), + ]); + } +}