Search in sources :

Example 51 with MutableDocument

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

the class QueryTest method testInFilters.

@Test
public void testInFilters() {
    Query query = Query.atPath(ResourcePath.fromString("collection")).filter(filter("zip", "in", asList(12345)));
    MutableDocument document = doc("collection/1", 0, map("zip", 12345));
    assertTrue(query.matches(document));
    // Value matches in array.
    document = doc("collection/1", 0, map("zip", asList(12345)));
    assertFalse(query.matches(document));
    // Non-type match.
    document = doc("collection/1", 0, map("zip", "12345"));
    assertFalse(query.matches(document));
    // Nested match.
    document = doc("collection/1", 0, map("zip", asList("12345", map("zip", 12345))));
    assertFalse(query.matches(document));
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 52 with MutableDocument

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

the class QueryTest method testInFiltersWithObjectValues.

@Test
public void testInFiltersWithObjectValues() {
    Query query = Query.atPath(ResourcePath.fromString("collection")).filter(filter("zip", "in", asList(map("a", asList(42)))));
    // Containing object in array.
    MutableDocument document = doc("collection/1", 0, map("zip", asList(map("a", asList(42)))));
    assertFalse(query.matches(document));
    // Containing object.
    document = doc("collection/1", 0, map("zip", map("a", asList(42))));
    assertTrue(query.matches(document));
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 53 with MutableDocument

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

the class QueryTest method testArrayContainsFiltersWithObjectValues.

@Test
public void testArrayContainsFiltersWithObjectValues() {
    // Search for arrays containing the object { a: [42] }
    Query query = Query.atPath(ResourcePath.fromString("collection")).filter(filter("array", "array-contains", map("a", asList(42))));
    // array without element
    MutableDocument document = doc("collection/1", 0, map("array", asList(map("a", 42L), map("a", asList(42L, 43L)), map("b", asList(42L)), map("a", asList(42L), "b", 42L))));
    assertFalse(query.matches(document));
    // array with element
    document = doc("collection/1", 0, map("array", asList(1L, "2", 42L, map("a", asList(42L)))));
    assertTrue(query.matches(document));
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 54 with MutableDocument

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

the class QueryTest method testFiltersObjects.

@Test
public void testFiltersObjects() {
    Query baseQuery = Query.atPath(ResourcePath.fromString("collection"));
    MutableDocument doc1 = doc("collection/doc", 0, map("tags", map("foo", "foo", "a", 0, "b", true, "c", Double.NaN)));
    List<Filter> matchingFilters = asList(filter("tags", "==", map("foo", "foo", "a", 0, "b", true, "c", Double.NaN)), filter("tags", "==", map("b", true, "a", 0, "foo", "foo", "c", Double.NaN)), filter("tags.foo", "==", "foo"));
    List<Filter> nonMatchingFilters = asList(filter("tags", "==", "foo"), filter("tags", "==", map("foo", "foo", "a", 0, "b", true)));
    for (Filter filter : matchingFilters) {
        assertTrue(baseQuery.filter(filter).matches(doc1));
    }
    for (Filter filter : nonMatchingFilters) {
        assertFalse(baseQuery.filter(filter).matches(doc1));
    }
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) Test(org.junit.Test)

Example 55 with MutableDocument

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

the class ViewTest method testViewsWithLimboDocumentsAreMarkedFromCache.

@Test
public void testViewsWithLimboDocumentsAreMarkedFromCache() {
    Query query = messageQuery();
    View view = new View(query, DocumentKey.emptyKeySet());
    MutableDocument doc1 = doc("rooms/eros/messages/0", 0, map());
    MutableDocument doc2 = doc("rooms/eros/messages/1", 0, map());
    // Doc1 is contained in the local view, but we are not yet CURRENT so it is expected that the
    // backend hasn't told us about all documents yet.
    ViewChange change = applyChanges(view, doc1);
    assertTrue(change.getSnapshot().isFromCache());
    // Add doc2 to generate a snapshot. Doc1 is still missing.
    View.DocumentChanges viewDocChanges = view.computeDocChanges(docUpdates(doc2));
    change = view.applyChanges(viewDocChanges, targetChange(ByteString.EMPTY, true, asList(doc2), null, null));
    // We are CURRENT but doc1 is in limbo.
    assertTrue(change.getSnapshot().isFromCache());
    // Add doc1 to the backend's result set.
    change = view.applyChanges(viewDocChanges, targetChange(ByteString.EMPTY, true, asList(doc1), null, null));
    assertFalse(change.getSnapshot().isFromCache());
}
Also used : MutableDocument(com.google.firebase.firestore.model.MutableDocument) 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