aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Example/Tests/Integration/API/FIRValidationTests.mm
blob: 599f1b20d6397c13349596c7953bf769c689f160 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#import <FirebaseFirestore/FirebaseFirestore.h>

#import <XCTest/XCTest.h>

#import "Firestore/Source/API/FIRFieldValue+Internal.h"
#import "Firestore/Source/API/FIRQuery+Internal.h"

#import "Firestore/Example/Tests/Util/FSTHelpers.h"
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"

// We have tests for passing nil when nil is not supposed to be allowed. So suppress the warnings.
#pragma clang diagnostic ignored "-Wnonnull"

@interface FIRValidationTests : FSTIntegrationTestCase
@end

@implementation FIRValidationTests

#pragma mark - FIRFirestoreSettings Validation

- (void)testNilHostFails {
  FIRFirestoreSettings *settings = self.db.settings;
  FSTAssertThrows(settings.host = nil,
                  @"host setting may not be nil. You should generally just use the default value "
                   "(which is firestore.googleapis.com)");
}

- (void)testNilDispatchQueueFails {
  FIRFirestoreSettings *settings = self.db.settings;
  FSTAssertThrows(settings.dispatchQueue = nil,
                  @"dispatch queue setting may not be nil. Create a new dispatch queue with "
                   "dispatch_queue_create(\"com.example.MyQueue\", NULL) or just use the default "
                   "(which is the main queue, returned from dispatch_get_main_queue())");
}

- (void)testChangingSettingsAfterUseFails {
  FIRFirestoreSettings *settings = self.db.settings;
  [[self.db documentWithPath:@"foo/bar"] setData:@{ @"a" : @42 }];
  settings.host = @"example.com";
  FSTAssertThrows(self.db.settings = settings,
                  @"Firestore instance has already been started and its settings can no longer be "
                  @"changed. You can only set settings before calling any other methods on "
                  @"a Firestore instance.");
}

#pragma mark - FIRFirestore Validation

- (void)testNilFIRAppFails {
  FSTAssertThrows(
      [FIRFirestore firestoreForApp:nil],
      @"FirebaseApp instance may not be nil. Use FirebaseApp.app() if you'd like to use the "
       "default FirebaseApp instance.");
}

// TODO(b/62410906): Test for firestoreForApp:database: with nil DatabaseID.

- (void)testNilTransactionBlocksFail {
  FSTAssertThrows([self.db runTransactionWithBlock:nil
                                        completion:^(id result, NSError *error) {
                                          XCTFail(@"Completion shouldn't run.");
                                        }],
                  @"Transaction block cannot be nil.");

  FSTAssertThrows(
      [self.db runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **pError) {
        XCTFail(@"Transaction block shouldn't run.");
        return nil;
      }
                            completion:nil],
      @"Transaction completion block cannot be nil.");
}

#pragma mark - Collection and Document Path Validation

- (void)testNilCollectionPathsFail {
  FIRDocumentReference *baseDocRef = [self.db documentWithPath:@"foo/bar"];
  NSString *nilError = @"Collection path cannot be nil.";
  FSTAssertThrows([self.db collectionWithPath:nil], nilError);
  FSTAssertThrows([baseDocRef collectionWithPath:nil], nilError);
}

- (void)testWrongLengthCollectionPathsFail {
  FIRDocumentReference *baseDocRef = [self.db documentWithPath:@"foo/bar"];
  NSArray *badAbsolutePaths = @[ @"foo/bar", @"foo/bar/baz/quu" ];
  NSArray *badRelativePaths = @[ @"", @"baz/quu" ];
  NSArray *badPathLengths = @[ @2, @4 ];
  NSString *errorFormat =
      @"Invalid collection reference. Collection references must have an odd "
      @"number of segments, but %@ has %@";
  for (NSUInteger i = 0; i < badAbsolutePaths.count; i++) {
    NSString *error =
        [NSString stringWithFormat:errorFormat, badAbsolutePaths[i], badPathLengths[i]];
    FSTAssertThrows([self.db collectionWithPath:badAbsolutePaths[i]], error);
    FSTAssertThrows([baseDocRef collectionWithPath:badRelativePaths[i]], error);
  }
}

- (void)testNilDocumentPathsFail {
  FIRCollectionReference *baseCollectionRef = [self.db collectionWithPath:@"foo"];
  NSString *nilError = @"Document path cannot be nil.";
  FSTAssertThrows([self.db documentWithPath:nil], nilError);
  FSTAssertThrows([baseCollectionRef documentWithPath:nil], nilError);
}

