Search in sources :

Example 6 with Query

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

the class SQLiteIndexManagerTest method testNotEqualsFilter.

@Test
public void testNotEqualsFilter() {
    setUpSingleValueFilter();
    Query query = query("coll").filter(filter("count", "!=", 2));
    verifyResults(query, "coll/val1", "coll/val3");
}
Also used : Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 7 with Query

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

the class SQLiteIndexManagerTest method testEqualsWithNotEqualsFilter.

@Test
public void testEqualsWithNotEqualsFilter() {
    indexManager.addFieldIndex(fieldIndex("coll", "a", Kind.ASCENDING, "b", Kind.ASCENDING));
    addDoc("coll/val1", map("a", 1, "b", 1));
    addDoc("coll/val2", map("a", 1, "b", 2));
    addDoc("coll/val3", map("a", 2, "b", 1));
    addDoc("coll/val4", map("a", 2, "b", 2));
    // Verifies that we apply the filter in the order of the field index
    Query query = query("coll").filter(filter("a", "==", 1)).filter(filter("b", "!=", 1));
    verifyResults(query, "coll/val2");
    query = query("coll").filter(filter("b", "!=", 1)).filter(filter("a", "==", 1));
    verifyResults(query, "coll/val2");
}
Also used : Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 8 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 9 with Query

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

the class SQLiteIndexManagerTest method testArrayContainsDoesNotMatchNonArray.

@Test
public void testArrayContainsDoesNotMatchNonArray() {
    // Set up two field indices. This causes two index entries to be written, but our query should
    // only use one index.
    setUpArrayValueFilter();
    setUpSingleValueFilter();
    addDoc("coll/nonmatching", map("values", 1));
    Query query = query("coll").filter(filter("values", "array-contains-any", Arrays.asList(1)));
    verifyResults(query, "coll/arr1");
}
Also used : Query(com.google.firebase.firestore.core.Query) Test(org.junit.Test)

Example 10 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:[2,foo]}", "coll/{array:[3,foo],int:3}", "coll/{array:[1]}");
    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:foo}", "coll/{array:[1]}");
    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:foo}", "coll/{array:[1]}");
    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:foo}", "coll/{array:[1]}");
    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:[1,foo],int:1}", "coll/{array:foo}", "coll/{array:[1]}");
    verifyResults(q.orderBy(orderBy("array", "desc")).endAt(bound(true, Collections.singletonList(2))), "coll/{array:[2,foo]}", "coll/{array:[3,foo],int:3}");
    verifyResults(q.orderBy(orderBy("array")).endAt(bound(false, Collections.singletonList(2))), "coll/{array:[1,foo],int:1}", "coll/{array:foo}", "coll/{array:[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:[2,foo]}", "coll/{array:[3,foo],int:3}");
    verifyResults(q.orderBy(orderBy("array")).endAt(bound(false, Arrays.asList(2, "foo"))), "coll/{array:[1,foo],int:1}", "coll/{array:foo}", "coll/{array:[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:true}}", "coll/{map:{field:false}}");
    verifyResults(q.orderBy(orderBy("map.field")), "coll/{map:{field:true}}", "coll/{map:{field:false}}");
}
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)

Aggregations

Query (com.google.firebase.firestore.core.Query)189 Test (org.junit.Test)179 BundledQuery (com.google.firebase.firestore.bundle.BundledQuery)40 NamedQuery (com.google.firebase.firestore.bundle.NamedQuery)39 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 DocumentSet (com.google.firebase.firestore.model.DocumentSet)12 MutableDocument (com.google.firebase.firestore.model.MutableDocument)8 ArrayList (java.util.ArrayList)8 TargetData (com.google.firebase.firestore.local.TargetData)7 FieldIndex (com.google.firebase.firestore.model.FieldIndex)7 DocumentKey (com.google.firebase.firestore.model.DocumentKey)6 ByteString (com.google.protobuf.ByteString)5 Document (com.google.firebase.firestore.model.Document)4 HashMap (java.util.HashMap)4 Target (com.google.firebase.firestore.core.Target)3 SnapshotVersion (com.google.firebase.firestore.model.SnapshotVersion)3 Map (java.util.Map)3