FIRIndexingTests.mm 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. /*
  2. * Copyright 2022 Google LLC
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  19. @interface FIRIndexingTests : FSTIntegrationTestCase
  20. @end
  21. @implementation FIRIndexingTests
  22. // Clears persistence for each test method to have a clean start.
  23. - (void)setUp {
  24. [super setUp];
  25. self.db = [self firestore];
  26. XCTestExpectation* exp = [self expectationWithDescription:@"clear persistence"];
  27. [self.db clearPersistenceWithCompletion:^(NSError*) {
  28. [exp fulfill];
  29. }];
  30. [self awaitExpectation:exp];
  31. }
  32. - (void)testCanConfigureIndexes {
  33. NSString* json = @"{\n"
  34. "\t\"indexes\": [{\n"
  35. "\t\t\t\"collectionGroup\": \"restaurants\",\n"
  36. "\t\t\t\"queryScope\": \"COLLECTION\",\n"
  37. "\t\t\t\"fields\": [{\n"
  38. "\t\t\t\t\t\"fieldPath\": \"price\",\n"
  39. "\t\t\t\t\t\"order\": \"ASCENDING\"\n"
  40. "\t\t\t\t},\n"
  41. "\t\t\t\t{\n"
  42. "\t\t\t\t\t\"fieldPath\": \"avgRating\",\n"
  43. "\t\t\t\t\t\"order\": \"DESCENDING\"\n"
  44. "\t\t\t\t}\n"
  45. "\t\t\t]\n"
  46. "\t\t},\n"
  47. "\t\t{\n"
  48. "\t\t\t\"collectionGroup\": \"restaurants\",\n"
  49. "\t\t\t\"queryScope\": \"COLLECTION\",\n"
  50. "\t\t\t\"fields\": [{\n"
  51. "\t\t\t\t\"fieldPath\": \"price\",\n"
  52. "\t\t\t\t\"order\": \"ASCENDING\"\n"
  53. "\t\t\t}]\n"
  54. "\t\t}\n"
  55. "\t],\n"
  56. "\t\"fieldOverrides\": []\n"
  57. "}";
  58. [self.db setIndexConfigurationFromJSON:json
  59. completion:^(NSError* error) {
  60. XCTAssertNil(error);
  61. }];
  62. }
  63. - (void)testBadJsonDoesNotCrashClient {
  64. [self.db setIndexConfigurationFromJSON:@"{,"
  65. completion:^(NSError* error) {
  66. XCTAssertNotNil(error);
  67. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  68. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  69. }];
  70. }
  71. - (void)testBadIndexDoesNotCrashClient {
  72. NSString* json = @"{\n"
  73. "\t\"indexes\": [{\n"
  74. "\t\t\"collectionGroup\": \"restaurants\",\n"
  75. "\t\t\"queryScope\": \"COLLECTION\",\n"
  76. "\t\t\"fields\": [{\n"
  77. "\t\t\t\"fieldPath\": \"price\",\n"
  78. "\t\t\t\"order\": \"ASCENDING\",\n"
  79. "\t\t]}\n"
  80. "\t}],\n"
  81. "\t\"fieldOverrides\": []\n"
  82. "}";
  83. [self.db setIndexConfigurationFromJSON:json
  84. completion:^(NSError* error) {
  85. XCTAssertNotNil(error);
  86. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  87. XCTAssertEqual(error.code, FIRFirestoreErrorCodeInvalidArgument);
  88. }];
  89. }
  90. @end