- (void)testWrongLengthDocumentPathsFail {
  FIRCollectionReference *baseCollectionRef = [self.db collectionWithPath:@"foo"];
  NSArray *badAbsolutePaths = @[ @"foo", @"foo/bar/baz" ];
  NSArray *badRelativePaths = @[ @"", @"bar/baz" ];
  NSArray *badPathLengths = @[ @1, @3 ];
  NSString *errorFormat =
      @"Invalid document reference. Document references must have an even "
      @"number of segments, but %@ has %@";
  for (NSUInteger i = 0; i < badAbsolutePaths.count; i++) {
    NSString *error =
        [NSString stringWithFormat:errorFormat, badAbsolutePaths[i], badPathLengths[i]];
    FSTAssertThrows([self.db documentWithPath:badAbsolutePaths[i]], error);
    FSTAssertThrows([baseCollectionRef documentWithPath:badRelativePaths[i]], error);
  }
}

- (void)testPathsWithEmptySegmentsFail {
  // We're only testing using collectionWithPath since the validation happens in BasePath which is
  // shared by all methods that accept paths.

  // leading / trailing slashes are okay.
  [self.db collectionWithPath:@"/foo/"];
  [self.db collectionWithPath:@"/foo"];
  [self.db collectionWithPath:@"foo/"];

  FSTAssertThrows([self.db collectionWithPath:@"foo//bar/baz"],
                  @"Invalid path (foo//bar/baz). Paths must not contain // in them.");
  FSTAssertThrows([self.db collectionWithPath:@"//foo"],
                  @"Invalid path (//foo). Paths must not contain // in them.");
  FSTAssertThrows([self.db collectionWithPath:@"foo//"],
                  @"Invalid path (foo//). Paths must not contain // in them.");
}

#pragma mark - Write Validation

- (void)testWritesWithNonDictionaryValuesFail {
  NSArray *badData = @[
    @42, @"test", @[ @1 ], [NSDate date], [NSNull null], [FIRFieldValue fieldValueForDelete],
    [FIRFieldValue fieldValueForServerTimestamp]
  ];

  for (id data in badData) {
    [self expectWrite:data toFailWithReason:@"Data to be written must be an NSDictionary."];
  }
}

- (void)testWritesWithDirectlyNestedArraysFail {
  [self expectWrite:@{
    @"nested-array" : @[ @1, @[ @2 ] ]
  }
      toFailWithReason:@"Nested arrays are not supported"];
}

