FSTLevelDBMigrationsTests.mm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  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/FSTLevelDBMigrations.h"
  20. #import "Firestore/Source/Local/FSTLevelDBQueryCache.h"
  21. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. using leveldb::DB;
  24. using leveldb::Options;
  25. using leveldb::Status;
  26. @interface FSTLevelDBMigrationsTests : XCTestCase
  27. @end
  28. @implementation FSTLevelDBMigrationsTests {
  29. std::shared_ptr<DB> _db;
  30. }
  31. - (void)setUp {
  32. Options options;
  33. options.error_if_exists = true;
  34. options.create_if_missing = true;
  35. NSString *dir = [FSTPersistenceTestHelpers levelDBDir];
  36. DB *db;
  37. Status status = DB::Open(options, [dir UTF8String], &db);
  38. XCTAssert(status.ok(), @"Failed to create db: %s", status.ToString().c_str());
  39. _db.reset(db);
  40. }
  41. - (void)tearDown {
  42. _db.reset();
  43. }
  44. - (void)testAddsTargetGlobal {
  45. FSTPBTargetGlobal *metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
  46. XCTAssertNil(metadata, @"Not expecting metadata yet, we should have an empty db");
  47. [FSTLevelDBMigrations runMigrationsOnDB:_db];
  48. metadata = [FSTLevelDBQueryCache readTargetMetadataFromDB:_db];
  49. XCTAssertNotNil(metadata, @"Migrations should have added the metadata");
  50. }
  51. - (void)testSetsVersionNumber {
  52. FSTLevelDBSchemaVersion initial = [FSTLevelDBMigrations schemaVersionForDB:_db];
  53. XCTAssertEqual(0, initial, "No version should be equivalent to 0");
  54. // Pick an arbitrary high migration number and migrate to it.
  55. [FSTLevelDBMigrations runMigrationsOnDB:_db];
  56. FSTLevelDBSchemaVersion actual = [FSTLevelDBMigrations schemaVersionForDB:_db];
  57. XCTAssertGreaterThan(actual, 0, @"Expected to migrate to a schema version > 0");
  58. }
  59. @end
  60. NS_ASSUME_NONNULL_END