FSTLevelDBRemoteDocumentCacheTests.mm 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576
  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. #include <string>
  17. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  18. #import "Firestore/Example/Tests/Local/FSTRemoteDocumentCacheTests.h"
  19. #import "Firestore/Source/Local/FSTLevelDB.h"
  20. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  21. #include "leveldb/db.h"
  22. NS_ASSUME_NONNULL_BEGIN
  23. using leveldb::WriteOptions;
  24. using firebase::firestore::util::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. [super tearDown];
  49. self.remoteDocumentCache = nil;
  50. self.persistence = nil;
  51. _db = nil;
  52. }
  53. - (void)writeDummyRowWithSegments:(NSArray<NSString *> *)segments {
  54. std::string key;
  55. for (NSString *segment in segments) {
  56. OrderedCode::WriteString(&key, segment.UTF8String);
  57. }
  58. _db.ptr->Put(WriteOptions(), key, kDummy);
  59. }
  60. @end
  61. NS_ASSUME_NONNULL_END