From 84832795c24ed0a3364f9b7585376c8ee473bbff Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Kasper=20Overg=C3=A5rd=20Nielsen?= Date: Thu, 27 Oct 2022 09:47:19 +0200 Subject: [PATCH] Backlink tests (wip) --- test/backlinks_test.dart | 54 +++++++++++++++++++ test/backlinks_test.g.dart | 104 +++++++++++++++++++++++++++++++++++++ 2 files changed, 158 insertions(+) create mode 100644 test/backlinks_test.dart create mode 100644 test/backlinks_test.g.dart diff --git a/test/backlinks_test.dart b/test/backlinks_test.dart new file mode 100644 index 0000000000..b94a334208 --- /dev/null +++ b/test/backlinks_test.dart @@ -0,0 +1,54 @@ +//////////////////////////////////////////////////////////////////////////////// +// +// 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? target; +} + +@RealmModel() +class _Target { + @Backlink(#target) + late Iterable<_Source> backlinks; // computed property, so must go last in generated class! + + String name = 'target'; +} + +Future main([List? args]) async { + await setupTests(args); + + test('x', () { + final config = Configuration.local([Target.schema, Source.schema]); + final realm = getRealm(config); + + final target = Target(); + final source = realm.write(() => realm.add(Source(target: target))); + + expect(source.target, target); + expect(target.backlinks, [source]); + }); +} diff --git a/test/backlinks_test.g.dart b/test/backlinks_test.g.dart new file mode 100644 index 0000000000..49759ab998 --- /dev/null +++ b/test/backlinks_test.g.dart @@ -0,0 +1,104 @@ +// GENERATED CODE - DO NOT MODIFY BY HAND + +part of 'backlinks_test.dart'; + +// ************************************************************************** +// RealmObjectGenerator +// ************************************************************************** + +class Source extends _Source with RealmEntity, RealmObjectBase, RealmObject { + static var _defaultsSet = false; + + Source({ + String name = 'source', + Target? target, + }) { + if (!_defaultsSet) { + _defaultsSet = RealmObjectBase.setDefaults({ + 'name': 'source', + }); + } + RealmObjectBase.set(this, 'name', name); + RealmObjectBase.set(this, 'target', target); + } + + Source._(); + + @override + String get name => RealmObjectBase.get(this, 'name') as String; + @override + set name(String value) => RealmObjectBase.set(this, 'name', value); + + @override + Target? get target => RealmObjectBase.get(this, 'target') as Target?; + @override + set target(covariant Target? value) => + RealmObjectBase.set(this, 'target', value); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + Source freeze() => RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(Source._); + return const SchemaObject(ObjectType.realmObject, Source, 'Source', [ + SchemaProperty('name', RealmPropertyType.string), + SchemaProperty('target', RealmPropertyType.object, + optional: true, linkTarget: 'Target'), + ]); + } +} + +class Target extends _Target with RealmEntity, RealmObjectBase, RealmObject { + static var _defaultsSet = false; + + Target({ + String name = 'target', + }) { + if (!_defaultsSet) { + _defaultsSet = RealmObjectBase.setDefaults({ + 'name': 'target', + }); + } + RealmObjectBase.set(this, 'name', name); + } + + Target._(); + + @override + String get name => RealmObjectBase.get(this, 'name') as String; + @override + set name(String value) => RealmObjectBase.set(this, 'name', value); + + @override + RealmResults get backlinks => + RealmObjectBase.get(this, 'backlinks') as RealmResults; + @override + set backlinks(covariant RealmResults value) => + throw RealmUnsupportedSetError(); + + @override + Stream> get changes => + RealmObjectBase.getChanges(this); + + @override + Target freeze() => RealmObjectBase.freezeObject(this); + + static SchemaObject get schema => _schema ??= _initSchema(); + static SchemaObject? _schema; + static SchemaObject _initSchema() { + RealmObjectBase.registerFactory(Target._); + return const SchemaObject(ObjectType.realmObject, Target, 'Target', [ + SchemaProperty('name', RealmPropertyType.string), + SchemaProperty('backlinks', RealmPropertyType.linkingObjects, + linkOriginProperty: 'target', + collectionType: RealmCollectionType.list, + linkTarget: 'Source'), + ]); + } +}