FSTLevelDBMigrationsTests.mm 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*
  2. * Copyright 2018 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 <XCTest/XCTest.h>
  17. #include <leveldb/db.h>
  18. #import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
  19. #import "Firestore/Source/Local/FSTLevelDBKey.h"
  20. #import "Firestore/Source/Local/FSTLevelDBMigrations.h"
  21. #import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
  22. #import "Firestore/Source/Local/FSTWriteGroup.h"
  23. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  24. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  25. NS_ASSUME_NONNULL_BEGIN
  26. using firebase::firestore::util::OrderedCode;
  27. using leveldb::DB;
  28. using leveldb::Options;
  29. using leveldb::Status;
  30. @interface FSTLevelDBMigrationsTests : XCTestCase
  31. @end
  32. @implementation FSTLevelDBMigrationsTests {
  33. std::shared_ptr<DB> _db;
  34. }
  35. - (void)setUp {
  36. Options options;
  37. options.error_if_exists = true;
  38. options.create_if_missing = true;
  39. NSString *dir = [FSTPersistenceTestHelpers levelDBDir];
  40. DB *db;
  41. Status status = DB::Open(options, [dir UTF8String], &db);
  42. XCTAssert(status.ok(), @"Failed to create db: %s", status.ToString().c_str());
  43. _db.reset(db);
  44. }
  45. - (void)tearDown {
  46. _db.reset();
  47. }
  48. - (void)testAddsTargetGlobal {
  49. FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
  50. XCTAssertNil(metadata, @"Not expecting metadata yet, we should have an empty db");
  51. [FSTLevelDBMigrations runMigrationsOnDB:_db];
  52. metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
  53. XCTAssertNotNil(metadata, @"Migrations should have added the metadata");
  54. }
  55. - (void)testSetsVersionNumber {
  56. FSTLevelDBSchemaVersion initial = [FSTLevelDBMigrations schemaVersionForDB:_db];
  57. XCTAssertEqual(0, initial, "No version should be equivalent to 0");
  58. // Pick an arbitrary high migration number and migrate to it.
  59. [FSTLevelDBMigrations runMigrationsOnDB:_db];
  60. FSTLevelDBSchemaVersion actual = [FSTLevelDBMigrations schemaVersionForDB:_db];
  61. XCTAssertGreaterThan(actual, 0, @"Expected to migrate to a schema version > 0");
  62. }
  63. - (void)testCountsQueries {
  64. NSUInteger expected = 50;
  65. FSTWriteGroup *group = [FSTWriteGroup groupWithAction:@"Setup"];
  66. for (int i = 0; i < expected; i++) {
  67. std::string key = [FSTLevelDBTargetKey keyWithTargetID:i];
  68. [group setData:"dummy" forKey:key];
  69. }
  70. // Add a dummy entry after the targets to make sure the iteration is correctly bounded.
  71. // Use a table that would sort logically right after that table 'target'.
  72. std::string dummyKey;
  73. // Magic number that indicates a table name follows. Needed to mimic the prefix to the target
  74. // table.
  75. OrderedCode::WriteSignedNumIncreasing(&dummyKey, 5);
  76. OrderedCode::WriteString(&dummyKey, "targetA");
  77. [group setData:"dummy" forKey:dummyKey];
  78. Status status = [group writeToDB:_db];
  79. XCTAssertTrue(status.ok(), @"Failed to write targets");
  80. [FSTLevelDBMigrations runMigrationsOnDB:_db];
  81. FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
  82. XCTAssertEqual(expected, metadata.targetCount, @"Failed to count all of the targets we added");
  83. }
  84. @end
  85. NS_ASSUME_NONNULL_END