FSTLevelDBMigrationsTests.mm 4.0 KB

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