FIRCursorTests.m 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  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 Firestore;
  17. #import <XCTest/XCTest.h>
  18. #import "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. @end