FIRValidationTests.mm 43 KB

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