Skip to content

Commit

Permalink
Create a common sync schema for all the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
desistefanova committed Sep 14, 2023
1 parent 6b9f2c9 commit b9472e1
Show file tree
Hide file tree
Showing 9 changed files with 169 additions and 69 deletions.
2 changes: 1 addition & 1 deletion test/app_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -260,7 +260,7 @@ Future<void> main([List<String>? args]) async {
final app = App(appConfiguration);

final user = await app.logIn(Credentials.anonymous());
final configuration = Configuration.flexibleSync(user, [Task.schema]);
final configuration = Configuration.flexibleSync(user, syncSchema);
final realm = getRealm(configuration);
final session = realm.syncSession;

Expand Down
28 changes: 14 additions & 14 deletions test/client_reset_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ Future<void> main([List<String>? args]) async {
final resetCompleter = Completer<void>();
final config = Configuration.flexibleSync(
user,
[Task.schema, Schedule.schema],
syncSchema,
clientResetHandler: ManualRecoveryHandler((syncError) {
resetCompleter.completeError(syncError);
}),
Expand All @@ -93,7 +93,7 @@ Future<void> main([List<String>? args]) async {
final resetCompleter = Completer<void>();
final config = Configuration.flexibleSync(
user,
[Task.schema, Schedule.schema],
syncSchema,
clientResetHandler: ManualRecoveryHandler((clientResetError) {
resetCompleter.completeError(clientResetError);
}),
Expand Down Expand Up @@ -122,7 +122,7 @@ Future<void> main([List<String>? args]) async {
final resetCompleter = Completer<bool>();
final config = Configuration.flexibleSync(
user,
[Task.schema, Schedule.schema],
syncSchema,
clientResetHandler: ManualRecoveryHandler((clientResetError) {
resetCompleter.completeError(clientResetError);
}),
Expand Down Expand Up @@ -152,7 +152,7 @@ Future<void> main([List<String>? args]) async {
final user = await getIntegrationUser(app);

final onManualResetFallback = Completer<void>();
final config = Configuration.flexibleSync(user, [Task.schema, Schedule.schema],
final config = Configuration.flexibleSync(user, syncSchema,
clientResetHandler: Creator.create(
clientResetHandlerType,
onBeforeReset: (beforeResetRealm) => throw Exception("This fails!"),
Expand All @@ -177,7 +177,7 @@ Future<void> main([List<String>? args]) async {
throw Exception("This fails!");
}

final config = Configuration.flexibleSync(user, [Task.schema, Schedule.schema],
final config = Configuration.flexibleSync(user, syncSchema,
clientResetHandler: Creator.create(
clientResetHandlerType,
onAfterRecovery: clientResetHandlerType != DiscardUnsyncedChangesHandler ? onAfterReset : null,
Expand All @@ -204,7 +204,7 @@ Future<void> main([List<String>? args]) async {
onAfterCompleter.complete();
}

final config = Configuration.flexibleSync(user, [Task.schema, Schedule.schema],
final config = Configuration.flexibleSync(user, syncSchema,
clientResetHandler: Creator.create(
clientResetHandlerType,
onBeforeReset: (beforeResetRealm) => onBeforeCompleter.complete(),
Expand Down Expand Up @@ -233,7 +233,7 @@ Future<void> main([List<String>? args]) async {
int onAfterRecoveryOccurred = 0;
final onAfterCompleter = Completer<void>();

final config = Configuration.flexibleSync(user, [Task.schema, Schedule.schema],
final config = Configuration.flexibleSync(user, syncSchema,
clientResetHandler: Creator.create(
clientResetHandlerType,
onBeforeReset: (beforeResetRealm) => onBeforeResetOccurred++,
Expand Down Expand Up @@ -300,7 +300,7 @@ Future<void> main([List<String>? args]) async {
final maybeProduct = Product(ObjectId(), "maybe synced");
comparer(Product p1, Product p2) => p1.id == p2.id;

final config = Configuration.flexibleSync(user, [Product.schema],
final config = Configuration.flexibleSync(user, syncSchema,
clientResetHandler: Creator.create(
clientResetHandlerType,
onBeforeReset: (beforeResetRealm) {
Expand Down Expand Up @@ -349,7 +349,7 @@ Future<void> main([List<String>? args]) async {
bool recovery = false;
bool discard = false;

final config = Configuration.flexibleSync(user, [Task.schema, Schedule.schema],
final config = Configuration.flexibleSync(user, syncSchema,
clientResetHandler: RecoverOrDiscardUnsyncedChangesHandler(
onBeforeReset: (beforeResetRealm) => onBeforeCompleter.complete(),
onAfterRecovery: (Realm beforeResetRealm, Realm afterResetRealm) {
Expand Down Expand Up @@ -389,7 +389,7 @@ Future<void> main([List<String>? args]) async {

final config = Configuration.flexibleSync(
user,
[Task.schema, Schedule.schema],
syncSchema,
clientResetHandler: DiscardUnsyncedChangesHandler(
onBeforeReset: (beforeResetRealm) async {
await Future<void>.delayed(Duration(seconds: 1));
Expand Down Expand Up @@ -426,7 +426,7 @@ Future<void> main([List<String>? args]) async {
late ClientResetError clientResetErrorOnManualFallback;
final config = Configuration.flexibleSync(
user,
[Task.schema, Schedule.schema],
syncSchema,
clientResetHandler: DiscardUnsyncedChangesHandler(
onBeforeReset: (beforeResetRealm) {
onBeforeResetOccurred++;
Expand Down Expand Up @@ -481,7 +481,7 @@ Future<void> main([List<String>? args]) async {

comparer(Task t1, ObjectId id) => t1.id == id;

final configA = Configuration.flexibleSync(userA, [Task.schema], clientResetHandler: RecoverUnsyncedChangesHandler(
final configA = Configuration.flexibleSync(userA, syncSchema, clientResetHandler: RecoverUnsyncedChangesHandler(
onAfterReset: (beforeResetRealm, afterResetRealm) {
try {
_checkProducts(beforeResetRealm, comparer, expectedList: [task0Id, task1Id], notExpectedList: [task2Id, task3Id]);
Expand All @@ -493,7 +493,7 @@ Future<void> main([List<String>? args]) async {
},
));

final configB = Configuration.flexibleSync(userB, [Schedule.schema, Task.schema], clientResetHandler: RecoverUnsyncedChangesHandler(
final configB = Configuration.flexibleSync(userB, syncSchema, clientResetHandler: RecoverUnsyncedChangesHandler(
onAfterReset: (beforeResetRealm, afterResetRealm) {
try {
_checkProducts(beforeResetRealm, comparer, expectedList: [task0Id, task1Id, task2Id, task3Id]);
Expand Down Expand Up @@ -540,7 +540,7 @@ Future<void> main([List<String>? args]) async {
late ClientResetError clientResetError;
final config = Configuration.flexibleSync(
user,
[Task.schema, Schedule.schema],
syncSchema,
clientResetHandler: ManualRecoveryHandler((syncError) {
clientResetError = syncError;
resetCompleter.complete();
Expand Down
25 changes: 12 additions & 13 deletions test/configuration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ Future<void> main([List<String>? args]) async {

var customDefaultRealmName = "myRealmName.realm";
Configuration.defaultRealmName = customDefaultRealmName;
var config = Configuration.flexibleSync(user, [Task.schema]);
var config = Configuration.flexibleSync(user, syncSchema);
expect(path.basename(config.path), path.basename(customDefaultRealmName));

var realm = getRealm(config);
Expand All @@ -91,7 +91,7 @@ Future<void> main([List<String>? args]) async {
//set a new defaultRealmName
customDefaultRealmName = "anotherRealmName.realm";
Configuration.defaultRealmName = customDefaultRealmName;
config = Configuration.flexibleSync(user, [Task.schema]);
config = Configuration.flexibleSync(user, syncSchema);
realm = getRealm(config);
expect(path.basename(realm.config.path), customDefaultRealmName);
});
Expand All @@ -108,7 +108,7 @@ Future<void> main([List<String>? args]) async {
var app = App(appConfig);
var user = await app.logIn(Credentials.anonymous());

var config = Configuration.flexibleSync(user, [Task.schema]);
var config = Configuration.flexibleSync(user, syncSchema);
expect(path.dirname(config.path), startsWith(path.dirname(customDefaultRealmPath)));

var realm = getRealm(config);
Expand All @@ -125,7 +125,7 @@ Future<void> main([List<String>? args]) async {

app = App(appConfig);
user = await app.logIn(Credentials.anonymous());
config = Configuration.flexibleSync(user, [Task.schema]);
config = Configuration.flexibleSync(user, syncSchema);
realm = getRealm(config);
expect(path.dirname(realm.config.path), startsWith(path.dirname(customDefaultRealmPath)));
});
Expand Down Expand Up @@ -497,7 +497,7 @@ Future<void> main([List<String>? args]) async {
final user = await app.logIn(Credentials.emailPassword(testUsername, testPassword));

var invoked = false;
var config = Configuration.flexibleSync(user, [Event.schema], shouldCompactCallback: (totalSize, usedSize) {
var config = Configuration.flexibleSync(user, syncSchema, shouldCompactCallback: (totalSize, usedSize) {
invoked = true;
return false;
});
Expand All @@ -510,7 +510,7 @@ Future<void> main([List<String>? args]) async {
final app = App(appConfig);
final user = await app.logIn(Credentials.emailPassword(testUsername, testPassword));

final config = Configuration.flexibleSync(user, [Car.schema]);
final config = Configuration.flexibleSync(user, syncSchema);

expect(config.path, contains(user.id));
expect(config.path, contains(appConfig.appId));
Expand All @@ -520,7 +520,7 @@ Future<void> main([List<String>? args]) async {
final app = App(appConfig);
final user = await app.logIn(Credentials.emailPassword(testUsername, testPassword));

final config = Configuration.flexibleSync(user, [Car.schema], path: 'my-custom-path.realm');
final config = Configuration.flexibleSync(user, syncSchema, path: 'my-custom-path.realm');

expect(config.path, 'my-custom-path.realm');
});
Expand All @@ -532,7 +532,7 @@ Future<void> main([List<String>? args]) async {
path.dirname(Configuration.defaultStoragePath),
path.basename('my-custom-realm-name.realm'),
);
final config = Configuration.flexibleSync(user, [Event.schema], path: customPath);
final config = Configuration.flexibleSync(user, syncSchema, path: customPath);
var realm = getRealm(config);
});

Expand All @@ -543,8 +543,7 @@ Future<void> main([List<String>? args]) async {
final dir = await Directory.systemTemp.createTemp();
final realmPath = path.join(dir.path, 'test.realm');

final schema = [Task.schema];
final flexibleSyncConfig = Configuration.flexibleSync(user, schema, path: realmPath);
final flexibleSyncConfig = Configuration.flexibleSync(user, syncSchema, path: realmPath);
final realm = getRealm(flexibleSyncConfig);
final oid = ObjectId();
realm.subscriptions.update((mutableSubscriptions) {
Expand All @@ -553,7 +552,7 @@ Future<void> main([List<String>? args]) async {
realm.write(() => realm.add(Task(oid)));
realm.close();

final disconnectedSyncConfig = Configuration.disconnectedSync(schema, path: realmPath);
final disconnectedSyncConfig = Configuration.disconnectedSync(syncSchema, path: realmPath);
final disconnectedRealm = getRealm(disconnectedSyncConfig);
expect(disconnectedRealm.find<Task>(oid), isNotNull);
});
Expand Down Expand Up @@ -611,7 +610,7 @@ Future<void> main([List<String>? args]) async {
final credentials = Credentials.anonymous();
final user = await app.logIn(credentials);

final config = Configuration.flexibleSync(user, [Task.schema], maxNumberOfActiveVersions: 1);
final config = Configuration.flexibleSync(user, syncSchema, maxNumberOfActiveVersions: 1);
final realm = await getRealmAsync(config); // First writing to the Realm when opening
realm.subscriptions.update((mutableSubscriptions) => mutableSubscriptions.add(realm.all<Task>()));
expect(() => realm.write(() {}), throws<RealmException>("in the Realm exceeded the limit of 1"));
Expand All @@ -621,7 +620,7 @@ Future<void> main([List<String>? args]) async {
final app = App(appConfiguration);
final credentials = Credentials.anonymous();
final user = await app.logIn(credentials);
final config = Configuration.flexibleSync(user, [Task.schema]);
final config = Configuration.flexibleSync(user, syncSchema);

final disconnectedConfig = Configuration.disconnectedSync([Task.schema], path: config.path, maxNumberOfActiveVersions: 1);
final realm = getRealm(disconnectedConfig); // First writing to the Realm when opening
Expand Down
92 changes: 92 additions & 0 deletions test/destructive_schema_test.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// ////////////////////////////////////////////////////////////////////////////////
// //
// // Copyright 2023 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 'dart:async';
// import 'package:cancellation_token/cancellation_token.dart';
// import 'package:test/test.dart' hide test, throws;
// import '../lib/realm.dart';
// import 'test.dart';

// part 'destructive_schema_test.g.dart';

// @RealmModel()
// @MapTo("Card")
// class _CardV1 {
// @PrimaryKey()
// @MapTo('_id')
// late ObjectId id;
// late String test;
// }

// @RealmModel()
// @MapTo("Card")
// class _CardV2 {
// @PrimaryKey()
// @MapTo('_id')
// late ObjectId id;
// }

// // Destructive schema change causes the server to force the translator to restart.
// // This means that the Flexible sync will be terminated and re-enabled.
// // During this operation no other tests working with flexible sync could be executed.
// // For this reason this test is relocated to a separate file,
// // which will be executed at the end after all the other tests.
// Future<void> main([List<String>? args]) async {
// await setupTests(args);

// baasTest('Realm can be deleted after destructive schema change (flexibleSync)', (configuration) async {
// final app = App(configuration);
// final CancellationToken cancellationToken = CancellationToken();
// final user = await app.logIn(Credentials.anonymous(reuseCredentials: false));
// final resetCompleter = Completer<void>();
// final configV1 = Configuration.flexibleSync(user, [CardV1.schema], clientResetHandler: ManualRecoveryHandler((syncError) {
// if (!resetCompleter.isCompleted) {
// cancellationToken.cancel();
// resetCompleter.completeError(syncError);
// }
// }));
// final realmV1 = getRealm(configV1);
// realmV1.subscriptions.update((mutableSubscriptions) {
// mutableSubscriptions.add(realmV1.all<CardV1>());
// });
// try {
// await realmV1.syncSession.waitForUpload(cancellationToken);
// } catch (e) {
// await resetCompleter.future;
// }
// realmV1.close();

// final user1 = await app.logIn(Credentials.anonymous(reuseCredentials: false));
// final configV2 = Configuration.flexibleSync(user1, [CardV2.schema], clientResetHandler: ManualRecoveryHandler((syncError) {
// if (!resetCompleter.isCompleted) {
// cancellationToken.cancel();
// resetCompleter.completeError(syncError);
// }
// }));

// final realmV2 = Realm(configV2);
// realmV2.subscriptions.update((mutableSubscriptions) {
// mutableSubscriptions.add(realmV2.all<CardV2>());
// });
// try {
// await realmV2.syncSession.waitForUpload(cancellationToken);
// } catch (e) {
// //await resetCompleter.future;
// }
// realmV2.close();
// });
// }
6 changes: 3 additions & 3 deletions test/embedded_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ Future<void> main([List<String>? args]) async {
final app = App(config);
final user = await getAnonymousUser(app);
final realmConfig = Configuration.flexibleSync(
user, [AllTypesEmbedded.schema, ObjectWithEmbedded.schema, RecursiveEmbedded1.schema, RecursiveEmbedded2.schema, RecursiveEmbedded3.schema]);
user, syncSchema);
return getRealm(realmConfig);
}

Expand All @@ -58,10 +58,10 @@ Future<void> main([List<String>? args]) async {
baasTest('Synchronized Realm with orphan embedded schemas throws', (configuration) async {
final app = App(configuration);
final user = await getIntegrationUser(app);
final config = Configuration.flexibleSync(user, [AllTypesEmbedded.schema]);
final config = Configuration.flexibleSync(user, syncSchema);

expect(() => getRealm(config), throws<RealmException>("Embedded object 'AllTypesEmbedded' is unreachable by any link path from top level objects"));
});
}, skip: "This test requires a new app service with missing embedded parent schema.");

test('Embedded object roundtrip', () {
final realm = getLocalRealm();
Expand Down
Loading

0 comments on commit b9472e1

Please sign in to comment.