Search in sources :

Example 26 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class MutationTest method testOverlayWithFieldDeletionOfNestedField.

@Test
public void testOverlayWithFieldDeletionOfNestedField() {
    MutableDocument doc = doc("collection/key", 1, map("foo", 1));
    Mutation patch1 = patchMutation("collection/key", map("foo", "foo-patched-value", "bar.baz", FieldValue.increment(1)));
    Mutation patch2 = patchMutation("collection/key", map("foo", "foo-patched-value", "bar.baz", FieldValue.serverTimestamp()));
    Mutation patch3 = patchMutation("collection/key", map("foo", "foo-patched-value", "bar.baz", FieldValue.delete()));
    verifyOverlayRoundTrips(doc, patch1, patch2, patch3);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Example 27 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class MutationTest method testTransitions.

@Test
public void testTransitions() {
    MutableDocument docV3 = doc("collection/key", 3, map());
    MutableDocument deletedV3 = deletedDoc("collection/key", 3);
    Mutation set = setMutation("collection/key", map());
    Mutation patch = patchMutation("collection/key", map());
    Mutation delete = deleteMutation("collection/key");
    MutableDocument docV7Deleted = deletedDoc("collection/key", 7).setHasCommittedMutations();
    MutableDocument docV7Committed = doc("collection/key", 7, map()).setHasCommittedMutations();
    MutableDocument docV7Unknown = unknownDoc("collection/key", 7);
    MutationResult mutationResult = new MutationResult(version(7), /*transformResults=*/
    Collections.emptyList());
    assertVersionTransitions(set, docV3, mutationResult, docV7Committed);
    assertVersionTransitions(set, deletedV3, mutationResult, docV7Committed);
    assertVersionTransitions(set, MutableDocument.newInvalidDocument(key("collection/key")), mutationResult, docV7Committed);
    assertVersionTransitions(patch, docV3, mutationResult, docV7Committed);
    assertVersionTransitions(patch, deletedV3, mutationResult, docV7Unknown);
    assertVersionTransitions(patch, MutableDocument.newInvalidDocument(key("collection/key")), mutationResult, docV7Unknown);
    assertVersionTransitions(delete, docV3, mutationResult, docV7Deleted);
    assertVersionTransitions(delete, deletedV3, mutationResult, docV7Deleted);
    assertVersionTransitions(delete, MutableDocument.newInvalidDocument(key("collection/key")), mutationResult, docV7Deleted);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Example 28 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class MutationTest method runPermutationTests.

/**
 * For each document in {@code docs}, calculate the overlay mutations of each possible
 * permutation, check whether this holds: document + overlay_mutation = document + mutation_list
 * Returns how many cases it has run.
 */
private int runPermutationTests(List<MutableDocument> docs, List<Mutation> mutations) {
    int testCases = 0;
    Collection<List<Mutation>> permutations = Collections2.permutations(Lists.newArrayList(mutations));
    for (MutableDocument doc : docs) {
        for (List<Mutation> permutation : permutations) {
            verifyOverlayRoundTrips(doc, permutation.toArray(new Mutation[] {}));
            testCases += 1;
        }
    }
    return testCases;
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) List(java.util.List) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)

Example 29 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class MutationTest method testAppliesLocalServerTimestampTransformsToDocuments.

@Test
public void testAppliesLocalServerTimestampTransformsToDocuments() {
    Map<String, Object> data = map("foo", map("bar", "bar-value"), "baz", "baz-value");
    MutableDocument transformedDoc = doc("collection/key", 1, data);
    Timestamp timestamp = Timestamp.now();
    Mutation transform = patchMutation("collection/key", map("foo.bar", FieldValue.serverTimestamp()));
    transform.applyToLocalView(transformedDoc, /* previousMask= */
    null, timestamp);
    // Server timestamps aren't parsed, so we manually insert it.
    ObjectValue expectedData = wrapObject(map("foo", map("bar", "<server-timestamp>"), "baz", "baz-value"));
    Value fieldValue = ServerTimestamps.valueOf(timestamp, wrap("bar-value"));
    expectedData.set(field("foo.bar"), fieldValue);
    MutableDocument expectedDoc = doc("collection/key", 1, expectedData).setHasLocalMutations();
    assertEquals(expectedDoc, transformedDoc);
}
Also used : ObjectValue(com.google.firebase.firestore.model.ObjectValue) MutableDocument(com.google.firebase.firestore.model.MutableDocument) ObjectValue(com.google.firebase.firestore.model.ObjectValue) FieldValue(com.google.firebase.firestore.FieldValue) Value(com.google.firestore.v1.Value) TestUtil.wrapObject(com.google.firebase.firestore.testutil.TestUtil.wrapObject) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Timestamp(com.google.firebase.Timestamp) Test(org.junit.Test)

Example 30 with MutableDocument

use of com.google.firebase.firestore.model.MutableDocument in project firebase-android-sdk by firebase.

the class MutationTest method testOverlayWithPatchThenMergeWithArrayUnion.

@Test
public void testOverlayWithPatchThenMergeWithArrayUnion() {
    MutableDocument doc = doc("collection/key", 1, map("foo", 1));
    Mutation patch = patchMutation("collection/key", map("foo", "foo-patched-value", "bar.baz", FieldValue.increment(1)));
    Mutation merge = mergeMutation("collection/key", map("arrays", FieldValue.arrayUnion(1, 2, 3)), Arrays.asList(field("arrays")));
    verifyOverlayRoundTrips(doc, patch, merge);
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) TestUtil.deleteMutation(com.google.firebase.firestore.testutil.TestUtil.deleteMutation) TestUtil.setMutation(com.google.firebase.firestore.testutil.TestUtil.setMutation) TestUtil.patchMutation(com.google.firebase.firestore.testutil.TestUtil.patchMutation) TestUtil.mergeMutation(com.google.firebase.firestore.testutil.TestUtil.mergeMutation) Mutation.calculateOverlayMutation(com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation) Test(org.junit.Test)

Aggregations

MutableDocument (com.google.firebase.firestore.model.MutableDocument)166 Test (org.junit.Test)125 DocumentKey (com.google.firebase.firestore.model.DocumentKey)43 Mutation.calculateOverlayMutation (com.google.firebase.firestore.model.mutation.Mutation.calculateOverlayMutation)30 TestUtil.deleteMutation (com.google.firebase.firestore.testutil.TestUtil.deleteMutation)30 TestUtil.mergeMutation (com.google.firebase.firestore.testutil.TestUtil.mergeMutation)30 TestUtil.patchMutation (com.google.firebase.firestore.testutil.TestUtil.patchMutation)30 TestUtil.setMutation (com.google.firebase.firestore.testutil.TestUtil.setMutation)30 HashMap (java.util.HashMap)22 TestUtil.wrapObject (com.google.firebase.firestore.testutil.TestUtil.wrapObject)18 ArrayList (java.util.ArrayList)18 TargetData (com.google.firebase.firestore.local.TargetData)15 WatchTargetChange (com.google.firebase.firestore.remote.WatchChange.WatchTargetChange)14 DocumentChange (com.google.firebase.firestore.remote.WatchChange.DocumentChange)13 ResourcePath (com.google.firebase.firestore.model.ResourcePath)12 Query (com.google.firebase.firestore.core.Query)10 Map (java.util.Map)10 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)8 Document (com.google.firebase.firestore.model.Document)7 Timestamp (com.google.firebase.Timestamp)6