Search in sources :

Example 71 with Query

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

the class SQLiteIndexManagerTest method testNoMatchingDocs.

@Test
public void testNoMatchingDocs() {
    setUpSingleValueFilter();
    Query query = query("coll").filter(filter("count", "==", -1));
    verifyResults(query);
}
Also used : Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 72 with Query

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

the class SQLiteIndexManagerTest method testLimitFilter.

@Test
public void testLimitFilter() {
    indexManager.addFieldIndex(fieldIndex("coll", "value", Kind.ASCENDING));
    addDoc("coll/doc1", map("value", 1));
    addDoc("coll/doc2", map("value", 1));
    addDoc("coll/doc3", map("value", 1));
    Query query = query("coll").filter(filter("value", "==", 1)).limitToFirst(2);
    verifyResults(query, "coll/doc1", "coll/doc2");
}
Also used : Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 73 with Query

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

the class SQLiteIndexManagerTest method testAdvancedQueries.

@Test
public void testAdvancedQueries() {
    // This test compares local query results with those received from the Java Server SDK.
    indexManager.addFieldIndex(fieldIndex("coll", "null", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "int", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "float", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "string", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "multi", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "array", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "array", Kind.DESCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "array", Kind.CONTAINS));
    indexManager.addFieldIndex(fieldIndex("coll", "map", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "map.field", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "prefix", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "prefix", Kind.ASCENDING, "suffix", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "a", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "a", Kind.ASCENDING, "b", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "a", Kind.DESCENDING, "b", Kind.ASCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "a", Kind.ASCENDING, "b", Kind.DESCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "a", Kind.DESCENDING, "b", Kind.DESCENDING));
    indexManager.addFieldIndex(fieldIndex("coll", "b", Kind.ASCENDING, "a", Kind.ASCENDING));
    List<Map<String, Object>> data = new ArrayList<Map<String, Object>>() {

        {
            add(map());
            add(map("int", 1, "array", Arrays.asList(1, "foo")));
            add(map("array", Arrays.asList(2, "foo")));
            add(map("int", 3, "array", Arrays.asList(3, "foo")));
            add(map("array", "foo"));
            add(map("array", Collections.singletonList(1)));
            add(map("float", -0.0, "string", "a"));
            add(map("float", 0, "string", "ab"));
            add(map("float", 0.0, "string", "b"));
            add(map("float", Double.NaN));
            add(map("multi", true));
            add(map("multi", 1));
            add(map("multi", "string"));
            add(map("multi", Collections.emptyList()));
            add(map("null", null));
            add(map("prefix", Arrays.asList(1, 2), "suffix", null));
            add(map("prefix", Collections.singletonList(1), "suffix", 2));
            add(map("map", map()));
            add(map("map", map("field", true)));
            add(map("map", map("field", false)));
            add(map("a", 0, "b", 0));
            add(map("a", 0, "b", 1));
            add(map("a", 1, "b", 0));
            add(map("a", 1, "b", 1));
            add(map("a", 2, "b", 0));
            add(map("a", 2, "b", 1));
        }
    };
    for (int i = 0; i < data.size(); ++i) {
        addDoc("coll/" + Values.canonicalId(wrap(data.get(i))), data.get(i));
    }
    Query q = query("coll");
    verifyResults(q.orderBy(orderBy("int")), "coll/{array:[1,foo],int:1}", "coll/{array:[3,foo],int:3}");
    verifyResults(q.filter(filter("float", "==", Double.NaN)), "coll/{float:NaN}");
    verifyResults(q.filter(filter("float", "==", -0.0)), "coll/{float:-0.0,string:a}", "coll/{float:0,string:ab}", "coll/{float:0.0,string:b}");
    verifyResults(q.filter(filter("float", "==", 0)), "coll/{float:-0.0,string:a}", "coll/{float:0,string:ab}", "coll/{float:0.0,string:b}");
    verifyResults(q.filter(filter("float", "==", 0.0)), "coll/{float:-0.0,string:a}", "coll/{float:0,string:ab}", "coll/{float:0.0,string:b}");
    verifyResults(q.filter(filter("string", "==", "a")), "coll/{float:-0.0,string:a}");
    verifyResults(q.filter(filter("string", ">", "a")), "coll/{float:0,string:ab}", "coll/{float:0.0,string:b}");
    verifyResults(q.filter(filter("string", ">=", "a")), "coll/{float:-0.0,string:a}", "coll/{float:0,string:ab}", "coll/{float:0.0,string:b}");
    verifyResults(q.filter(filter("string", "<", "b")), "coll/{float:-0.0,string:a}", "coll/{float:0,string:ab}");
    verifyResults(q.filter(filter("string", "<", "coll")), "coll/{float:-0.0,string:a}", "coll/{float:0,string:ab}", "coll/{float:0.0,string:b}");
    verifyResults(q.filter(filter("string", ">", "a")).filter(filter("string", "<", "b")), "coll/{float:0,string:ab}");
    verifyResults(q.filter(filter("array", "array-contains", "foo")), "coll/{array:[1,foo],int:1}", "coll/{array:[2,foo]}", "coll/{array:[3,foo],int:3}");
    verifyResults(q.filter(filter("array", "array-contains-any", Arrays.asList(1, "foo"))), "coll/{array:[1,foo],int:1}", "coll/{array:[1]}", "coll/{array:[2,foo]}", "coll/{array:[3,foo],int:3}");
    verifyResults(q.filter(filter("multi", ">=", true)), "coll/{multi:true}");
    verifyResults(q.filter(filter("multi", ">=", 0)), "coll/{multi:1}");
    verifyResults(q.filter(filter("multi", ">=", "")), "coll/{multi:string}");
    verifyResults(q.filter(filter("multi", ">=", Collections.emptyList())), "coll/{multi:[]}");
    verifyResults(q.filter(filter("multi", "!=", true)), "coll/{multi:1}", "coll/{multi:string}", "coll/{multi:[]}");
    verifyResults(q.filter(filter("multi", "in", Arrays.asList(true, 1))), "coll/{multi:true}", "coll/{multi:1}");
    verifyResults(q.filter(filter("multi", "not-in", Arrays.asList(true, 1))), "coll/{multi:string}", "coll/{multi:[]}");
    verifyResults(q.orderBy(orderBy("array")).startAt(bound(true, Collections.singletonList(2))), "coll/{array:[2,foo]}", "coll/{array:[3,foo],int:3}");
    verifyResults(q.orderBy(orderBy("array", "desc")).startAt(bound(true, Collections.singletonList(2))), "coll/{array:[1,foo],int:1}", "coll/{array:[1]}", "coll/{array:foo}");
    verifyResults(q.orderBy(orderBy("array", "desc")).startAt(bound(true, Collections.singletonList(2))).limitToFirst(2), "coll/{array:[1,foo],int:1}", "coll/{array:[1]}");
    verifyResults(q.orderBy(orderBy("array")).startAt(bound(false, Collections.singletonList(2))), "coll/{array:[2,foo]}", "coll/{array:[3,foo],int:3}");
    verifyResults(q.orderBy(orderBy("array", "desc")).startAt(bound(false, Collections.singletonList(2))), "coll/{array:[1,foo],int:1}", "coll/{array:[1]}", "coll/{array:foo}");
    verifyResults(q.orderBy(orderBy("array", "desc")).startAt(bound(false, Collections.singletonList(2))).limitToFirst(2), "coll/{array:[1,foo],int:1}", "coll/{array:[1]}");
    verifyResults(q.orderBy(orderBy("array")).startAt(bound(false, Arrays.asList(2, "foo"))), "coll/{array:[3,foo],int:3}");
    verifyResults(q.orderBy(orderBy("array", "desc")).startAt(bound(false, Arrays.asList(2, "foo"))), "coll/{array:[1,foo],int:1}", "coll/{array:[1]}", "coll/{array:foo}");
    verifyResults(q.orderBy(orderBy("array", "desc")).startAt(bound(false, Arrays.asList(2, "foo"))).limitToFirst(2), "coll/{array:[1,foo],int:1}", "coll/{array:[1]}");
    verifyResults(q.orderBy(orderBy("array")).endAt(bound(true, Collections.singletonList(2))), "coll/{array:foo}", "coll/{array:[1]}", "coll/{array:[1,foo],int:1}");
    verifyResults(q.orderBy(orderBy("array", "desc")).endAt(bound(true, Collections.singletonList(2))), "coll/{array:[3,foo],int:3}", "coll/{array:[2,foo]}");
    verifyResults(q.orderBy(orderBy("array")).endAt(bound(false, Collections.singletonList(2))), "coll/{array:foo}", "coll/{array:[1]}", "coll/{array:[1,foo],int:1}");
    verifyResults(q.orderBy(orderBy("array")).endAt(bound(false, Collections.singletonList(2))).limitToFirst(2), "coll/{array:foo}", "coll/{array:[1]}");
    verifyResults(q.orderBy(orderBy("array", "desc")).endAt(bound(false, Collections.singletonList(2))), "coll/{array:[3,foo],int:3}", "coll/{array:[2,foo]}");
    verifyResults(q.orderBy(orderBy("array")).endAt(bound(false, Arrays.asList(2, "foo"))), "coll/{array:foo}", "coll/{array:[1]}", "coll/{array:[1,foo],int:1}");
    verifyResults(q.orderBy(orderBy("array")).endAt(bound(false, Arrays.asList(2, "foo"))).limitToFirst(2), "coll/{array:foo}", "coll/{array:[1]}");
    verifyResults(q.orderBy(orderBy("array", "desc")).endAt(bound(false, Arrays.asList(2, "foo"))), "coll/{array:[3,foo],int:3}");
    verifyResults(q.orderBy(orderBy("a")).orderBy(orderBy("b")).limitToFirst(1), "coll/{a:0,b:0}");
    verifyResults(q.orderBy(orderBy("a", "desc")).orderBy(orderBy("b")).limitToFirst(1), "coll/{a:2,b:0}");
    verifyResults(q.orderBy(orderBy("a")).orderBy(orderBy("b", "desc")).limitToFirst(1), "coll/{a:0,b:1}");
    verifyResults(q.orderBy(orderBy("a", "desc")).orderBy(orderBy("b", "desc")).limitToFirst(1), "coll/{a:2,b:1}");
    verifyResults(q.filter(filter("a", ">", 0)).filter(filter("b", "==", 1)), "coll/{a:1,b:1}", "coll/{a:2,b:1}");
    verifyResults(q.filter(filter("a", "==", 1)).filter(filter("b", "==", 1)), "coll/{a:1,b:1}");
    verifyResults(q.filter(filter("a", "!=", 0)).filter(filter("b", "==", 1)), "coll/{a:1,b:1}", "coll/{a:2,b:1}");
    verifyResults(q.filter(filter("b", "==", 1)).filter(filter("a", "!=", 0)), "coll/{a:1,b:1}", "coll/{a:2,b:1}");
    verifyResults(q.filter(filter("a", "not-in", Arrays.asList(0, 1))), "coll/{a:2,b:0}", "coll/{a:2,b:1}");
    verifyResults(q.filter(filter("a", "not-in", Arrays.asList(0, 1))).filter(filter("b", "==", 1)), "coll/{a:2,b:1}");
    verifyResults(q.filter(filter("b", "==", 1)).filter(filter("a", "not-in", Arrays.asList(0, 1))), "coll/{a:2,b:1}");
    verifyResults(q.filter(filter("null", "==", null)), "coll/{null:null}");
    verifyResults(q.orderBy(orderBy("null")), "coll/{null:null}");
    verifyResults(q.filter(filter("prefix", "==", Arrays.asList(1, 2))), "coll/{prefix:[1,2],suffix:null}");
    verifyResults(q.filter(filter("prefix", "==", Collections.singletonList(1))).filter(filter("suffix", "==", 2)), "coll/{prefix:[1],suffix:2}");
    verifyResults(q.filter(filter("map", "==", map())), "coll/{map:{}}");
    verifyResults(q.filter(filter("map", "==", map("field", true))), "coll/{map:{field:true}}");
    verifyResults(q.filter(filter("map.field", "==", true)), "coll/{map:{field:true}}");
    verifyResults(q.orderBy(orderBy("map")), "coll/{map:{}}", "coll/{map:{field:false}}", "coll/{map:{field:true}}");
    verifyResults(q.orderBy(orderBy("map.field")), "coll/{map:{field:false}}", "coll/{map:{field:true}}");
}
Also used : Query(com.google.firebase.firestore.core.Query) ArrayList(java.util.ArrayList) Map(java.util.Map) TestUtil.docMap(com.google.firebase.firestore.testutil.TestUtil.docMap) Test(org.junit.Test)

Example 74 with Query

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

the class SQLiteIndexManagerTest method testStartAtFilter.

@Test
public void testStartAtFilter() {
    setUpSingleValueFilter();
    Query query = query("coll").orderBy(orderBy("count")).startAt(bound(/* inclusive= */
    true, 2));
    verifyResults(query, "coll/val2", "coll/val3");
}
Also used : Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 75 with Query

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

the class SQLiteIndexManagerTest method testInFilter.

@Test
public void testInFilter() {
    setUpSingleValueFilter();
    Query query = query("coll").filter(filter("count", "in", Arrays.asList(1, 3)));
    verifyResults(query, "coll/val1", "coll/val3");
}
Also used : 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