Skip to content

Commit 14d7c0f

Browse files
committed
unit test now working
1 parent 2627d9d commit 14d7c0f

File tree

4 files changed

+45
-53
lines changed

4 files changed

+45
-53
lines changed

examples/dart/bin/myapp.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,17 @@ void main(List<String> arguments) async {
1212
realm.add(Car(ObjectId(), "Audi", model: 'A8'));
1313
realm.add(Car(ObjectId(), "Mercedes", model: 'G Wagon'));
1414
});
15-
print("Bundled realm location: " + realm.config.path);
15+
print("Bundled realm location: ${realm.config.path}");
1616
realm.close();
1717
// :snippet-end:
1818
Future<void> createSyncedBundle() async {
19-
final APP_ID = 'flutter-flexible-luccm';
19+
final appId = 'flutter-flexible-luccm';
2020
// :snippet-start: create-synced-bundle
2121
print("Bundling synced realm");
2222

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

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

57-
print("Bundled realm location: " + bundledConfig.path);
57+
print("Bundled realm location: ${bundledConfig.path}");
5858
realm.close();
5959
// :snippet-end:
6060
}

examples/dart/test/open_flexible_sync_realm_test.dart

Lines changed: 32 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -68,69 +68,59 @@ void main() {
6868
cleanUpRealm(realm, app);
6969
});
7070

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

79-
Future<Configuration> _subscribeForAtlasAddedData(App app, {String? queryDifferentiator, int itemsCount = 100}) async {
80-
final productNamePrefix = queryDifferentiator ?? generateRandomString(10);
79+
Future<Configuration> subscribeForAtlasAddedData(App app, {int itemsCount = 100}) async {
80+
final productNamePrefix = "a";
8181
final user1 = await app.logIn(Credentials.anonymous(reuseCredentials: false));
8282
final config1 = Configuration.flexibleSync(user1, [Tricycle.schema]);
8383
final realm1 = Realm(config1);
84-
await _addSubscriptions(realm1, productNamePrefix);
84+
await addSubscriptions(realm1, productNamePrefix);
8585
realm1.close();
86+
87+
final user2 = await app.logIn(Credentials.anonymous(reuseCredentials: false));
88+
final config2 = Configuration.flexibleSync(user2, [Tricycle.schema]);
89+
final realm2 = Realm(config2);
90+
91+
await addSubscriptions(realm2, productNamePrefix);
92+
93+
final trikes = realm2.all<Tricycle>();
94+
realm2.write(() {
95+
realm2.deleteMany(trikes);
96+
realm2.add(Tricycle(1001, "a1001"));
97+
realm2.add(Tricycle(2002, "a2002"));
98+
realm2.add(Tricycle(3003, "a3003"));
99+
});
100+
101+
await realm2.syncSession.waitForUpload();
102+
await realm2.syncSession.waitForDownload();
103+
realm2.close();
86104
return config1;
87105
}
88106

89107
test("Track upload progress", () async {
90-
final config = await _subscribeForAtlasAddedData(app);
91-
92-
int printCount = 0;
108+
final config = await subscribeForAtlasAddedData(app);
109+
// :snippet-start: async-open-track-progress
93110
double progressEstimate = -1;
94-
95-
final syncedRealm = await Realm.open(config, onProgressCallback: (syncProgress) {
96-
printCount++;
111+
final realm = await Realm.open(config, onProgressCallback: (syncProgress) {
97112
progressEstimate = syncProgress.progressEstimate;
113+
print('Sync progress: ${progressEstimate * 100}% complete.');
114+
if (progressEstimate == 1.0) {
115+
//transfer is complete
116+
}
98117
});
99-
100-
await syncedRealm.syncSession.waitForUpload();
101-
await syncedRealm.syncSession.waitForDownload();
102-
expect(syncedRealm.isClosed, false);
103-
expect(printCount, 1);
118+
// :snippet-end:
119+
expect(realm.isClosed, false);
104120
expect(progressEstimate, 1.0);
121+
cleanUpRealm(realm, app);
105122
});
106-
107123

108-
test('Track download progress', () async {
109-
print("start");
110-
final credentials = Credentials.anonymous();
111-
final currentUser = await app.logIn(credentials);
112-
double progress = -1;
113-
final config = Configuration.flexibleSync(currentUser, [Tricycle.schema]);
114-
// :snippet-start: async-open-track-progress
115-
print(progress);
116-
117-
final realm = await Realm.open(config, onProgressCallback: (syncProgress) {
118-
progress = syncProgress.progressEstimate;
119-
print("here");
120-
// Percent complete == progress * 100
121-
print(progress);
122-
if (syncProgress.progressEstimate == 1.0) {
123-
// Transfer is complete
124-
}
125-
});
126-
await realm.syncSession.waitForUpload();
127-
await realm.syncSession.waitForDownload();
128-
129-
// :snippet-end:
130-
/*expect(realm.isClosed, false);
131-
expect(progress, greaterThanOrEqualTo(0));
132-
cleanUpRealm(realm, app);*/
133-
});
134124
test('Cancel download in progress', () async {
135125
final credentials = Credentials.anonymous();
136126
final currentUser = await app.logIn(credentials);

source/examples/generated/flutter/manage_sync_session_test.snippet.monitor-progress.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,15 @@
11
final stream = realm.syncSession.getProgressStream(
22
ProgressDirection.upload, ProgressMode.forCurrentlyOutstandingWork);
3-
3+
double progressEstimate = -1;
44
late StreamSubscription streamListener;
55
streamListener = stream.listen((syncProgressEvent) {
6-
final progressEstimate = syncProgressEvent.progressEstimate;
6+
progressEstimate = syncProgressEvent.progressEstimate;
77

88
if (progressEstimate < 1.0) {
99
print('Upload progress: ${progressEstimate * 100}%');
1010
}
1111
}, onDone: () {
12+
print('Upload progress: ${progressEstimate * 100}%');
1213
print("Upload complete");
1314
}, onError: (error) {
1415
print("An error occurred: $error");
Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1+
double progressEstimate = -1;
12
final realm = await Realm.open(config, onProgressCallback: (syncProgress) {
2-
progress = syncProgress.progressEstimate;
3-
// Percent complete == progress * 100
4-
if (syncProgress.progressEstimate == 1.0) {
5-
//transfer is complete
6-
}
3+
progressEstimate = syncProgress.progressEstimate;
4+
print('Sync progress: ${progressEstimate * 100}% complete.');
5+
if (progressEstimate == 1.0) {
6+
//transfer is complete
7+
}
78
});

0 commit comments

Comments
 (0)