| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197 |
- /*
- * Copyright 2017 Google
- *
- * Licensed under the Apache License, Version 2.0 (the "License");
- * you may not use this file except in compliance with the License.
- * You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
- @import FirebaseFirestore;
- #import <XCTest/XCTest.h>
- #import "Firestore/Source/Core/FSTFirestoreClient.h"
- #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
- @interface FIRQueryTests : FSTIntegrationTestCase
- @end
- @implementation FIRQueryTests
- - (void)testLimitQueries {
- FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
- @"a" : @{@"k" : @"a"},
- @"b" : @{@"k" : @"b"},
- @"c" : @{@"k" : @"c"}
- }];
- FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collRef queryLimitedTo:2]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"a"}, @{@"k" : @"b"} ]));
- }
- - (void)testLimitQueriesWithDescendingSortOrder {
- FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
- @"a" : @{@"k" : @"a", @"sort" : @0},
- @"b" : @{@"k" : @"b", @"sort" : @1},
- @"c" : @{@"k" : @"c", @"sort" : @1},
- @"d" : @{@"k" : @"d", @"sort" : @2},
- }];
- FIRQuerySnapshot *snapshot =
- [self readDocumentSetForRef:[[collRef queryOrderedByField:@"sort" descending:YES]
- queryLimitedTo:2]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
- @{ @"k" : @"d",
- @"sort" : @2 },
- @{ @"k" : @"c",
- @"sort" : @1 }
- ]));
- }
- - (void)testKeyOrderIsDescendingForDescendingInequality {
- FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
- @"a" : @{@"foo" : @42},
- @"b" : @{@"foo" : @42.0},
- @"c" : @{@"foo" : @42},
- @"d" : @{@"foo" : @21},
- @"e" : @{@"foo" : @21.0},
- @"f" : @{@"foo" : @66},
- @"g" : @{@"foo" : @66.0},
- }];
- FIRQuerySnapshot *snapshot =
- [self readDocumentSetForRef:[[collRef queryWhereField:@"foo" isGreaterThan:@21]
- queryOrderedByField:@"foo"
- descending:YES]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
- }
- - (void)testUnaryFilterQueries {
- FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
- @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
- @"b" : @{@"null" : [NSNull null], @"nan" : @0},
- @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
- }];
- FIRQuerySnapshot *results =
- [self readDocumentSetForRef:[[collRef queryWhereField:@"null" isEqualTo:[NSNull null]]
- queryWhereField:@"nan"
- isEqualTo:@(NAN)]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[
- @{ @"null" : [NSNull null],
- @"nan" : @(NAN) }
- ]));
- }
- - (void)testQueryWithFieldPaths {
- FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
- @"a" : @{@"a" : @1},
- @"b" : @{@"a" : @2},
- @"c" : @{@"a" : @3}
- }];
- FIRQuery *query =
- [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]] isLessThan:@3];
- query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
- descending:YES];
- FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
- XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
- }
- - (void)testFilterOnInfinity {
- FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
- @"a" : @{@"inf" : @(INFINITY)},
- @"b" : @{@"inf" : @(-INFINITY)}
- }];
- FIRQuerySnapshot *results =
- [self readDocumentSetForRef:[collRef queryWhereField:@"inf" isEqualTo:@(INFINITY)]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{ @"inf" : @(INFINITY) } ]));
- }
- - (void)testCanExplicitlySortByDocumentID {
- NSDictionary *testDocs = @{
- @"a" : @{@"key" : @"a"},
- @"b" : @{@"key" : @"b"},
- @"c" : @{@"key" : @"c"},
- };
- FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
- // Ideally this would be descending to validate it's different than
- // the default, but that requires an extra index
- FIRQuerySnapshot *docs =
- [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
- (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
- }
- - (void)testCanQueryByDocumentID {
- NSDictionary *testDocs = @{
- @"aa" : @{@"key" : @"aa"},
- @"ab" : @{@"key" : @"ab"},
- @"ba" : @{@"key" : @"ba"},
- @"bb" : @{@"key" : @"bb"},
- };
- FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
- FIRQuerySnapshot *docs =
- [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
- isEqualTo:@"ab"]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
- }
- - (void)testCanQueryByDocumentIDs {
- NSDictionary *testDocs = @{
- @"aa" : @{@"key" : @"aa"},
- @"ab" : @{@"key" : @"ab"},
- @"ba" : @{@"key" : @"ba"},
- @"bb" : @{@"key" : @"bb"},
- };
- FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
- FIRQuerySnapshot *docs =
- [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
- isEqualTo:@"ab"]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
- docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
- isGreaterThan:@"aa"]
- queryWhereFieldPath:[FIRFieldPath documentID]
- isLessThanOrEqualTo:@"ba"]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
- }
- - (void)testCanQueryByDocumentIDsUsingRefs {
- NSDictionary *testDocs = @{
- @"aa" : @{@"key" : @"aa"},
- @"ab" : @{@"key" : @"ab"},
- @"ba" : @{@"key" : @"ba"},
- @"bb" : @{@"key" : @"bb"},
- };
- FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
- FIRQuerySnapshot *docs = [self
- readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
- isEqualTo:[collection documentWithPath:@"ab"]]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
- docs = [self
- readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
- isGreaterThan:[collection documentWithPath:@"aa"]]
- queryWhereFieldPath:[FIRFieldPath documentID]
- isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
- XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
- }
- @end
|