- (void)testWritesWithIndirectlyNestedArraysSucceed {
  NSDictionary<NSString *, id> *data = @{ @"nested-array" : @[ @1, @{ @"foo" : @[ @2 ] } ] };

  FIRDocumentReference *ref = [self documentRef];
  FIRDocumentReference *ref2 = [self documentRef];

  XCTestExpectation *expectation = [self expectationWithDescription:@"setData"];
  [ref setData:data
      completion:^(NSError *_Nullable error) {
        XCTAssertNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];

  expectation = [self expectationWithDescription:@"batch.setData"];
  [[[ref.firestore batch] setData:data forDocument:ref]
      commitWithCompletion:^(NSError *_Nullable error) {
        XCTAssertNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];

  expectation = [self expectationWithDescription:@"updateData"];
  [ref updateData:data
       completion:^(NSError *_Nullable error) {
         XCTAssertNil(error);
         [expectation fulfill];
       }];
  [self awaitExpectations];

  expectation = [self expectationWithDescription:@"batch.updateData"];
  [[[ref.firestore batch] updateData:data forDocument:ref]
      commitWithCompletion:^(NSError *_Nullable error) {
        XCTAssertNil(error);
        [expectation fulfill];
      }];
  [self awaitExpectations];

  XCTestExpectation *transactionDone = [self expectationWithDescription:@"transaction done"];
  [ref.firestore runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **pError) {
    // Note ref2 does not exist at this point so set that and update ref.
    [transaction updateData:data forDocument:ref];
    [transaction setData:data forDocument:ref2];
    return nil;
  }
      completion:^(id result, NSError *error) {
        // ends up being a no-op transaction.
        XCTAssertNil(error);
        [transactionDone fulfill];
      }];
  [self awaitExpectations];
}

- (void)testWritesWithInvalidTypesFail {
  [self expectWrite:@{
    @"foo" : @{@"bar" : self}
  }
      toFailWithReason:@"Unsupported type: FIRValidationTests (found in field foo.bar)"];
}

- (void)testWritesWithLargeNumbersFail {
  NSNumber *num = @((unsigned long long)LONG_MAX + 1);
  NSString *reason =
      [NSString stringWithFormat:@"NSNumber (%@) is too large (found in field num)", num];
  [self expectWrite:@{@"num" : num} toFailWithReason:reason];
}

- (void)testWritesWithReferencesToADifferentDatabaseFail {
  FIRDocumentReference *ref =
      [[self firestoreWithProjectID:@"different-db"] documentWithPath:@"baz/quu"];
  id data = @{@"foo" : ref};
  [self expectWrite:data
      toFailWithReason:
          [NSString
              stringWithFormat:@"Document Reference is for database different-db/(default) but "
                                "should be for database %@/(default) (found in field foo)",
                               [FSTIntegrationTestCase projectID]]];
}

- (void)testWritesWithReservedFieldsFail {
  [self expectWrite:@{
    @"__baz__" : @1
  }
      toFailWithReason:@"Document fields cannot begin and end with __ (found in field __baz__)"];
  [self expectWrite:@{
    @"foo" : @{@"__baz__" : @1}
  }
      toFailWithReason:
          @"Document fields cannot begin and end with __ (found in field foo.__baz__)"];
  [self expectWrite:@{
    @"__baz__" : @{@"foo" : @1}
  }
      toFailWithReason:@"Document fields cannot begin and end with __ (found in field __baz__)"];

  [self expectUpdate:@{
    @"foo.__baz__" : @1
  }
      toFailWithReason:
          @"Document fields cannot begin and end with __ (found in field foo.__baz__)"];
  [self expectUpdate:@{
    @"__baz__.foo" : @1
  }
      toFailWithReason:
          @"Document fields cannot begin and end with __ (found in field __baz__.foo)"];
  [self expectUpdate:@{
    @1 : @1
  }
      toFailWithReason:@"Dictionary keys in updateData: must be NSStrings or FIRFieldPaths."];
}

- (void)testSetsWithFieldValueDeleteFail {
  [self expectSet:@{@"foo" : [FIRFieldValue fieldValueForDelete]}
      toFailWithReason:
          @"FieldValue.delete() can only be used with updateData() and setData() with "
          @"merge:true (found in field foo)"];
}

- (void)testUpdatesWithNestedFieldValueDeleteFail {
  [self expectUpdate:@{
    @"foo" : @{@"bar" : [FIRFieldValue fieldValueForDelete]}
  }
      toFailWithReason:
          @"FieldValue.delete() can only appear at the top level of your update data "
           "(found in field foo.bar)"];
}

- (void)testBatchWritesWithIncorrectReferencesFail {
  FIRFirestore *db1 = [self firestore];
  FIRFirestore *db2 = [self firestore];
  XCTAssertNotEqual(db1, db2);

  NSString *reason = @"Provided document reference is from a different Firestore instance.";
  id data = @{ @"foo" : @1 };
  FIRDocumentReference *badRef = [db2 documentWithPath:@"foo/bar"];
  FIRWriteBatch *batch = [db1 batch];
  FSTAssertThrows([batch setData:data forDocument:badRef], reason);
  FSTAssertThrows([batch setData:data forDocument:badRef merge:YES], reason);
  FSTAssertThrows([batch updateData:data forDocument:badRef], reason);
  FSTAssertThrows([batch deleteDocument:badRef], reason);
}

- (void)testTransactionWritesWithIncorrectReferencesFail {
  FIRFirestore *db1 = [self firestore];
  FIRFirestore *db2 = [self firestore];
  XCTAssertNotEqual(db1, db2);

  NSString *reason = @"Provided document reference is from a different Firestore instance.";
  id data = @{ @"foo" : @1 };
  FIRDocumentReference *badRef = [db2 documentWithPath:@"foo/bar"];

  XCTestExpectation *transactionDone = [self expectationWithDescription:@"transaction done"];
  [db1 runTransactionWithBlock:^id(FIRTransaction *txn, NSError **pError) {
    FSTAssertThrows([txn getDocument:badRef error:nil], reason);
    FSTAssertThrows([txn setData:data forDocument:badRef], reason);
    FSTAssertThrows([txn setData:data forDocument:badRef merge:YES], reason);
    FSTAssertThrows([txn updateData:data forDocument:badRef], reason);
    FSTAssertThrows([txn deleteDocument:badRef], reason);
    return nil;
  }
      completion:^(id result, NSError *error) {
        // ends up being a no-op transaction.
        XCTAssertNil(error);
        [transactionDone fulfill];
      }];
  [self awaitExpectations];
}

#pragma mark - Field Path validation
// TODO(b/37244157): More validation for invalid field paths.

- (void)testFieldPathsWithEmptySegmentsFail {
  NSArray *badFieldPaths = @[ @"", @"foo..baz", @".foo", @"foo." ];

  for (NSString *fieldPath in badFieldPaths) {
    NSString *reason =
        [NSString stringWithFormat:
                      @"Invalid field path (%@). Paths must not be empty, begin with "
                      @"'.', end with '.', or contain '..'",
                      fieldPath];
    [self expectFieldPath:fieldPath toFailWithReason:reason];
  }
}

- (void)testFieldPathsWithInvalidSegmentsFail {
  NSArray *badFieldPaths = @[ @"foo~bar", @"foo*bar", @"foo/bar", @"foo[1", @"foo]1", @"foo[1]" ];

  for (NSString *fieldPath in badFieldPaths) {
    NSString *reason =
        [NSString stringWithFormat:
                      @"Invalid field path (%@). Paths must not contain '~', '*', '/', '[', or ']'",
                      fieldPath];
    [self expectFieldPath:fieldPath toFailWithReason:reason];
  }
}

#pragma mark - ArrayUnion / ArrayRemove Validation

- (void)testArrayTransformsInQueriesFail {
  FSTAssertThrows(
      [[self collectionRef] queryWhereField:@"test"
                                  isEqualTo:@{
                                    @"test" : [FIRFieldValue fieldValueForArrayUnion:@[ @1 ]]
                                  }],
      @"FieldValue.arrayUnion() can only be used with updateData() and setData() (found in field "
       "test)");

  FSTAssertThrows(
      [[self collectionRef] queryWhereField:@"test"
                                  isEqualTo:@{
                                    @"test" : [FIRFieldValue fieldValueForArrayRemove:@[ @1 ]]
                                  }],
      @"FieldValue.arrayRemove() can only be used with updateData() and setData() (found in field "
      @"test)");
}

- (void)testInvalidArrayTransformElementFails {
  [self expectWrite:@{
    @"foo" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, self ]]
  }
      toFailWithReason:@"Unsupported type: FIRValidationTests"];

  [self expectWrite:@{
    @"foo" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, self ]]
  }
      toFailWithReason:@"Unsupported type: FIRValidationTests"];
}

