FSTSerializerBetaTests.mm 37 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897
  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/Remote/FSTSerializerBeta.h"
  17. #import <FirebaseFirestore/FIRFieldPath.h>
  18. #import <FirebaseFirestore/FIRFieldValue.h>
  19. #import <FirebaseFirestore/FIRFirestoreErrors.h>
  20. #import <FirebaseFirestore/FIRGeoPoint.h>
  21. #import <FirebaseFirestore/FIRTimestamp.h>
  22. #import <GRPCClient/GRPCCall.h>
  23. #import <XCTest/XCTest.h>
  24. #include <vector>
  25. #import "Firestore/Protos/objc/firestore/local/MaybeDocument.pbobjc.h"
  26. #import "Firestore/Protos/objc/firestore/local/Mutation.pbobjc.h"
  27. #import "Firestore/Protos/objc/google/firestore/v1beta1/Common.pbobjc.h"
  28. #import "Firestore/Protos/objc/google/firestore/v1beta1/Document.pbobjc.h"
  29. #import "Firestore/Protos/objc/google/firestore/v1beta1/Firestore.pbobjc.h"
  30. #import "Firestore/Protos/objc/google/firestore/v1beta1/Query.pbobjc.h"
  31. #import "Firestore/Protos/objc/google/firestore/v1beta1/Write.pbobjc.h"
  32. #import "Firestore/Protos/objc/google/rpc/Status.pbobjc.h"
  33. #import "Firestore/Protos/objc/google/type/Latlng.pbobjc.h"
  34. #import "Firestore/Source/API/FIRFieldValue+Internal.h"
  35. #import "Firestore/Source/Core/FSTQuery.h"
  36. #import "Firestore/Source/Local/FSTQueryData.h"
  37. #import "Firestore/Source/Model/FSTDocument.h"
  38. #import "Firestore/Source/Model/FSTDocumentKey.h"
  39. #import "Firestore/Source/Model/FSTFieldValue.h"
  40. #import "Firestore/Source/Model/FSTMutation.h"
  41. #import "Firestore/Source/Model/FSTMutationBatch.h"
  42. #import "Firestore/Source/Remote/FSTWatchChange.h"
  43. #import "Firestore/Example/Tests/API/FSTAPIHelpers.h"
  44. #import "Firestore/Example/Tests/Util/FSTHelpers.h"
  45. #include "Firestore/core/src/firebase/firestore/model/database_id.h"
  46. #include "Firestore/core/src/firebase/firestore/model/field_mask.h"
  47. #include "Firestore/core/src/firebase/firestore/model/field_transform.h"
  48. #include "Firestore/core/src/firebase/firestore/model/precondition.h"
  49. #include "Firestore/core/src/firebase/firestore/util/string_apple.h"
  50. #include "Firestore/core/test/firebase/firestore/testutil/testutil.h"
  51. namespace testutil = firebase::firestore::testutil;
  52. namespace util = firebase::firestore::util;
  53. using firebase::Timestamp;
  54. using firebase::firestore::model::DatabaseId;
  55. using firebase::firestore::model::FieldMask;
  56. using firebase::firestore::model::FieldTransform;
  57. using firebase::firestore::model::Precondition;
  58. using firebase::firestore::model::SnapshotVersion;
  59. NS_ASSUME_NONNULL_BEGIN
  60. @interface FSTSerializerBeta (Test)
  61. - (GCFSValue *)encodedNull;
  62. - (GCFSValue *)encodedBool:(BOOL)value;
  63. - (GCFSValue *)encodedDouble:(double)value;
  64. - (GCFSValue *)encodedInteger:(int64_t)value;
  65. - (GCFSValue *)encodedString:(NSString *)value;
  66. - (GCFSValue *)encodedDate:(NSDate *)value;
  67. - (GCFSDocumentMask *)encodedFieldMask:(const FieldMask &)fieldMask;
  68. - (NSMutableArray<GCFSDocumentTransform_FieldTransform *> *)encodedFieldTransforms:
  69. (const std::vector<FieldTransform> &)fieldTransforms;
  70. - (GCFSStructuredQuery_Filter *)encodedRelationFilter:(FSTRelationFilter *)filter;
  71. @end
  72. @interface GCFSStructuredQuery_Order (Test)
  73. + (instancetype)messageWithProperty:(NSString *)property ascending:(BOOL)ascending;
  74. @end
  75. @implementation GCFSStructuredQuery_Order (Test)
  76. + (instancetype)messageWithProperty:(NSString *)property ascending:(BOOL)ascending {
  77. GCFSStructuredQuery_Order *order = [GCFSStructuredQuery_Order message];
  78. order.field.fieldPath = property;
  79. order.direction = ascending ? GCFSStructuredQuery_Direction_Ascending
  80. : GCFSStructuredQuery_Direction_Descending;
  81. return order;
  82. }
  83. @end
  84. @interface FSTSerializerBetaTests : XCTestCase {
  85. DatabaseId _databaseId;
  86. }
  87. @property(nonatomic, strong) FSTSerializerBeta *serializer;
  88. @end
  89. @implementation FSTSerializerBetaTests
  90. - (void)setUp {
  91. _databaseId = DatabaseId("p", "d");
  92. self.serializer = [[FSTSerializerBeta alloc] initWithDatabaseID:&_databaseId];
  93. }
  94. - (void)testEncodesNull {
  95. FSTFieldValue *model = [FSTNullValue nullValue];
  96. GCFSValue *proto = [GCFSValue message];
  97. proto.nullValue = GPBNullValue_NullValue;
  98. [self assertRoundTripForModel:model proto:proto type:GCFSValue_ValueType_OneOfCase_NullValue];
  99. }
  100. - (void)testEncodesBool {
  101. NSArray<NSNumber *> *examples = @[ @YES, @NO ];
  102. for (NSNumber *example in examples) {
  103. FSTFieldValue *model = FSTTestFieldValue(example);
  104. GCFSValue *proto = [GCFSValue message];
  105. proto.booleanValue = [example boolValue];
  106. [self assertRoundTripForModel:model
  107. proto:proto
  108. type:GCFSValue_ValueType_OneOfCase_BooleanValue];
  109. }
  110. }
  111. - (void)testEncodesIntegers {
  112. NSArray<NSNumber *> *examples = @[ @(LLONG_MIN), @(-100), @(-1), @0, @1, @100, @(LLONG_MAX) ];
  113. for (NSNumber *example in examples) {
  114. FSTFieldValue *model = FSTTestFieldValue(example);
  115. GCFSValue *proto = [GCFSValue message];
  116. proto.integerValue = [example longLongValue];
  117. [self assertRoundTripForModel:model
  118. proto:proto
  119. type:GCFSValue_ValueType_OneOfCase_IntegerValue];
  120. }
  121. }
  122. - (void)testEncodesDoubles {
  123. NSArray<NSNumber *> *examples = @[
  124. // normal negative numbers.
  125. @(-INFINITY), @(-DBL_MAX), @(LLONG_MIN * 1.0 - 1.0), @(-2.0), @(-1.1), @(-1.0), @(-DBL_MIN),
  126. // negative smallest subnormal, zeroes, positive smallest subnormal
  127. @(-0x1.0p-1074), @(-0.0), @(0.0), @(0x1.0p-1074),
  128. // and the rest
  129. @(DBL_MIN), @0.1, @1.1, @(LLONG_MAX * 1.0), @(DBL_MAX), @(INFINITY),
  130. // NaN.
  131. @(0.0 / 0.0)
  132. ];
  133. for (NSNumber *example in examples) {
  134. FSTFieldValue *model = FSTTestFieldValue(example);
  135. GCFSValue *proto = [GCFSValue message];
  136. proto.doubleValue = [example doubleValue];
  137. [self assertRoundTripForModel:model proto:proto type:GCFSValue_ValueType_OneOfCase_DoubleValue];
  138. }
  139. }
  140. - (void)testEncodesStrings {
  141. NSArray<NSString *> *examples = @[
  142. @"",
  143. @"a",
  144. @"abc def",
  145. @"æ",
  146. @"\0\ud7ff\ue000\uffff",
  147. @"(╯°□°)╯︵ ┻━┻",
  148. ];
  149. for (NSString *example in examples) {
  150. FSTFieldValue *model = FSTTestFieldValue(example);
  151. GCFSValue *proto = [GCFSValue message];
  152. proto.stringValue = example;
  153. [self assertRoundTripForModel:model proto:proto type:GCFSValue_ValueType_OneOfCase_StringValue];
  154. }
  155. }
  156. - (void)testEncodesDates {
  157. NSDateComponents *dateWithNanos = FSTTestDateComponents(2016, 1, 2, 10, 20, 50);
  158. dateWithNanos.nanosecond = 500000000;
  159. NSArray<NSDate *> *examples = @[
  160. [[NSCalendar currentCalendar] dateFromComponents:dateWithNanos],
  161. FSTTestDate(2016, 6, 17, 10, 50, 15)
  162. ];
  163. GCFSValue *timestamp1 = [GCFSValue message];
  164. timestamp1.timestampValue.seconds = 1451730050;
  165. timestamp1.timestampValue.nanos = 500000000;
  166. GCFSValue *timestamp2 = [GCFSValue message];
  167. timestamp2.timestampValue.seconds = 1466160615;
  168. timestamp2.timestampValue.nanos = 0;
  169. NSArray<GCFSValue *> *expectedTimestamps = @[ timestamp1, timestamp2 ];
  170. for (NSUInteger i = 0; i < [examples count]; i++) {
  171. [self assertRoundTripForModel:FSTTestFieldValue(examples[i])
  172. proto:expectedTimestamps[i]
  173. type:GCFSValue_ValueType_OneOfCase_TimestampValue];
  174. }
  175. }
  176. - (void)testEncodesGeoPoints {
  177. NSArray<FIRGeoPoint *> *examples =
  178. @[ FSTTestGeoPoint(0, 0), FSTTestGeoPoint(1.24, 4.56), FSTTestGeoPoint(-90, 180) ];
  179. for (FIRGeoPoint *example in examples) {
  180. FSTFieldValue *model = FSTTestFieldValue(example);
  181. GCFSValue *proto = [GCFSValue message];
  182. proto.geoPointValue = [GTPLatLng message];
  183. proto.geoPointValue.latitude = example.latitude;
  184. proto.geoPointValue.longitude = example.longitude;
  185. [self assertRoundTripForModel:model
  186. proto:proto
  187. type:GCFSValue_ValueType_OneOfCase_GeoPointValue];
  188. }
  189. }
  190. - (void)testEncodesBlobs {
  191. NSArray<NSData *> *examples = @[
  192. FSTTestData(-1),
  193. FSTTestData(0, -1),
  194. FSTTestData(0, 1, 2, -1),
  195. FSTTestData(255, -1),
  196. FSTTestData(0, 1, 255, -1),
  197. ];
  198. for (NSData *example in examples) {
  199. FSTFieldValue *model = FSTTestFieldValue(example);
  200. GCFSValue *proto = [GCFSValue message];
  201. proto.bytesValue = example;
  202. [self assertRoundTripForModel:model proto:proto type:GCFSValue_ValueType_OneOfCase_BytesValue];
  203. }
  204. }
  205. - (void)testEncodesResourceNames {
  206. FSTDocumentKeyReference *reference = FSTTestRef("project", DatabaseId::kDefault, @"foo/bar");
  207. _databaseId = DatabaseId("project", DatabaseId::kDefault);
  208. GCFSValue *proto = [GCFSValue message];
  209. proto.referenceValue = @"projects/project/databases/(default)/documents/foo/bar";
  210. [self assertRoundTripForModel:FSTTestFieldValue(reference)
  211. proto:proto
  212. type:GCFSValue_ValueType_OneOfCase_ReferenceValue];
  213. }
  214. - (void)testEncodesArrays {
  215. FSTFieldValue *model = FSTTestFieldValue(@[ @YES, @"foo" ]);
  216. GCFSValue *proto = [GCFSValue message];
  217. [proto.arrayValue.valuesArray addObjectsFromArray:@[
  218. [self.serializer encodedBool:YES], [self.serializer encodedString:@"foo"]
  219. ]];
  220. [self assertRoundTripForModel:model proto:proto type:GCFSValue_ValueType_OneOfCase_ArrayValue];
  221. }
  222. - (void)testEncodesEmptyMap {
  223. FSTFieldValue *model = [FSTObjectValue objectValue];
  224. GCFSValue *proto = [GCFSValue message];
  225. proto.mapValue = [GCFSMapValue message];
  226. [self assertRoundTripForModel:model proto:proto type:GCFSValue_ValueType_OneOfCase_MapValue];
  227. }
  228. - (void)testEncodesNestedObjects {
  229. FSTFieldValue *model = FSTTestFieldValue(@{
  230. @"b" : @YES,
  231. @"d" : @(DBL_MAX),
  232. @"i" : @1,
  233. @"n" : [NSNull null],
  234. @"s" : @"foo",
  235. @"a" : @[ @2, @"bar",
  236. @{ @"b" : @NO } ],
  237. @"o" : @{
  238. @"d" : @100,
  239. @"nested" : @{@"e" : @(LLONG_MIN)},
  240. },
  241. });
  242. GCFSValue *innerObject = [GCFSValue message];
  243. innerObject.mapValue.fields[@"b"] = [self.serializer encodedBool:NO];
  244. GCFSValue *middleArray = [GCFSValue message];
  245. [middleArray.arrayValue.valuesArray addObjectsFromArray:@[
  246. [self.serializer encodedInteger:2], [self.serializer encodedString:@"bar"], innerObject
  247. ]];
  248. innerObject = [GCFSValue message];
  249. innerObject.mapValue.fields[@"e"] = [self.serializer encodedInteger:LLONG_MIN];
  250. GCFSValue *middleObject = [GCFSValue message];
  251. [middleObject.mapValue.fields addEntriesFromDictionary:@{
  252. @"d" : [self.serializer encodedInteger:100],
  253. @"nested" : innerObject
  254. }];
  255. GCFSValue *proto = [GCFSValue message];
  256. [proto.mapValue.fields addEntriesFromDictionary:@{
  257. @"b" : [self.serializer encodedBool:YES],
  258. @"d" : [self.serializer encodedDouble:DBL_MAX],
  259. @"i" : [self.serializer encodedInteger:1],
  260. @"n" : [self.serializer encodedNull],
  261. @"s" : [self.serializer encodedString:@"foo"],
  262. @"a" : middleArray,
  263. @"o" : middleObject
  264. }];
  265. [self assertRoundTripForModel:model proto:proto type:GCFSValue_ValueType_OneOfCase_MapValue];
  266. }
  267. - (void)assertRoundTripForModel:(FSTFieldValue *)model
  268. proto:(GCFSValue *)value
  269. type:(GCFSValue_ValueType_OneOfCase)type {
  270. GCFSValue *actualProto = [self.serializer encodedFieldValue:model];
  271. XCTAssertEqual(actualProto.valueTypeOneOfCase, type);
  272. XCTAssertEqualObjects(actualProto, value);
  273. FSTFieldValue *actualModel = [self.serializer decodedFieldValue:value];
  274. XCTAssertEqualObjects(actualModel, model);
  275. }
  276. - (void)testEncodesSetMutation {
  277. FSTSetMutation *mutation = FSTTestSetMutation(@"docs/1", @{ @"a" : @"b", @"num" : @1 });
  278. GCFSWrite *proto = [GCFSWrite message];
  279. proto.update = [self.serializer encodedDocumentWithFields:mutation.value key:mutation.key];
  280. [self assertRoundTripForMutation:mutation proto:proto];
  281. }
  282. - (void)testEncodesPatchMutation {
  283. FSTPatchMutation *mutation =
  284. FSTTestPatchMutation("docs/1",
  285. @{ @"a" : @"b",
  286. @"num" : @1,
  287. @"some.de\\\\ep.th\\ing'" : @2 },
  288. {});
  289. GCFSWrite *proto = [GCFSWrite message];
  290. proto.update = [self.serializer encodedDocumentWithFields:mutation.value key:mutation.key];
  291. proto.updateMask = [self.serializer encodedFieldMask:mutation.fieldMask];
  292. proto.currentDocument.exists = YES;
  293. [self assertRoundTripForMutation:mutation proto:proto];
  294. }
  295. - (void)testEncodesDeleteMutation {
  296. FSTDeleteMutation *mutation = FSTTestDeleteMutation(@"docs/1");
  297. GCFSWrite *proto = [GCFSWrite message];
  298. proto.delete_p = @"projects/p/databases/d/documents/docs/1";
  299. [self assertRoundTripForMutation:mutation proto:proto];
  300. }
  301. - (void)testEncodesServerTimestampTransformMutation {
  302. FSTTransformMutation *mutation = FSTTestTransformMutation(@"docs/1", @{
  303. @"a" : [FIRFieldValue fieldValueForServerTimestamp],
  304. @"bar.baz" : [FIRFieldValue fieldValueForServerTimestamp]
  305. });
  306. GCFSWrite *proto = [GCFSWrite message];
  307. proto.transform = [GCFSDocumentTransform message];
  308. proto.transform.document = [self.serializer encodedDocumentKey:mutation.key];
  309. proto.transform.fieldTransformsArray =
  310. [self.serializer encodedFieldTransforms:mutation.fieldTransforms];
  311. proto.currentDocument.exists = YES;
  312. [self assertRoundTripForMutation:mutation proto:proto];
  313. }
  314. - (void)testEncodesArrayTransformMutations {
  315. FSTTransformMutation *mutation = FSTTestTransformMutation(@"docs/1", @{
  316. @"a" : [FIRFieldValue fieldValueForArrayUnion:@[ @"a", @2 ]],
  317. @"bar.baz" : [FIRFieldValue fieldValueForArrayRemove:@[
  318. @{ @"x" : @1 }
  319. ]]
  320. });
  321. GCFSWrite *proto = [GCFSWrite message];
  322. proto.transform = [GCFSDocumentTransform message];
  323. proto.transform.document = [self.serializer encodedDocumentKey:mutation.key];
  324. GCFSDocumentTransform_FieldTransform *arrayUnion = [GCFSDocumentTransform_FieldTransform message];
  325. arrayUnion.fieldPath = @"a";
  326. arrayUnion.appendMissingElements = [GCFSArrayValue message];
  327. NSMutableArray *unionElements = arrayUnion.appendMissingElements.valuesArray;
  328. [unionElements addObject:[self.serializer encodedFieldValue:FSTTestFieldValue(@"a")]];
  329. [unionElements addObject:[self.serializer encodedFieldValue:FSTTestFieldValue(@2)]];
  330. [proto.transform.fieldTransformsArray addObject:arrayUnion];
  331. GCFSDocumentTransform_FieldTransform *arrayRemove =
  332. [GCFSDocumentTransform_FieldTransform message];
  333. arrayRemove.fieldPath = @"bar.baz";
  334. arrayRemove.removeAllFromArray_p = [GCFSArrayValue message];
  335. NSMutableArray *removeElements = arrayRemove.removeAllFromArray_p.valuesArray;
  336. [removeElements addObject:[self.serializer encodedFieldValue:FSTTestFieldValue(@{ @"x" : @1 })]];
  337. [proto.transform.fieldTransformsArray addObject:arrayRemove];
  338. proto.currentDocument.exists = YES;
  339. [self assertRoundTripForMutation:mutation proto:proto];
  340. }
  341. - (void)testEncodesSetMutationWithPrecondition {
  342. FSTSetMutation *mutation =
  343. [[FSTSetMutation alloc] initWithKey:FSTTestDocKey(@"foo/bar")
  344. value:FSTTestObjectValue(
  345. @{ @"a" : @"b",
  346. @"num" : @1 })
  347. precondition:Precondition::UpdateTime(testutil::Version(4))];
  348. GCFSWrite *proto = [GCFSWrite message];
  349. proto.update = [self.serializer encodedDocumentWithFields:mutation.value key:mutation.key];
  350. proto.currentDocument.updateTime = [self.serializer encodedTimestamp:Timestamp{0, 4000}];
  351. [self assertRoundTripForMutation:mutation proto:proto];
  352. }
  353. - (void)assertRoundTripForMutation:(FSTMutation *)mutation proto:(GCFSWrite *)proto {
  354. GCFSWrite *actualProto = [self.serializer encodedMutation:mutation];
  355. XCTAssertEqualObjects(actualProto, proto);
  356. FSTMutation *actualMutation = [self.serializer decodedMutation:proto];
  357. XCTAssertEqualObjects(actualMutation, mutation);
  358. }
  359. - (void)testDecodesMutationResult {
  360. GCFSWriteResult *proto = [GCFSWriteResult message];
  361. proto.updateTime = [self.serializer encodedTimestamp:Timestamp{0, 4000}];
  362. [proto.transformResultsArray addObject:[self.serializer encodedString:@"result"]];
  363. FSTMutationResult *result = [self.serializer decodedMutationResult:proto];
  364. XCTAssertEqual(result.version.value(), (SnapshotVersion{Timestamp{0, 4000}}));
  365. XCTAssertEqualObjects(result.transformResults, @[ [FSTStringValue stringValue:@"result"] ]);
  366. }
  367. - (void)testDecodesDeleteMutationResult {
  368. GCFSWriteResult *proto = [GCFSWriteResult message];
  369. // Deletes don't set updateTime (or transformResults).
  370. FSTMutationResult *result = [self.serializer decodedMutationResult:proto];
  371. XCTAssertFalse(result.version.has_value());
  372. XCTAssertEqual(result.transformResults.count, 0);
  373. }
  374. - (void)testRoundTripSpecialFieldNames {
  375. FSTMutation *set = FSTTestSetMutation(@"collection/key", @{
  376. @"field" : [NSString stringWithFormat:@"field %d", 1],
  377. @"field.dot" : @2,
  378. @"field\\slash" : @3
  379. });
  380. GCFSWrite *encoded = [self.serializer encodedMutation:set];
  381. FSTMutation *decoded = [self.serializer decodedMutation:encoded];
  382. XCTAssertEqualObjects(set, decoded);
  383. }
  384. - (void)testEncodesListenRequestLabels {
  385. FSTQuery *query = FSTTestQuery("collection/key");
  386. FSTQueryData *queryData = [[FSTQueryData alloc] initWithQuery:query
  387. targetID:2
  388. listenSequenceNumber:3
  389. purpose:FSTQueryPurposeListen];
  390. NSDictionary<NSString *, NSString *> *result =
  391. [self.serializer encodedListenRequestLabelsForQueryData:queryData];
  392. XCTAssertNil(result);
  393. queryData = [[FSTQueryData alloc] initWithQuery:query
  394. targetID:2
  395. listenSequenceNumber:3
  396. purpose:FSTQueryPurposeLimboResolution];
  397. result = [self.serializer encodedListenRequestLabelsForQueryData:queryData];
  398. XCTAssertEqualObjects(result, @{@"goog-listen-tags" : @"limbo-document"});
  399. queryData = [[FSTQueryData alloc] initWithQuery:query
  400. targetID:2
  401. listenSequenceNumber:3
  402. purpose:FSTQueryPurposeExistenceFilterMismatch];
  403. result = [self.serializer encodedListenRequestLabelsForQueryData:queryData];
  404. XCTAssertEqualObjects(result, @{@"goog-listen-tags" : @"existence-filter-mismatch"});
  405. }
  406. - (void)testEncodesRelationFilter {
  407. FSTRelationFilter *input = (FSTRelationFilter *)FSTTestFilter("item.part.top", @"==", @"food");
  408. GCFSStructuredQuery_Filter *actual = [self.serializer encodedRelationFilter:input];
  409. GCFSStructuredQuery_Filter *expected = [GCFSStructuredQuery_Filter message];
  410. GCFSStructuredQuery_FieldFilter *prop = expected.fieldFilter;
  411. prop.field.fieldPath = @"item.part.top";
  412. prop.op = GCFSStructuredQuery_FieldFilter_Operator_Equal;
  413. prop.value.stringValue = @"food";
  414. XCTAssertEqualObjects(actual, expected);
  415. }
  416. - (void)testEncodesArrayContainsFilter {
  417. FSTRelationFilter *input =
  418. (FSTRelationFilter *)FSTTestFilter("item.tags", @"array_contains", @"food");
  419. GCFSStructuredQuery_Filter *actual = [self.serializer encodedRelationFilter:input];
  420. GCFSStructuredQuery_Filter *expected = [GCFSStructuredQuery_Filter message];
  421. GCFSStructuredQuery_FieldFilter *prop = expected.fieldFilter;
  422. prop.field.fieldPath = @"item.tags";
  423. prop.op = GCFSStructuredQuery_FieldFilter_Operator_ArrayContains;
  424. prop.value.stringValue = @"food";
  425. XCTAssertEqualObjects(actual, expected);
  426. }
  427. #pragma mark - encodedQuery
  428. - (void)testEncodesFirstLevelKeyQueries {
  429. FSTQuery *q = FSTTestQuery("docs/1");
  430. FSTQueryData *model = [self queryDataForQuery:q];
  431. GCFSTarget *expected = [GCFSTarget message];
  432. [expected.documents.documentsArray addObject:@"projects/p/databases/d/documents/docs/1"];
  433. expected.targetId = 1;
  434. [self assertRoundTripForQueryData:model proto:expected];
  435. }
  436. - (void)testEncodesFirstLevelAncestorQueries {
  437. FSTQuery *q = FSTTestQuery("messages");
  438. FSTQueryData *model = [self queryDataForQuery:q];
  439. GCFSTarget *expected = [GCFSTarget message];
  440. expected.query.parent = @"projects/p/databases/d";
  441. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  442. from.collectionId = @"messages";
  443. [expected.query.structuredQuery.fromArray addObject:from];
  444. [expected.query.structuredQuery.orderByArray
  445. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:YES]];
  446. expected.targetId = 1;
  447. [self assertRoundTripForQueryData:model proto:expected];
  448. }
  449. - (void)testEncodesNestedAncestorQueries {
  450. FSTQuery *q = FSTTestQuery("rooms/1/messages/10/attachments");
  451. FSTQueryData *model = [self queryDataForQuery:q];
  452. GCFSTarget *expected = [GCFSTarget message];
  453. expected.query.parent = @"projects/p/databases/d/documents/rooms/1/messages/10";
  454. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  455. from.collectionId = @"attachments";
  456. [expected.query.structuredQuery.fromArray addObject:from];
  457. [expected.query.structuredQuery.orderByArray
  458. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:YES]];
  459. expected.targetId = 1;
  460. [self assertRoundTripForQueryData:model proto:expected];
  461. }
  462. - (void)testEncodesSingleFiltersAtFirstLevelCollections {
  463. FSTQuery *q = [FSTTestQuery("docs") queryByAddingFilter:FSTTestFilter("prop", @"<", @(42))];
  464. FSTQueryData *model = [self queryDataForQuery:q];
  465. GCFSTarget *expected = [GCFSTarget message];
  466. expected.query.parent = @"projects/p/databases/d";
  467. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  468. from.collectionId = @"docs";
  469. [expected.query.structuredQuery.fromArray addObject:from];
  470. [expected.query.structuredQuery.orderByArray
  471. addObject:[GCFSStructuredQuery_Order messageWithProperty:@"prop" ascending:YES]];
  472. [expected.query.structuredQuery.orderByArray
  473. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:YES]];
  474. GCFSStructuredQuery_FieldFilter *filter = expected.query.structuredQuery.where.fieldFilter;
  475. filter.field.fieldPath = @"prop";
  476. filter.op = GCFSStructuredQuery_FieldFilter_Operator_LessThan;
  477. filter.value.integerValue = 42;
  478. expected.targetId = 1;
  479. [self assertRoundTripForQueryData:model proto:expected];
  480. }
  481. - (void)testEncodesMultipleFiltersOnDeeperCollections {
  482. FSTQuery *q = [[[FSTTestQuery("rooms/1/messages/10/attachments")
  483. queryByAddingFilter:FSTTestFilter("prop", @">=", @(42))]
  484. queryByAddingFilter:FSTTestFilter("author", @"==", @"dimond")]
  485. queryByAddingFilter:FSTTestFilter("tags", @"array_contains", @"pending")];
  486. FSTQueryData *model = [self queryDataForQuery:q];
  487. GCFSTarget *expected = [GCFSTarget message];
  488. expected.query.parent = @"projects/p/databases/d/documents/rooms/1/messages/10";
  489. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  490. from.collectionId = @"attachments";
  491. [expected.query.structuredQuery.fromArray addObject:from];
  492. GCFSStructuredQuery_Filter *filter1 = [GCFSStructuredQuery_Filter message];
  493. GCFSStructuredQuery_FieldFilter *field1 = filter1.fieldFilter;
  494. field1.field.fieldPath = @"prop";
  495. field1.op = GCFSStructuredQuery_FieldFilter_Operator_GreaterThanOrEqual;
  496. field1.value.integerValue = 42;
  497. GCFSStructuredQuery_Filter *filter2 = [GCFSStructuredQuery_Filter message];
  498. GCFSStructuredQuery_FieldFilter *field2 = filter2.fieldFilter;
  499. field2.field.fieldPath = @"author";
  500. field2.op = GCFSStructuredQuery_FieldFilter_Operator_Equal;
  501. field2.value.stringValue = @"dimond";
  502. GCFSStructuredQuery_Filter *filter3 = [GCFSStructuredQuery_Filter message];
  503. GCFSStructuredQuery_FieldFilter *field3 = filter3.fieldFilter;
  504. field3.field.fieldPath = @"tags";
  505. field3.op = GCFSStructuredQuery_FieldFilter_Operator_ArrayContains;
  506. field3.value.stringValue = @"pending";
  507. GCFSStructuredQuery_CompositeFilter *composite =
  508. expected.query.structuredQuery.where.compositeFilter;
  509. composite.op = GCFSStructuredQuery_CompositeFilter_Operator_And;
  510. [composite.filtersArray addObject:filter1];
  511. [composite.filtersArray addObject:filter2];
  512. [composite.filtersArray addObject:filter3];
  513. [expected.query.structuredQuery.orderByArray
  514. addObject:[GCFSStructuredQuery_Order messageWithProperty:@"prop" ascending:YES]];
  515. [expected.query.structuredQuery.orderByArray
  516. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:YES]];
  517. expected.targetId = 1;
  518. [self assertRoundTripForQueryData:model proto:expected];
  519. }
  520. - (void)testEncodesNullFilter {
  521. [self unaryFilterTestWithValue:[NSNull null]
  522. expectedUnaryOperator:GCFSStructuredQuery_UnaryFilter_Operator_IsNull];
  523. }
  524. - (void)testEncodesNanFilter {
  525. [self unaryFilterTestWithValue:@(NAN)
  526. expectedUnaryOperator:GCFSStructuredQuery_UnaryFilter_Operator_IsNan];
  527. }
  528. - (void)unaryFilterTestWithValue:(id)value
  529. expectedUnaryOperator:(GCFSStructuredQuery_UnaryFilter_Operator)op {
  530. FSTQuery *q = [FSTTestQuery("docs") queryByAddingFilter:FSTTestFilter("prop", @"==", value)];
  531. FSTQueryData *model = [self queryDataForQuery:q];
  532. GCFSTarget *expected = [GCFSTarget message];
  533. expected.query.parent = @"projects/p/databases/d";
  534. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  535. from.collectionId = @"docs";
  536. [expected.query.structuredQuery.fromArray addObject:from];
  537. [expected.query.structuredQuery.orderByArray
  538. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:YES]];
  539. GCFSStructuredQuery_UnaryFilter *filter = expected.query.structuredQuery.where.unaryFilter;
  540. filter.field.fieldPath = @"prop";
  541. filter.op = op;
  542. expected.targetId = 1;
  543. [self assertRoundTripForQueryData:model proto:expected];
  544. }
  545. - (void)testEncodesSortOrders {
  546. FSTQuery *q = [FSTTestQuery("docs")
  547. queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("prop")
  548. ascending:YES]];
  549. FSTQueryData *model = [self queryDataForQuery:q];
  550. GCFSTarget *expected = [GCFSTarget message];
  551. expected.query.parent = @"projects/p/databases/d";
  552. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  553. from.collectionId = @"docs";
  554. [expected.query.structuredQuery.fromArray addObject:from];
  555. [expected.query.structuredQuery.orderByArray
  556. addObject:[GCFSStructuredQuery_Order messageWithProperty:@"prop" ascending:YES]];
  557. [expected.query.structuredQuery.orderByArray
  558. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:YES]];
  559. expected.targetId = 1;
  560. [self assertRoundTripForQueryData:model proto:expected];
  561. }
  562. - (void)testEncodesSortOrdersDescending {
  563. FSTQuery *q = [FSTTestQuery("rooms/1/messages/10/attachments")
  564. queryByAddingSortOrder:[FSTSortOrder sortOrderWithFieldPath:testutil::Field("prop")
  565. ascending:NO]];
  566. FSTQueryData *model = [self queryDataForQuery:q];
  567. GCFSTarget *expected = [GCFSTarget message];
  568. expected.query.parent = @"projects/p/databases/d/documents/rooms/1/messages/10";
  569. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  570. from.collectionId = @"attachments";
  571. [expected.query.structuredQuery.fromArray addObject:from];
  572. [expected.query.structuredQuery.orderByArray
  573. addObject:[GCFSStructuredQuery_Order messageWithProperty:@"prop" ascending:NO]];
  574. [expected.query.structuredQuery.orderByArray
  575. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:NO]];
  576. expected.targetId = 1;
  577. [self assertRoundTripForQueryData:model proto:expected];
  578. }
  579. - (void)testEncodesLimits {
  580. FSTQuery *q = [FSTTestQuery("docs") queryBySettingLimit:26];
  581. FSTQueryData *model = [self queryDataForQuery:q];
  582. GCFSTarget *expected = [GCFSTarget message];
  583. expected.query.parent = @"projects/p/databases/d";
  584. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  585. from.collectionId = @"docs";
  586. [expected.query.structuredQuery.fromArray addObject:from];
  587. [expected.query.structuredQuery.orderByArray
  588. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:YES]];
  589. expected.query.structuredQuery.limit.value = 26;
  590. expected.targetId = 1;
  591. [self assertRoundTripForQueryData:model proto:expected];
  592. }
  593. - (void)testEncodesResumeTokens {
  594. FSTQuery *q = FSTTestQuery("docs");
  595. FSTQueryData *model = [[FSTQueryData alloc] initWithQuery:q
  596. targetID:1
  597. listenSequenceNumber:0
  598. purpose:FSTQueryPurposeListen
  599. snapshotVersion:SnapshotVersion::None()
  600. resumeToken:FSTTestData(1, 2, 3, -1)];
  601. GCFSTarget *expected = [GCFSTarget message];
  602. expected.query.parent = @"projects/p/databases/d";
  603. GCFSStructuredQuery_CollectionSelector *from = [GCFSStructuredQuery_CollectionSelector message];
  604. from.collectionId = @"docs";
  605. [expected.query.structuredQuery.fromArray addObject:from];
  606. [expected.query.structuredQuery.orderByArray
  607. addObject:[GCFSStructuredQuery_Order messageWithProperty:kDocumentKeyPath ascending:YES]];
  608. expected.targetId = 1;
  609. expected.resumeToken = FSTTestData(1, 2, 3, -1);
  610. [self assertRoundTripForQueryData:model proto:expected];
  611. }
  612. - (FSTQueryData *)queryDataForQuery:(FSTQuery *)query {
  613. return [[FSTQueryData alloc] initWithQuery:query
  614. targetID:1
  615. listenSequenceNumber:0
  616. purpose:FSTQueryPurposeListen
  617. snapshotVersion:SnapshotVersion::None()
  618. resumeToken:[NSData data]];
  619. }
  620. - (void)assertRoundTripForQueryData:(FSTQueryData *)queryData proto:(GCFSTarget *)proto {
  621. // Verify that the encoded FSTQueryData matches the target.
  622. GCFSTarget *actualProto = [self.serializer encodedTarget:queryData];
  623. XCTAssertEqualObjects(actualProto, proto);
  624. // We don't have deserialization logic for full targets since they're not used for RPC
  625. // interaction, but the query deserialization only *is* used for the local store.
  626. FSTQuery *actualModel;
  627. if (proto.targetTypeOneOfCase == GCFSTarget_TargetType_OneOfCase_Query) {
  628. actualModel = [self.serializer decodedQueryFromQueryTarget:proto.query];
  629. } else {
  630. actualModel = [self.serializer decodedQueryFromDocumentsTarget:proto.documents];
  631. }
  632. XCTAssertEqualObjects(actualModel, queryData.query);
  633. }
  634. - (void)testConvertsTargetChangeWithAdded {
  635. FSTWatchChange *expected =
  636. [[FSTWatchTargetChange alloc] initWithState:FSTWatchTargetChangeStateAdded
  637. targetIDs:@[ @1, @4 ]
  638. resumeToken:[NSData data]
  639. cause:nil];
  640. GCFSListenResponse *listenResponse = [GCFSListenResponse message];
  641. listenResponse.targetChange.targetChangeType = GCFSTargetChange_TargetChangeType_Add;
  642. [listenResponse.targetChange.targetIdsArray addValue:1];
  643. [listenResponse.targetChange.targetIdsArray addValue:4];
  644. FSTWatchChange *actual = [self.serializer decodedWatchChange:listenResponse];
  645. XCTAssertEqualObjects(actual, expected);
  646. }
  647. - (void)testConvertsTargetChangeWithRemoved {
  648. FSTWatchChange *expected = [[FSTWatchTargetChange alloc]
  649. initWithState:FSTWatchTargetChangeStateRemoved
  650. targetIDs:@[ @1, @4 ]
  651. resumeToken:FSTTestData(0, 1, 2, -1)
  652. cause:[NSError errorWithDomain:FIRFirestoreErrorDomain
  653. code:FIRFirestoreErrorCodePermissionDenied
  654. userInfo:@{
  655. NSLocalizedDescriptionKey : @"Error message",
  656. }]];
  657. GCFSListenResponse *listenResponse = [GCFSListenResponse message];
  658. listenResponse.targetChange.targetChangeType = GCFSTargetChange_TargetChangeType_Remove;
  659. listenResponse.targetChange.cause.code = FIRFirestoreErrorCodePermissionDenied;
  660. listenResponse.targetChange.cause.message = @"Error message";
  661. listenResponse.targetChange.resumeToken = FSTTestData(0, 1, 2, -1);
  662. [listenResponse.targetChange.targetIdsArray addValue:1];
  663. [listenResponse.targetChange.targetIdsArray addValue:4];
  664. FSTWatchChange *actual = [self.serializer decodedWatchChange:listenResponse];
  665. XCTAssertEqualObjects(actual, expected);
  666. }
  667. - (void)testConvertsTargetChangeWithNoChange {
  668. FSTWatchChange *expected =
  669. [[FSTWatchTargetChange alloc] initWithState:FSTWatchTargetChangeStateNoChange
  670. targetIDs:@[ @1, @4 ]
  671. resumeToken:[NSData data]
  672. cause:nil];
  673. GCFSListenResponse *listenResponse = [GCFSListenResponse message];
  674. listenResponse.targetChange.targetChangeType = GCFSTargetChange_TargetChangeType_NoChange;
  675. [listenResponse.targetChange.targetIdsArray addValue:1];
  676. [listenResponse.targetChange.targetIdsArray addValue:4];
  677. FSTWatchChange *actual = [self.serializer decodedWatchChange:listenResponse];
  678. XCTAssertEqualObjects(actual, expected);
  679. }
  680. - (void)testConvertsDocumentChangeWithTargetIds {
  681. FSTWatchChange *expected = [[FSTDocumentWatchChange alloc]
  682. initWithUpdatedTargetIDs:@[ @1, @2 ]
  683. removedTargetIDs:@[]
  684. documentKey:FSTTestDocKey(@"coll/1")
  685. document:FSTTestDoc("coll/1", 5, @{@"foo" : @"bar"}, NO)];
  686. GCFSListenResponse *listenResponse = [GCFSListenResponse message];
  687. listenResponse.documentChange.document.name = @"projects/p/databases/d/documents/coll/1";
  688. listenResponse.documentChange.document.updateTime.nanos = 5000;
  689. GCFSValue *fooValue = [GCFSValue message];
  690. fooValue.stringValue = @"bar";
  691. [listenResponse.documentChange.document.fields setObject:fooValue forKey:@"foo"];
  692. [listenResponse.documentChange.targetIdsArray addValue:1];
  693. [listenResponse.documentChange.targetIdsArray addValue:2];
  694. FSTWatchChange *actual = [self.serializer decodedWatchChange:listenResponse];
  695. XCTAssertEqualObjects(actual, expected);
  696. }
  697. - (void)testConvertsDocumentChangeWithRemovedTargetIds {
  698. FSTWatchChange *expected = [[FSTDocumentWatchChange alloc]
  699. initWithUpdatedTargetIDs:@[ @2 ]
  700. removedTargetIDs:@[ @1 ]
  701. documentKey:FSTTestDocKey(@"coll/1")
  702. document:FSTTestDoc("coll/1", 5, @{@"foo" : @"bar"}, NO)];
  703. GCFSListenResponse *listenResponse = [GCFSListenResponse message];
  704. listenResponse.documentChange.document.name = @"projects/p/databases/d/documents/coll/1";
  705. listenResponse.documentChange.document.updateTime.nanos = 5000;
  706. GCFSValue *fooValue = [GCFSValue message];
  707. fooValue.stringValue = @"bar";
  708. [listenResponse.documentChange.document.fields setObject:fooValue forKey:@"foo"];
  709. [listenResponse.documentChange.removedTargetIdsArray addValue:1];
  710. [listenResponse.documentChange.targetIdsArray addValue:2];
  711. FSTWatchChange *actual = [self.serializer decodedWatchChange:listenResponse];
  712. XCTAssertEqualObjects(actual, expected);
  713. }
  714. - (void)testConvertsDocumentChangeWithDeletions {
  715. FSTWatchChange *expected =
  716. [[FSTDocumentWatchChange alloc] initWithUpdatedTargetIDs:@[]
  717. removedTargetIDs:@[ @1, @2 ]
  718. documentKey:FSTTestDocKey(@"coll/1")
  719. document:FSTTestDeletedDoc("coll/1", 5)];
  720. GCFSListenResponse *listenResponse = [GCFSListenResponse message];
  721. listenResponse.documentDelete.document = @"projects/p/databases/d/documents/coll/1";
  722. listenResponse.documentDelete.readTime.nanos = 5000;
  723. [listenResponse.documentDelete.removedTargetIdsArray addValue:1];
  724. [listenResponse.documentDelete.removedTargetIdsArray addValue:2];
  725. FSTWatchChange *actual = [self.serializer decodedWatchChange:listenResponse];
  726. XCTAssertEqualObjects(actual, expected);
  727. }
  728. - (void)testConvertsDocumentChangeWithRemoves {
  729. FSTWatchChange *expected =
  730. [[FSTDocumentWatchChange alloc] initWithUpdatedTargetIDs:@[]
  731. removedTargetIDs:@[ @1, @2 ]
  732. documentKey:FSTTestDocKey(@"coll/1")
  733. document:nil];
  734. GCFSListenResponse *listenResponse = [GCFSListenResponse message];
  735. listenResponse.documentRemove.document = @"projects/p/databases/d/documents/coll/1";
  736. [listenResponse.documentRemove.removedTargetIdsArray addValue:1];
  737. [listenResponse.documentRemove.removedTargetIdsArray addValue:2];
  738. FSTWatchChange *actual = [self.serializer decodedWatchChange:listenResponse];
  739. XCTAssertEqualObjects(actual, expected);
  740. }
  741. @end
  742. NS_ASSUME_NONNULL_END