Skip to content

Commit 5ed73d8

Browse files
committed
fix test
1 parent 8febea6 commit 5ed73d8

File tree

2 files changed

+23
-73
lines changed

2 files changed

+23
-73
lines changed

lib/src/adapter/adapter.dart

Lines changed: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -180,12 +180,12 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
180180
return model;
181181
}
182182

183-
Future<void> _runInIsolate(
184-
Future<void> fn(ProviderContainer container)) async {
183+
Future<R> _runInIsolate<R>(
184+
FutureOr<R> fn(ProviderContainer container)) async {
185185
final ppath = Directory(storage.path).parent.path;
186186
final ip = _internalProviders!;
187187

188-
await Isolate.run(() async {
188+
return await Isolate.run(() async {
189189
late final ProviderContainer container;
190190
try {
191191
container = ProviderContainer(
@@ -198,7 +198,7 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
198198

199199
await container.read(initializeWith(ip).future);
200200

201-
fn(container);
201+
return fn(container);
202202
} finally {
203203
container.read(localStorageProvider).db.dispose();
204204
}
@@ -207,27 +207,32 @@ abstract class _BaseAdapter<T extends DataModelMixin<T>> with _Lifecycle {
207207

208208
Future<void> saveManyLocal(Iterable<DataModelMixin> models,
209209
{bool notify = true}) async {
210-
// ref.read(localStorageProvider).db.updates.listen((event) {
211-
// print('received; $event');
212-
// });
213-
await _runInIsolate((container) async {
210+
final savedKeys = await _runInIsolate((container) async {
214211
final db = container.read(localStorageProvider).db;
212+
final savedKeys = <String>[];
215213

216214
final grouped = models.groupSetsBy((e) => e._adapter);
217215
for (final e in grouped.entries) {
218216
final adapter = e.key;
219217
// TODO use prepareMultiple
220218
final ps = db.prepare(
221-
'REPLACE INTO ${adapter.internalType} (key, data) VALUES (?, ?)');
219+
'REPLACE INTO ${adapter.internalType} (key, data) VALUES (?, ?) RETURNING key;');
222220
for (final model in e.value) {
223221
final key = model._key!.detypifyKey();
224222
final map = adapter.serialize(model, withRelationships: false);
225223
final data = jsonEncode(map);
226-
ps.execute([key, data]);
224+
final result = ps.select([key, data]);
225+
savedKeys.add(
226+
result.first['key'].toString().typifyWith(adapter.internalType));
227227
}
228228
ps.dispose();
229229
}
230+
return savedKeys;
230231
});
232+
233+
if (notify) {
234+
core._notify(savedKeys, type: DataGraphEventType.updateNode);
235+
}
231236
}
232237

233238
/// Deletes model of type [T] from local storage.

test/repository/remote_adapter_watch_test.dart

Lines changed: 8 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,10 @@ Stream<DataState<T>> _streamFor<T>(DataStateNotifier<T> notifier) {
2323
return controller.stream.asBroadcastStream();
2424
}
2525

26+
// can use (stream, emitsInOrder) or (stream.first, completion)
27+
// await expectLater(stream.first,
28+
// completion(DataState<List<Familia>>([], isLoading: true)));
29+
2630
void main() async {
2731
setUp(setUpFn);
2832
tearDown(tearDownFn);
@@ -69,14 +73,6 @@ void main() async {
6973
final notifier = container.familia.watchAllNotifier(remote: true);
7074
final stream = _streamFor(notifier);
7175

72-
// await expectLater(stream.first,
73-
// completion(DataState<List<Familia>>([], isLoading: true)));
74-
// await expectLater(
75-
// stream.first,
76-
// completion(isA<DataState>()
77-
// .having((s) => s.isLoading, 'isLoading', isFalse)
78-
// .having((s) => s.exception, 'exception', isA<Exception>())));
79-
8076
await expectLater(
8177
stream,
8278
emitsInOrder([
@@ -86,7 +82,6 @@ void main() async {
8682
.having((s) => s.exception, 'exception', isA<Exception>())
8783
]),
8884
);
89-
print('1st ok');
9085

9186
// now server will successfully respond with two familia
9287
container.read(responseProvider.notifier).state = TestResponse.json('''
@@ -95,66 +90,16 @@ void main() async {
9590

9691
// reload
9792
await notifier.reload();
98-
print('after reload');
99-
100-
// TODO fix this
101-
// not working here as saveMany/save are not emitting
102-
// this was there before:
103-
// core._notify([op.edge.from, op.edge.to],
104-
// metadata: op.edge.name, type: type);
105-
// (now should listen to stream from database and emit, cause isolates)
10693

10794
final familia = Familia(id: '1', surname: 'Corleone');
10895
final familia2 = Familia(id: '2', surname: 'Soprano');
10996

11097
await expectLater(
111-
stream.first,
112-
completion(
113-
DataState<List<Familia>>([], isLoading: false),
114-
),
98+
stream,
99+
emitsInOrder([
100+
DataState<List<Familia>>([familia, familia2], isLoading: false),
101+
]),
115102
);
116-
print('2nd ok');
117-
118-
// final listener = Listener<DataState<List<Familia>>?>();
119-
120-
// container.read(responseProvider.notifier).state =
121-
// TestResponse((_) => throw Exception('unreachable'));
122-
// final notifier = container.familia.watchAllNotifier();
123-
124-
// dispose = notifier.addListener(listener);
125-
126-
// verify(listener(DataState([], isLoading: true))).called(1);
127-
// await oneMs();
128-
129-
// // finished loading but found the network unreachable
130-
// verify(listener(argThat())
131-
// .called(1);
132-
// verifyNoMoreInteractions(listener);
133-
134-
// // now server will successfully respond with two familia
135-
// container.read(responseProvider.notifier).state = TestResponse.json('''
136-
// [{ "id": "1", "surname": "Corleone" }, { "id": "2", "surname": "Soprano" }]
137-
// ''');
138-
139-
// // reload
140-
// await notifier.reload();
141-
142-
// final familia = Familia(id: '1', surname: 'Corleone');
143-
// final familia2 = Familia(id: '2', surname: 'Soprano');
144-
145-
// // loads again, for now exception remains
146-
// verify(listener(argThat(isA<DataState>()
147-
// .having((s) => s.isLoading, 'isLoading', isTrue)
148-
// .having((s) => s.exception, 'exception', isA<Exception>()))))
149-
// .called(1);
150-
151-
// await oneMs();
152-
153-
// // now responds with models, loading done, and no exception
154-
// verify(listener(argThat(isA<DataState>().having(
155-
// (s) => s.model, 'model', unorderedEquals([familia, familia2])))))
156-
// .called(1);
157-
// verifyNoMoreInteractions(listener);
158103
});
159104

160105
test('watchOne with remote=true', () async {

0 commit comments

Comments
 (0)