FIRDatabaseTests.mm 51 KB

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