FIRCursorTests.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  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/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  19. @interface FIRCursorTests : FSTIntegrationTestCase
  20. @end
  21. @implementation FIRCursorTests
  22. - (void)testCanPageThroughItems {
  23. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  24. @"a" : @{@"v" : @"a"},
  25. @"b" : @{@"v" : @"b"},
  26. @"c" : @{@"v" : @"c"},
  27. @"d" : @{@"v" : @"d"},
  28. @"e" : @{@"v" : @"e"},
  29. @"f" : @{@"v" : @"f"}
  30. }];
  31. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[testCollection queryLimitedTo:2]];
  32. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"v" : @"a"}, @{@"v" : @"b"} ]));
  33. FIRDocumentSnapshot *lastDoc = snapshot.documents.lastObject;
  34. snapshot = [self
  35. readDocumentSetForRef:[[testCollection queryLimitedTo:3] queryStartingAfterDocument:lastDoc]];
  36. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot),
  37. (@[ @{@"v" : @"c"}, @{@"v" : @"d"}, @{@"v" : @"e"} ]));
  38. lastDoc = snapshot.documents.lastObject;
  39. snapshot = [self
  40. readDocumentSetForRef:[[testCollection queryLimitedTo:1] queryStartingAfterDocument:lastDoc]];
  41. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), @[ @{@"v" : @"f"} ]);
  42. lastDoc = snapshot.documents.lastObject;
  43. snapshot = [self
  44. readDocumentSetForRef:[[testCollection queryLimitedTo:3] queryStartingAfterDocument:lastDoc]];
  45. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), @[]);
  46. }
  47. - (void)testCanBeCreatedFromDocuments {
  48. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  49. @"a" : @{@"v" : @"a", @"sort" : @1.0},
  50. @"b" : @{@"v" : @"b", @"sort" : @2.0},
  51. @"c" : @{@"v" : @"c", @"sort" : @2.0},
  52. @"d" : @{@"v" : @"d", @"sort" : @2.0},
  53. @"e" : @{@"v" : @"e", @"sort" : @0.0},
  54. @"f" : @{@"v" : @"f", @"nosort" : @1.0} // should not show up
  55. }];
  56. FIRQuery *query = [testCollection queryOrderedByField:@"sort"];
  57. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:[testCollection documentWithPath:@"c"]];
  58. XCTAssertTrue(snapshot.exists);
  59. FIRQuerySnapshot *querySnapshot =
  60. [self readDocumentSetForRef:[query queryStartingAtDocument:snapshot]];
  61. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot),
  62. (@[ @{@"v" : @"c", @"sort" : @2.0}, @{@"v" : @"d", @"sort" : @2.0} ]));
  63. querySnapshot = [self readDocumentSetForRef:[query queryEndingBeforeDocument:snapshot]];
  64. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot), (@[
  65. @{@"v" : @"e", @"sort" : @0.0}, @{@"v" : @"a", @"sort" : @1.0},
  66. @{@"v" : @"b", @"sort" : @2.0}
  67. ]));
  68. }
  69. - (void)testCanBeCreatedFromValues {
  70. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  71. @"a" : @{@"v" : @"a", @"sort" : @1.0},
  72. @"b" : @{@"v" : @"b", @"sort" : @2.0},
  73. @"c" : @{@"v" : @"c", @"sort" : @2.0},
  74. @"d" : @{@"v" : @"d", @"sort" : @2.0},
  75. @"e" : @{@"v" : @"e", @"sort" : @0.0},
  76. @"f" : @{@"v" : @"f", @"nosort" : @1.0} // should not show up
  77. }];
  78. FIRQuery *query = [testCollection queryOrderedByField:@"sort"];
  79. FIRQuerySnapshot *querySnapshot =
  80. [self readDocumentSetForRef:[query queryStartingAtValues:@[ @2.0 ]]];
  81. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot), (@[
  82. @{@"v" : @"b", @"sort" : @2.0}, @{@"v" : @"c", @"sort" : @2.0},
  83. @{@"v" : @"d", @"sort" : @2.0}
  84. ]));
  85. querySnapshot = [self readDocumentSetForRef:[query queryEndingBeforeValues:@[ @2.0 ]]];
  86. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot),
  87. (@[ @{@"v" : @"e", @"sort" : @0.0}, @{@"v" : @"a", @"sort" : @1.0} ]));
  88. }
  89. - (void)testCanBeCreatedUsingDocumentId {
  90. NSDictionary *testDocs = @{
  91. @"a" : @{@"k" : @"a"},
  92. @"b" : @{@"k" : @"b"},
  93. @"c" : @{@"k" : @"c"},
  94. @"d" : @{@"k" : @"d"},
  95. @"e" : @{@"k" : @"e"}
  96. };
  97. FIRCollectionReference *writer = [[[[self firestore] collectionWithPath:@"parent-collection"]
  98. documentWithAutoID] collectionWithPath:@"sub-collection"];
  99. [self writeAllDocuments:testDocs toCollection:writer];
  100. FIRCollectionReference *reader = [[self firestore] collectionWithPath:writer.path];
  101. FIRQuerySnapshot *querySnapshot =
  102. [self readDocumentSetForRef:[[[reader queryOrderedByFieldPath:[FIRFieldPath documentID]]
  103. queryStartingAtValues:@[ @"b" ]]
  104. queryEndingBeforeValues:@[ @"d" ]]];
  105. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot),
  106. (@[ @{@"k" : @"b"}, @{@"k" : @"c"} ]));
  107. }
  108. - (void)testCanBeUsedWithReferenceValues {
  109. FIRFirestore *db = [self firestore];
  110. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  111. @"a" : @{@"k" : @"1a", @"ref" : [db documentWithPath:@"1/a"]},
  112. @"b" : @{@"k" : @"1b", @"ref" : [db documentWithPath:@"1/b"]},
  113. @"c" : @{@"k" : @"2a", @"ref" : [db documentWithPath:@"2/a"]},
  114. @"d" : @{@"k" : @"2b", @"ref" : [db documentWithPath:@"2/b"]},
  115. @"e" : @{@"k" : @"3a", @"ref" : [db documentWithPath:@"3/a"]},
  116. }];
  117. FIRQuery *query = [testCollection queryOrderedByField:@"ref"];
  118. FIRQuerySnapshot *querySnapshot = [self
  119. readDocumentSetForRef:[[query queryStartingAfterValues:@[ [db documentWithPath:@"1/a"] ]]
  120. queryEndingAtValues:@[ [db documentWithPath:@"2/b"] ]]];
  121. NSMutableArray<NSString *> *actual = [NSMutableArray array];
  122. [querySnapshot.documents enumerateObjectsUsingBlock:^(FIRDocumentSnapshot *_Nonnull doc,
  123. NSUInteger idx, BOOL *_Nonnull stop) {
  124. [actual addObject:doc.data[@"k"]];
  125. }];
  126. XCTAssertEqualObjects(actual, (@[ @"1b", @"2a", @"2b" ]));
  127. }
  128. - (void)testCanBeUsedInDescendingQueries {
  129. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  130. @"a" : @{@"v" : @"a", @"sort" : @1.0},
  131. @"b" : @{@"v" : @"b", @"sort" : @2.0},
  132. @"c" : @{@"v" : @"c", @"sort" : @2.0},
  133. @"d" : @{@"v" : @"d", @"sort" : @3.0},
  134. @"e" : @{@"v" : @"e", @"sort" : @0.0},
  135. @"f" : @{@"v" : @"f", @"nosort" : @1.0} // should not show up
  136. }];
  137. FIRQuery *query = [[testCollection queryOrderedByField:@"sort" descending:YES]
  138. queryOrderedByFieldPath:[FIRFieldPath documentID]
  139. descending:YES];
  140. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[query queryStartingAtValues:@[ @2.0 ]]];
  141. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  142. @{@"v" : @"c", @"sort" : @2.0}, @{@"v" : @"b", @"sort" : @2.0},
  143. @{@"v" : @"a", @"sort" : @1.0}, @{@"v" : @"e", @"sort" : @0.0}
  144. ]));
  145. snapshot = [self readDocumentSetForRef:[query queryEndingBeforeValues:@[ @2.0 ]]];
  146. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{@"v" : @"d", @"sort" : @3.0} ]));
  147. }
  148. FIRTimestamp *TimestampWithMicros(int64_t seconds, int32_t micros) {
  149. // Firestore only supports microsecond resolution, so use a microsecond as a minimum value for
  150. // nanoseconds.
  151. return [FIRTimestamp timestampWithSeconds:seconds nanoseconds:micros * 1000];
  152. }
  153. - (void)testTimestampsCanBePassedToQueriesAsLimits {
  154. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  155. @"a" : @{@"timestamp" : TimestampWithMicros(100, 2)},
  156. @"b" : @{@"timestamp" : TimestampWithMicros(100, 5)},
  157. @"c" : @{@"timestamp" : TimestampWithMicros(100, 3)},
  158. @"d" : @{@"timestamp" : TimestampWithMicros(100, 1)},
  159. // Number of microseconds deliberately repeated.
  160. @"e" : @{@"timestamp" : TimestampWithMicros(100, 5)},
  161. @"f" : @{@"timestamp" : TimestampWithMicros(100, 4)},
  162. }];
  163. FIRQuery *query = [testCollection queryOrderedByField:@"timestamp"];
  164. FIRQuerySnapshot *querySnapshot =
  165. [self readDocumentSetForRef:[[query queryStartingAfterValues:@[ TimestampWithMicros(100, 2) ]]
  166. queryEndingAtValues:@[ TimestampWithMicros(100, 5) ]]];
  167. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[ @"c", @"f", @"b", @"e" ]));
  168. }
  169. - (void)testTimestampsCanBePassedToQueriesInWhereClause {
  170. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  171. @"a" : @{
  172. @"timestamp" : TimestampWithMicros(100, 7),
  173. },
  174. @"b" : @{
  175. @"timestamp" : TimestampWithMicros(100, 4),
  176. },
  177. @"c" : @{
  178. @"timestamp" : TimestampWithMicros(100, 8),
  179. },
  180. @"d" : @{
  181. @"timestamp" : TimestampWithMicros(100, 5),
  182. },
  183. @"e" : @{
  184. @"timestamp" : TimestampWithMicros(100, 6),
  185. }
  186. }];
  187. FIRQuerySnapshot *querySnapshot =
  188. [self readDocumentSetForRef:[[testCollection queryWhereField:@"timestamp"
  189. isGreaterThanOrEqualTo:TimestampWithMicros(100, 5)]
  190. queryWhereField:@"timestamp"
  191. isLessThan:TimestampWithMicros(100, 8)]];
  192. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[ @"d", @"e", @"a" ]));
  193. }
  194. - (void)testTimestampsAreTruncatedToMicroseconds {
  195. FIRTimestamp *nanos = [FIRTimestamp timestampWithSeconds:0 nanoseconds:123456789];
  196. FIRTimestamp *micros = [FIRTimestamp timestampWithSeconds:0 nanoseconds:123456000];
  197. FIRTimestamp *millis = [FIRTimestamp timestampWithSeconds:0 nanoseconds:123000000];
  198. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  199. @"a" : @{@"timestamp" : nanos},
  200. }];
  201. FIRQuerySnapshot *querySnapshot =
  202. [self readDocumentSetForRef:[testCollection queryWhereField:@"timestamp" isEqualTo:nanos]];
  203. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[ @"a" ]));
  204. // Because Timestamp should have been truncated to microseconds, the microsecond timestamp
  205. // should be considered equal to the nanosecond one.
  206. querySnapshot = [self readDocumentSetForRef:[testCollection queryWhereField:@"timestamp"
  207. isEqualTo:micros]];
  208. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[ @"a" ]));
  209. // The truncation is just to the microseconds, however, so the millisecond timestamp should be
  210. // treated as different and thus the query should return no results.
  211. querySnapshot = [self readDocumentSetForRef:[testCollection queryWhereField:@"timestamp"
  212. isEqualTo:millis]];
  213. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[]));
  214. }
  215. @end