- (void)testArraysInArrayTransformsFail {
  // This would result in a directly nested array which is not supported.
  [self expectWrite:@{
    @"foo" : [FIRFieldValue fieldValueForArrayUnion:@[ @1, @[ @"nested" ] ]]
  }
      toFailWithReason:@"Nested arrays are not supported"];

  [self expectWrite:@{
    @"foo" : [FIRFieldValue fieldValueForArrayRemove:@[ @1, @[ @"nested" ] ]]
  }
      toFailWithReason:@"Nested arrays are not supported"];
}

#pragma mark - Query Validation

- (void)testQueryWithNonPositiveLimitFails {
  FSTAssertThrows([[self collectionRef] queryLimitedTo:0],
                  @"Invalid Query. Query limit (0) is invalid. Limit must be positive.");
  FSTAssertThrows([[self collectionRef] queryLimitedTo:-1],
                  @"Invalid Query. Query limit (-1) is invalid. Limit must be positive.");
}

- (void)testNonEqualityQueriesOnNullOrNaNFail {
  FSTAssertThrows([[self collectionRef] queryWhereField:@"a" isGreaterThan:nil],
                  @"Invalid Query. You can only perform equality comparisons on nil / NSNull.");
  FSTAssertThrows([[self collectionRef] queryWhereField:@"a" isGreaterThan:[NSNull null]],
                  @"Invalid Query. You can only perform equality comparisons on nil / NSNull.");
  FSTAssertThrows([[self collectionRef] queryWhereField:@"a" arrayContains:nil],
                  @"Invalid Query. You can only perform equality comparisons on nil / NSNull.");
  FSTAssertThrows([[self collectionRef] queryWhereField:@"a" arrayContains:[NSNull null]],
                  @"Invalid Query. You can only perform equality comparisons on nil / NSNull.");

  FSTAssertThrows([[self collectionRef] queryWhereField:@"a" isGreaterThan:@(NAN)],
                  @"Invalid Query. You can only perform equality comparisons on NaN.");
  FSTAssertThrows([[self collectionRef] queryWhereField:@"a" arrayContains:@(NAN)],
                  @"Invalid Query. You can only perform equality comparisons on NaN.");
}

