FIRDatabaseTests.mm 45 KB

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