FSTLocalSerializerTests.mm 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  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/FSTLocalSerializer.h"
  17. #import <FirebaseFirestore/FIRTimestamp.h>
  18. #import <XCTest/XCTest.h>
  19. #import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h"
  20. #import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
  21. #import "Firestore/Protos/objc/firestore/local/Target.pbobjc.h"
  22. #import "Firestore/Protos/objc/google/firestore/v1/Common.pbobjc.h"
  23. #import "Firestore/Protos/objc/google/firestore/v1/Document.pbobjc.h"
  24. #import "Firestore/Protos/objc/google/firestore/v1/Firestore.pbobjc.h"
  25. #import "Firestore/Protos/objc/google/firestore/v1/Query.pbobjc.h"
  26. #import "Firestore/Protos/objc/google/firestore/v1/Write.pbobjc.h"
  27. #import "Firestore/Protos/objc/google/type/Latlng.pbobjc.h"
  28. #import "Firestore/Source/Core/FSTQuery.h"
  29. #import "Firestore/Source/Local/FSTQueryData.h"
  30. #import "Firestore/Source/Model/FSTDocument.h"
  31. #import "Firestore/Source/Model/FSTFieldValue.h"
  32. #import "Firestore/Source/Model/FSTMutation.h"
  33. #import "Firestore/Source/Model/FSTMutationBatch.h"
  34. #import "Firestore/Source/Remote/FSTSerializerBeta.h"
  35. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  36. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  37. #include "Firestore/core/src/firebase/firestore/model/field_mask.h"
  38. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  39. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  40. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  41. namespace testutil = firebase::firestore::testutil;
  42. using firebase::firestore::model::DatabaseId;
  43. using firebase::firestore::model::FieldMask;
  44. using firebase::firestore::model::Precondition;
  45. using firebase::firestore::model::SnapshotVersion;
  46. using firebase::firestore::model::TargetId;
  47. NS_ASSUME_NONNULL_BEGIN
  48. @interface FSTSerializerBeta (Test)
  49. - (GCFSValue *)encodedNull;
  50. - (GCFSValue *)encodedBool:(BOOL)value;
  51. - (GCFSValue *)encodedDouble:(double)value;
  52. - (GCFSValue *)encodedInteger:(int64_t)value;
  53. - (GCFSValue *)encodedString:(NSString *)value;
  54. @end
  55. @interface FSTLocalSerializerTests : XCTestCase {
  56. DatabaseId _databaseId;
  57. }
  58. @property(nonatomic, strong) FSTLocalSerializer *serializer;
  59. @property(nonatomic, strong) FSTSerializerBeta *remoteSerializer;
  60. @end
  61. @implementation FSTLocalSerializerTests
  62. - (void)setUp {
  63. _databaseId = DatabaseId("p", "d");
  64. self.remoteSerializer = [[FSTSerializerBeta alloc] initWithDatabaseID:&_databaseId];
  65. self.serializer = [[FSTLocalSerializer alloc] initWithRemoteSerializer:self.remoteSerializer];
  66. }
  67. - (void)testEncodesMutationBatch {
  68. FSTMutation *set = FSTTestSetMutation(@"foo/bar", @{@"a" : @"b", @"num" : @1});
  69. FSTMutation *patch =
  70. [[FSTPatchMutation alloc] initWithKey:FSTTestDocKey(@"bar/baz")
  71. fieldMask:FieldMask{testutil::Field("a")}
  72. value:FSTTestObjectValue(@{@"a" : @"b", @"num" : @1})
  73. precondition:Precondition::Exists(true)];
  74. FSTMutation *del = FSTTestDeleteMutation(@"baz/quux");
  75. FIRTimestamp *writeTime = [FIRTimestamp timestamp];
  76. FSTMutationBatch *model = [[FSTMutationBatch alloc] initWithBatchID:42
  77. localWriteTime:writeTime
  78. mutations:@[ set, patch, del ]];
  79. GCFSWrite *setProto = [GCFSWrite message];
  80. setProto.update.name = @"projects/p/databases/d/documents/foo/bar";
  81. [setProto.update.fields addEntriesFromDictionary:@{
  82. @"a" : [self.remoteSerializer encodedString:@"b"],
  83. @"num" : [self.remoteSerializer encodedInteger:1]
  84. }];
  85. GCFSWrite *patchProto = [GCFSWrite message];
  86. patchProto.update.name = @"projects/p/databases/d/documents/bar/baz";
  87. [patchProto.update.fields addEntriesFromDictionary:@{
  88. @"a" : [self.remoteSerializer encodedString:@"b"],
  89. @"num" : [self.remoteSerializer encodedInteger:1]
  90. }];
  91. [patchProto.updateMask.fieldPathsArray addObjectsFromArray:@[ @"a" ]];
  92. patchProto.currentDocument.exists = YES;
  93. GCFSWrite *delProto = [GCFSWrite message];
  94. delProto.delete_p = @"projects/p/databases/d/documents/baz/quux";
  95. GPBTimestamp *writeTimeProto = [GPBTimestamp message];
  96. writeTimeProto.seconds = writeTime.seconds;
  97. writeTimeProto.nanos = writeTime.nanoseconds;
  98. FSTPBWriteBatch *batchProto = [FSTPBWriteBatch message];
  99. batchProto.batchId = 42;
  100. [batchProto.writesArray addObjectsFromArray:@[ setProto, patchProto, delProto ]];
  101. batchProto.localWriteTime = writeTimeProto;
  102. XCTAssertEqualObjects([self.serializer encodedMutationBatch:model], batchProto);
  103. FSTMutationBatch *decoded = [self.serializer decodedMutationBatch:batchProto];
  104. XCTAssertEqual(decoded.batchID, model.batchID);
  105. XCTAssertEqualObjects(decoded.localWriteTime, model.localWriteTime);
  106. XCTAssertEqualObjects(decoded.mutations, model.mutations);
  107. XCTAssertEqual([decoded keys], [model keys]);
  108. }
  109. - (void)testEncodesDocumentAsMaybeDocument {
  110. FSTDocument *doc = FSTTestDoc("some/path", 42, @{@"foo" : @"bar"}, FSTDocumentStateSynced);
  111. FSTPBMaybeDocument *maybeDocProto = [FSTPBMaybeDocument message];
  112. maybeDocProto.document = [GCFSDocument message];
  113. maybeDocProto.document.name = @"projects/p/databases/d/documents/some/path";
  114. [maybeDocProto.document.fields addEntriesFromDictionary:@{
  115. @"foo" : [self.remoteSerializer encodedString:@"bar"],
  116. }];
  117. maybeDocProto.document.updateTime.seconds = 0;
  118. maybeDocProto.document.updateTime.nanos = 42000;
  119. XCTAssertEqualObjects([self.serializer encodedMaybeDocument:doc], maybeDocProto);
  120. FSTMaybeDocument *decoded = [self.serializer decodedMaybeDocument:maybeDocProto];
  121. XCTAssertEqualObjects(decoded, doc);
  122. }
  123. - (void)testEncodesUnknownDocumentAsMaybeDocument {
  124. FSTUnknownDocument *doc = FSTTestUnknownDoc("some/path", 42);
  125. FSTPBMaybeDocument *maybeDocProto = [FSTPBMaybeDocument message];
  126. maybeDocProto.unknownDocument = [FSTPBUnknownDocument message];
  127. maybeDocProto.unknownDocument.name = @"projects/p/databases/d/documents/some/path";
  128. maybeDocProto.unknownDocument.version.seconds = 0;
  129. maybeDocProto.unknownDocument.version.nanos = 42000;
  130. maybeDocProto.hasCommittedMutations = true;
  131. XCTAssertEqualObjects([self.serializer encodedMaybeDocument:doc], maybeDocProto);
  132. FSTMaybeDocument *decoded = [self.serializer decodedMaybeDocument:maybeDocProto];
  133. XCTAssertEqualObjects(decoded, doc);
  134. }
  135. - (void)testEncodesDeletedDocumentAsMaybeDocument {
  136. FSTDeletedDocument *deletedDoc = FSTTestDeletedDoc("some/path", 42, false);
  137. FSTPBMaybeDocument *maybeDocProto = [FSTPBMaybeDocument message];
  138. maybeDocProto.noDocument = [FSTPBNoDocument message];
  139. maybeDocProto.noDocument.name = @"projects/p/databases/d/documents/some/path";
  140. maybeDocProto.noDocument.readTime.seconds = 0;
  141. maybeDocProto.noDocument.readTime.nanos = 42000;
  142. XCTAssertEqualObjects([self.serializer encodedMaybeDocument:deletedDoc], maybeDocProto);
  143. FSTMaybeDocument *decoded = [self.serializer decodedMaybeDocument:maybeDocProto];
  144. XCTAssertEqualObjects(decoded, deletedDoc);
  145. }
  146. - (void)testEncodesQueryData {
  147. FSTQuery *query = FSTTestQuery("room");
  148. TargetId targetID = 42;
  149. SnapshotVersion version = testutil::Version(1039);
  150. NSData *resumeToken = FSTTestResumeTokenFromSnapshotVersion(1039);
  151. FSTQueryData *queryData = [[FSTQueryData alloc] initWithQuery:query
  152. targetID:targetID
  153. listenSequenceNumber:10
  154. purpose:FSTQueryPurposeListen
  155. snapshotVersion:version
  156. resumeToken:resumeToken];
  157. // Let the RPC serializer test various permutations of query serialization.
  158. GCFSTarget_QueryTarget *queryTarget = [self.remoteSerializer encodedQueryTarget:query];
  159. FSTPBTarget *expected = [FSTPBTarget message];
  160. expected.targetId = targetID;
  161. expected.lastListenSequenceNumber = 10;
  162. expected.snapshotVersion.nanos = 1039000;
  163. expected.resumeToken = [resumeToken copy];
  164. expected.query.parent = queryTarget.parent;
  165. expected.query.structuredQuery = queryTarget.structuredQuery;
  166. XCTAssertEqualObjects([self.serializer encodedQueryData:queryData], expected);
  167. FSTQueryData *decoded = [self.serializer decodedQueryData:expected];
  168. XCTAssertEqualObjects(decoded, queryData);
  169. }
  170. @end
  171. NS_ASSUME_NONNULL_END