FIRDatabaseTests.mm 45 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221
  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 <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  19. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  20. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  21. #import "Firestore/Source/Core/FSTFirestoreClient.h"
  22. using firebase::firestore::util::TimerId;
  23. @interface FIRDatabaseTests : FSTIntegrationTestCase
  24. @end
  25. @implementation FIRDatabaseTests
  26. - (void)testCanUpdateAnExistingDocument {
  27. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  28. NSDictionary<NSString *, id> *initialData =
  29. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  30. NSDictionary<NSString *, id> *updateData =
  31. @{@"desc" : @"NewDescription", @"owner.email" : @"new@xyz.com"};
  32. NSDictionary<NSString *, id> *finalData =
  33. @{@"desc" : @"NewDescription", @"owner" : @{@"name" : @"Jonny", @"email" : @"new@xyz.com"}};
  34. [self writeDocumentRef:doc data:initialData];
  35. XCTestExpectation *updateCompletion = [self expectationWithDescription:@"updateData"];
  36. [doc updateData:updateData
  37. completion:^(NSError *_Nullable error) {
  38. XCTAssertNil(error);
  39. [updateCompletion fulfill];
  40. }];
  41. [self awaitExpectations];
  42. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  43. XCTAssertTrue(result.exists);
  44. XCTAssertEqualObjects(result.data, finalData);
  45. }
  46. - (void)testCanUpdateAnUnknownDocument {
  47. [self readerAndWriterOnDocumentRef:^(NSString *path, FIRDocumentReference *readerRef,
  48. FIRDocumentReference *writerRef) {
  49. [self writeDocumentRef:writerRef data:@{@"a" : @"a"}];
  50. [self updateDocumentRef:readerRef data:@{@"b" : @"b"}];
  51. FIRDocumentSnapshot *writerSnap = [self readDocumentForRef:writerRef
  52. source:FIRFirestoreSourceCache];
  53. XCTAssertTrue(writerSnap.exists);
  54. XCTestExpectation *expectation =
  55. [self expectationWithDescription:@"testCanUpdateAnUnknownDocument"];
  56. [readerRef getDocumentWithSource:FIRFirestoreSourceCache
  57. completion:^(FIRDocumentSnapshot *doc, NSError *_Nullable error) {
  58. XCTAssertNotNil(error);
  59. [expectation fulfill];
  60. }];
  61. [self awaitExpectations];
  62. writerSnap = [self readDocumentForRef:writerRef];
  63. XCTAssertEqualObjects(writerSnap.data, (@{@"a" : @"a", @"b" : @"b"}));
  64. FIRDocumentSnapshot *readerSnap = [self readDocumentForRef:writerRef];
  65. XCTAssertEqualObjects(readerSnap.data, (@{@"a" : @"a", @"b" : @"b"}));
  66. }];
  67. }
  68. - (void)testCanDeleteAFieldWithAnUpdate {
  69. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  70. NSDictionary<NSString *, id> *initialData =
  71. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  72. NSDictionary<NSString *, id> *updateData =
  73. @{@"owner.email" : [FIRFieldValue fieldValueForDelete]};
  74. NSDictionary<NSString *, id> *finalData =
  75. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny"}};
  76. [self writeDocumentRef:doc data:initialData];
  77. [self updateDocumentRef:doc data:updateData];
  78. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  79. XCTAssertTrue(result.exists);
  80. XCTAssertEqualObjects(result.data, finalData);
  81. }
  82. - (void)testDeleteDocument {
  83. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  84. NSDictionary<NSString *, id> *data = @{@"value" : @"foo"};
  85. [self writeDocumentRef:doc data:data];
  86. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  87. XCTAssertEqualObjects(result.data, data);
  88. [self deleteDocumentRef:doc];
  89. result = [self readDocumentForRef:doc];
  90. XCTAssertFalse(result.exists);
  91. }
  92. - (void)testCanRetrieveDocumentThatDoesNotExist {
  93. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  94. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  95. XCTAssertNil(result.data);
  96. XCTAssertNil(result[@"foo"]);
  97. }
  98. - (void)testCannotUpdateNonexistentDocument {
  99. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  100. XCTestExpectation *setCompletion = [self expectationWithDescription:@"setData"];
  101. [doc updateData:@{@"owner" : @"abc"}
  102. completion:^(NSError *_Nullable error) {
  103. XCTAssertNotNil(error);
  104. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  105. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  106. [setCompletion fulfill];
  107. }];
  108. [self awaitExpectations];
  109. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  110. XCTAssertFalse(result.exists);
  111. }
  112. - (void)testCanOverwriteDataAnExistingDocumentUsingSet {
  113. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  114. NSDictionary<NSString *, id> *initialData =
  115. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  116. NSDictionary<NSString *, id> *udpateData = @{@"desc" : @"NewDescription"};
  117. [self writeDocumentRef:doc data:initialData];
  118. [self writeDocumentRef:doc data:udpateData];
  119. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  120. XCTAssertEqualObjects(document.data, udpateData);
  121. }
  122. - (void)testCanMergeDataWithAnExistingDocumentUsingSet {
  123. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  124. NSDictionary<NSString *, id> *initialData =
  125. @{@"desc" : @"Description", @"owner.data" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  126. NSDictionary<NSString *, id> *mergeData =
  127. @{@"updated" : @YES, @"owner.data" : @{@"name" : @"Sebastian"}};
  128. NSDictionary<NSString *, id> *finalData = @{
  129. @"desc" : @"Description",
  130. @"updated" : @YES,
  131. @"owner.data" : @{@"name" : @"Sebastian", @"email" : @"abc@xyz.com"}
  132. };
  133. [self writeDocumentRef:doc data:initialData];
  134. XCTestExpectation *completed =
  135. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  136. [doc setData:mergeData
  137. merge:YES
  138. completion:^(NSError *error) {
  139. XCTAssertNil(error);
  140. [completed fulfill];
  141. }];
  142. [self awaitExpectations];
  143. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  144. XCTAssertEqualObjects(document.data, finalData);
  145. }
  146. - (void)testCanMergeEmptyObject {
  147. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  148. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  149. id<FIRListenerRegistration> listenerRegistration =
  150. [doc addSnapshotListener:[accumulator valueEventHandler]];
  151. [self writeDocumentRef:doc data:@{}];
  152. FIRDocumentSnapshot *snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  153. XCTAssertEqualObjects(snapshot.data, @{});
  154. [self mergeDocumentRef:doc data:@{@"a" : @{}} fields:@[ @"a" ]];
  155. snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  156. XCTAssertEqualObjects(snapshot.data, @{@"a" : @{}});
  157. [self mergeDocumentRef:doc data:@{@"b" : @{}}];
  158. snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  159. XCTAssertEqualObjects(snapshot.data, (@{@"a" : @{}, @"b" : @{}}));
  160. snapshot = [self readDocumentForRef:doc source:FIRFirestoreSourceServer];
  161. XCTAssertEqualObjects(snapshot.data, (@{@"a" : @{}, @"b" : @{}}));
  162. [listenerRegistration remove];
  163. }
  164. - (void)testCanMergeServerTimestamps {
  165. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  166. NSDictionary<NSString *, id> *initialData = @{
  167. @"updated" : @NO,
  168. };
  169. NSDictionary<NSString *, id> *mergeData = @{
  170. @"time" : [FIRFieldValue fieldValueForServerTimestamp],
  171. @"nested" : @{@"time" : [FIRFieldValue fieldValueForServerTimestamp]}
  172. };
  173. [self writeDocumentRef:doc data:initialData];
  174. XCTestExpectation *completed =
  175. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  176. [doc setData:mergeData
  177. merge:YES
  178. completion:^(NSError *error) {
  179. XCTAssertNil(error);
  180. [completed fulfill];
  181. }];
  182. [self awaitExpectations];
  183. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  184. XCTAssertEqual(document[@"updated"], @NO);
  185. XCTAssertTrue([document[@"time"] isKindOfClass:[FIRTimestamp class]]);
  186. XCTAssertTrue([document[@"nested.time"] isKindOfClass:[FIRTimestamp class]]);
  187. }
  188. - (void)testCanDeleteFieldUsingMerge {
  189. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  190. NSDictionary<NSString *, id> *initialData =
  191. @{@"untouched" : @YES, @"foo" : @"bar", @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}};
  192. NSDictionary<NSString *, id> *mergeData = @{
  193. @"foo" : [FIRFieldValue fieldValueForDelete],
  194. @"nested" : @{@"foo" : [FIRFieldValue fieldValueForDelete]}
  195. };
  196. [self writeDocumentRef:doc data:initialData];
  197. XCTestExpectation *completed =
  198. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  199. [doc setData:mergeData
  200. merge:YES
  201. completion:^(NSError *error) {
  202. XCTAssertNil(error);
  203. [completed fulfill];
  204. }];
  205. [self awaitExpectations];
  206. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  207. XCTAssertEqual(document[@"untouched"], @YES);
  208. XCTAssertNil(document[@"foo"]);
  209. XCTAssertEqual(document[@"nested.untouched"], @YES);
  210. XCTAssertNil(document[@"nested.foo"]);
  211. }
  212. - (void)testCanDeleteFieldUsingMergeFields {
  213. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  214. NSDictionary<NSString *, id> *initialData = @{
  215. @"untouched" : @YES,
  216. @"foo" : @"bar",
  217. @"inner" : @{@"removed" : @YES, @"foo" : @"bar"},
  218. @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}
  219. };
  220. NSDictionary<NSString *, id> *mergeData = @{
  221. @"foo" : [FIRFieldValue fieldValueForDelete],
  222. @"inner" : @{@"foo" : [FIRFieldValue fieldValueForDelete]},
  223. @"nested" : @{
  224. @"untouched" : [FIRFieldValue fieldValueForDelete],
  225. @"foo" : [FIRFieldValue fieldValueForDelete]
  226. }
  227. };
  228. NSDictionary<NSString *, id> *finalData =
  229. @{@"untouched" : @YES, @"inner" : @{}, @"nested" : @{@"untouched" : @YES}};
  230. [self writeDocumentRef:doc data:initialData];
  231. XCTestExpectation *completed =
  232. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  233. [doc setData:mergeData
  234. mergeFields:@[ @"foo", @"inner", @"nested.foo" ]
  235. completion:^(NSError *error) {
  236. XCTAssertNil(error);
  237. [completed fulfill];
  238. }];
  239. [self awaitExpectations];
  240. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  241. XCTAssertEqualObjects([document data], finalData);
  242. }
  243. - (void)testCanSetServerTimestampsUsingMergeFields {
  244. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  245. NSDictionary<NSString *, id> *initialData =
  246. @{@"untouched" : @YES, @"foo" : @"bar", @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}};
  247. NSDictionary<NSString *, id> *mergeData = @{
  248. @"foo" : [FIRFieldValue fieldValueForServerTimestamp],
  249. @"inner" : @{@"foo" : [FIRFieldValue fieldValueForServerTimestamp]},
  250. @"nested" : @{@"foo" : [FIRFieldValue fieldValueForServerTimestamp]}
  251. };
  252. [self writeDocumentRef:doc data:initialData];
  253. XCTestExpectation *completed =
  254. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  255. [doc setData:mergeData
  256. mergeFields:@[ @"foo", @"inner", @"nested.foo" ]
  257. completion:^(NSError *error) {
  258. XCTAssertNil(error);
  259. [completed fulfill];
  260. }];
  261. [self awaitExpectations];
  262. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  263. XCTAssertTrue([document exists]);
  264. XCTAssertTrue([document[@"foo"] isKindOfClass:[FIRTimestamp class]]);
  265. XCTAssertTrue([document[@"inner.foo"] isKindOfClass:[FIRTimestamp class]]);
  266. XCTAssertTrue([document[@"nested.foo"] isKindOfClass:[FIRTimestamp class]]);
  267. }
  268. - (void)testMergeReplacesArrays {
  269. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  270. NSDictionary<NSString *, id> *initialData = @{
  271. @"untouched" : @YES,
  272. @"data" : @"old",
  273. @"topLevel" : @[ @"old", @"old" ],
  274. @"mapInArray" : @[ @{@"data" : @"old"} ]
  275. };
  276. NSDictionary<NSString *, id> *mergeData =
  277. @{@"data" : @"new", @"topLevel" : @[ @"new" ], @"mapInArray" : @[ @{@"data" : @"new"} ]};
  278. NSDictionary<NSString *, id> *finalData = @{
  279. @"untouched" : @YES,
  280. @"data" : @"new",
  281. @"topLevel" : @[ @"new" ],
  282. @"mapInArray" : @[ @{@"data" : @"new"} ]
  283. };
  284. [self writeDocumentRef:doc data:initialData];
  285. XCTestExpectation *completed =
  286. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  287. [doc setData:mergeData
  288. merge:YES
  289. completion:^(NSError *error) {
  290. XCTAssertNil(error);
  291. [completed fulfill];
  292. }];
  293. [self awaitExpectations];
  294. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  295. XCTAssertEqualObjects(document.data, finalData);
  296. }
  297. - (void)testCannotSpecifyFieldMaskForMissingField {
  298. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  299. XCTAssertThrowsSpecific(
  300. { [doc setData:@{} mergeFields:@[ @"foo" ]]; }, NSException,
  301. @"Field 'foo' is specified in your field mask but missing from your input data.");
  302. }
  303. - (void)testCanSetASubsetOfFieldsUsingMask {
  304. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  305. NSDictionary<NSString *, id> *initialData =
  306. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  307. NSDictionary<NSString *, id> *finalData = @{@"desc" : @"Description", @"owner" : @"Sebastian"};
  308. [self writeDocumentRef:doc data:initialData];
  309. XCTestExpectation *completed =
  310. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  311. [doc setData:@{@"desc" : @"NewDescription", @"owner" : @"Sebastian"}
  312. mergeFields:@[ @"owner" ]
  313. completion:^(NSError *error) {
  314. XCTAssertNil(error);
  315. [completed fulfill];
  316. }];
  317. [self awaitExpectations];
  318. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  319. XCTAssertEqualObjects(document.data, finalData);
  320. }
  321. - (void)testDoesNotApplyFieldDeleteOutsideOfMask {
  322. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  323. NSDictionary<NSString *, id> *initialData =
  324. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  325. NSDictionary<NSString *, id> *finalData = @{@"desc" : @"Description", @"owner" : @"Sebastian"};
  326. [self writeDocumentRef:doc data:initialData];
  327. XCTestExpectation *completed =
  328. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  329. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForDelete], @"owner" : @"Sebastian"}
  330. mergeFields:@[ @"owner" ]
  331. completion:^(NSError *error) {
  332. XCTAssertNil(error);
  333. [completed fulfill];
  334. }];
  335. [self awaitExpectations];
  336. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  337. XCTAssertEqualObjects(document.data, finalData);
  338. }
  339. - (void)testDoesNotApplyFieldTransformOutsideOfMask {
  340. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  341. NSDictionary<NSString *, id> *initialData =
  342. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  343. NSDictionary<NSString *, id> *finalData = @{@"desc" : @"Description", @"owner" : @"Sebastian"};
  344. [self writeDocumentRef:doc data:initialData];
  345. XCTestExpectation *completed =
  346. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  347. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForServerTimestamp], @"owner" : @"Sebastian"}
  348. mergeFields:@[ @"owner" ]
  349. completion:^(NSError *error) {
  350. XCTAssertNil(error);
  351. [completed fulfill];
  352. }];
  353. [self awaitExpectations];
  354. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  355. XCTAssertEqualObjects(document.data, finalData);
  356. }
  357. - (void)testCanSetEmptyFieldMask {
  358. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  359. NSDictionary<NSString *, id> *initialData =
  360. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  361. NSDictionary<NSString *, id> *finalData = initialData;
  362. [self writeDocumentRef:doc data:initialData];
  363. XCTestExpectation *completed =
  364. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  365. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForServerTimestamp], @"owner" : @"Sebastian"}
  366. mergeFields:@[]
  367. completion:^(NSError *error) {
  368. XCTAssertNil(error);
  369. [completed fulfill];
  370. }];
  371. [self awaitExpectations];
  372. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  373. XCTAssertEqualObjects(document.data, finalData);
  374. }
  375. - (void)testCanSpecifyFieldsMultipleTimesInFieldMask {
  376. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  377. NSDictionary<NSString *, id> *initialData =
  378. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  379. NSDictionary<NSString *, id> *finalData =
  380. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Sebastian", @"email" : @"new@xyz.com"}};
  381. [self writeDocumentRef:doc data:initialData];
  382. XCTestExpectation *completed =
  383. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  384. [doc setData:@{
  385. @"desc" : @"NewDescription",
  386. @"owner" : @{@"name" : @"Sebastian", @"email" : @"new@xyz.com"}
  387. }
  388. mergeFields:@[ @"owner.name", @"owner", @"owner" ]
  389. completion:^(NSError *error) {
  390. XCTAssertNil(error);
  391. [completed fulfill];
  392. }];
  393. [self awaitExpectations];
  394. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  395. XCTAssertEqualObjects(document.data, finalData);
  396. }
  397. - (void)testAddingToACollectionYieldsTheCorrectDocumentReference {
  398. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  399. FIRDocumentReference *ref = [coll addDocumentWithData:@{@"foo" : @1}];
  400. XCTestExpectation *getCompletion = [self expectationWithDescription:@"getData"];
  401. [ref getDocumentWithCompletion:^(FIRDocumentSnapshot *_Nullable document,
  402. NSError *_Nullable error) {
  403. XCTAssertNil(error);
  404. XCTAssertEqualObjects(document.data, (@{@"foo" : @1}));
  405. [getCompletion fulfill];
  406. }];
  407. [self awaitExpectations];
  408. }
  409. - (void)testListenCanBeCalledMultipleTimes {
  410. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  411. FIRDocumentReference *doc = [coll documentWithAutoID];
  412. XCTestExpectation *completed = [self expectationWithDescription:@"multiple addSnapshotListeners"];
  413. __block NSDictionary<NSString *, id> *resultingData;
  414. // Shut the compiler up about strong references to doc.
  415. FIRDocumentReference *__weak weakDoc = doc;
  416. [doc setData:@{@"foo" : @"bar"}
  417. completion:^(NSError *error1) {
  418. XCTAssertNil(error1);
  419. FIRDocumentReference *strongDoc = weakDoc;
  420. [strongDoc addSnapshotListener:^(FIRDocumentSnapshot *snapshot2, NSError *error2) {
  421. XCTAssertNil(error2);
  422. FIRDocumentReference *strongDoc2 = weakDoc;
  423. [strongDoc2 addSnapshotListener:^(FIRDocumentSnapshot *snapshot3, NSError *error3) {
  424. XCTAssertNil(error3);
  425. resultingData = snapshot3.data;
  426. [completed fulfill];
  427. }];
  428. }];
  429. }];
  430. [self awaitExpectations];
  431. XCTAssertEqualObjects(resultingData, @{@"foo" : @"bar"});
  432. }
  433. - (void)testDocumentSnapshotEvents_nonExistent {
  434. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  435. XCTestExpectation *snapshotCompletion = [self expectationWithDescription:@"snapshot"];
  436. __block int callbacks = 0;
  437. id<FIRListenerRegistration> listenerRegistration =
  438. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  439. callbacks++;
  440. if (callbacks == 1) {
  441. XCTAssertNotNil(doc);
  442. XCTAssertFalse(doc.exists);
  443. [snapshotCompletion fulfill];
  444. } else {
  445. XCTFail("Should not have received this callback");
  446. }
  447. }];
  448. [self awaitExpectations];
  449. [listenerRegistration remove];
  450. }
  451. - (void)testDocumentSnapshotEvents_forAdd {
  452. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  453. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  454. __block XCTestExpectation *dataCompletion;
  455. __block int callbacks = 0;
  456. id<FIRListenerRegistration> listenerRegistration =
  457. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  458. callbacks++;
  459. if (callbacks == 1) {
  460. XCTAssertNotNil(doc);
  461. XCTAssertFalse(doc.exists);
  462. [emptyCompletion fulfill];
  463. } else if (callbacks == 2) {
  464. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  465. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  466. [dataCompletion fulfill];
  467. } else {
  468. XCTFail("Should not have received this callback");
  469. }
  470. }];
  471. [self awaitExpectations];
  472. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  473. [docRef setData:@{@"a" : @1}];
  474. [self awaitExpectations];
  475. [listenerRegistration remove];
  476. }
  477. - (void)testDocumentSnapshotEvents_forAddIncludingMetadata {
  478. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  479. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  480. __block XCTestExpectation *dataCompletion;
  481. __block int callbacks = 0;
  482. id<FIRListenerRegistration> listenerRegistration = [docRef
  483. addSnapshotListenerWithIncludeMetadataChanges:YES
  484. listener:^(FIRDocumentSnapshot *_Nullable doc,
  485. NSError *error) {
  486. callbacks++;
  487. if (callbacks == 1) {
  488. XCTAssertNotNil(doc);
  489. XCTAssertFalse(doc.exists);
  490. [emptyCompletion fulfill];
  491. } else if (callbacks == 2) {
  492. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  493. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  494. } else if (callbacks == 3) {
  495. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  496. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  497. [dataCompletion fulfill];
  498. } else {
  499. XCTFail("Should not have received this callback");
  500. }
  501. }];
  502. [self awaitExpectations];
  503. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  504. [docRef setData:@{@"a" : @1}];
  505. [self awaitExpectations];
  506. [listenerRegistration remove];
  507. }
  508. - (void)testDocumentSnapshotEvents_forChange {
  509. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  510. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  511. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  512. [self writeDocumentRef:docRef data:initialData];
  513. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  514. __block XCTestExpectation *changeCompletion;
  515. __block int callbacks = 0;
  516. id<FIRListenerRegistration> listenerRegistration =
  517. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  518. callbacks++;
  519. if (callbacks == 1) {
  520. XCTAssertEqualObjects(doc.data, initialData);
  521. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  522. [initialCompletion fulfill];
  523. } else if (callbacks == 2) {
  524. XCTAssertEqualObjects(doc.data, changedData);
  525. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  526. [changeCompletion fulfill];
  527. } else {
  528. XCTFail("Should not have received this callback");
  529. }
  530. }];
  531. [self awaitExpectations];
  532. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  533. [docRef setData:changedData];
  534. [self awaitExpectations];
  535. [listenerRegistration remove];
  536. }
  537. - (void)testDocumentSnapshotEvents_forChangeIncludingMetadata {
  538. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  539. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  540. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  541. [self writeDocumentRef:docRef data:initialData];
  542. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  543. __block XCTestExpectation *changeCompletion;
  544. __block int callbacks = 0;
  545. id<FIRListenerRegistration> listenerRegistration = [docRef
  546. addSnapshotListenerWithIncludeMetadataChanges:YES
  547. listener:^(FIRDocumentSnapshot *_Nullable doc,
  548. NSError *error) {
  549. callbacks++;
  550. if (callbacks == 1) {
  551. XCTAssertEqualObjects(doc.data, initialData);
  552. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  553. XCTAssertEqual(doc.metadata.isFromCache, YES);
  554. } else if (callbacks == 2) {
  555. XCTAssertEqualObjects(doc.data, initialData);
  556. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  557. XCTAssertEqual(doc.metadata.isFromCache, NO);
  558. [initialCompletion fulfill];
  559. } else if (callbacks == 3) {
  560. XCTAssertEqualObjects(doc.data, changedData);
  561. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  562. XCTAssertEqual(doc.metadata.isFromCache, NO);
  563. } else if (callbacks == 4) {
  564. XCTAssertEqualObjects(doc.data, changedData);
  565. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  566. XCTAssertEqual(doc.metadata.isFromCache, NO);
  567. [changeCompletion fulfill];
  568. } else {
  569. XCTFail("Should not have received this callback");
  570. }
  571. }];
  572. [self awaitExpectations];
  573. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  574. [docRef setData:changedData];
  575. [self awaitExpectations];
  576. [listenerRegistration remove];
  577. }
  578. - (void)testDocumentSnapshotEvents_forDelete {
  579. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  580. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  581. [self writeDocumentRef:docRef data:initialData];
  582. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  583. __block XCTestExpectation *changeCompletion;
  584. __block int callbacks = 0;
  585. id<FIRListenerRegistration> listenerRegistration =
  586. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  587. callbacks++;
  588. if (callbacks == 1) {
  589. XCTAssertEqualObjects(doc.data, initialData);
  590. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  591. XCTAssertEqual(doc.metadata.isFromCache, YES);
  592. [initialCompletion fulfill];
  593. } else if (callbacks == 2) {
  594. XCTAssertFalse(doc.exists);
  595. [changeCompletion fulfill];
  596. } else {
  597. XCTFail("Should not have received this callback");
  598. }
  599. }];
  600. [self awaitExpectations];
  601. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  602. [docRef deleteDocument];
  603. [self awaitExpectations];
  604. [listenerRegistration remove];
  605. }
  606. - (void)testDocumentSnapshotEvents_forDeleteIncludingMetadata {
  607. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  608. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  609. [self writeDocumentRef:docRef data:initialData];
  610. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  611. __block XCTestExpectation *changeCompletion;
  612. __block int callbacks = 0;
  613. id<FIRListenerRegistration> listenerRegistration = [docRef
  614. addSnapshotListenerWithIncludeMetadataChanges:YES
  615. listener:^(FIRDocumentSnapshot *_Nullable doc,
  616. NSError *error) {
  617. callbacks++;
  618. if (callbacks == 1) {
  619. XCTAssertEqualObjects(doc.data, initialData);
  620. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  621. XCTAssertEqual(doc.metadata.isFromCache, YES);
  622. } else if (callbacks == 2) {
  623. XCTAssertEqualObjects(doc.data, initialData);
  624. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  625. XCTAssertEqual(doc.metadata.isFromCache, NO);
  626. [initialCompletion fulfill];
  627. } else if (callbacks == 3) {
  628. XCTAssertFalse(doc.exists);
  629. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  630. XCTAssertEqual(doc.metadata.isFromCache, NO);
  631. [changeCompletion fulfill];
  632. } else {
  633. XCTFail("Should not have received this callback");
  634. }
  635. }];
  636. [self awaitExpectations];
  637. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  638. [docRef deleteDocument];
  639. [self awaitExpectations];
  640. [listenerRegistration remove];
  641. }
  642. - (void)testQuerySnapshotEvents_forAdd {
  643. FIRCollectionReference *roomsRef = [self collectionRef];
  644. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  645. NSDictionary<NSString *, id> *newData = @{@"a" : @1};
  646. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  647. __block XCTestExpectation *changeCompletion;
  648. __block int callbacks = 0;
  649. id<FIRListenerRegistration> listenerRegistration =
  650. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  651. callbacks++;
  652. if (callbacks == 1) {
  653. XCTAssertEqual(docSet.count, 0);
  654. [emptyCompletion fulfill];
  655. } else if (callbacks == 2) {
  656. XCTAssertEqual(docSet.count, 1);
  657. XCTAssertTrue([docSet.documents[0] isKindOfClass:[FIRQueryDocumentSnapshot class]]);
  658. XCTAssertEqualObjects(docSet.documents[0].data, newData);
  659. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  660. [changeCompletion fulfill];
  661. } else {
  662. XCTFail("Should not have received a third callback");
  663. }
  664. }];
  665. [self awaitExpectations];
  666. changeCompletion = [self expectationWithDescription:@"changed snapshot"];
  667. [docRef setData:newData];
  668. [self awaitExpectations];
  669. [listenerRegistration remove];
  670. }
  671. - (void)testQuerySnapshotEvents_forChange {
  672. FIRCollectionReference *roomsRef = [self collectionRef];
  673. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  674. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  675. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  676. [self writeDocumentRef:docRef data:initialData];
  677. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  678. __block XCTestExpectation *changeCompletion;
  679. __block int callbacks = 0;
  680. id<FIRListenerRegistration> listenerRegistration =
  681. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  682. callbacks++;
  683. if (callbacks == 1) {
  684. XCTAssertEqual(docSet.count, 1);
  685. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  686. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  687. [initialCompletion fulfill];
  688. } else if (callbacks == 2) {
  689. XCTAssertEqual(docSet.count, 1);
  690. XCTAssertEqualObjects(docSet.documents[0].data, changedData);
  691. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  692. [changeCompletion fulfill];
  693. } else {
  694. XCTFail("Should not have received a third callback");
  695. }
  696. }];
  697. [self awaitExpectations];
  698. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  699. [docRef setData:changedData];
  700. [self awaitExpectations];
  701. [listenerRegistration remove];
  702. }
  703. - (void)testQuerySnapshotEvents_forDelete {
  704. FIRCollectionReference *roomsRef = [self collectionRef];
  705. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  706. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  707. [self writeDocumentRef:docRef data:initialData];
  708. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  709. __block XCTestExpectation *changeCompletion;
  710. __block int callbacks = 0;
  711. id<FIRListenerRegistration> listenerRegistration =
  712. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  713. callbacks++;
  714. if (callbacks == 1) {
  715. XCTAssertEqual(docSet.count, 1);
  716. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  717. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  718. [initialCompletion fulfill];
  719. } else if (callbacks == 2) {
  720. XCTAssertEqual(docSet.count, 0);
  721. [changeCompletion fulfill];
  722. } else {
  723. XCTFail("Should not have received a third callback");
  724. }
  725. }];
  726. [self awaitExpectations];
  727. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  728. [docRef deleteDocument];
  729. [self awaitExpectations];
  730. [listenerRegistration remove];
  731. }
  732. - (void)testExposesFirestoreOnDocumentReferences {
  733. FIRDocumentReference *doc = [self.db documentWithPath:@"foo/bar"];
  734. XCTAssertEqual(doc.firestore, self.db);
  735. }
  736. - (void)testExposesFirestoreOnQueries {
  737. FIRQuery *q = [[self.db collectionWithPath:@"foo"] queryLimitedTo:5];
  738. XCTAssertEqual(q.firestore, self.db);
  739. }
  740. - (void)testDocumentReferenceEquality {
  741. FIRFirestore *firestore = self.db;
  742. FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
  743. XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
  744. XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);
  745. XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);
  746. FIRFirestore *otherFirestore = [self firestore];
  747. XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
  748. }
  749. - (void)testQueryReferenceEquality {
  750. FIRFirestore *firestore = self.db;
  751. FIRQuery *query =
  752. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  753. isEqualTo:@42];
  754. FIRQuery *query2 =
  755. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  756. isEqualTo:@42];
  757. XCTAssertEqualObjects(query, query2);
  758. FIRQuery *query3 =
  759. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
  760. isEqualTo:@42];
  761. XCTAssertNotEqualObjects(query, query3);
  762. FIRFirestore *otherFirestore = [self firestore];
  763. FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
  764. queryWhereField:@"baz"
  765. isEqualTo:@42];
  766. XCTAssertNotEqualObjects(query, query4);
  767. }
  768. - (void)testCanTraverseCollectionsAndDocuments {
  769. NSString *expected = @"a/b/c/d";
  770. // doc path from root Firestore.
  771. XCTAssertEqualObjects([self.db documentWithPath:@"a/b/c/d"].path, expected);
  772. // collection path from root Firestore.
  773. XCTAssertEqualObjects([[self.db collectionWithPath:@"a/b/c"] documentWithPath:@"d"].path,
  774. expected);
  775. // doc path from CollectionReference.
  776. XCTAssertEqualObjects([[self.db collectionWithPath:@"a"] documentWithPath:@"b/c/d"].path,
  777. expected);
  778. // collection path from DocumentReference.
  779. XCTAssertEqualObjects([[self.db documentWithPath:@"a/b"] collectionWithPath:@"c/d/e"].path,
  780. @"a/b/c/d/e");
  781. }
  782. - (void)testCanTraverseCollectionAndDocumentParents {
  783. FIRCollectionReference *collection = [self.db collectionWithPath:@"a/b/c"];
  784. XCTAssertEqualObjects(collection.path, @"a/b/c");
  785. FIRDocumentReference *doc = collection.parent;
  786. XCTAssertEqualObjects(doc.path, @"a/b");
  787. collection = doc.parent;
  788. XCTAssertEqualObjects(collection.path, @"a");
  789. FIRDocumentReference *nilDoc = collection.parent;
  790. XCTAssertNil(nilDoc);
  791. }
  792. - (void)testUpdateFieldsWithDots {
  793. FIRDocumentReference *doc = [self documentRef];
  794. [self writeDocumentRef:doc data:@{@"a.b" : @"old", @"c.d" : @"old"}];
  795. [self updateDocumentRef:doc data:@{[[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"}];
  796. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  797. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  798. XCTAssertNil(error);
  799. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  800. [expectation fulfill];
  801. }];
  802. [self awaitExpectations];
  803. }
  804. - (void)testUpdateNestedFields {
  805. FIRDocumentReference *doc = [self documentRef];
  806. [self writeDocumentRef:doc
  807. data:@{
  808. @"a" : @{@"b" : @"old"},
  809. @"c" : @{@"d" : @"old"},
  810. @"e" : @{@"f" : @"old"}
  811. }];
  812. [self updateDocumentRef:doc
  813. data:@{
  814. @"a.b" : @"new",
  815. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  816. }];
  817. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  818. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  819. XCTAssertNil(error);
  820. XCTAssertEqualObjects(snapshot.data, (@{
  821. @"a" : @{@"b" : @"new"},
  822. @"c" : @{@"d" : @"new"},
  823. @"e" : @{@"f" : @"old"}
  824. }));
  825. [expectation fulfill];
  826. }];
  827. [self awaitExpectations];
  828. }
  829. - (void)testCollectionID {
  830. XCTAssertEqualObjects([self.db collectionWithPath:@"foo"].collectionID, @"foo");
  831. XCTAssertEqualObjects([self.db collectionWithPath:@"foo/bar/baz"].collectionID, @"baz");
  832. }
  833. - (void)testDocumentID {
  834. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar"].documentID, @"bar");
  835. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux");
  836. }
  837. - (void)testCanQueueWritesWhileOffline {
  838. XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"];
  839. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"];
  840. FIRDocumentReference *doc = [self documentRef];
  841. FIRFirestore *firestore = doc.firestore;
  842. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  843. [firestore disableNetworkWithCompletion:^(NSError *error) {
  844. XCTAssertNil(error);
  845. [doc setData:data
  846. completion:^(NSError *error) {
  847. XCTAssertNil(error);
  848. [writeEpectation fulfill];
  849. }];
  850. [firestore enableNetworkWithCompletion:^(NSError *error) {
  851. XCTAssertNil(error);
  852. [networkExpectation fulfill];
  853. }];
  854. }];
  855. [self awaitExpectations];
  856. XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"];
  857. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  858. XCTAssertNil(error);
  859. XCTAssertEqualObjects(snapshot.data, data);
  860. XCTAssertFalse(snapshot.metadata.isFromCache);
  861. [getExpectation fulfill];
  862. }];
  863. [self awaitExpectations];
  864. }
  865. - (void)testCanGetDocumentsWhileOffline {
  866. FIRDocumentReference *doc = [self documentRef];
  867. FIRFirestore *firestore = doc.firestore;
  868. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  869. XCTestExpectation *failExpectation =
  870. [self expectationWithDescription:@"offline read with no cached data"];
  871. XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"];
  872. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"];
  873. __weak FIRDocumentReference *weakDoc = doc;
  874. [firestore disableNetworkWithCompletion:^(NSError *error) {
  875. XCTAssertNil(error);
  876. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  877. XCTAssertNotNil(error);
  878. [failExpectation fulfill];
  879. }];
  880. [doc setData:data
  881. completion:^(NSError *_Nullable error) {
  882. XCTAssertNil(error);
  883. [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  884. XCTAssertNil(error);
  885. // Verify that we are not reading from cache.
  886. XCTAssertFalse(snapshot.metadata.isFromCache);
  887. [onlineExpectation fulfill];
  888. }];
  889. }];
  890. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  891. XCTAssertNil(error);
  892. // Verify that we are reading from cache.
  893. XCTAssertTrue(snapshot.metadata.fromCache);
  894. XCTAssertEqualObjects(snapshot.data, data);
  895. [firestore enableNetworkWithCompletion:^(NSError *error) {
  896. [networkExpectation fulfill];
  897. }];
  898. }];
  899. }];
  900. [self awaitExpectations];
  901. }
  902. - (void)testWriteStreamReconnectsAfterIdle {
  903. FIRDocumentReference *doc = [self documentRef];
  904. FIRFirestore *firestore = doc.firestore;
  905. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  906. [self queueForFirestore:firestore] -> RunScheduledOperationsUntil(TimerId::WriteStreamIdle);
  907. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  908. }
  909. - (void)testWatchStreamReconnectsAfterIdle {
  910. FIRDocumentReference *doc = [self documentRef];
  911. FIRFirestore *firestore = doc.firestore;
  912. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  913. [self queueForFirestore:firestore] -> RunScheduledOperationsUntil(TimerId::ListenStreamIdle);
  914. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  915. }
  916. - (void)testCanDisableNetwork {
  917. FIRDocumentReference *doc = [self documentRef];
  918. FIRFirestore *firestore = doc.firestore;
  919. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  920. [self awaitExpectations];
  921. [firestore
  922. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network again"]];
  923. [self awaitExpectations];
  924. [firestore
  925. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  926. [self awaitExpectations];
  927. [firestore
  928. disableNetworkWithCompletion:[self
  929. completionForExpectationWithName:@"Disable network again"]];
  930. [self awaitExpectations];
  931. [firestore
  932. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Final enable network"]];
  933. [self awaitExpectations];
  934. }
  935. @end