Search in sources :

Example 91 with Query

use of com.google.firebase.firestore.core.Query in project firebase-android-sdk by firebase.

the class BundleSerializerTest method testDecodesLimitToLastQuery.

@Test
public void testDecodesLimitToLastQuery() throws JSONException {
    String json = "{ from: [ { collectionId: 'coll' } ], limit: {value: 5 } }";
    Query query = TestUtil.query("coll").limitToLast(5);
    assertDecodesNamedQuery(json, query);
}
Also used : Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 92 with Query

use of com.google.firebase.firestore.core.Query in project firebase-android-sdk by firebase.

the class LocalStoreTestCase method testQueriesIncludeLocallyModifiedDocuments.

@Test
public void testQueriesIncludeLocallyModifiedDocuments() {
    assumeFalse(garbageCollectorIsEager());
    // This test verifies that queries that have a persisted TargetMapping include documents that
    // were modified by local edits after the target mapping was written.
    Query query = query("foo").filter(filter("matches", "==", true));
    int targetId = allocateQuery(query);
    applyRemoteEvent(addedRemoteEvent(doc("foo/a", 10, map("matches", true)), targetId));
    applyRemoteEvent(noChangeEvent(targetId, 10));
    updateViews(targetId, /* fromCache= */
    false);
    // Execute the query based on the RemoteEvent.
    executeQuery(query);
    assertQueryReturned("foo/a");
    // Write a document.
    writeMutation(setMutation("foo/b", map("matches", true)));
    // Execute the query and make sure that the pending mutation is included in the result.
    executeQuery(query);
    assertQueryReturned("foo/a", "foo/b");
    acknowledgeMutation(11);
    // Execute the query and make sure that the acknowledged mutation is included in the result.
    executeQuery(query);
    assertQueryReturned("foo/a", "foo/b");
}
Also used : BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 93 with Query

use of com.google.firebase.firestore.core.Query in project firebase-android-sdk by firebase.

the class LocalStoreTestCase method testHandlesSetMutationThenTransformThenRemoteEventThenTransform.

@Test
public void testHandlesSetMutationThenTransformThenRemoteEventThenTransform() {
    Query query = query("foo");
    allocateQuery(query);
    assertTargetId(2);
    writeMutation(setMutation("foo/bar", map("sum", 0)));
    assertChanged(doc("foo/bar", 0, map("sum", 0)).setHasLocalMutations());
    assertContains(doc("foo/bar", 0, map("sum", 0)).setHasLocalMutations());
    applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 1, map("sum", 0)), asList(2), emptyList()));
    acknowledgeMutation(1);
    assertChanged(doc("foo/bar", 1, map("sum", 0)));
    assertContains(doc("foo/bar", 1, map("sum", 0)));
    writeMutation(patchMutation("foo/bar", map("sum", FieldValue.increment(1))));
    assertChanged(doc("foo/bar", 1, map("sum", 1)).setHasLocalMutations());
    assertContains(doc("foo/bar", 1, map("sum", 1)).setHasLocalMutations());
    // The value in this remote event gets ignored since we still have a pending transform mutation.
    applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 2, map("sum", 1337)), asList(2), emptyList()));
    assertChanged(doc("foo/bar", 2, map("sum", 1)).setHasLocalMutations());
    assertContains(doc("foo/bar", 2, map("sum", 1)).setHasLocalMutations());
    // Add another increment. Note that we still compute the increment based on the local value.
    writeMutation(patchMutation("foo/bar", map("sum", FieldValue.increment(2))));
    assertChanged(doc("foo/bar", 2, map("sum", 3)).setHasLocalMutations());
    assertContains(doc("foo/bar", 2, map("sum", 3)).setHasLocalMutations());
    acknowledgeMutationWithTransformResults(3, 1);
    assertChanged(doc("foo/bar", 3, map("sum", 3)).setHasLocalMutations());
    assertContains(doc("foo/bar", 3, map("sum", 3)).setHasLocalMutations());
    acknowledgeMutationWithTransformResults(4, 1339);
    assertChanged(doc("foo/bar", 4, map("sum", 1339)).setHasCommittedMutations());
    assertContains(doc("foo/bar", 4, map("sum", 1339)).setHasCommittedMutations());
}
Also used : BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 94 with Query

