Skip to content

Commit

Permalink
unit test now working
Browse files Browse the repository at this point in the history
  • Loading branch information
MongoCaleb committed Aug 8, 2024
1 parent 2627d9d commit 14d7c0f
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 53 deletions.
8 changes: 4 additions & 4 deletions examples/dart/bin/myapp.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,17 @@ void main(List<String> arguments) async {
realm.add(Car(ObjectId(), "Audi", model: 'A8'));
realm.add(Car(ObjectId(), "Mercedes", model: 'G Wagon'));
});
print("Bundled realm location: " + realm.config.path);
print("Bundled realm location: ${realm.config.path}");
realm.close();
// :snippet-end:
Future<void> createSyncedBundle() async {
final APP_ID = 'flutter-flexible-luccm';
final appId = 'flutter-flexible-luccm';
// :snippet-start: create-synced-bundle
print("Bundling synced realm");

// You must connect to the Device Sync server with an authenticated
// user to work with the synced realm.
final app = App(AppConfiguration(APP_ID));
final app = App(AppConfiguration(appId));
// Check if current user exists and log anonymous user if not.
final user = app.currentUser ?? await app.logIn(Credentials.anonymous());

Expand Down Expand Up @@ -54,7 +54,7 @@ void main(List<String> arguments) async {
path: 'sync_bundle.realm');
realm.writeCopy(bundledConfig);

print("Bundled realm location: " + bundledConfig.path);
print("Bundled realm location: ${bundledConfig.path}");
realm.close();
// :snippet-end:
}
Expand Down
74 changes: 32 additions & 42 deletions examples/dart/test/open_flexible_sync_realm_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -68,69 +68,59 @@ void main() {
cleanUpRealm(realm, app);
});

Future<void> _addSubscriptions(Realm realm, String searchByPrefix) async {
Future<void> addSubscriptions(Realm realm, String searchByPrefix) async {
final query = realm.query<Tricycle>(r'name BEGINSWITH $0', ["a"]);
if (realm.subscriptions.find(query) == null) {
realm.subscriptions.update((mutableSubscriptions) => mutableSubscriptions.add(query));
}
await realm.subscriptions.waitForSynchronization();
}

Future<Configuration> _subscribeForAtlasAddedData(App app, {String? queryDifferentiator, int itemsCount = 100}) async {
final productNamePrefix = queryDifferentiator ?? generateRandomString(10);
Future<Configuration> subscribeForAtlasAddedData(App app, {int itemsCount = 100}) async {
final productNamePrefix = "a";
final user1 = await app.logIn(Credentials.anonymous(reuseCredentials: false));
final config1 = Configuration.flexibleSync(user1, [Tricycle.schema]);
final realm1 = Realm(config1);
await _addSubscriptions(realm1, productNamePrefix);
await addSubscriptions(realm1, productNamePrefix);
realm1.close();

final user2 = await app.logIn(Credentials.anonymous(reuseCredentials: false));
final config2 = Configuration.flexibleSync(user2, [Tricycle.schema]);
final realm2 = Realm(config2);

await addSubscriptions(realm2, productNamePrefix);

final trikes = realm2.all<Tricycle>();
realm2.write(() {
realm2.deleteMany(trikes);
realm2.add(Tricycle(1001, "a1001"));
realm2.add(Tricycle(2002, "a2002"));
realm2.add(Tricycle(3003, "a3003"));
});

await realm2.syncSession.waitForUpload();
await realm2.syncSession.waitForDownload();
realm2.close();
return config1;
}

test("Track upload progress", () async {
final config = await _subscribeForAtlasAddedData(app);

int printCount = 0;
final config = await subscribeForAtlasAddedData(app);
// :snippet-start: async-open-track-progress
double progressEstimate = -1;

final syncedRealm = await Realm.open(config, onProgressCallback: (syncProgress) {
printCount++;
final realm = await Realm.open(config, onProgressCallback: (syncProgress) {
progressEstimate = syncProgress.progressEstimate;
print('Sync progress: ${progressEstimate * 100}% complete.');
if (progressEstimate == 1.0) {
//transfer is complete
}
});

await syncedRealm.syncSession.waitForUpload();
await syncedRealm.syncSession.waitForDownload();
expect(syncedRealm.isClosed, false);
expect(printCount, 1);
// :snippet-end:
expect(realm.isClosed, false);
expect(progressEstimate, 1.0);
cleanUpRealm(realm, app);
});


test('Track download progress', () async {
print("start");
final credentials = Credentials.anonymous();
final currentUser = await app.logIn(credentials);
double progress = -1;
final config = Configuration.flexibleSync(currentUser, [Tricycle.schema]);
// :snippet-start: async-open-track-progress
print(progress);

final realm = await Realm.open(config, onProgressCallback: (syncProgress) {
progress = syncProgress.progressEstimate;
print("here");
// Percent complete == progress * 100
print(progress);
if (syncProgress.progressEstimate == 1.0) {
// Transfer is complete
}
});
await realm.syncSession.waitForUpload();
await realm.syncSession.waitForDownload();

// :snippet-end:
/*expect(realm.isClosed, false);
expect(progress, greaterThanOrEqualTo(0));
cleanUpRealm(realm, app);*/
});
test('Cancel download in progress', () async {
final credentials = Credentials.anonymous();
final currentUser = await app.logIn(credentials);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
final stream = realm.syncSession.getProgressStream(
ProgressDirection.upload, ProgressMode.forCurrentlyOutstandingWork);

double progressEstimate = -1;
late StreamSubscription streamListener;
streamListener = stream.listen((syncProgressEvent) {
final progressEstimate = syncProgressEvent.progressEstimate;
progressEstimate = syncProgressEvent.progressEstimate;

if (progressEstimate < 1.0) {
print('Upload progress: ${progressEstimate * 100}%');
}
}, onDone: () {
print('Upload progress: ${progressEstimate * 100}%');
print("Upload complete");
}, onError: (error) {
print("An error occurred: $error");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
double progressEstimate = -1;
final realm = await Realm.open(config, onProgressCallback: (syncProgress) {
progress = syncProgress.progressEstimate;
// Percent complete == progress * 100
if (syncProgress.progressEstimate == 1.0) {
//transfer is complete
}
progressEstimate = syncProgress.progressEstimate;
print('Sync progress: ${progressEstimate * 100}% complete.');
if (progressEstimate == 1.0) {
//transfer is complete
}
});

0 comments on commit 14d7c0f

Please sign in to comment.