FSTLevelDBRemoteDocumentCacheTests.mm 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. /*
  2. * Copyright 2017 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 "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
  17. #include <leveldb/db.h>
  18. #include "Firestore/Port/ordered_code.h"
  19. #import "Firestore/Source/Local/FSTLevelDB.h"
  20. #import "Firestore/Source/Local/FSTLevelDBKey.h"
  21. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. using leveldb::WriteOptions;
  24. using Firestore::OrderedCode;
  25. // A dummy document value, useful for testing code that's known to examine only document keys.
  26. static const char *kDummy = "1";
  27. /**
  28. * The tests for FSTLevelDBRemoteDocumentCache are performed on the FSTRemoteDocumentCache
  29. * protocol in FSTRemoteDocumentCacheTests. This class is merely responsible for setting up and
  30. * tearing down the @a remoteDocumentCache.
  31. */
  32. @interface FSTLevelDBRemoteDocumentCacheTests : FSTRemoteDocumentCacheTests
  33. @end
  34. @implementation FSTLevelDBRemoteDocumentCacheTests {
  35. FSTLevelDB *_db;
  36. }
  37. - (void)setUp {
  38. [super setUp];
  39. _db = [FSTPersistenceTestHelpers levelDBPersistence];
  40. self.persistence = _db;
  41. self.remoteDocumentCache = [self.persistence remoteDocumentCache];
  42. // Write a couple dummy rows that should appear before/after the remote_documents table to make
  43. // sure the tests are unaffected.
  44. [self writeDummyRowWithSegments:@[ @"remote_documentr", @"foo", @"bar" ]];
  45. [self writeDummyRowWithSegments:@[ @"remote_documentsa", @"foo", @"bar" ]];
  46. }
  47. - (void)tearDown {
  48. [self.remoteDocumentCache shutdown];
  49. self.remoteDocumentCache = nil;
  50. self.persistence = nil;
  51. _db = nil;
  52. [super tearDown];
  53. }
  54. - (void)writeDummyRowWithSegments:(NSArray<NSString *> *)segments {
  55. std::string key;
  56. for (NSString *segment in segments) {
  57. OrderedCode::WriteString(&key, segment.UTF8String);
  58. }
  59. _db.ptr->Put(WriteOptions(), key, kDummy);
  60. }
  61. @end
  62. NS_ASSUME_NONNULL_END