FIRValidationTests.mm 43 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928
  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. #include <limits>
  19. #import "FirebaseCore/Extension/FIROptionsInternal.h"
  20. #import "Firestore/Source/API/FIRFieldValue+Internal.h"
  21. #import "Firestore/Source/API/FIRQuery+Internal.h"
  22. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  23. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  24. #include "Firestore/core/test/unit/testutil/app_testing.h"
  25. using firebase::firestore::testutil::AppForUnitTesting;
  26. using firebase::firestore::testutil::OptionsForUnitTesting;
  27. // We have tests for passing nil when nil is not supposed to be allowed. So suppress the warnings.
  28. #pragma clang diagnostic ignored "-Wnonnull"
  29. @interface FIRValidationTests : FSTIntegrationTestCase
  30. @end
  31. @implementation FIRValidationTests
  32. #pragma mark - FIRFirestoreSettings Validation
  33. - (void)testNilHostFails {
  34. FIRFirestoreSettings *settings = self.db.settings;
  35. FSTAssertThrows(settings.host = nil,
  36. @"Host setting may not be nil. You should generally just use the default value "
  37. "(which is firestore.googleapis.com)");
  38. }
  39. - (void)testNilDispatchQueueFails {
  40. FIRFirestoreSettings *settings = self.db.settings;
  41. FSTAssertThrows(settings.dispatchQueue = nil,
  42. @"Dispatch queue setting may not be nil. Create a new dispatch queue with "
  43. "dispatch_queue_create(\"com.example.MyQueue\", NULL) or just use the default "
  44. "(which is the main queue, returned from dispatch_get_main_queue())");
  45. }
  46. - (void)testChangingSettingsAfterUseFails {
  47. FIRFirestoreSettings *settings = self.db.settings;
  48. [[self.db documentWithPath:@"foo/bar"] setData:@{@"a" : @42}];
  49. settings.host = @"example.com";
  50. FSTAssertThrows(self.db.settings = settings,
  51. @"Firestore instance has already been started and its settings can no longer be "
  52. @"changed. You can only set settings before calling any other methods on "
  53. @"a Firestore instance.");
  54. }
  55. #pragma mark - FIRFirestore Validation
  56. - (void)testNilFIRAppFails {
  57. FSTAssertThrows(
  58. [FIRFirestore firestoreForApp:nil],
  59. @"FirebaseApp instance may not be nil. Use FirebaseApp.app() if you'd like to use the "
  60. "default FirebaseApp instance.");
  61. }
  62. - (void)testNilProjectIDFails {
  63. FIROptions *options = OptionsForUnitTesting("ignored");
  64. options.projectID = nil;
  65. FIRApp *app = AppForUnitTesting(options);
  66. FSTAssertThrows([FIRFirestore firestoreForApp:app],
  67. @"FIROptions.projectID must be set to a valid project ID.");
  68. }
  69. // TODO(b/62410906): Test for firestoreForApp:database: with nil DatabaseID.
  70. - (void)testNilTransactionBlocksFail {
  71. FSTAssertThrows([self.db runTransactionWithBlock:nil
  72. completion:^(id, NSError *) {
  73. XCTFail(@"Completion shouldn't run.");
  74. }],
  75. @"Transaction block cannot be nil.");
  76. FSTAssertThrows([self.db
  77. runTransactionWithBlock:^id(FIRTransaction *, NSError **) {
  78. XCTFail(@"Transaction block shouldn't run.");
  79. return nil;
  80. }
  81. completion:nil],
  82. @"Transaction completion block cannot be nil.");
  83. }
  84. #pragma mark - Collection and Document Path Validation
  85. - (void)testNilCollectionPathsFail {
  86. FIRDocumentReference *baseDocRef = [self.db documentWithPath:@"foo/bar"];
  87. NSString *nilError = @"Collection path cannot be nil.";
  88. FSTAssertThrows([self.db collectionWithPath:nil], nilError);
  89. FSTAssertThrows([baseDocRef collectionWithPath:nil], nilError);
  90. }
  91. - (void)testEmptyCollectionPathsFail {
  92. FIRDocumentReference *baseDocRef = [self.db documentWithPath:@"foo/bar"];
  93. NSString *emptyError = @"Collection path cannot be empty.";
  94. FSTAssertThrows([self.db collectionWithPath:@""], emptyError);
  95. FSTAssertThrows([baseDocRef collectionWithPath:@""], emptyError);
  96. }
  97. - (void)testWrongLengthCollectionPathsFail {
  98. FIRDocumentReference *baseDocRef = [self.db documentWithPath:@"foo/bar"];
  99. NSArray *badAbsolutePaths = @[ @"foo/bar/baz/quu", @"foo/bar/baz/quu/x/y" ];
  100. NSArray *badRelativePaths = @[ @"baz/quu", @"baz/quu/x/y" ];
  101. NSArray *badPathLengths = @[ @4, @6 ];
  102. NSString *errorFormat = @"Invalid collection reference. Collection references must have an odd "
  103. @"number of segments, but %@ has %@";
  104. for (NSUInteger i = 0; i < badAbsolutePaths.count; i++) {
  105. NSString *error =
  106. [NSString stringWithFormat:errorFormat, badAbsolutePaths[i], badPathLengths[i]];
  107. FSTAssertThrows([self.db collectionWithPath:badAbsolutePaths[i]], error);
  108. FSTAssertThrows([baseDocRef collectionWithPath:badRelativePaths[i]], error);
  109. }
  110. }
  111. - (void)testNilCollectionGroupPathsFail {
  112. NSString *nilError = @"Collection ID cannot be nil.";
  113. FSTAssertThrows([self.db collectionGroupWithID:nil], nilError);
  114. }
  115. - (void)testEmptyCollectionGroupPathsFail {
  116. NSString *emptyError = @"Collection ID cannot be empty.";
  117. FSTAssertThrows([self.db collectionGroupWithID:@""], emptyError);
  118. }
  119. - (void)testNilDocumentPathsFail {
  120. FIRCollectionReference *baseCollectionRef = [self.db collectionWithPath:@"foo"];
  121. NSString *nilError = @"Document path cannot be nil.";
  122. FSTAssertThrows([self.db documentWithPath:nil], nilError);
  123. FSTAssertThrows([baseCollectionRef documentWithPath:nil], nilError);
  124. }
  125. - (void)testEmptyDocumentPathsFail {
  126. FIRCollectionReference *baseCollectionRef = [self.db collectionWithPath:@"foo"];
  127. NSString *emptyError = @"Document path cannot be empty.";
  128. FSTAssertThrows([self.db documentWithPath:@""], emptyError);
  129. FSTAssertThrows([baseCollectionRef documentWithPath:@""], emptyError);
  130. }
  131. - (void)testWrongLengthDocumentPathsFail {
  132. FIRCollectionReference *baseCollectionRef = [self.db collectionWithPath:@"foo"];
  133. NSArray *badAbsolutePaths = @[ @"foo/bar/baz", @"foo/bar/baz/x/y" ];
  134. NSArray *badRelativePaths = @[ @"bar/baz", @"bar/baz/x/y" ];
  135. NSArray *badPathLengths = @[ @3, @5 ];
  136. NSString *errorFormat = @"Invalid document reference. Document references must have an even "
  137. @"number of segments, but %@ has %@";
  138. for (NSUInteger i = 0; i < badAbsolutePaths.count; i++) {
  139. NSString *error =
  140. [NSString stringWithFormat:errorFormat, badAbsolutePaths[i], badPathLengths[i]];
  141. FSTAssertThrows([self.db documentWithPath:badAbsolutePaths[i]], error);
  142. FSTAssertThrows([baseCollectionRef documentWithPath:badRelativePaths[i]], error);
  143. }
  144. }
  145. - (void)testPathsWithEmptySegmentsFail {
  146. // We're only testing using collectionWithPath since the validation happens in BasePath which is
  147. // shared by all methods that accept paths.
  148. // leading / trailing slashes are okay.
  149. [self.db collectionWithPath:@"/foo/"];
  150. [self.db collectionWithPath:@"/foo"];
  151. [self.db collectionWithPath:@"foo/"];
  152. FSTAssertThrows([self.db collectionWithPath:@"foo//bar/baz"],
  153. @"Invalid path (foo//bar/baz). Paths must not contain // in them.");
  154. FSTAssertThrows([self.db collectionWithPath:@"//foo"],
  155. @"Invalid path (//foo). Paths must not contain // in them.");
  156. FSTAssertThrows([self.db collectionWithPath:@"foo//"],
  157. @"Invalid path (foo//). Paths must not contain // in them.");
  158. }
  159. #pragma mark - Write Validation
  160. - (void)testWritesWithNonDictionaryValuesFail {
  161. NSArray *badData = @[
  162. @42, @"test", @[ @1 ], [NSDate date], [NSNull null], [FIRFieldValue fieldValueForDelete],
  163. [FIRFieldValue fieldValueForServerTimestamp]
  164. ];
  165. for (id data in badData) {
  166. [self expectWrite:data toFailWithReason:@"Data to be written must be an NSDictionary."];
  167. }
  168. }
  169. - (void)testWritesWithDirectlyNestedArraysFail {
  170. [self expectWrite:@{@"nested-array" : @[ @1, @[ @2 ] ]}
  171. toFailWithReason:@"Nested arrays are not supported"];
  172. }
  173. - (void)testWritesWithIndirectlyNestedArraysSucceed {
  174. NSDictionary<NSString *, id> *data = @{@"nested-array" : @[ @1, @{@"foo" : @[ @2 ]} ]};
  175. FIRDocumentReference *ref = [self documentRef];
  176. FIRDocumentReference *ref2 = [self documentRef];
  177. XCTestExpectation *expectation = [self expectationWithDescription:@"setData"];
  178. [ref setData:data
  179. completion:^(NSError *_Nullable error) {
  180. XCTAssertNil(error);
  181. [expectation fulfill];
  182. }];
  183. [self awaitExpectations];
  184. expectation = [self expectationWithDescription:@"batch.setData"];
  185. [[[ref.firestore batch] setData:data
  186. forDocument:ref] commitWithCompletion:^(NSError *_Nullable error) {
  187. XCTAssertNil(error);
  188. [expectation fulfill];
  189. }];
  190. [self awaitExpectations];
  191. expectation = [self expectationWithDescription:@"updateData"];
  192. [ref updateData:data
  193. completion:^(NSError *_Nullable error) {
  194. XCTAssertNil(error);
  195. [expectation fulfill];
  196. }];
  197. [self awaitExpectations];
  198. expectation = [self expectationWithDescription:@"batch.updateData"];
  199. [[[ref.firestore batch] updateData:data
  200. forDocument:ref] commitWithCompletion:^(NSError *_Nullable error) {
  201. XCTAssertNil(error);
  202. [expectation fulfill];
  203. }];
  204. [self awaitExpectations];
  205. XCTestExpectation *transactionDone = [self expectationWithDescription:@"transaction done"];
  206. [ref.firestore
  207. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **) {
  208. // Note ref2 does not exist at this point so set that and update ref.
  209. [transaction updateData:data forDocument:ref];
  210. [transaction setData:data forDocument:ref2];
  211. return nil;
  212. }
  213. completion:^(id, NSError *error) {
  214. // ends up being a no-op transaction.
  215. XCTAssertNil(error);
  216. [transactionDone fulfill];
  217. }];
  218. [self awaitExpectations];
  219. }
  220. - (void)testWritesWithInvalidTypesFail {
  221. [self expectWrite:@{@"foo" : @{@"bar" : self}}
  222. toFailWithReason:@"Unsupported type: FIRValidationTests (found in field foo.bar)"];
  223. }
  224. - (void)testWritesWithLargeNumbersFail {
  225. NSNumber *num = @(static_cast<uint64_t>(std::numeric_limits<int64_t>::max()) + 1);
  226. NSString *reason =
  227. [NSString stringWithFormat:@"NSNumber (%@) is too large (found in field num)", num];
  228. [self expectWrite:@{@"num" : num} toFailWithReason:reason];
  229. }
  230. - (void)testWritesWithReferencesToADifferentDatabaseFail {
  231. FIRDocumentReference *ref =
  232. [[self firestoreWithProjectID:@"different-db"] documentWithPath:@"baz/quu"];
  233. id data = @{@"foo" : ref};
  234. [self expectWrite:data
  235. toFailWithReason:
  236. [NSString
  237. stringWithFormat:@"Document Reference is for database different-db/(default) but "
  238. "should be for database %@/(default) (found in field foo)",
  239. [FSTIntegrationTestCase projectID]]];
  240. }
  241. - (void)testWritesWithReservedFieldsFail {
  242. [self expectWrite:@{@"__baz__" : @1}
  243. toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
  244. @"field __baz__)"];
  245. [self expectWrite:@{@"foo" : @{@"__baz__" : @1}}
  246. toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
  247. @"field foo.__baz__)"];
  248. [self expectWrite:@{@"__baz__" : @{@"foo" : @1}}
  249. toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
  250. @"field __baz__)"];
  251. [self expectUpdate:@{@"foo.__baz__" : @1}
  252. toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
  253. @"field foo.__baz__)"];
  254. [self expectUpdate:@{@"__baz__.foo" : @1}
  255. toFailWithReason:@"Invalid data. Document fields cannot begin and end with \"__\" (found in "
  256. @"field __baz__.foo)"];
  257. [self expectUpdate:@{@1 : @1}
  258. toFailWithReason:@"Dictionary keys in updateData: must be NSStrings or FIRFieldPaths."];
  259. }
  260. - (void)testWritesMustNotContainEmptyFieldNames {
  261. [self expectSet:@{@"" : @"foo"}
  262. toFailWithReason:@"Invalid data. Document fields must not be empty (found in field ``)"];
  263. }
  264. - (void)testSetsWithFieldValueDeleteFail {
  265. [self expectSet:@{@"foo" : [FIRFieldValue fieldValueForDelete]}
  266. toFailWithReason:@"FieldValue.delete() can only be used with updateData() and setData() with "
  267. @"merge:true (found in field foo)"];
  268. }
  269. - (void)testUpdatesWithNestedFieldValueDeleteFail {
  270. [self expectUpdate:@{@"foo" : @{@"bar" : [FIRFieldValue fieldValueForDelete]}}
  271. toFailWithReason:@"FieldValue.delete() can only appear at the top level of your update data "
  272. "(found in field foo.bar)"];
  273. }
  274. - (void)testBatchWritesWithIncorrectReferencesFail {
  275. FIRFirestore *db1 = [self firestore];
  276. FIRFirestore *db2 = [self firestore];
  277. XCTAssertNotEqual(db1, db2);
  278. NSString *reason = @"Provided document reference is from a different Cloud Firestore instance.";
  279. id data = @{@"foo" : @1};
  280. FIRDocumentReference *badRef = [db2 documentWithPath:@"foo/bar"];
  281. FIRWriteBatch *batch = [db1 batch];
  282. FSTAssertThrows([batch setData:data forDocument:badRef], reason);
  283. FSTAssertThrows([batch setData:data forDocument:badRef merge:YES], reason);
  284. FSTAssertThrows([batch updateData:data forDocument:badRef], reason);
  285. FSTAssertThrows([batch deleteDocument:badRef], reason);
  286. }
  287. - (void)testTransactionWritesWithIncorrectReferencesFail {
  288. FIRFirestore *db1 = [self firestore];
  289. FIRFirestore *db2 = [self firestore];
  290. XCTAssertNotEqual(db1, db2);
  291. NSString *reason = @"Provided document reference is from a different Cloud Firestore instance.";
  292. id data = @{@"foo" : @1};
  293. FIRDocumentReference *badRef = [db2 documentWithPath:@"foo/bar"];
  294. XCTestExpectation *transactionDone = [self expectationWithDescription:@"transaction done"];
  295. [db1
  296. runTransactionWithBlock:^id(FIRTransaction *txn, NSError **) {
  297. FSTAssertThrows([txn getDocument:badRef error:nil], reason);
  298. FSTAssertThrows([txn setData:data forDocument:badRef], reason);
  299. FSTAssertThrows([txn setData:data forDocument:badRef merge:YES], reason);
  300. FSTAssertThrows([txn updateData:data forDocument:badRef], reason);
  301. FSTAssertThrows([txn deleteDocument:badRef], reason);
  302. return nil;
  303. }
  304. completion:^(id, NSError *error) {
  305. // ends up being a no-op transaction.
  306. XCTAssertNil(error);
  307. [transactionDone fulfill];
  308. }];
  309. [self awaitExpectations];
  310. }
  311. #pragma mark - Field Path validation
  312. // TODO(b/37244157): More validation for invalid field paths.
  313. - (void)testFieldPathsWithEmptySegmentsFail {
  314. NSArray *badFieldPaths = @[ @"", @"foo..baz", @".foo", @"foo." ];
  315. for (NSString *fieldPath in badFieldPaths) {
  316. NSString *reason =
  317. [NSString stringWithFormat:@"Invalid field path (%@). Paths must not be empty, begin with "
  318. @"'.', end with '.', or contain '..'",
  319. fieldPath];
  320. [self expectFieldPath:fieldPath toFailWithReason:reason];
  321. }
  322. }
  323. - (void)testFieldPathsWithInvalidSegmentsFail {
  324. NSArray *badFieldPaths = @[ @"foo~bar", @"foo*bar", @"foo/bar", @"foo[1", @"foo]1", @"foo[1]" ];
  325. for (NSString *fieldPath in badFieldPaths) {
  326. NSString *reason =
  327. [NSString stringWithFormat:
  328. @"Invalid field path (%@). Paths must not contain '~', '*', '/', '[', or ']'",
  329. fieldPath];
  330. [self expectFieldPath:fieldPath toFailWithReason:reason];
  331. }
  332. }
  333. #pragma mark - ArrayUnion / ArrayRemove Validation
  334. - (void)testArrayTransformsInQueriesFail {
  335. FSTAssertThrows(
  336. [[self collectionRef]
  337. queryWhereField:@"test"
  338. isEqualTo:@{@"test" : [FIRFieldValue fieldValueForArrayUnion:@[ @1 ]]}],
  339. @"FieldValue.arrayUnion() can only be used with updateData() and setData() (found in field "
  340. "test)");
  341. FSTAssertThrows(
  342. [[self collectionRef]
  343. queryWhereField:@"test"
  344. isEqualTo:@{@"test" : [FIRFieldValue fieldValueForArrayRemove:@[ @1 ]]}],
  345. @"FieldValue.arrayRemove() can only be used with updateData() and setData() (found in field "
  346. @"test)");
  347. }
  348. - (void)testInvalidArrayTransformElementFails {
  349. [self expectWrite:@{@"foo" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, self ]]}
  350. toFailWithReason:@"Unsupported type: FIRValidationTests"];
  351. [self expectWrite:@{@"foo" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, self ]]}
  352. toFailWithReason:@"Unsupported type: FIRValidationTests"];
  353. }
  354. - (void)testArraysInArrayTransformsFail {
  355. // This would result in a directly nested array which is not supported.
  356. [self expectWrite:@{@"foo" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @[ @"nested" ] ]]}
  357. toFailWithReason:@"Nested arrays are not supported"];
  358. [self expectWrite:@{@"foo" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @[ @"nested" ] ]]}
  359. toFailWithReason:@"Nested arrays are not supported"];
  360. }
  361. #pragma mark - Query Validation
  362. - (void)testQueryWithNonPositiveLimitFails {
  363. FSTAssertThrows([[self collectionRef] queryLimitedTo:0],
  364. @"Invalid Query. Query limit (0) is invalid. Limit must be positive.");
  365. FSTAssertThrows([[self collectionRef] queryLimitedTo:-1],
  366. @"Invalid Query. Query limit (-1) is invalid. Limit must be positive.");
  367. FSTAssertThrows([[self collectionRef] queryLimitedToLast:0],
  368. @"Invalid Query. Query limit (0) is invalid. Limit must be positive.");
  369. FSTAssertThrows([[self collectionRef] queryLimitedToLast:-1],
  370. @"Invalid Query. Query limit (-1) is invalid. Limit must be positive.");
  371. }
  372. - (void)testQueryCannotBeCreatedFromDocumentsMissingSortValues {
  373. FIRCollectionReference *testCollection =
  374. [self collectionRefWithDocuments:@{@"f" : @{@"v" : @"f", @"nosort" : @1.0}}];
  375. FIRQuery *query = [testCollection queryOrderedByField:@"sort"];
  376. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:[testCollection documentWithPath:@"f"]];
  377. XCTAssertTrue(snapshot.exists);
  378. NSString *reason = @"Invalid query. You are trying to start or end a query using a document for "
  379. "which the field 'sort' (used as the order by) does not exist.";
  380. FSTAssertThrows([query queryStartingAtDocument:snapshot], reason);
  381. FSTAssertThrows([query queryStartingAfterDocument:snapshot], reason);
  382. FSTAssertThrows([query queryEndingBeforeDocument:snapshot], reason);
  383. FSTAssertThrows([query queryEndingAtDocument:snapshot], reason);
  384. }
  385. - (void)testQueriesCannotBeSortedByAnUncommittedServerTimestamp {
  386. __weak FIRCollectionReference *collection = [self collectionRef];
  387. FIRFirestore *db = [self firestore];
  388. [db disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  389. [self awaitExpectations];
  390. XCTestExpectation *offlineCallbackDone =
  391. [self expectationWithDescription:@"offline callback done"];
  392. XCTestExpectation *onlineCallbackDone = [self expectationWithDescription:@"online callback done"];
  393. [collection addSnapshotListener:^(FIRQuerySnapshot *snapshot, NSError *error) {
  394. XCTAssertNil(error);
  395. // Skip the initial empty snapshot.
  396. if (snapshot.empty) return;
  397. XCTAssertEqual(snapshot.count, 1);
  398. FIRQueryDocumentSnapshot *docSnap = snapshot.documents[0];
  399. if (snapshot.metadata.pendingWrites) {
  400. // Offline snapshot. Since the server timestamp is uncommitted, we
  401. // shouldn't be able to query by it.
  402. NSString *reason =
  403. @"Invalid query. You are trying to start or end a query using a document for which the "
  404. @"field 'timestamp' is an uncommitted server timestamp. (Since the value of this field "
  405. @"is unknown, you cannot start/end a query with it.)";
  406. FSTAssertThrows([[[collection queryOrderedByField:@"timestamp"] queryEndingAtDocument:docSnap]
  407. addSnapshotListener:^(FIRQuerySnapshot *, NSError *){
  408. }],
  409. reason);
  410. [offlineCallbackDone fulfill];
  411. } else {
  412. // Online snapshot. Since the server timestamp is committed, we should be able to query by it.
  413. [[[collection queryOrderedByField:@"timestamp"] queryEndingAtDocument:docSnap]
  414. addSnapshotListener:^(FIRQuerySnapshot *, NSError *){
  415. }];
  416. [onlineCallbackDone fulfill];
  417. }
  418. }];
  419. FIRDocumentReference *document = [collection documentWithAutoID];
  420. [document setData:@{@"timestamp" : [FIRFieldValue fieldValueForServerTimestamp]}];
  421. [self awaitExpectations];
  422. [db enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  423. [self awaitExpectations];
  424. }
  425. - (void)testQueryBoundMustNotHaveMoreComponentsThanSortOrders {
  426. FIRCollectionReference *testCollection = [self collectionRef];
  427. FIRQuery *query = [testCollection queryOrderedByField:@"foo"];
  428. NSString *reason = @"Invalid query. You are trying to start or end a query using more values "
  429. "than were specified in the order by.";
  430. // More elements than order by
  431. FSTAssertThrows(([query queryStartingAtValues:@[ @1, @2 ]]), reason);
  432. FSTAssertThrows(([[query queryOrderedByField:@"bar"] queryStartingAtValues:@[ @1, @2, @3 ]]),
  433. reason);
  434. }
  435. - (void)testQueryOrderedByKeyBoundMustBeAStringWithoutSlashes {
  436. FIRQuery *query = [[self.db collectionWithPath:@"collection"]
  437. queryOrderedByFieldPath:[FIRFieldPath documentID]];
  438. FIRQuery *cgQuery = [[self.db collectionGroupWithID:@"collection"]
  439. queryOrderedByFieldPath:[FIRFieldPath documentID]];
  440. FSTAssertThrows([query queryStartingAtValues:@[ @1 ]],
  441. @"Invalid query. Expected a string for the document ID.");
  442. FSTAssertThrows([query queryStartingAtValues:@[ @"foo/bar" ]],
  443. @"Invalid query. When querying a collection and ordering by document "
  444. "ID, you must pass a plain document ID, but 'foo/bar' contains a slash.");
  445. FSTAssertThrows([cgQuery queryStartingAtValues:@[ @"foo" ]],
  446. @"Invalid query. When querying a collection group and ordering by "
  447. "document ID, you must pass a value that results in a valid document path, "
  448. "but 'foo' is not because it contains an odd number of segments.");
  449. }
  450. - (void)testQueryMustNotSpecifyStartingOrEndingPointAfterOrder {
  451. FIRCollectionReference *testCollection = [self collectionRef];
  452. FIRQuery *query = [testCollection queryOrderedByField:@"foo"];
  453. NSString *reason =
  454. @"Invalid query. You must not specify a starting point before specifying the order by.";
  455. FSTAssertThrows([[query queryStartingAtValues:@[ @1 ]] queryOrderedByField:@"bar"], reason);
  456. FSTAssertThrows([[query queryStartingAfterValues:@[ @1 ]] queryOrderedByField:@"bar"], reason);
  457. reason = @"Invalid query. You must not specify an ending point before specifying the order by.";
  458. FSTAssertThrows([[query queryEndingAtValues:@[ @1 ]] queryOrderedByField:@"bar"], reason);
  459. FSTAssertThrows([[query queryEndingBeforeValues:@[ @1 ]] queryOrderedByField:@"bar"], reason);
  460. }
  461. - (void)testQueriesFilteredByDocumentIdMustUseStringsOrDocumentReferences {
  462. FIRCollectionReference *collection = [self collectionRef];
  463. NSString *reason = @"Invalid query. When querying by document ID you must provide a valid "
  464. "document ID, but it was an empty string.";
  465. FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@""], reason);
  466. reason = @"Invalid query. When querying a collection by document ID you must provide a "
  467. "plain document ID, but 'foo/bar/baz' contains a '/' character.";
  468. FSTAssertThrows(
  469. [collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@"foo/bar/baz"], reason);
  470. reason = @"Invalid query. When querying by document ID you must provide a valid string or "
  471. "DocumentReference, but it was of type:";
  472. FSTAssertExceptionPrefix([collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@1],
  473. reason);
  474. reason = @"Invalid query. When querying a collection group by document ID, the value "
  475. "provided must result in a valid document path, but 'foo/bar/baz' is not because it "
  476. "has an odd number of segments.";
  477. FSTAssertThrows(
  478. [[self.db collectionGroupWithID:@"collection"] queryWhereFieldPath:[FIRFieldPath documentID]
  479. isEqualTo:@"foo/bar/baz"],
  480. reason);
  481. reason =
  482. @"Invalid query. You can't perform arrayContains queries on document ID since document IDs "
  483. "are not arrays.";
  484. FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] arrayContains:@1],
  485. reason);
  486. }
  487. - (void)testQueriesUsingInAndDocumentIdMustHaveProperDocumentReferencesInArray {
  488. FIRCollectionReference *collection = [self collectionRef];
  489. NSString *reason = @"Invalid query. When querying by document ID you must provide a valid "
  490. "document ID, but it was an empty string.";
  491. FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] in:@[ @"" ]], reason);
  492. reason = @"Invalid query. When querying a collection by document ID you must provide a "
  493. "plain document ID, but 'foo/bar/baz' contains a '/' character.";
  494. FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] in:@[ @"foo/bar/baz" ]],
  495. reason);
  496. reason = @"Invalid query. When querying by document ID you must provide a valid string or "
  497. "DocumentReference, but it was of type:";
  498. NSArray *value = @[ @1, @2 ];
  499. FSTAssertExceptionPrefix([collection queryWhereFieldPath:[FIRFieldPath documentID] in:value],
  500. reason);
  501. reason = @"Invalid query. When querying a collection group by document ID, the value "
  502. "provided must result in a valid document path, but 'foo' is not because it "
  503. "has an odd number of segments.";
  504. FSTAssertThrows(
  505. [[self.db collectionGroupWithID:@"collection"] queryWhereFieldPath:[FIRFieldPath documentID]
  506. in:@[ @"foo" ]],
  507. reason);
  508. }
  509. - (void)testQueryInequalityFieldMustMatchFirstOrderByField {
  510. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  511. FIRQuery *base = [coll queryWhereField:@"x" isGreaterThanOrEqualTo:@32];
  512. FSTAssertThrows([base queryWhereField:@"y" isLessThan:@"cat"],
  513. @"Invalid Query. All where filters with an inequality (notEqual, lessThan, "
  514. "lessThanOrEqual, greaterThan, or greaterThanOrEqual) must be on the same "
  515. "field. But you have inequality filters on 'x' and 'y'");
  516. NSString *reason =
  517. @"Invalid query. You have a where filter with "
  518. "an inequality (notEqual, lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) "
  519. "on field 'x' and so you must also use 'x' as your first queryOrderedBy field, "
  520. "but your first queryOrderedBy is currently on field 'y' instead.";
  521. FSTAssertThrows([base queryOrderedByField:@"y"], reason);
  522. FSTAssertThrows([[coll queryOrderedByField:@"y"] queryWhereField:@"x" isGreaterThan:@32], reason);
  523. FSTAssertThrows([[base queryOrderedByField:@"y"] queryOrderedByField:@"x"], reason);
  524. FSTAssertThrows([[[coll queryOrderedByField:@"y"] queryOrderedByField:@"x"] queryWhereField:@"x"
  525. isGreaterThan:@32],
  526. reason);
  527. FSTAssertThrows([[coll queryOrderedByField:@"y"] queryWhereField:@"x" isNotEqualTo:@32], reason);
  528. XCTAssertNoThrow([base queryWhereField:@"x" isLessThanOrEqualTo:@"cat"],
  529. @"Same inequality fields work");
  530. XCTAssertNoThrow([base queryWhereField:@"y" isEqualTo:@"cat"],
  531. @"Inequality and equality on different fields works");
  532. XCTAssertNoThrow([base queryWhereField:@"y" arrayContains:@"cat"],
  533. @"Inequality and array_contains on different fields works");
  534. XCTAssertNoThrow([base queryWhereField:@"y" arrayContainsAny:@[ @"cat" ]],
  535. @"array-contains-any on different fields works");
  536. XCTAssertNoThrow([base queryWhereField:@"y" in:@[ @"cat" ]], @"IN on different fields works");
  537. XCTAssertNoThrow([base queryOrderedByField:@"x"], @"inequality same as order by works");
  538. XCTAssertNoThrow([[coll queryOrderedByField:@"x"] queryWhereField:@"x" isGreaterThan:@32],
  539. @"inequality same as order by works");
  540. XCTAssertNoThrow([[base queryOrderedByField:@"x"] queryOrderedByField:@"y"],
  541. @"inequality same as first order by works.");
  542. XCTAssertNoThrow([[[coll queryOrderedByField:@"x"] queryOrderedByField:@"y"] queryWhereField:@"x"
  543. isGreaterThan:@32],
  544. @"inequality same as first order by works.");
  545. XCTAssertNoThrow([[coll queryOrderedByField:@"x"] queryWhereField:@"y" isEqualTo:@"cat"],
  546. @"equality different than orderBy works.");
  547. XCTAssertNoThrow([[coll queryOrderedByField:@"x"] queryWhereField:@"y" arrayContains:@"cat"],
  548. @"array_contains different than orderBy works.");
  549. }
  550. - (void)testQueriesWithMultipleNotEqualAndInequalitiesFail {
  551. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  552. FSTAssertThrows([[coll queryWhereField:@"x" isNotEqualTo:@1] queryWhereField:@"x"
  553. isNotEqualTo:@2],
  554. @"Invalid Query. You cannot use more than one 'notEqual' filter.");
  555. FSTAssertThrows([[coll queryWhereField:@"x" isNotEqualTo:@1] queryWhereField:@"y"
  556. isNotEqualTo:@2],
  557. @"Invalid Query. All where filters with an inequality (notEqual, lessThan, "
  558. "lessThanOrEqual, greaterThan, or greaterThanOrEqual) must be on "
  559. "the same field. But you have inequality filters on 'x' and 'y'");
  560. }
  561. - (void)testQueriesWithMultipleArrayFiltersFail {
  562. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  563. FSTAssertThrows([[coll queryWhereField:@"foo" arrayContains:@1] queryWhereField:@"foo"
  564. arrayContains:@2],
  565. @"Invalid Query. You cannot use more than one 'arrayContains' filter.");
  566. FSTAssertThrows(
  567. [[coll queryWhereField:@"foo" arrayContains:@1] queryWhereField:@"foo"
  568. arrayContainsAny:@[ @2 ]],
  569. @"Invalid Query. You cannot use 'arrayContainsAny' filters with 'arrayContains' filters.");
  570. FSTAssertThrows(
  571. [[coll queryWhereField:@"foo" arrayContainsAny:@[ @1 ]] queryWhereField:@"foo"
  572. arrayContains:@2],
  573. @"Invalid Query. You cannot use 'arrayContains' filters with 'arrayContainsAny' filters.");
  574. }
  575. - (void)testQueriesWithNotEqualAndNotInFiltersFail {
  576. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  577. FSTAssertThrows([[coll queryWhereField:@"foo" notIn:@[ @1 ]] queryWhereField:@"foo"
  578. isNotEqualTo:@2],
  579. @"Invalid Query. You cannot use 'notEqual' filters with 'notIn' filters.");
  580. FSTAssertThrows([[coll queryWhereField:@"foo" isNotEqualTo:@2] queryWhereField:@"foo"
  581. notIn:@[ @1 ]],
  582. @"Invalid Query. You cannot use 'notIn' filters with 'notEqual' filters.");
  583. }
  584. - (void)testQueriesWithMultipleDisjunctiveFiltersFail {
  585. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  586. FSTAssertThrows([[coll queryWhereField:@"foo" in:@[ @1 ]] queryWhereField:@"foo" in:@[ @2 ]],
  587. @"Invalid Query. You cannot use more than one 'in' filter.");
  588. FSTAssertThrows([[coll queryWhereField:@"foo" arrayContainsAny:@[ @1 ]] queryWhereField:@"foo"
  589. arrayContainsAny:@[ @2 ]],
  590. @"Invalid Query. You cannot use more than one 'arrayContainsAny' filter.");
  591. FSTAssertThrows([[coll queryWhereField:@"foo" notIn:@[ @1 ]] queryWhereField:@"foo"
  592. notIn:@[ @2 ]],
  593. @"Invalid Query. You cannot use more than one 'notIn' filter.");
  594. FSTAssertThrows([[coll queryWhereField:@"foo" arrayContainsAny:@[ @1 ]] queryWhereField:@"foo"
  595. in:@[ @2 ]],
  596. @"Invalid Query. You cannot use 'in' filters with 'arrayContainsAny' filters.");
  597. FSTAssertThrows([[coll queryWhereField:@"foo" in:@[ @1 ]] queryWhereField:@"foo"
  598. arrayContainsAny:@[ @2 ]],
  599. @"Invalid Query. You cannot use 'arrayContainsAny' filters with 'in' filters.");
  600. FSTAssertThrows(
  601. [[coll queryWhereField:@"foo" arrayContainsAny:@[ @1 ]] queryWhereField:@"foo" notIn:@[ @2 ]],
  602. @"Invalid Query. You cannot use 'notIn' filters with 'arrayContainsAny' filters.");
  603. FSTAssertThrows(
  604. [[coll queryWhereField:@"foo" notIn:@[ @1 ]] queryWhereField:@"foo" arrayContainsAny:@[ @2 ]],
  605. @"Invalid Query. You cannot use 'arrayContainsAny' filters with 'notIn' filters.");
  606. FSTAssertThrows([[coll queryWhereField:@"foo" in:@[ @1 ]] queryWhereField:@"foo" notIn:@[ @2 ]],
  607. @"Invalid Query. You cannot use 'notIn' filters with 'in' filters.");
  608. FSTAssertThrows([[coll queryWhereField:@"foo" notIn:@[ @1 ]] queryWhereField:@"foo" in:@[ @2 ]],
  609. @"Invalid Query. You cannot use 'in' filters with 'notIn' filters.");
  610. // This is redundant with the above tests, but makes sure our validation doesn't get confused.
  611. FSTAssertThrows([[[coll queryWhereField:@"foo"
  612. in:@[ @1 ]] queryWhereField:@"foo"
  613. arrayContains:@2] queryWhereField:@"foo"
  614. arrayContainsAny:@[ @2 ]],
  615. @"Invalid Query. You cannot use 'arrayContainsAny' filters with 'in' filters.");
  616. FSTAssertThrows(
  617. [[[coll queryWhereField:@"foo"
  618. arrayContains:@1] queryWhereField:@"foo" in:@[ @2 ]] queryWhereField:@"foo"
  619. arrayContainsAny:@[ @2 ]],
  620. @"Invalid Query. You cannot use 'arrayContainsAny' filters with 'arrayContains' filters.");
  621. FSTAssertThrows([[[coll queryWhereField:@"foo"
  622. notIn:@[ @1 ]] queryWhereField:@"foo"
  623. arrayContains:@2] queryWhereField:@"foo"
  624. arrayContainsAny:@[ @2 ]],
  625. @"Invalid Query. You cannot use 'arrayContains' filters with 'notIn' filters.");
  626. FSTAssertThrows([[[coll queryWhereField:@"foo"
  627. arrayContains:@1] queryWhereField:@"foo"
  628. in:@[ @2 ]] queryWhereField:@"foo"
  629. notIn:@[ @2 ]],
  630. @"Invalid Query. You cannot use 'notIn' filters with 'arrayContains' filters.");
  631. }
  632. - (void)testQueriesCanUseInWithArrayContain {
  633. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  634. XCTAssertNoThrow([[coll queryWhereField:@"foo" arrayContains:@1] queryWhereField:@"foo"
  635. in:@[ @2 ]],
  636. @"arrayContains with IN works.");
  637. XCTAssertNoThrow([[coll queryWhereField:@"foo" in:@[ @1 ]] queryWhereField:@"foo"
  638. arrayContains:@2],
  639. @"IN with arrayContains works.");
  640. FSTAssertThrows([[[coll queryWhereField:@"foo"
  641. in:@[ @1 ]] queryWhereField:@"foo"
  642. arrayContains:@2] queryWhereField:@"foo"
  643. arrayContains:@3],
  644. @"Invalid Query. You cannot use more than one 'arrayContains' filter.");
  645. FSTAssertThrows([[[coll queryWhereField:@"foo"
  646. arrayContains:@1] queryWhereField:@"foo"
  647. in:@[ @2 ]] queryWhereField:@"foo"
  648. in:@[ @3 ]],
  649. @"Invalid Query. You cannot use more than one 'in' filter.");
  650. }
  651. - (void)testQueriesInAndArrayContainsAnyArrayRules {
  652. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  653. FSTAssertThrows([coll queryWhereField:@"foo" in:@[]],
  654. @"Invalid Query. A non-empty array is required for 'in' filters.");
  655. FSTAssertThrows([coll queryWhereField:@"foo" notIn:@[]],
  656. @"Invalid Query. A non-empty array is required for 'notIn' filters.");
  657. FSTAssertThrows([coll queryWhereField:@"foo" arrayContainsAny:@[]],
  658. @"Invalid Query. A non-empty array is required for 'arrayContainsAny' filters.");
  659. // The 10 element max includes duplicates.
  660. NSArray *values = @[ @1, @2, @3, @4, @5, @6, @7, @8, @9, @9, @9 ];
  661. FSTAssertThrows(
  662. [coll queryWhereField:@"foo" in:values],
  663. @"Invalid Query. 'in' filters support a maximum of 10 elements in the value array.");
  664. FSTAssertThrows([coll queryWhereField:@"foo" arrayContainsAny:values],
  665. @"Invalid Query. 'arrayContainsAny' filters support a maximum of 10 elements"
  666. " in the value array.");
  667. FSTAssertThrows(
  668. [coll queryWhereField:@"foo" notIn:values],
  669. @"Invalid Query. 'notIn' filters support a maximum of 10 elements in the value array.");
  670. }
  671. #pragma mark - GeoPoint Validation
  672. - (void)testInvalidGeoPointParameters {
  673. [self verifyExceptionForInvalidLatitude:NAN];
  674. [self verifyExceptionForInvalidLatitude:-INFINITY];
  675. [self verifyExceptionForInvalidLatitude:INFINITY];
  676. [self verifyExceptionForInvalidLatitude:-90.1];
  677. [self verifyExceptionForInvalidLatitude:90.1];
  678. [self verifyExceptionForInvalidLongitude:NAN];
  679. [self verifyExceptionForInvalidLongitude:-INFINITY];
  680. [self verifyExceptionForInvalidLongitude:INFINITY];
  681. [self verifyExceptionForInvalidLongitude:-180.1];
  682. [self verifyExceptionForInvalidLongitude:180.1];
  683. }
  684. #pragma mark - Helpers
  685. /** Performs a write using each write API and makes sure it fails with the expected reason. */
  686. - (void)expectWrite:(id)data toFailWithReason:(NSString *)reason {
  687. [self expectWrite:data toFailWithReason:reason includeSets:YES includeUpdates:YES];
  688. }
  689. /** Performs a write using each set API and makes sure it fails with the expected reason. */
  690. - (void)expectSet:(id)data toFailWithReason:(NSString *)reason {
  691. [self expectWrite:data toFailWithReason:reason includeSets:YES includeUpdates:NO];
  692. }
  693. /** Performs a write using each update API and makes sure it fails with the expected reason. */
  694. - (void)expectUpdate:(id)data toFailWithReason:(NSString *)reason {
  695. [self expectWrite:data toFailWithReason:reason includeSets:NO includeUpdates:YES];
  696. }
  697. /**
  698. * Performs a write using each set and/or update API and makes sure it fails with the expected
  699. * reason.
  700. */
  701. - (void)expectWrite:(id)data
  702. toFailWithReason:(NSString *)reason
  703. includeSets:(BOOL)includeSets
  704. includeUpdates:(BOOL)includeUpdates {
  705. FIRDocumentReference *ref = [self documentRef];
  706. if (includeSets) {
  707. FSTAssertThrows([ref setData:data], reason, @"for %@", data);
  708. FSTAssertThrows([[ref.firestore batch] setData:data forDocument:ref], reason, @"for %@", data);
  709. }
  710. if (includeUpdates) {
  711. FSTAssertThrows([ref updateData:data], reason, @"for %@", data);
  712. FSTAssertThrows([[ref.firestore batch] updateData:data forDocument:ref], reason, @"for %@",
  713. data);
  714. }
  715. XCTestExpectation *transactionDone = [self expectationWithDescription:@"transaction done"];
  716. [ref.firestore
  717. runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **) {
  718. if (includeSets) {
  719. FSTAssertThrows([transaction setData:data forDocument:ref], reason, @"for %@", data);
  720. }
  721. if (includeUpdates) {
  722. FSTAssertThrows([transaction updateData:data forDocument:ref], reason, @"for %@", data);
  723. }
  724. return nil;
  725. }
  726. completion:^(id, NSError *error) {
  727. // ends up being a no-op transaction.
  728. XCTAssertNil(error);
  729. [transactionDone fulfill];
  730. }];
  731. [self awaitExpectations];
  732. }
  733. - (void)testFieldNamesMustNotBeEmpty {
  734. NSString *reason = @"Invalid field path. Provided names must not be empty.";
  735. FSTAssertThrows([[FIRFieldPath alloc] initWithFields:@[]], reason);
  736. reason = @"Invalid field name at index 0. Field names must not be empty.";
  737. FSTAssertThrows([[FIRFieldPath alloc] initWithFields:@[ @"" ]], reason);
  738. reason = @"Invalid field name at index 1. Field names must not be empty.";
  739. FSTAssertThrows(([[FIRFieldPath alloc] initWithFields:@[ @"foo", @"" ]]), reason);
  740. }
  741. /**
  742. * Tests a field path with all of our APIs that accept field paths and ensures they fail with the
  743. * specified reason.
  744. */
  745. - (void)expectFieldPath:(NSString *)fieldPath toFailWithReason:(NSString *)reason {
  746. // Get an arbitrary snapshot we can use for testing.
  747. FIRDocumentReference *docRef = [self documentRef];
  748. [self writeDocumentRef:docRef data:@{@"test" : @1}];
  749. FIRDocumentSnapshot *snapshot = [self readDocumentForRef:docRef];
  750. // Update paths.
  751. NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  752. dict[fieldPath] = @1;
  753. [self expectUpdate:dict toFailWithReason:reason];
  754. // Snapshot fields.
  755. FSTAssertThrows(snapshot[fieldPath], reason);
  756. // Query filter / order fields.
  757. FIRCollectionReference *collection = [self collectionRef];
  758. FSTAssertThrows([collection queryWhereField:fieldPath isEqualTo:@1], reason);
  759. // isLessThan, etc. omitted for brevity since the code path is trivially shared.
  760. FSTAssertThrows([collection queryOrderedByField:fieldPath], reason);
  761. }
  762. - (void)verifyExceptionForInvalidLatitude:(double)latitude {
  763. NSString *reason = [NSString
  764. stringWithFormat:@"GeoPoint requires a latitude value in the range of [-90, 90], but was %g",
  765. latitude];
  766. FSTAssertThrows([[FIRGeoPoint alloc] initWithLatitude:latitude longitude:0], reason);
  767. }
  768. - (void)verifyExceptionForInvalidLongitude:(double)longitude {
  769. NSString *reason =
  770. [NSString stringWithFormat:
  771. @"GeoPoint requires a longitude value in the range of [-180, 180], but was %g",
  772. longitude];
  773. FSTAssertThrows([[FIRGeoPoint alloc] initWithLatitude:0 longitude:longitude], reason);
  774. }
  775. @end