- (void)testQueryCannotBeCreatedFromDocumentsMissingSortValues {
  FIRCollectionReference *testCollection = [self collectionRefWithDocuments:@{
    @"f" : @{@"v" : @"f", @"nosort" : @1.0}
  }];

  FIRQuery *query = [testCollection queryOrderedByField:@"sort"];
  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:[testCollection documentWithPath:@"f"]];
  XCTAssertTrue(snapshot.exists);

  NSString *reason =
      @"Invalid query. You are trying to start or end a query using a document for "
       "which the field 'sort' (used as the order by) does not exist.";
  FSTAssertThrows([query queryStartingAtDocument:snapshot], reason);
  FSTAssertThrows([query queryStartingAfterDocument:snapshot], reason);
  FSTAssertThrows([query queryEndingBeforeDocument:snapshot], reason);
  FSTAssertThrows([query queryEndingAtDocument:snapshot], reason);
}

- (void)testQueryBoundMustNotHaveMoreComponentsThanSortOrders {
  FIRCollectionReference *testCollection = [self collectionRef];
  FIRQuery *query = [testCollection queryOrderedByField:@"foo"];

  NSString *reason =
      @"Invalid query. You are trying to start or end a query using more values "
       "than were specified in the order by.";
  // More elements than order by
  FSTAssertThrows(([query queryStartingAtValues:@[ @1, @2 ]]), reason);
  FSTAssertThrows(([[query queryOrderedByField:@"bar"] queryStartingAtValues:@[ @1, @2, @3 ]]),
                  reason);
}

- (void)testQueryOrderedByKeyBoundMustBeAStringWithoutSlashes {
  FIRCollectionReference *testCollection = [self collectionRef];
  FIRQuery *query = [testCollection queryOrderedByFieldPath:[FIRFieldPath documentID]];
  FSTAssertThrows([query queryStartingAtValues:@[ @1 ]],
                  @"Invalid query. Expected a string for the document ID.");
  FSTAssertThrows([query queryStartingAtValues:@[ @"foo/bar" ]],
                  @"Invalid query. Document ID 'foo/bar' contains a slash.");
}

- (void)testQueryMustNotSpecifyStartingOrEndingPointAfterOrder {
  FIRCollectionReference *testCollection = [self collectionRef];
  FIRQuery *query = [testCollection queryOrderedByField:@"foo"];
  NSString *reason =
      @"Invalid query. You must not specify a starting point before specifying the order by.";
  FSTAssertThrows([[query queryStartingAtValues:@[ @1 ]] queryOrderedByField:@"bar"], reason);
  FSTAssertThrows([[query queryStartingAfterValues:@[ @1 ]] queryOrderedByField:@"bar"], reason);
  reason = @"Invalid query. You must not specify an ending point before specifying the order by.";
  FSTAssertThrows([[query queryEndingAtValues:@[ @1 ]] queryOrderedByField:@"bar"], reason);
  FSTAssertThrows([[query queryEndingBeforeValues:@[ @1 ]] queryOrderedByField:@"bar"], reason);
}

- (void)testQueriesFilteredByDocumentIDMustUseStringsOrDocumentReferences {
  FIRCollectionReference *collection = [self collectionRef];
  NSString *reason =
      @"Invalid query. When querying by document ID you must provide a valid "
       "document ID, but it was an empty string.";
  FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@""], reason);

  reason =
      @"Invalid query. When querying by document ID you must provide a valid document ID, "
       "but 'foo/bar/baz' contains a '/' character.";
  FSTAssertThrows(
      [collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@"foo/bar/baz"], reason);

  reason =
      @"Invalid query. When querying by document ID you must provide a valid string or "
       "DocumentReference, but it was of type: __NSCFNumber";
  FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] isEqualTo:@1], reason);

  reason =
      @"Invalid query. You can't perform arrayContains queries on document ID since document IDs "
       "are not arrays.";
  FSTAssertThrows([collection queryWhereFieldPath:[FIRFieldPath documentID] arrayContains:@1],
                  reason);
}

