|
18 | 18 | import com.amplifyframework.AmplifyException; |
19 | 19 | import com.amplifyframework.core.model.Model; |
20 | 20 | import com.amplifyframework.core.model.ModelSchema; |
| 21 | +import com.amplifyframework.core.model.SerializedModel; |
21 | 22 | import com.amplifyframework.core.model.query.Where; |
22 | 23 | import com.amplifyframework.core.model.query.predicate.QueryPredicates; |
23 | 24 | import com.amplifyframework.datastore.DataStoreException; |
@@ -630,6 +631,70 @@ public void existingUpdateIncomingUpdateWithoutConditionRewritesExistingMutation |
630 | 631 | ); |
631 | 632 | } |
632 | 633 |
|
| 634 | + /** |
| 635 | + * When there is an existing SerializedModel update mutation, and a new SerializedModel update mutation comes in, |
| 636 | + * then we need to merge any existing mutations for that modelId and create the new one. |
| 637 | + * @throws AmplifyException On failure to find the serializedModel difference. |
| 638 | + * @throws InterruptedException If interrupted while awaiting terminal result in test observer |
| 639 | + */ |
| 640 | + @Test |
| 641 | + public void existingSerializedModelUpdateIncomingUpdateWithoutConditionMergesWithExistingMutation() |
| 642 | + throws AmplifyException, InterruptedException { |
| 643 | + // Arrange an existing update mutation |
| 644 | + BlogOwner modelInSqlLite = BlogOwner.builder() |
| 645 | + .name("Papa Tony") |
| 646 | + .wea("Something") |
| 647 | + .build(); |
| 648 | + |
| 649 | + BlogOwner initialUpdate = BlogOwner.builder() |
| 650 | + .name("Tony Jr") |
| 651 | + .id(modelInSqlLite.getId()) |
| 652 | + .build(); |
| 653 | + |
| 654 | + PendingMutation<SerializedModel> initialUpdatePendingMutation = |
| 655 | + PendingMutation.update(SerializedModel.difference(initialUpdate, modelInSqlLite, schema), schema); |
| 656 | + String existingUpdateId = initialUpdatePendingMutation.getMutationId().toString(); |
| 657 | + mutationOutbox.enqueue(initialUpdatePendingMutation).blockingAwait(); |
| 658 | + |
| 659 | + // Act: try to enqueue a new update mutation when there already is one |
| 660 | + BlogOwner incomingUpdatedModel = BlogOwner.builder() |
| 661 | + .name("Papa Tony") |
| 662 | + .wea("something else") |
| 663 | + .id(modelInSqlLite.getId()) |
| 664 | + .build(); |
| 665 | + PendingMutation<SerializedModel> incomingUpdate = PendingMutation.update( |
| 666 | + SerializedModel.difference(incomingUpdatedModel, modelInSqlLite, schema), |
| 667 | + schema); |
| 668 | + String incomingUpdateId = incomingUpdate.getMutationId().toString(); |
| 669 | + TestObserver<Void> enqueueObserver = mutationOutbox.enqueue(incomingUpdate).test(); |
| 670 | + |
| 671 | + // Assert: OK. The new mutation is accepted |
| 672 | + enqueueObserver.await(TIMEOUT_MS, TimeUnit.MILLISECONDS); |
| 673 | + enqueueObserver.assertComplete(); |
| 674 | + |
| 675 | + // Assert: the existing mutation has been removed |
| 676 | + assertRecordCountForMutationId(existingUpdateId, 0); |
| 677 | + |
| 678 | + // And the new one has been added to the queue |
| 679 | + assertRecordCountForMutationId(incomingUpdateId, 0); |
| 680 | + |
| 681 | + List<PersistentRecord> pendingMutationsFromStorage = getAllPendingMutationRecordFromStorage(); |
| 682 | + for (PersistentRecord record : pendingMutationsFromStorage) { |
| 683 | + if (!record.getContainedModelId().equals(incomingUpdate.getMutatedItem().getId())) { |
| 684 | + pendingMutationsFromStorage.remove(record); |
| 685 | + } |
| 686 | + } |
| 687 | + // Ensure the new one is in storage. |
| 688 | + PendingMutation<SerializedModel> storedMutation = |
| 689 | + converter.fromRecord(pendingMutationsFromStorage.get(0)); |
| 690 | + // This is the name from the second model, not the first!! |
| 691 | + assertEquals(initialUpdate.getName(), |
| 692 | + storedMutation.getMutatedItem().getSerializedData().get("name")); |
| 693 | + // wea got merged from existing model!! |
| 694 | + assertEquals(incomingUpdatedModel.getWea(), |
| 695 | + storedMutation.getMutatedItem().getSerializedData().get("wea")); |
| 696 | + } |
| 697 | + |
633 | 698 | /** |
634 | 699 | * When there is an existing creation mutation, and an update comes in, |
635 | 700 | * the exiting creation should be updated with the contents of the incoming |
@@ -1032,4 +1097,8 @@ private void assertRecordCountForMutationId(String mutationId, int expectedCount |
1032 | 1097 | private List<PersistentRecord> getPendingMutationRecordFromStorage(String mutationId) throws DataStoreException { |
1033 | 1098 | return storage.query(PersistentRecord.class, Where.id(mutationId)); |
1034 | 1099 | } |
| 1100 | + |
| 1101 | + private List<PersistentRecord> getAllPendingMutationRecordFromStorage() throws DataStoreException { |
| 1102 | + return storage.query(PersistentRecord.class, Where.matchesAll()); |
| 1103 | + } |
1035 | 1104 | } |
0 commit comments