FIRDatabaseTests.mm 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222
  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
  796. data:@{(id)[[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"}];
  797. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  798. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  799. XCTAssertNil(error);
  800. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  801. [expectation fulfill];
  802. }];
  803. [self awaitExpectations];
  804. }
  805. - (void)testUpdateNestedFields {
  806. FIRDocumentReference *doc = [self documentRef];
  807. [self writeDocumentRef:doc
  808. data:@{
  809. @"a" : @{@"b" : @"old"},
  810. @"c" : @{@"d" : @"old"},
  811. @"e" : @{@"f" : @"old"}
  812. }];
  813. [self updateDocumentRef:doc
  814. data:@{
  815. (id) @"a.b" : @"new",
  816. (id)[[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  817. }];
  818. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  819. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  820. XCTAssertNil(error);
  821. XCTAssertEqualObjects(snapshot.data, (@{
  822. @"a" : @{@"b" : @"new"},
  823. @"c" : @{@"d" : @"new"},
  824. @"e" : @{@"f" : @"old"}
  825. }));
  826. [expectation fulfill];
  827. }];
  828. [self awaitExpectations];
  829. }
  830. - (void)testCollectionID {
  831. XCTAssertEqualObjects([self.db collectionWithPath:@"foo"].collectionID, @"foo");
  832. XCTAssertEqualObjects([self.db collectionWithPath:@"foo/bar/baz"].collectionID, @"baz");
  833. }
  834. - (void)testDocumentID {
  835. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar"].documentID, @"bar");
  836. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux");
  837. }
  838. - (void)testCanQueueWritesWhileOffline {
  839. XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"];
  840. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"];
  841. FIRDocumentReference *doc = [self documentRef];
  842. FIRFirestore *firestore = doc.firestore;
  843. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  844. [firestore disableNetworkWithCompletion:^(NSError *error) {
  845. XCTAssertNil(error);
  846. [doc setData:data
  847. completion:^(NSError *error) {
  848. XCTAssertNil(error);
  849. [writeEpectation fulfill];
  850. }];
  851. [firestore enableNetworkWithCompletion:^(NSError *error) {
  852. XCTAssertNil(error);
  853. [networkExpectation fulfill];
  854. }];
  855. }];
  856. [self awaitExpectations];
  857. XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"];
  858. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  859. XCTAssertNil(error);
  860. XCTAssertEqualObjects(snapshot.data, data);
  861. XCTAssertFalse(snapshot.metadata.isFromCache);
  862. [getExpectation fulfill];
  863. }];
  864. [self awaitExpectations];
  865. }
  866. - (void)testCanGetDocumentsWhileOffline {
  867. FIRDocumentReference *doc = [self documentRef];
  868. FIRFirestore *firestore = doc.firestore;
  869. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  870. XCTestExpectation *failExpectation =
  871. [self expectationWithDescription:@"offline read with no cached data"];
  872. XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"];
  873. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"];
  874. __weak FIRDocumentReference *weakDoc = doc;
  875. [firestore disableNetworkWithCompletion:^(NSError *error) {
  876. XCTAssertNil(error);
  877. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  878. XCTAssertNotNil(error);
  879. [failExpectation fulfill];
  880. }];
  881. [doc setData:data
  882. completion:^(NSError *_Nullable error) {
  883. XCTAssertNil(error);
  884. [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  885. XCTAssertNil(error);
  886. // Verify that we are not reading from cache.
  887. XCTAssertFalse(snapshot.metadata.isFromCache);
  888. [onlineExpectation fulfill];
  889. }];
  890. }];
  891. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  892. XCTAssertNil(error);
  893. // Verify that we are reading from cache.
  894. XCTAssertTrue(snapshot.metadata.fromCache);
  895. XCTAssertEqualObjects(snapshot.data, data);
  896. [firestore enableNetworkWithCompletion:^(NSError *error) {
  897. [networkExpectation fulfill];
  898. }];
  899. }];
  900. }];
  901. [self awaitExpectations];
  902. }
  903. - (void)testWriteStreamReconnectsAfterIdle {
  904. FIRDocumentReference *doc = [self documentRef];
  905. FIRFirestore *firestore = doc.firestore;
  906. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  907. [self queueForFirestore:firestore] -> RunScheduledOperationsUntil(TimerId::WriteStreamIdle);
  908. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  909. }
  910. - (void)testWatchStreamReconnectsAfterIdle {
  911. FIRDocumentReference *doc = [self documentRef];
  912. FIRFirestore *firestore = doc.firestore;
  913. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  914. [self queueForFirestore:firestore] -> RunScheduledOperationsUntil(TimerId::ListenStreamIdle);
  915. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  916. }
  917. - (void)testCanDisableNetwork {
  918. FIRDocumentReference *doc = [self documentRef];
  919. FIRFirestore *firestore = doc.firestore;
  920. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  921. [self awaitExpectations];
  922. [firestore
  923. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network again"]];
  924. [self awaitExpectations];
  925. [firestore
  926. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  927. [self awaitExpectations];
  928. [firestore
  929. disableNetworkWithCompletion:[self
  930. completionForExpectationWithName:@"Disable network again"]];
  931. [self awaitExpectations];
  932. [firestore
  933. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Final enable network"]];
  934. [self awaitExpectations];
  935. }
  936. @end