- (void)testQueryInequalityFieldMustMatchFirstOrderByField {
  FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  FIRQuery *base = [coll queryWhereField:@"x" isGreaterThanOrEqualTo:@32];

  FSTAssertThrows([base queryWhereField:@"y" isLessThan:@"cat"],
                  @"Invalid Query. All where filters with an inequality (lessThan, "
                   "lessThanOrEqual, greaterThan, or greaterThanOrEqual) must be on the same "
                   "field. But you have inequality filters on 'x' and 'y'");

  NSString *reason =
      @"Invalid query. You have a where filter with "
       "an inequality (lessThan, lessThanOrEqual, greaterThan, or greaterThanOrEqual) "
       "on field 'x' and so you must also use 'x' as your first queryOrderedBy field, "
       "but your first queryOrderedBy is currently on field 'y' instead.";
  FSTAssertThrows([base queryOrderedByField:@"y"], reason);
  FSTAssertThrows([[coll queryOrderedByField:@"y"] queryWhereField:@"x" isGreaterThan:@32], reason);
  FSTAssertThrows([[base queryOrderedByField:@"y"] queryOrderedByField:@"x"], reason);
  FSTAssertThrows([[[coll queryOrderedByField:@"y"] queryOrderedByField:@"x"] queryWhereField:@"x"
                                                                                isGreaterThan:@32],
                  reason);

  XCTAssertNoThrow([base queryWhereField:@"x" isLessThanOrEqualTo:@"cat"],
                   @"Same inequality fields work");

  XCTAssertNoThrow([base queryWhereField:@"y" isEqualTo:@"cat"],
                   @"Inequality and equality on different fields works");
  XCTAssertNoThrow([base queryWhereField:@"y" arrayContains:@"cat"],
                   @"Inequality and array_contains on different fields works");

  XCTAssertNoThrow([base queryOrderedByField:@"x"], @"inequality same as order by works");
  XCTAssertNoThrow([[coll queryOrderedByField:@"x"] queryWhereField:@"x" isGreaterThan:@32],
                   @"inequality same as order by works");
  XCTAssertNoThrow([[base queryOrderedByField:@"x"] queryOrderedByField:@"y"],
                   @"inequality same as first order by works.");
  XCTAssertNoThrow([[[coll queryOrderedByField:@"x"] queryOrderedByField:@"y"] queryWhereField:@"x"
                                                                                 isGreaterThan:@32],
                   @"inequality same as first order by works.");

  XCTAssertNoThrow([[coll queryOrderedByField:@"x"] queryWhereField:@"y" isEqualTo:@"cat"],
                   @"equality different than orderBy works.");
  XCTAssertNoThrow([[coll queryOrderedByField:@"x"] queryWhereField:@"y" arrayContains:@"cat"],
                   @"array_contains different than orderBy works.");
}

- (void)testQueryMustNotHaveMultipleArrayContainsFilters {
  FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  FSTAssertThrows(
      [[coll queryWhereField:@"foo" arrayContains:@1] queryWhereField:@"foo" arrayContains:@2],
      @"Invalid Query. Queries only support a single arrayContains filter.");
}

#pragma mark - GeoPoint Validation

- (void)testInvalidGeoPointParameters {
  [self verifyExceptionForInvalidLatitude:NAN];
  [self verifyExceptionForInvalidLatitude:-INFINITY];
  [self verifyExceptionForInvalidLatitude:INFINITY];
  [self verifyExceptionForInvalidLatitude:-90.1];
  [self verifyExceptionForInvalidLatitude:90.1];

  [self verifyExceptionForInvalidLongitude:NAN];
  [self verifyExceptionForInvalidLongitude:-INFINITY];
  [self verifyExceptionForInvalidLongitude:INFINITY];
  [self verifyExceptionForInvalidLongitude:-180.1];
  [self verifyExceptionForInvalidLongitude:180.1];
}

#pragma mark - Helpers

/** Performs a write using each write API and makes sure it fails with the expected reason. */
- (void)expectWrite:(id)data toFailWithReason:(NSString *)reason {
  [self expectWrite:data toFailWithReason:reason includeSets:YES includeUpdates:YES];
}

