FSTLevelDBRemoteDocumentCacheTests.mm 2.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. #import "Firestore/Source/Local/FSTLevelDBKey.h"
  21. #include "Firestore/core/src/firebase/firestore/util/ordered_code.h"
  22. #include "leveldb/db.h"
  23. NS_ASSUME_NONNULL_BEGIN
  24. using leveldb::WriteOptions;
  25. using firebase::firestore::util::OrderedCode;
  26. // A dummy document value, useful for testing code that's known to examine only document keys.
  27. static const char *kDummy = "1";
  28. /**
  29. * The tests for FSTLevelDBRemoteDocumentCache are performed on the FSTRemoteDocumentCache
  30. * protocol in FSTRemoteDocumentCacheTests. This class is merely responsible for setting up and
  31. * tearing down the @a remoteDocumentCache.
  32. */
  33. @interface FSTLevelDBRemoteDocumentCacheTests : FSTRemoteDocumentCacheTests
  34. @end
  35. @implementation FSTLevelDBRemoteDocumentCacheTests {
  36. FSTLevelDB *_db;
  37. }
  38. - (void)setUp {
  39. [super setUp];
  40. _db = [FSTPersistenceTestHelpers levelDBPersistence];
  41. self.persistence = _db;
  42. self.remoteDocumentCache = [self.persistence remoteDocumentCache];
  43. // Write a couple dummy rows that should appear before/after the remote_documents table to make
  44. // sure the tests are unaffected.
  45. [self writeDummyRowWithSegments:@[ @"remote_documentr", @"foo", @"bar" ]];
  46. [self writeDummyRowWithSegments:@[ @"remote_documentsa", @"foo", @"bar" ]];
  47. }
  48. - (void)tearDown {
  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