FSTSerializerBetaTests.m 32 KB

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