FSTLevelDBMigrationsTests.mm 4.0 KB

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