FIRQueryTests.m 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. @import FirebaseFirestore;
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  19. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  20. @interface FIRQueryTests : FSTIntegrationTestCase
  21. @end
  22. @implementation FIRQueryTests
  23. - (void)testLimitQueries {
  24. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  25. @"a" : @{@"k" : @"a"},
  26. @"b" : @{@"k" : @"b"},
  27. @"c" : @{@"k" : @"c"}
  28. }];
  29. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[collRef queryLimitedTo:2]];
  30. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"k" : @"a"}, @{@"k" : @"b"} ]));
  31. }
  32. - (void)testLimitQueriesWithDescendingSortOrder {
  33. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  34. @"a" : @{@"k" : @"a", @"sort" : @0},
  35. @"b" : @{@"k" : @"b", @"sort" : @1},
  36. @"c" : @{@"k" : @"c", @"sort" : @1},
  37. @"d" : @{@"k" : @"d", @"sort" : @2},
  38. }];
  39. FIRQuerySnapshot *snapshot =
  40. [self readDocumentSetForRef:[[collRef queryOrderedByField:@"sort" descending:YES]
  41. queryLimitedTo:2]];
  42. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  43. @{ @"k" : @"d",
  44. @"sort" : @2 },
  45. @{ @"k" : @"c",
  46. @"sort" : @1 }
  47. ]));
  48. }
  49. - (void)testKeyOrderIsDescendingForDescendingInequality {
  50. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  51. @"a" : @{@"foo" : @42},
  52. @"b" : @{@"foo" : @42.0},
  53. @"c" : @{@"foo" : @42},
  54. @"d" : @{@"foo" : @21},
  55. @"e" : @{@"foo" : @21.0},
  56. @"f" : @{@"foo" : @66},
  57. @"g" : @{@"foo" : @66.0},
  58. }];
  59. FIRQuerySnapshot *snapshot =
  60. [self readDocumentSetForRef:[[collRef queryWhereField:@"foo" isGreaterThan:@21]
  61. queryOrderedByField:@"foo"
  62. descending:YES]];
  63. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"g", @"f", @"c", @"b", @"a" ]));
  64. }
  65. - (void)testUnaryFilterQueries {
  66. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  67. @"a" : @{@"null" : [NSNull null], @"nan" : @(NAN)},
  68. @"b" : @{@"null" : [NSNull null], @"nan" : @0},
  69. @"c" : @{@"null" : @NO, @"nan" : @(NAN)}
  70. }];
  71. FIRQuerySnapshot *results =
  72. [self readDocumentSetForRef:[[collRef queryWhereField:@"null" isEqualTo:[NSNull null]]
  73. queryWhereField:@"nan"
  74. isEqualTo:@(NAN)]];
  75. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[
  76. @{ @"null" : [NSNull null],
  77. @"nan" : @(NAN) }
  78. ]));
  79. }
  80. - (void)testQueryWithFieldPaths {
  81. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  82. @"a" : @{@"a" : @1},
  83. @"b" : @{@"a" : @2},
  84. @"c" : @{@"a" : @3}
  85. }];
  86. FIRQuery *query =
  87. [collRef queryWhereFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]] isLessThan:@3];
  88. query = [query queryOrderedByFieldPath:[[FIRFieldPath alloc] initWithFields:@[ @"a" ]]
  89. descending:YES];
  90. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:query];
  91. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(snapshot), (@[ @"b", @"a" ]));
  92. }
  93. - (void)testFilterOnInfinity {
  94. FIRCollectionReference *collRef = [self collectionRefWithDocuments:@{
  95. @"a" : @{@"inf" : @(INFINITY)},
  96. @"b" : @{@"inf" : @(-INFINITY)}
  97. }];
  98. FIRQuerySnapshot *results =
  99. [self readDocumentSetForRef:[collRef queryWhereField:@"inf" isEqualTo:@(INFINITY)]];
  100. XCTAssertEqualObjects(FIRQuerySnapshotGetData(results), (@[ @{ @"inf" : @(INFINITY) } ]));
  101. }
  102. - (void)testCanExplicitlySortByDocumentID {
  103. NSDictionary *testDocs = @{
  104. @"a" : @{@"key" : @"a"},
  105. @"b" : @{@"key" : @"b"},
  106. @"c" : @{@"key" : @"c"},
  107. };
  108. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  109. // Ideally this would be descending to validate it's different than
  110. // the default, but that requires an extra index
  111. FIRQuerySnapshot *docs =
  112. [self readDocumentSetForRef:[collection queryOrderedByFieldPath:[FIRFieldPath documentID]]];
  113. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs),
  114. (@[ testDocs[@"a"], testDocs[@"b"], testDocs[@"c"] ]));
  115. }
  116. - (void)testCanQueryByDocumentID {
  117. NSDictionary *testDocs = @{
  118. @"aa" : @{@"key" : @"aa"},
  119. @"ab" : @{@"key" : @"ab"},
  120. @"ba" : @{@"key" : @"ba"},
  121. @"bb" : @{@"key" : @"bb"},
  122. };
  123. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  124. FIRQuerySnapshot *docs =
  125. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  126. isEqualTo:@"ab"]];
  127. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  128. }
  129. - (void)testCanQueryByDocumentIDs {
  130. NSDictionary *testDocs = @{
  131. @"aa" : @{@"key" : @"aa"},
  132. @"ab" : @{@"key" : @"ab"},
  133. @"ba" : @{@"key" : @"ba"},
  134. @"bb" : @{@"key" : @"bb"},
  135. };
  136. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  137. FIRQuerySnapshot *docs =
  138. [self readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  139. isEqualTo:@"ab"]];
  140. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  141. docs = [self readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  142. isGreaterThan:@"aa"]
  143. queryWhereFieldPath:[FIRFieldPath documentID]
  144. isLessThanOrEqualTo:@"ba"]];
  145. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  146. }
  147. - (void)testCanQueryByDocumentIDsUsingRefs {
  148. NSDictionary *testDocs = @{
  149. @"aa" : @{@"key" : @"aa"},
  150. @"ab" : @{@"key" : @"ab"},
  151. @"ba" : @{@"key" : @"ba"},
  152. @"bb" : @{@"key" : @"bb"},
  153. };
  154. FIRCollectionReference *collection = [self collectionRefWithDocuments:testDocs];
  155. FIRQuerySnapshot *docs = [self
  156. readDocumentSetForRef:[collection queryWhereFieldPath:[FIRFieldPath documentID]
  157. isEqualTo:[collection documentWithPath:@"ab"]]];
  158. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"] ]));
  159. docs = [self
  160. readDocumentSetForRef:[[collection queryWhereFieldPath:[FIRFieldPath documentID]
  161. isGreaterThan:[collection documentWithPath:@"aa"]]
  162. queryWhereFieldPath:[FIRFieldPath documentID]
  163. isLessThanOrEqualTo:[collection documentWithPath:@"ba"]]];
  164. XCTAssertEqualObjects(FIRQuerySnapshotGetData(docs), (@[ testDocs[@"ab"], testDocs[@"ba"] ]));
  165. }
  166. @end