use of com.google.firebase.firestore.core.Query in project firebase-android-sdk by firebase.

the class LocalStoreTestCase method testCollectsGarbageAfterChangeBatch.

@Test
public void testCollectsGarbageAfterChangeBatch() {
    assumeTrue(garbageCollectorIsEager());
    Query query = query("foo");
    allocateQuery(query);
    assertTargetId(2);
    List<Integer> none = asList();
    List<Integer> two = asList(2);
    applyRemoteEvent(addedRemoteEvent(doc("foo/bar", 2, map("foo", "bar")), two, none));
    assertContains(doc("foo/bar", 2, map("foo", "bar")));
    applyRemoteEvent(updateRemoteEvent(doc("foo/bar", 2, map("foo", "baz")), none, two));
    assertNotContains("foo/bar");
}
Also used : BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 95 with Query

use of com.google.firebase.firestore.core.Query in project firebase-android-sdk by firebase.

the class LocalStoreTestCase method testLastLimboFreeSnapshotIsAdvancedDuringViewProcessing.

@Test
public void testLastLimboFreeSnapshotIsAdvancedDuringViewProcessing() {
    // This test verifies that the `lastLimboFreeSnapshot` version for TargetData is advanced when
    // we compute a limbo-free free view and that the mapping is persisted when we release a target.
    Query query = query("foo");
    Target target = query.toTarget();
    int targetId = allocateQuery(query);
    // Advance the target snapshot.
    applyRemoteEvent(noChangeEvent(targetId, 10));
    // At this point, we have not yet confirmed that the target is limbo free.
    TargetData cachedTargetData = localStore.getTargetData(target);
    Assert.assertEquals(SnapshotVersion.NONE, cachedTargetData.getLastLimboFreeSnapshotVersion());
    // Mark the view synced, which updates the last limbo free snapshot version.
    updateViews(targetId, /* fromCache=*/
    false);
    cachedTargetData = localStore.getTargetData(target);
    Assert.assertEquals(version(10), cachedTargetData.getLastLimboFreeSnapshotVersion());
    // The last limbo free snapshot version is persisted even if we release the target.
    releaseTarget(targetId);
    if (!garbageCollectorIsEager()) {
        cachedTargetData = localStore.getTargetData(target);
        Assert.assertEquals(version(10), cachedTargetData.getLastLimboFreeSnapshotVersion());
    }
}
Also used : Target(com.google.firebase.firestore.core.Target) BundledQuery(com.google.firebase.firestore.bundle.BundledQuery) NamedQuery(com.google.firebase.firestore.bundle.NamedQuery) Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Aggregations

Query (com.google.firebase.firestore.core.Query)205 Test (org.junit.Test)195 BundledQuery (com.google.firebase.firestore.bundle.BundledQuery)42 NamedQuery (com.google.firebase.firestore.bundle.NamedQuery)41 DocumentSet (com.google.firebase.firestore.model.DocumentSet)18 StructuredQuery (com.google.firestore.v1.StructuredQuery)16 Target (com.google.firestore.v1.Target)13 DocumentsTarget (com.google.firestore.v1.Target.DocumentsTarget)13 QueryTarget (com.google.firestore.v1.Target.QueryTarget)13 MutableDocument (com.google.firebase.firestore.model.MutableDocument)12 FieldIndex (com.google.firebase.firestore.model.FieldIndex)8 TargetData (com.google.firebase.firestore.local.TargetData)7 ArrayList (java.util.ArrayList)7 DocumentKey (com.google.firebase.firestore.model.DocumentKey)5 ByteString (com.google.protobuf.ByteString)5 HashMap (java.util.HashMap)4 Document (com.google.firebase.firestore.model.Document)3 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)3 Timestamp (com.google.firebase.Timestamp)2 QueryListener (com.google.firebase.firestore.core.QueryListener)2