6
6
//
7
7
8
8
import XCTest
9
+ import AmplifyTestCommon
9
10
10
11
@testable import Amplify
11
12
@testable import AWSDataStoreCategoryPlugin
@@ -106,21 +107,35 @@ class AWSAPICategoryPluginTests: XCTestCase {
106
107
dataStorePublisher: dataStorePublisher,
107
108
validAPIPluginKey: " MockAPICategoryPlugin " ,
108
109
validAuthPluginKey: " MockAuthCategoryPlugin " )
110
+
111
+ let finishNotReceived = expectation (
112
+ description: " publisher should not receive .finished completion event after stop() is called " )
113
+ finishNotReceived. isInverted = true
114
+
109
115
do {
110
116
try plugin. configure ( using: nil )
111
- XCTAssert ( plugin. storageEngine == nil )
117
+ XCTAssertNil ( plugin. storageEngine)
118
+
119
+ let sink = plugin. publisher. sink { completion in
120
+ switch completion {
121
+ case . finished:
122
+ finishNotReceived. fulfill ( )
123
+ case . failure( let error) :
124
+ XCTFail ( " Error \( error) " )
125
+ }
126
+ } receiveValue: { _ in }
112
127
113
128
let semaphore = DispatchSemaphore ( value: 0 )
114
129
plugin. start ( completion: { _ in
115
- XCTAssert ( plugin. storageEngine != nil )
116
- XCTAssert ( plugin. dataStorePublisher != nil )
130
+ XCTAssertNotNil ( plugin. storageEngine)
131
+ XCTAssertNotNil ( plugin. dataStorePublisher)
117
132
semaphore. signal ( )
118
133
} )
119
134
semaphore. wait ( )
120
135
121
136
plugin. stop ( completion: { _ in
122
- XCTAssert ( plugin. storageEngine == nil )
123
- XCTAssert ( plugin. dataStorePublisher == nil )
137
+ XCTAssertNil ( plugin. storageEngine)
138
+ XCTAssertNotNil ( plugin. dataStorePublisher)
124
139
semaphore. signal ( )
125
140
} )
126
141
semaphore. wait ( )
@@ -130,14 +145,14 @@ class AWSAPICategoryPluginTests: XCTestCase {
130
145
}
131
146
132
147
plugin. start ( completion: { _ in
133
- XCTAssert ( plugin. storageEngine != nil )
134
- XCTAssert ( plugin. dataStorePublisher != nil )
148
+ XCTAssertNotNil ( plugin. storageEngine)
149
+ XCTAssertNotNil ( plugin. dataStorePublisher)
135
150
} )
136
-
151
+ waitForExpectations ( timeout: 1.0 )
152
+ sink. cancel ( )
137
153
} catch {
138
154
XCTFail ( " DataStore configuration should not fail with nil configuration. \( error) " )
139
155
}
140
- waitForExpectations ( timeout: 1.0 )
141
156
}
142
157
143
158
func testStorageEngineStartClearStart( ) throws {
@@ -161,21 +176,35 @@ class AWSAPICategoryPluginTests: XCTestCase {
161
176
dataStorePublisher: dataStorePublisher,
162
177
validAPIPluginKey: " MockAPICategoryPlugin " ,
163
178
validAuthPluginKey: " MockAuthCategoryPlugin " )
179
+
180
+ let finishNotReceived = expectation (
181
+ description: " publisher should not receive .finished completion event after clear() is called " )
182
+ finishNotReceived. isInverted = true
183
+
164
184
do {
165
185
try plugin. configure ( using: nil )
166
- XCTAssert ( plugin. storageEngine == nil )
186
+ XCTAssertNil ( plugin. storageEngine)
187
+
188
+ let sink = plugin. publisher. sink { completion in
189
+ switch completion {
190
+ case . finished:
191
+ finishNotReceived. fulfill ( )
192
+ case . failure( let error) :
193
+ XCTFail ( " Error \( error) " )
194
+ }
195
+ } receiveValue: { _ in }
167
196
168
197
let semaphore = DispatchSemaphore ( value: 0 )
169
198
plugin. start ( completion: { _ in
170
- XCTAssert ( plugin. storageEngine != nil )
171
- XCTAssert ( plugin. dataStorePublisher != nil )
199
+ XCTAssertNotNil ( plugin. storageEngine)
200
+ XCTAssertNotNil ( plugin. dataStorePublisher)
172
201
semaphore. signal ( )
173
202
} )
174
203
semaphore. wait ( )
175
204
176
205
plugin. clear ( completion: { _ in
177
- XCTAssert ( plugin. storageEngine == nil )
178
- XCTAssert ( plugin. dataStorePublisher == nil )
206
+ XCTAssertNil ( plugin. storageEngine)
207
+ XCTAssertNotNil ( plugin. dataStorePublisher)
179
208
semaphore. signal ( )
180
209
} )
181
210
semaphore. wait ( )
@@ -184,14 +213,14 @@ class AWSAPICategoryPluginTests: XCTestCase {
184
213
}
185
214
186
215
plugin. start ( completion: { _ in
187
- XCTAssert ( plugin. storageEngine != nil )
188
- XCTAssert ( plugin. dataStorePublisher != nil )
216
+ XCTAssertNotNil ( plugin. storageEngine)
217
+ XCTAssertNotNil ( plugin. dataStorePublisher)
189
218
} )
190
-
219
+ waitForExpectations ( timeout: 1.0 )
220
+ sink. cancel ( )
191
221
} catch {
192
222
XCTFail ( " DataStore configuration should not fail with nil configuration. \( error) " )
193
223
}
194
- waitForExpectations ( timeout: 1.0 )
195
224
}
196
225
197
226
func testStorageEngineQueryClearQuery( ) throws {
@@ -216,21 +245,35 @@ class AWSAPICategoryPluginTests: XCTestCase {
216
245
dataStorePublisher: dataStorePublisher,
217
246
validAPIPluginKey: " MockAPICategoryPlugin " ,
218
247
validAuthPluginKey: " MockAuthCategoryPlugin " )
248
+
249
+ let finishNotReceived = expectation (
250
+ description: " publisher should not receive .finished completion event after clear() is called " )
251
+ finishNotReceived. isInverted = true
252
+
219
253
do {
220
254
try plugin. configure ( using: nil )
221
- XCTAssert ( plugin. storageEngine == nil )
255
+ XCTAssertNil ( plugin. storageEngine)
256
+
257
+ let sink = plugin. publisher. sink { completion in
258
+ switch completion {
259
+ case . finished:
260
+ finishNotReceived. fulfill ( )
261
+ case . failure( let error) :
262
+ XCTFail ( " Error \( error) " )
263
+ }
264
+ } receiveValue: { _ in }
222
265
223
266
let semaphore = DispatchSemaphore ( value: 0 )
224
267
plugin. query ( ExampleWithEveryType . self, completion: { _ in
225
- XCTAssert ( plugin. storageEngine != nil )
226
- XCTAssert ( plugin. dataStorePublisher != nil )
268
+ XCTAssertNotNil ( plugin. storageEngine)
269
+ XCTAssertNotNil ( plugin. dataStorePublisher)
227
270
semaphore. signal ( )
228
271
} )
229
272
semaphore. wait ( )
230
273
231
274
plugin. clear ( completion: { _ in
232
- XCTAssert ( plugin. storageEngine == nil )
233
- XCTAssert ( plugin. dataStorePublisher == nil )
275
+ XCTAssertNil ( plugin. storageEngine)
276
+ XCTAssertNotNil ( plugin. dataStorePublisher)
234
277
semaphore. signal ( )
235
278
} )
236
279
semaphore. wait ( )
@@ -239,14 +282,14 @@ class AWSAPICategoryPluginTests: XCTestCase {
239
282
}
240
283
241
284
plugin. query ( ExampleWithEveryType . self, completion: { _ in
242
- XCTAssert ( plugin. storageEngine != nil )
243
- XCTAssert ( plugin. dataStorePublisher != nil )
285
+ XCTAssertNotNil ( plugin. storageEngine)
286
+ XCTAssertNotNil ( plugin. dataStorePublisher)
244
287
} )
245
-
288
+ waitForExpectations ( timeout: 1.0 )
289
+ sink. cancel ( )
246
290
} catch {
247
291
XCTFail ( " DataStore configuration should not fail with nil configuration. \( error) " )
248
292
}
249
- waitForExpectations ( timeout: 1.0 )
250
293
}
251
294
252
295
func expect( _ expectation: XCTestExpectation , _ currCount: Int , _ expectedCount: Int ) -> Int {
@@ -256,4 +299,163 @@ class AWSAPICategoryPluginTests: XCTestCase {
256
299
}
257
300
return count
258
301
}
302
+
303
+ /// - Given: Datastore plugin is initialized
304
+ /// - When:
305
+ /// - plugin.start() is called
306
+ /// - plugin.clear() is called
307
+ /// - a mutation event is sent
308
+ /// - Then: The subscriber to plugin's publisher should receive the mutation
309
+
310
+ func testStorageEngineStartClearSend( ) {
311
+ let startExpectation = expectation ( description: " Start Sync should be called with start " )
312
+ let clearExpectation = expectation ( description: " Clear should be called " )
313
+
314
+ var count = 0
315
+ let storageEngine = MockStorageEngineBehavior ( )
316
+ storageEngine. responders [ . startSync] = StartSyncResponder { _ in
317
+ count = self . expect ( startExpectation, count, 1 )
318
+ }
319
+ storageEngine. responders [ . clear] = ClearResponder { _ in
320
+ count = self . expect ( clearExpectation, count, 2 )
321
+ }
322
+
323
+ let storageEngineBehaviorFactory : StorageEngineBehaviorFactory = { _, _, _, _, _, _ throws in
324
+ return storageEngine
325
+ }
326
+ let dataStorePublisher = DataStorePublisher ( )
327
+ let plugin = AWSDataStorePlugin ( modelRegistration: TestModelRegistration ( ) ,
328
+ storageEngineBehaviorFactory: storageEngineBehaviorFactory,
329
+ dataStorePublisher: dataStorePublisher,
330
+ validAPIPluginKey: " MockAPICategoryPlugin " ,
331
+ validAuthPluginKey: " MockAuthCategoryPlugin " )
332
+
333
+ let finishNotReceived = expectation (
334
+ description: " publisher should not receive .finished completion event after clear() is called " )
335
+ finishNotReceived. isInverted = true
336
+
337
+ let publisherReceivedValue = expectation (
338
+ description: " publisher should receive a value when mutation event is sent " )
339
+
340
+ do {
341
+ try plugin. configure ( using: nil )
342
+ XCTAssertNil ( plugin. storageEngine)
343
+
344
+ let sink = plugin. publisher. sink { completion in
345
+ switch completion {
346
+ case . finished:
347
+ finishNotReceived. fulfill ( )
348
+ case . failure( let error) :
349
+ XCTFail ( " Error \( error) " )
350
+ }
351
+ } receiveValue: { event in
352
+ XCTAssertEqual ( event. modelId, " 12345 " )
353
+ publisherReceivedValue. fulfill ( )
354
+ }
355
+
356
+ let semaphore = DispatchSemaphore ( value: 0 )
357
+ plugin. start ( completion: { _ in
358
+ XCTAssertNotNil ( plugin. storageEngine)
359
+ XCTAssertNotNil ( plugin. dataStorePublisher)
360
+ semaphore. signal ( )
361
+ } )
362
+ semaphore. wait ( )
363
+
364
+ plugin. clear ( completion: { _ in
365
+ XCTAssertNil ( plugin. storageEngine)
366
+ XCTAssertNotNil ( plugin. dataStorePublisher)
367
+ semaphore. signal ( )
368
+ } )
369
+ semaphore. wait ( )
370
+
371
+ let mockModel = MockSynced ( id: " 12345 " )
372
+ try plugin. dataStorePublisher? . send ( input: MutationEvent ( model: mockModel,
373
+ modelSchema: mockModel. schema,
374
+ mutationType: . create) )
375
+
376
+ waitForExpectations ( timeout: 1.0 )
377
+ sink. cancel ( )
378
+ } catch {
379
+ XCTFail ( " DataStore configuration should not fail with nil configuration. \( error) " )
380
+ }
381
+ }
382
+
383
+ /// - Given: Datastore plugin is initialized
384
+ /// - When:
385
+ /// - plugin.start() is called
386
+ /// - plugin.stop() is called
387
+ /// - a mutation event is sent
388
+ /// - Then: The subscriber to plugin's publisher should receive the mutation
389
+ func testStorageEngineStartStopSend( ) {
390
+ let startExpectation = expectation ( description: " Start Sync should be called with start " )
391
+ let stopExpectation = expectation ( description: " Stop should be called " )
392
+
393
+ var count = 0
394
+ let storageEngine = MockStorageEngineBehavior ( )
395
+ storageEngine. responders [ . startSync] = StartSyncResponder { _ in
396
+ count = self . expect ( startExpectation, count, 1 )
397
+ }
398
+ storageEngine. responders [ . stopSync] = StopSyncResponder { _ in
399
+ count = self . expect ( stopExpectation, count, 2 )
400
+ }
401
+
402
+ let storageEngineBehaviorFactory : StorageEngineBehaviorFactory = { _, _, _, _, _, _ throws in
403
+ return storageEngine
404
+ }
405
+ let dataStorePublisher = DataStorePublisher ( )
406
+ let plugin = AWSDataStorePlugin ( modelRegistration: TestModelRegistration ( ) ,
407
+ storageEngineBehaviorFactory: storageEngineBehaviorFactory,
408
+ dataStorePublisher: dataStorePublisher,
409
+ validAPIPluginKey: " MockAPICategoryPlugin " ,
410
+ validAuthPluginKey: " MockAuthCategoryPlugin " )
411
+
412
+ let finishNotReceived = expectation (
413
+ description: " publisher should not receive .finished completion event after stop() is called " )
414
+ finishNotReceived. isInverted = true
415
+
416
+ let publisherReceivedValue = expectation (
417
+ description: " publisher should receive a value when mutation event is sent " )
418
+
419
+ do {
420
+ try plugin. configure ( using: nil )
421
+ XCTAssertNil ( plugin. storageEngine)
422
+
423
+ let sink = plugin. publisher. sink { completion in
424
+ switch completion {
425
+ case . finished:
426
+ finishNotReceived. fulfill ( )
427
+ case . failure( let error) :
428
+ XCTFail ( " Error \( error) " )
429
+ }
430
+ } receiveValue: { event in
431
+ XCTAssertEqual ( event. modelId, " 12345 " )
432
+ publisherReceivedValue. fulfill ( )
433
+ }
434
+
435
+ let semaphore = DispatchSemaphore ( value: 0 )
436
+ plugin. start ( completion: { _ in
437
+ XCTAssertNotNil ( plugin. storageEngine)
438
+ XCTAssertNotNil ( plugin. dataStorePublisher)
439
+ semaphore. signal ( )
440
+ } )
441
+ semaphore. wait ( )
442
+
443
+ plugin. stop ( completion: { _ in
444
+ XCTAssertNil ( plugin. storageEngine)
445
+ XCTAssertNotNil ( plugin. dataStorePublisher)
446
+ semaphore. signal ( )
447
+ } )
448
+ semaphore. wait ( )
449
+
450
+ let mockModel = MockSynced ( id: " 12345 " )
451
+ try plugin. dataStorePublisher? . send ( input: MutationEvent ( model: mockModel,
452
+ modelSchema: mockModel. schema,
453
+ mutationType: . create) )
454
+
455
+ waitForExpectations ( timeout: 1.0 )
456
+ sink. cancel ( )
457
+ } catch {
458
+ XCTFail ( " DataStore configuration should not fail with nil configuration. \( error) " )
459
+ }
460
+ }
259
461
}
0 commit comments