FSTWriteGroupTests.mm 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121
  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/Source/Local/FSTWriteGroup.h"
  17. #import <XCTest/XCTest.h>
  18. #include <leveldb/db.h>
  19. #import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
  20. #import "Firestore/Source/Local/FSTLevelDB.h"
  21. #import "Firestore/Source/Local/FSTLevelDBKey.h"
  22. #import "Firestore/Example/Tests/Local/FSTPersistenceTestHelpers.h"
  23. using leveldb::ReadOptions;
  24. using leveldb::Status;
  25. NS_ASSUME_NONNULL_BEGIN
  26. @interface FSTWriteGroupTests : XCTestCase
  27. @end
  28. @implementation FSTWriteGroupTests {
  29. FSTLevelDB *_db;
  30. }
  31. - (void)setUp {
  32. [super setUp];
  33. _db = [FSTPersistenceTestHelpers levelDBPersistence];
  34. }
  35. - (void)tearDown {
  36. _db = nil;
  37. [super tearDown];
  38. }
  39. - (void)testCommit {
  40. std::string key = [FSTLevelDBMutationKey keyWithUserID:"user1" batchID:42];
  41. FSTPBWriteBatch *message = [FSTPBWriteBatch message];
  42. message.batchId = 42;
  43. // This is a test that shows that committing an empty group does not fail. There are no side
  44. // effects to verify though.
  45. FSTWriteGroup *group = [_db startGroupWithAction:@"Empty commit"];
  46. XCTAssertNoThrow([_db commitGroup:group]);
  47. group = [_db startGroupWithAction:@"Put"];
  48. [group setMessage:message forKey:key];
  49. std::string value;
  50. Status status = _db.ptr->Get(ReadOptions(), key, &value);
  51. XCTAssertTrue(status.IsNotFound());
  52. [_db commitGroup:group];
  53. status = _db.ptr->Get(ReadOptions(), key, &value);
  54. XCTAssertTrue(status.ok());
  55. group = [_db startGroupWithAction:@"Delete"];
  56. [group removeMessageForKey:key];
  57. status = _db.ptr->Get(ReadOptions(), key, &value);
  58. XCTAssertTrue(status.ok());
  59. [_db commitGroup:group];
  60. status = _db.ptr->Get(ReadOptions(), key, &value);
  61. XCTAssertTrue(status.IsNotFound());
  62. }
  63. - (void)testDescription {
  64. std::string key = [FSTLevelDBMutationKey keyWithUserID:"user1" batchID:42];
  65. FSTPBWriteBatch *message = [FSTPBWriteBatch message];
  66. message.batchId = 42;
  67. FSTWriteGroup *group = [FSTWriteGroup groupWithAction:@"Action"];
  68. XCTAssertEqualObjects([group description], @"<FSTWriteGroup for Action: 0 changes (0 bytes):>");
  69. [group setMessage:message forKey:key];
  70. XCTAssertEqualObjects([group description],
  71. @"<FSTWriteGroup for Action: 1 changes (2 bytes):\n"
  72. " - Put [mutation: userID=user1 batchID=42] (2 bytes)>");
  73. [group removeMessageForKey:key];
  74. XCTAssertEqualObjects([group description],
  75. @"<FSTWriteGroup for Action: 2 changes (2 bytes):\n"
  76. " - Put [mutation: userID=user1 batchID=42] (2 bytes)\n"
  77. " - Delete [mutation: userID=user1 batchID=42]>");
  78. }
  79. - (void)testCommittingWrongGroupThrows {
  80. // If you don't create the group through persistence, it should throw.
  81. FSTWriteGroup *group = [FSTWriteGroup groupWithAction:@"group"];
  82. XCTAssertThrows([_db commitGroup:group]);
  83. }
  84. - (void)testCommittingTwiceThrows {
  85. FSTWriteGroup *group = [_db startGroupWithAction:@"group"];
  86. [_db commitGroup:group];
  87. XCTAssertThrows([_db commitGroup:group]);
  88. }
  89. - (void)testNestingGroupsThrows {
  90. [_db startGroupWithAction:@"group1"];
  91. XCTAssertThrows([_db startGroupWithAction:@"group2"]);
  92. }
  93. @end
  94. NS_ASSUME_NONNULL_END