Skip to content

Commit

Permalink
Backlink tests (wip)
Browse files Browse the repository at this point in the history
  • Loading branch information
nielsenko committed Oct 27, 2022
1 parent 50889d4 commit 46bca46
Show file tree
Hide file tree
Showing 3 changed files with 227 additions and 0 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
"dart.lineLength": 160,
"cSpell.words": [
"apikeys",
"backlinks",
"BEGINSWITH",
"bson",
"deallocated",
Expand Down
98 changes: 98 additions & 0 deletions test/backlinks_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
////////////////////////////////////////////////////////////////////////////////
//
// Copyright 2022 Realm Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
////////////////////////////////////////////////////////////////////////////////
import 'package:realm_common/realm_common.dart';
import 'package:test/expect.dart';

import '../lib/realm.dart';
import 'test.dart';

part 'backlinks_test.g.dart';

@RealmModel()
class _Source {
String name = 'source';
_Target? oneTarget;
List<_Target> manyTargets = [];
}

@RealmModel()
class _Target {
@Backlink(#oneTarget)
late Iterable<_Source> oneToMany; // computed property, so must go last in generated class!

String name = 'target';

@Backlink(#manyTargets)
late Iterable<_Source> manyToMany; // computed property, so must go last in generated class!
}

Future<void> main([List<String>? args]) async {
await setupTests(args);

test('Backlinks 1-1(ish)', () {
final config = Configuration.local([Target.schema, Source.schema]);
final realm = getRealm(config);

final target = Target();
final source = realm.write(() => realm.add(Source(oneTarget: target)));

expect(source.oneTarget, target);
expect(target.oneToMany, [source]);
});

test('Backlinks 1-many', () {
final config = Configuration.local([Target.schema, Source.schema]);
final realm = getRealm(config);

final target = Target();
final sources = List.generate(100, (i) => Source(oneTarget: target, name: '$i'));
realm.write(() => realm.addAll(sources));

expect(target.oneToMany, sources);
});

test('Backlinks many-many', () {
final config = Configuration.local([Target.schema, Source.schema]);
final realm = getRealm(config);

final targets = List.generate(100, (i) => Target(name: '$i'));
final sources = List.generate(100, (i) => Source(manyTargets: targets));

realm.write(() => realm.addAll(sources));

for (final t in targets) {
expect(t.manyToMany, sources);
}

for (final s in sources) {
expect(s.manyTargets, targets);
}
});

test('Backlinks query', () {
final config = Configuration.local([Target.schema, Source.schema]);
final realm = getRealm(config);

final target = Target();
final sources = List.generate(100, (i) => Source(oneTarget: target, name: '$i'));
realm.write(() => realm.addAll(sources));

expect(target.oneToMany.query(r'name = $0', ['42']), realm.query<Source>(r'name == $0', ['42']));
});
}
128 changes: 128 additions & 0 deletions test/backlinks_test.g.dart

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

0 comments on commit 46bca46

Please sign in to comment.