/** Performs a write using each set API and makes sure it fails with the expected reason. */
- (void)expectSet:(id)data toFailWithReason:(NSString *)reason {
  [self expectWrite:data toFailWithReason:reason includeSets:YES includeUpdates:NO];
}

/** Performs a write using each update API and makes sure it fails with the expected reason. */
- (void)expectUpdate:(id)data toFailWithReason:(NSString *)reason {
  [self expectWrite:data toFailWithReason:reason includeSets:NO includeUpdates:YES];
}

/**
 * Performs a write using each set and/or update API and makes sure it fails with the expected
 * reason.
 */
- (void)expectWrite:(id)data
    toFailWithReason:(NSString *)reason
         includeSets:(BOOL)includeSets
      includeUpdates:(BOOL)includeUpdates {
  FIRDocumentReference *ref = [self documentRef];
  if (includeSets) {
    FSTAssertThrows([ref setData:data], reason, @"for %@", data);
    FSTAssertThrows([[ref.firestore batch] setData:data forDocument:ref], reason, @"for %@", data);
  }

  if (includeUpdates) {
    FSTAssertThrows([ref updateData:data], reason, @"for %@", data);
    FSTAssertThrows([[ref.firestore batch] updateData:data forDocument:ref], reason, @"for %@",
                    data);
  }

  XCTestExpectation *transactionDone = [self expectationWithDescription:@"transaction done"];
  [ref.firestore runTransactionWithBlock:^id(FIRTransaction *transaction, NSError **pError) {
    if (includeSets) {
      FSTAssertThrows([transaction setData:data forDocument:ref], reason, @"for %@", data);
    }
    if (includeUpdates) {
      FSTAssertThrows([transaction updateData:data forDocument:ref], reason, @"for %@", data);
    }
    return nil;
  }
      completion:^(id result, NSError *error) {
        // ends up being a no-op transaction.
        XCTAssertNil(error);
        [transactionDone fulfill];
      }];
  [self awaitExpectations];
}

- (void)testFieldNamesMustNotBeEmpty {
  NSString *reason = @"Invalid field path. Provided names must not be empty.";
  FSTAssertThrows([[FIRFieldPath alloc] initWithFields:@[]], reason);

  reason = @"Invalid field name at index 0. Field names must not be empty.";
  FSTAssertThrows([[FIRFieldPath alloc] initWithFields:@[ @"" ]], reason);

  reason = @"Invalid field name at index 1. Field names must not be empty.";
  FSTAssertThrows(([[FIRFieldPath alloc] initWithFields:@[ @"foo", @"" ]]), reason);
}

/**
 * Tests a field path with all of our APIs that accept field paths and ensures they fail with the
 * specified reason.
 */
- (void)expectFieldPath:(NSString *)fieldPath toFailWithReason:(NSString *)reason {
  // Get an arbitrary snapshot we can use for testing.
  FIRDocumentReference *docRef = [self documentRef];
  [self writeDocumentRef:docRef data:@{ @"test" : @1 }];
  FIRDocumentSnapshot *snapshot = [self readDocumentForRef:docRef];

  // Update paths.
  NSMutableDictionary *dict = [NSMutableDictionary dictionary];
  dict[fieldPath] = @1;
  [self expectUpdate:dict toFailWithReason:reason];

  // Snapshot fields.
  FSTAssertThrows(snapshot[fieldPath], reason);

  // Query filter / order fields.
  FIRCollectionReference *collection = [self collectionRef];
  FSTAssertThrows([collection queryWhereField:fieldPath isEqualTo:@1], reason);
  // isLessThan, etc. omitted for brevity since the code path is trivially shared.
  FSTAssertThrows([collection queryOrderedByField:fieldPath], reason);
}

- (void)verifyExceptionForInvalidLatitude:(double)latitude {
  NSString *reason = [NSString
      stringWithFormat:@"GeoPoint requires a latitude value in the range of [-90, 90], but was %f",
                       latitude];
  FSTAssertThrows([[FIRGeoPoint alloc] initWithLatitude:latitude longitude:0], reason);
}

- (void)verifyExceptionForInvalidLongitude:(double)longitude {
  NSString *reason =
      [NSString stringWithFormat:
                    @"GeoPoint requires a longitude value in the range of [-180, 180], but was %f",
                    longitude];
  FSTAssertThrows([[FIRGeoPoint alloc] initWithLatitude:0 longitude:longitude], reason);
}

@end