FIRCursorTests.mm 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270
  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",
  63. @"sort" : @2.0 },
  64. @{ @"v" : @"d",
  65. @"sort" : @2.0 }
  66. ]));
  67. querySnapshot = [self readDocumentSetForRef:[query queryEndingBeforeDocument:snapshot]];
  68. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot), (@[
  69. @{ @"v" : @"e",
  70. @"sort" : @0.0 },
  71. @{ @"v" : @"a",
  72. @"sort" : @1.0 },
  73. @{ @"v" : @"b",
  74. @"sort" : @2.0 }
  75. ]));
  76. }
  77. - (void)testCanBeCreatedFromValues {
  78. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  79. @"a" : @{@"v" : @"a", @"sort" : @1.0},
  80. @"b" : @{@"v" : @"b", @"sort" : @2.0},
  81. @"c" : @{@"v" : @"c", @"sort" : @2.0},
  82. @"d" : @{@"v" : @"d", @"sort" : @2.0},
  83. @"e" : @{@"v" : @"e", @"sort" : @0.0},
  84. @"f" : @{@"v" : @"f", @"nosort" : @1.0} // should not show up
  85. }];
  86. FIRQuery *query = [testCollection queryOrderedByField:@"sort"];
  87. FIRQuerySnapshot *querySnapshot =
  88. [self readDocumentSetForRef:[query queryStartingAtValues:@[ @2.0 ]]];
  89. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot), (@[
  90. @{ @"v" : @"b",
  91. @"sort" : @2.0 },
  92. @{ @"v" : @"c",
  93. @"sort" : @2.0 },
  94. @{ @"v" : @"d",
  95. @"sort" : @2.0 }
  96. ]));
  97. querySnapshot = [self readDocumentSetForRef:[query queryEndingBeforeValues:@[ @2.0 ]]];
  98. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot), (@[
  99. @{ @"v" : @"e",
  100. @"sort" : @0.0 },
  101. @{ @"v" : @"a",
  102. @"sort" : @1.0 }
  103. ]));
  104. }
  105. - (void)testCanBeCreatedUsingDocumentId {
  106. NSDictionary *testDocs = @{
  107. @"a" : @{@"k" : @"a"},
  108. @"b" : @{@"k" : @"b"},
  109. @"c" : @{@"k" : @"c"},
  110. @"d" : @{@"k" : @"d"},
  111. @"e" : @{@"k" : @"e"}
  112. };
  113. FIRCollectionReference *writer = [[[[self firestore] collectionWithPath:@"parent-collection"]
  114. documentWithAutoID] collectionWithPath:@"sub-collection"];
  115. [self writeAllDocuments:testDocs toCollection:writer];
  116. FIRCollectionReference *reader = [[self firestore] collectionWithPath:writer.path];
  117. FIRQuerySnapshot *querySnapshot =
  118. [self readDocumentSetForRef:[[[reader queryOrderedByFieldPath:[FIRFieldPath documentID]]
  119. queryStartingAtValues:@[ @"b" ]]
  120. queryEndingBeforeValues:@[ @"d" ]]];
  121. XCTAssertEqualObjects(FIRQuerySnapshotGetData(querySnapshot),
  122. (@[ @{@"k" : @"b"}, @{@"k" : @"c"} ]));
  123. }
  124. - (void)testCanBeUsedWithReferenceValues {
  125. FIRFirestore *db = [self firestore];
  126. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  127. @"a" : @{@"k" : @"1a", @"ref" : [db documentWithPath:@"1/a"]},
  128. @"b" : @{@"k" : @"1b", @"ref" : [db documentWithPath:@"1/b"]},
  129. @"c" : @{@"k" : @"2a", @"ref" : [db documentWithPath:@"2/a"]},
  130. @"d" : @{@"k" : @"2b", @"ref" : [db documentWithPath:@"2/b"]},
  131. @"e" : @{@"k" : @"3a", @"ref" : [db documentWithPath:@"3/a"]},
  132. }];
  133. FIRQuery *query = [testCollection queryOrderedByField:@"ref"];
  134. FIRQuerySnapshot *querySnapshot = [self
  135. readDocumentSetForRef:[[query queryStartingAfterValues:@[ [db documentWithPath:@"1/a"] ]]
  136. queryEndingAtValues:@[ [db documentWithPath:@"2/b"] ]]];
  137. NSMutableArray<NSString *> *actual = [NSMutableArray array];
  138. [querySnapshot.documents enumerateObjectsUsingBlock:^(FIRDocumentSnapshot *_Nonnull doc,
  139. NSUInteger idx, BOOL *_Nonnull stop) {
  140. [actual addObject:doc.data[@"k"]];
  141. }];
  142. XCTAssertEqualObjects(actual, (@[ @"1b", @"2a", @"2b" ]));
  143. }
  144. - (void)testCanBeUsedInDescendingQueries {
  145. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  146. @"a" : @{@"v" : @"a", @"sort" : @1.0},
  147. @"b" : @{@"v" : @"b", @"sort" : @2.0},
  148. @"c" : @{@"v" : @"c", @"sort" : @2.0},
  149. @"d" : @{@"v" : @"d", @"sort" : @3.0},
  150. @"e" : @{@"v" : @"e", @"sort" : @0.0},
  151. @"f" : @{@"v" : @"f", @"nosort" : @1.0} // should not show up
  152. }];
  153. FIRQuery *query = [[testCollection queryOrderedByField:@"sort" descending:YES]
  154. queryOrderedByFieldPath:[FIRFieldPath documentID]
  155. descending:YES];
  156. FIRQuerySnapshot *snapshot = [self readDocumentSetForRef:[query queryStartingAtValues:@[ @2.0 ]]];
  157. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[
  158. @{ @"v" : @"c",
  159. @"sort" : @2.0 },
  160. @{ @"v" : @"b",
  161. @"sort" : @2.0 },
  162. @{ @"v" : @"a",
  163. @"sort" : @1.0 },
  164. @{ @"v" : @"e",
  165. @"sort" : @0.0 }
  166. ]));
  167. snapshot = [self readDocumentSetForRef:[query queryEndingBeforeValues:@[ @2.0 ]]];
  168. XCTAssertEqualObjects(FIRQuerySnapshotGetData(snapshot), (@[ @{ @"v" : @"d", @"sort" : @3.0 } ]));
  169. }
  170. FIRTimestamp *TimestampWithMicros(int64_t seconds, int32_t micros) {
  171. // Firestore only supports microsecond resolution, so use a microsecond as a minimum value for
  172. // nanoseconds.
  173. return [FIRTimestamp timestampWithSeconds:seconds nanoseconds:micros * 1000];
  174. }
  175. - (void)testTimestampsCanBePassedToQueriesAsLimits {
  176. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  177. @"a" : @{@"timestamp" : TimestampWithMicros(100, 2)},
  178. @"b" : @{@"timestamp" : TimestampWithMicros(100, 5)},
  179. @"c" : @{@"timestamp" : TimestampWithMicros(100, 3)},
  180. @"d" : @{@"timestamp" : TimestampWithMicros(100, 1)},
  181. // Number of microseconds deliberately repeated.
  182. @"e" : @{@"timestamp" : TimestampWithMicros(100, 5)},
  183. @"f" : @{@"timestamp" : TimestampWithMicros(100, 4)},
  184. }];
  185. FIRQuery *query = [testCollection queryOrderedByField:@"timestamp"];
  186. FIRQuerySnapshot *querySnapshot =
  187. [self readDocumentSetForRef:[[query queryStartingAfterValues:@[ TimestampWithMicros(100, 2) ]]
  188. queryEndingAtValues:@[ TimestampWithMicros(100, 5) ]]];
  189. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[ @"c", @"f", @"b", @"e" ]));
  190. }
  191. - (void)testTimestampsCanBePassedToQueriesInWhereClause {
  192. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  193. @"a" : @{
  194. @"timestamp" : TimestampWithMicros(100, 7),
  195. },
  196. @"b" : @{
  197. @"timestamp" : TimestampWithMicros(100, 4),
  198. },
  199. @"c" : @{
  200. @"timestamp" : TimestampWithMicros(100, 8),
  201. },
  202. @"d" : @{
  203. @"timestamp" : TimestampWithMicros(100, 5),
  204. },
  205. @"e" : @{
  206. @"timestamp" : TimestampWithMicros(100, 6),
  207. }
  208. }];
  209. FIRQuerySnapshot *querySnapshot =
  210. [self readDocumentSetForRef:[[testCollection queryWhereField:@"timestamp"
  211. isGreaterThanOrEqualTo:TimestampWithMicros(100, 5)]
  212. queryWhereField:@"timestamp"
  213. isLessThan:TimestampWithMicros(100, 8)]];
  214. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[ @"d", @"e", @"a" ]));
  215. }
  216. - (void)testTimestampsAreTruncatedToMicroseconds {
  217. FIRTimestamp *nanos = [FIRTimestamp timestampWithSeconds:0 nanoseconds:123456789];
  218. FIRTimestamp *micros = [FIRTimestamp timestampWithSeconds:0 nanoseconds:123456000];
  219. FIRTimestamp *millis = [FIRTimestamp timestampWithSeconds:0 nanoseconds:123000000];
  220. FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
  221. @"a" : @{@"timestamp" : nanos},
  222. }];
  223. FIRQuerySnapshot *querySnapshot =
  224. [self readDocumentSetForRef:[testCollection queryWhereField:@"timestamp" isEqualTo:nanos]];
  225. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[ @"a" ]));
  226. // Because Timestamp should have been truncated to microseconds, the microsecond timestamp
  227. // should be considered equal to the nanosecond one.
  228. querySnapshot =
  229. [self readDocumentSetForRef:[testCollection queryWhereField:@"timestamp" isEqualTo:micros]];
  230. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[ @"a" ]));
  231. // The truncation is just to the microseconds, however, so the millisecond timestamp should be
  232. // treated as different and thus the query should return no results.
  233. querySnapshot =
  234. [self readDocumentSetForRef:[testCollection queryWhereField:@"timestamp" isEqualTo:millis]];
  235. XCTAssertEqualObjects(FIRQuerySnapshotGetIDs(querySnapshot), (@[]));
  236. }
  237. @end