FIRDatabaseTests.mm 71 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891
  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. // TODO(wuandy): Delete this once isPersistenceEnabled and cacheSizeBytes are removed.
  17. #pragma clang diagnostic ignored "-Wdeprecated-declarations"
  18. #import <FirebaseFirestore/FirebaseFirestore.h>
  19. #import <XCTest/XCTest.h>
  20. #import "FirebaseCore/Extension/FIRAppInternal.h"
  21. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  22. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  23. #import "Firestore/Source/API/FIRFirestore+Internal.h"
  24. #import "Firestore/Source/API/FIRLocalCacheSettings+Internal.h"
  25. #include "Firestore/core/src/api/query_snapshot.h"
  26. #include "Firestore/core/src/api/settings.h"
  27. #include "Firestore/core/src/core/firestore_client.h"
  28. #include "Firestore/core/src/model/database_id.h"
  29. #include "Firestore/core/src/util/string_apple.h"
  30. #include "Firestore/core/test/unit/testutil/app_testing.h"
  31. using api::Settings;
  32. using firebase::firestore::model::DatabaseId;
  33. using firebase::firestore::testutil::AppForUnitTesting;
  34. using firebase::firestore::util::MakeNSString;
  35. using firebase::firestore::util::MakeString;
  36. using firebase::firestore::util::TimerId;
  37. @interface FIRDatabaseTests : FSTIntegrationTestCase
  38. @end
  39. @implementation FIRDatabaseTests
  40. - (void)tearDown {
  41. [FIRApp resetApps];
  42. [super tearDown];
  43. }
  44. - (void)testCanUpdateAnExistingDocument {
  45. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  46. NSDictionary<NSString *, id> *initialData =
  47. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  48. NSDictionary<NSString *, id> *updateData =
  49. @{@"desc" : @"NewDescription", @"owner.email" : @"new@xyz.com"};
  50. NSDictionary<NSString *, id> *finalData =
  51. @{@"desc" : @"NewDescription", @"owner" : @{@"name" : @"Jonny", @"email" : @"new@xyz.com"}};
  52. [self writeDocumentRef:doc data:initialData];
  53. XCTestExpectation *updateCompletion = [self expectationWithDescription:@"updateData"];
  54. [doc updateData:updateData
  55. completion:^(NSError *_Nullable error) {
  56. XCTAssertNil(error);
  57. [updateCompletion fulfill];
  58. }];
  59. [self awaitExpectations];
  60. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  61. XCTAssertTrue(result.exists);
  62. XCTAssertEqualObjects(result.data, finalData);
  63. }
  64. - (void)testEqualityComparison {
  65. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  66. NSDictionary<NSString *, id> *initialData =
  67. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  68. [self writeDocumentRef:doc data:initialData];
  69. FIRDocumentSnapshot *snap1 = [self readDocumentForRef:doc];
  70. FIRDocumentSnapshot *snap2 = [self readDocumentForRef:doc];
  71. FIRDocumentSnapshot *snap3 = [self readDocumentForRef:doc];
  72. XCTAssertTrue([snap1.metadata isEqual:snap2.metadata]);
  73. XCTAssertTrue([snap2.metadata isEqual:snap3.metadata]);
  74. XCTAssertTrue([snap1.documentID isEqual:snap2.documentID]);
  75. XCTAssertTrue([snap2.documentID isEqual:snap3.documentID]);
  76. XCTAssertTrue(snap1.exists == snap2.exists);
  77. XCTAssertTrue(snap2.exists == snap3.exists);
  78. XCTAssertTrue([snap1.reference isEqual:snap2.reference]);
  79. XCTAssertTrue([snap2.reference isEqual:snap3.reference]);
  80. XCTAssertTrue([[snap1 data] isEqual:[snap2 data]]);
  81. XCTAssertTrue([[snap2 data] isEqual:[snap3 data]]);
  82. XCTAssertTrue([snap1 isEqual:snap2]);
  83. XCTAssertTrue([snap2 isEqual:snap3]);
  84. }
  85. - (void)testCanUpdateAnUnknownDocument {
  86. [self readerAndWriterOnDocumentRef:^(FIRDocumentReference *readerRef,
  87. FIRDocumentReference *writerRef) {
  88. [self writeDocumentRef:writerRef data:@{@"a" : @"a"}];
  89. [self updateDocumentRef:readerRef data:@{@"b" : @"b"}];
  90. FIRDocumentSnapshot *writerSnap = [self readDocumentForRef:writerRef
  91. source:FIRFirestoreSourceCache];
  92. XCTAssertTrue(writerSnap.exists);
  93. XCTestExpectation *expectation =
  94. [self expectationWithDescription:@"testCanUpdateAnUnknownDocument"];
  95. [readerRef getDocumentWithSource:FIRFirestoreSourceCache
  96. completion:^(FIRDocumentSnapshot *, NSError *_Nullable error) {
  97. XCTAssertNotNil(error);
  98. [expectation fulfill];
  99. }];
  100. [self awaitExpectations];
  101. writerSnap = [self readDocumentForRef:writerRef];
  102. XCTAssertEqualObjects(writerSnap.data, (@{@"a" : @"a", @"b" : @"b"}));
  103. FIRDocumentSnapshot *readerSnap = [self readDocumentForRef:writerRef];
  104. XCTAssertEqualObjects(readerSnap.data, (@{@"a" : @"a", @"b" : @"b"}));
  105. }];
  106. }
  107. - (void)testCanDeleteAFieldWithAnUpdate {
  108. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  109. NSDictionary<NSString *, id> *initialData =
  110. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  111. NSDictionary<NSString *, id> *updateData =
  112. @{@"owner.email" : [FIRFieldValue fieldValueForDelete]};
  113. NSDictionary<NSString *, id> *finalData =
  114. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny"}};
  115. [self writeDocumentRef:doc data:initialData];
  116. [self updateDocumentRef:doc data:updateData];
  117. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  118. XCTAssertTrue(result.exists);
  119. XCTAssertEqualObjects(result.data, finalData);
  120. }
  121. - (void)testDeleteDocument {
  122. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  123. NSDictionary<NSString *, id> *data = @{@"value" : @"foo"};
  124. [self writeDocumentRef:doc data:data];
  125. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  126. XCTAssertEqualObjects(result.data, data);
  127. [self deleteDocumentRef:doc];
  128. result = [self readDocumentForRef:doc];
  129. XCTAssertFalse(result.exists);
  130. }
  131. - (void)testCanRetrieveDocumentThatDoesNotExist {
  132. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  133. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  134. XCTAssertNil(result.data);
  135. XCTAssertNil(result[@"foo"]);
  136. }
  137. - (void)testCannotUpdateNonexistentDocument {
  138. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  139. XCTestExpectation *setCompletion = [self expectationWithDescription:@"setData"];
  140. [doc updateData:@{@"owner" : @"abc"}
  141. completion:^(NSError *_Nullable error) {
  142. XCTAssertNotNil(error);
  143. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  144. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  145. [setCompletion fulfill];
  146. }];
  147. [self awaitExpectations];
  148. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  149. XCTAssertFalse(result.exists);
  150. }
  151. - (void)testCanOverwriteDataAnExistingDocumentUsingSet {
  152. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  153. NSDictionary<NSString *, id> *initialData =
  154. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  155. NSDictionary<NSString *, id> *udpateData = @{@"desc" : @"NewDescription"};
  156. [self writeDocumentRef:doc data:initialData];
  157. [self writeDocumentRef:doc data:udpateData];
  158. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  159. XCTAssertEqualObjects(document.data, udpateData);
  160. }
  161. - (void)testCanMergeDataWithAnExistingDocumentUsingSet {
  162. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  163. NSDictionary<NSString *, id> *initialData =
  164. @{@"desc" : @"Description", @"owner.data" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  165. NSDictionary<NSString *, id> *mergeData =
  166. @{@"updated" : @YES, @"owner.data" : @{@"name" : @"Sebastian"}};
  167. NSDictionary<NSString *, id> *finalData = @{
  168. @"desc" : @"Description",
  169. @"updated" : @YES,
  170. @"owner.data" : @{@"name" : @"Sebastian", @"email" : @"abc@xyz.com"}
  171. };
  172. [self writeDocumentRef:doc data:initialData];
  173. XCTestExpectation *completed =
  174. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  175. [doc setData:mergeData
  176. merge:YES
  177. completion:^(NSError *error) {
  178. XCTAssertNil(error);
  179. [completed fulfill];
  180. }];
  181. [self awaitExpectations];
  182. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  183. XCTAssertEqualObjects(document.data, finalData);
  184. }
  185. - (void)testCanMergeEmptyObject {
  186. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  187. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  188. id<FIRListenerRegistration> listenerRegistration =
  189. [doc addSnapshotListener:[accumulator valueEventHandler]];
  190. [self writeDocumentRef:doc data:@{}];
  191. FIRDocumentSnapshot *snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  192. XCTAssertEqualObjects(snapshot.data, @{});
  193. [self mergeDocumentRef:doc data:@{@"a" : @{}} fields:@[ @"a" ]];
  194. snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  195. XCTAssertEqualObjects(snapshot.data, @{@"a" : @{}});
  196. [self mergeDocumentRef:doc data:@{@"b" : @{}}];
  197. snapshot = [accumulator awaitEventWithName:@"Snapshot"];
  198. XCTAssertEqualObjects(snapshot.data, (@{@"a" : @{}, @"b" : @{}}));
  199. snapshot = [self readDocumentForRef:doc source:FIRFirestoreSourceServer];
  200. XCTAssertEqualObjects(snapshot.data, (@{@"a" : @{}, @"b" : @{}}));
  201. [listenerRegistration remove];
  202. }
  203. - (void)testCanMergeServerTimestamps {
  204. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  205. NSDictionary<NSString *, id> *initialData = @{
  206. @"updated" : @NO,
  207. };
  208. NSDictionary<NSString *, id> *mergeData = @{
  209. @"time" : [FIRFieldValue fieldValueForServerTimestamp],
  210. @"nested" : @{@"time" : [FIRFieldValue fieldValueForServerTimestamp]}
  211. };
  212. [self writeDocumentRef:doc data:initialData];
  213. XCTestExpectation *completed =
  214. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  215. [doc setData:mergeData
  216. merge:YES
  217. completion:^(NSError *error) {
  218. XCTAssertNil(error);
  219. [completed fulfill];
  220. }];
  221. [self awaitExpectations];
  222. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  223. XCTAssertEqual(document[@"updated"], @NO);
  224. XCTAssertTrue([document[@"time"] isKindOfClass:[FIRTimestamp class]]);
  225. XCTAssertTrue([document[@"nested.time"] isKindOfClass:[FIRTimestamp class]]);
  226. }
  227. - (void)testCanDeleteFieldUsingMerge {
  228. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  229. NSDictionary<NSString *, id> *initialData =
  230. @{@"untouched" : @YES, @"foo" : @"bar", @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}};
  231. NSDictionary<NSString *, id> *mergeData = @{
  232. @"foo" : [FIRFieldValue fieldValueForDelete],
  233. @"nested" : @{@"foo" : [FIRFieldValue fieldValueForDelete]}
  234. };
  235. [self writeDocumentRef:doc data:initialData];
  236. XCTestExpectation *completed =
  237. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  238. [doc setData:mergeData
  239. merge:YES
  240. completion:^(NSError *error) {
  241. XCTAssertNil(error);
  242. [completed fulfill];
  243. }];
  244. [self awaitExpectations];
  245. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  246. XCTAssertEqual(document[@"untouched"], @YES);
  247. XCTAssertNil(document[@"foo"]);
  248. XCTAssertEqual(document[@"nested.untouched"], @YES);
  249. XCTAssertNil(document[@"nested.foo"]);
  250. }
  251. - (void)testCanDeleteFieldUsingMergeFields {
  252. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  253. NSDictionary<NSString *, id> *initialData = @{
  254. @"untouched" : @YES,
  255. @"foo" : @"bar",
  256. @"inner" : @{@"removed" : @YES, @"foo" : @"bar"},
  257. @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}
  258. };
  259. NSDictionary<NSString *, id> *mergeData = @{
  260. @"foo" : [FIRFieldValue fieldValueForDelete],
  261. @"inner" : @{@"foo" : [FIRFieldValue fieldValueForDelete]},
  262. @"nested" : @{
  263. @"untouched" : [FIRFieldValue fieldValueForDelete],
  264. @"foo" : [FIRFieldValue fieldValueForDelete]
  265. }
  266. };
  267. NSDictionary<NSString *, id> *finalData =
  268. @{@"untouched" : @YES, @"inner" : @{}, @"nested" : @{@"untouched" : @YES}};
  269. [self writeDocumentRef:doc data:initialData];
  270. XCTestExpectation *completed =
  271. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  272. [doc setData:mergeData
  273. mergeFields:@[ @"foo", @"inner", @"nested.foo" ]
  274. completion:^(NSError *error) {
  275. XCTAssertNil(error);
  276. [completed fulfill];
  277. }];
  278. [self awaitExpectations];
  279. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  280. XCTAssertEqualObjects([document data], finalData);
  281. }
  282. - (void)testCanSetServerTimestampsUsingMergeFields {
  283. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  284. NSDictionary<NSString *, id> *initialData =
  285. @{@"untouched" : @YES, @"foo" : @"bar", @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}};
  286. NSDictionary<NSString *, id> *mergeData = @{
  287. @"foo" : [FIRFieldValue fieldValueForServerTimestamp],
  288. @"inner" : @{@"foo" : [FIRFieldValue fieldValueForServerTimestamp]},
  289. @"nested" : @{@"foo" : [FIRFieldValue fieldValueForServerTimestamp]}
  290. };
  291. [self writeDocumentRef:doc data:initialData];
  292. XCTestExpectation *completed =
  293. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  294. [doc setData:mergeData
  295. mergeFields:@[ @"foo", @"inner", @"nested.foo" ]
  296. completion:^(NSError *error) {
  297. XCTAssertNil(error);
  298. [completed fulfill];
  299. }];
  300. [self awaitExpectations];
  301. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  302. XCTAssertTrue([document exists]);
  303. XCTAssertTrue([document[@"foo"] isKindOfClass:[FIRTimestamp class]]);
  304. XCTAssertTrue([document[@"inner.foo"] isKindOfClass:[FIRTimestamp class]]);
  305. XCTAssertTrue([document[@"nested.foo"] isKindOfClass:[FIRTimestamp class]]);
  306. }
  307. - (void)testMergeReplacesArrays {
  308. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  309. NSDictionary<NSString *, id> *initialData = @{
  310. @"untouched" : @YES,
  311. @"data" : @"old",
  312. @"topLevel" : @[ @"old", @"old" ],
  313. @"mapInArray" : @[ @{@"data" : @"old"} ]
  314. };
  315. NSDictionary<NSString *, id> *mergeData =
  316. @{@"data" : @"new", @"topLevel" : @[ @"new" ], @"mapInArray" : @[ @{@"data" : @"new"} ]};
  317. NSDictionary<NSString *, id> *finalData = @{
  318. @"untouched" : @YES,
  319. @"data" : @"new",
  320. @"topLevel" : @[ @"new" ],
  321. @"mapInArray" : @[ @{@"data" : @"new"} ]
  322. };
  323. [self writeDocumentRef:doc data:initialData];
  324. XCTestExpectation *completed =
  325. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  326. [doc setData:mergeData
  327. merge:YES
  328. completion:^(NSError *error) {
  329. XCTAssertNil(error);
  330. [completed fulfill];
  331. }];
  332. [self awaitExpectations];
  333. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  334. XCTAssertEqualObjects(document.data, finalData);
  335. }
  336. - (void)testCannotSpecifyFieldMaskForMissingField {
  337. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  338. XCTAssertThrowsSpecific(
  339. [doc setData:@{} mergeFields:@[ @"foo" ]], NSException,
  340. @"Field 'foo' is specified in your field mask but missing from your input data.");
  341. }
  342. - (void)testCanSetASubsetOfFieldsUsingMask {
  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" : @"NewDescription", @"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)testDoesNotApplyFieldDeleteOutsideOfMask {
  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 = @{@"desc" : @"Description", @"owner" : @"Sebastian"};
  365. [self writeDocumentRef:doc data:initialData];
  366. XCTestExpectation *completed =
  367. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  368. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForDelete], @"owner" : @"Sebastian"}
  369. mergeFields:@[ @"owner" ]
  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)testDoesNotApplyFieldTransformOutsideOfMask {
  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 = @{@"desc" : @"Description", @"owner" : @"Sebastian"};
  383. [self writeDocumentRef:doc data:initialData];
  384. XCTestExpectation *completed =
  385. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  386. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForServerTimestamp], @"owner" : @"Sebastian"}
  387. mergeFields:@[ @"owner" ]
  388. completion:^(NSError *error) {
  389. XCTAssertNil(error);
  390. [completed fulfill];
  391. }];
  392. [self awaitExpectations];
  393. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  394. XCTAssertEqualObjects(document.data, finalData);
  395. }
  396. - (void)testCanSetEmptyFieldMask {
  397. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  398. NSDictionary<NSString *, id> *initialData =
  399. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  400. NSDictionary<NSString *, id> *finalData = initialData;
  401. [self writeDocumentRef:doc data:initialData];
  402. XCTestExpectation *completed =
  403. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  404. [doc setData:@{@"desc" : [FIRFieldValue fieldValueForServerTimestamp], @"owner" : @"Sebastian"}
  405. mergeFields:@[]
  406. completion:^(NSError *error) {
  407. XCTAssertNil(error);
  408. [completed fulfill];
  409. }];
  410. [self awaitExpectations];
  411. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  412. XCTAssertEqualObjects(document.data, finalData);
  413. }
  414. - (void)testCanSpecifyFieldsMultipleTimesInFieldMask {
  415. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  416. NSDictionary<NSString *, id> *initialData =
  417. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  418. NSDictionary<NSString *, id> *finalData =
  419. @{@"desc" : @"Description", @"owner" : @{@"name" : @"Sebastian", @"email" : @"new@xyz.com"}};
  420. [self writeDocumentRef:doc data:initialData];
  421. XCTestExpectation *completed =
  422. [self expectationWithDescription:@"testCanSetASubsetOfFieldsUsingMask"];
  423. [doc setData:@{
  424. @"desc" : @"NewDescription",
  425. @"owner" : @{@"name" : @"Sebastian", @"email" : @"new@xyz.com"}
  426. }
  427. mergeFields:@[ @"owner.name", @"owner", @"owner" ]
  428. completion:^(NSError *error) {
  429. XCTAssertNil(error);
  430. [completed fulfill];
  431. }];
  432. [self awaitExpectations];
  433. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  434. XCTAssertEqualObjects(document.data, finalData);
  435. }
  436. - (void)testAddingToACollectionYieldsTheCorrectDocumentReference {
  437. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  438. FIRDocumentReference *ref = [coll addDocumentWithData:@{@"foo" : @1}];
  439. XCTestExpectation *getCompletion = [self expectationWithDescription:@"getData"];
  440. [ref getDocumentWithCompletion:^(FIRDocumentSnapshot *_Nullable document,
  441. NSError *_Nullable error) {
  442. XCTAssertNil(error);
  443. XCTAssertEqualObjects(document.data, (@{@"foo" : @1}));
  444. [getCompletion fulfill];
  445. }];
  446. [self awaitExpectations];
  447. }
  448. - (void)testSnapshotsInSyncListenerFiresAfterListenersInSync {
  449. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  450. FIRDocumentReference *ref = [coll addDocumentWithData:@{@"foo" : @1}];
  451. NSMutableArray<NSString *> *events = [NSMutableArray array];
  452. XCTestExpectation *gotInitialSnapshot = [self expectationWithDescription:@"gotInitialSnapshot"];
  453. __block bool setupComplete = false;
  454. [ref addSnapshotListener:^(FIRDocumentSnapshot *, NSError *error) {
  455. XCTAssertNil(error);
  456. [events addObject:@"doc"];
  457. // Wait for the initial event from the backend so that we know we'll get exactly one snapshot
  458. // event for our local write below.
  459. if (!setupComplete) {
  460. setupComplete = true;
  461. [gotInitialSnapshot fulfill];
  462. }
  463. }];
  464. [self awaitExpectations];
  465. [events removeAllObjects];
  466. XCTestExpectation *done = [self expectationWithDescription:@"SnapshotsInSyncListenerDone"];
  467. [ref.firestore addSnapshotsInSyncListener:^() {
  468. [events addObject:@"snapshots-in-sync"];
  469. if ([events count] == 3) {
  470. // We should have an initial snapshots-in-sync event, then a snapshot event
  471. // for set(), then another event to indicate we're in sync again.
  472. NSArray<NSString *> *expected = @[ @"snapshots-in-sync", @"doc", @"snapshots-in-sync" ];
  473. XCTAssertEqualObjects(events, expected);
  474. [done fulfill];
  475. }
  476. }];
  477. [self writeDocumentRef:ref data:@{@"foo" : @3}];
  478. [self awaitExpectation:done];
  479. }
  480. - (void)testSnapshotsInSyncRemoveIsIdempotent {
  481. // This test merely verifies that calling remove multiple times doesn't
  482. // explode.
  483. auto listener = [self.db addSnapshotsInSyncListener:^(){
  484. }];
  485. [listener remove];
  486. [listener remove];
  487. }
  488. - (void)testListenCanBeCalledMultipleTimes {
  489. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  490. FIRDocumentReference *doc = [coll documentWithAutoID];
  491. XCTestExpectation *completed = [self expectationWithDescription:@"multiple addSnapshotListeners"];
  492. __block NSDictionary<NSString *, id> *resultingData;
  493. // Shut the compiler up about strong references to doc.
  494. FIRDocumentReference *__weak weakDoc = doc;
  495. [doc setData:@{@"foo" : @"bar"}
  496. completion:^(NSError *error1) {
  497. XCTAssertNil(error1);
  498. FIRDocumentReference *strongDoc = weakDoc;
  499. [strongDoc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *error2) {
  500. XCTAssertNil(error2);
  501. FIRDocumentReference *strongDoc2 = weakDoc;
  502. [strongDoc2 addSnapshotListener:^(FIRDocumentSnapshot *snapshot3, NSError *error3) {
  503. XCTAssertNil(error3);
  504. resultingData = snapshot3.data;
  505. [completed fulfill];
  506. }];
  507. }];
  508. }];
  509. [self awaitExpectations];
  510. XCTAssertEqualObjects(resultingData, @{@"foo" : @"bar"});
  511. }
  512. - (void)testDocumentSnapshotEvents_nonExistent {
  513. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  514. XCTestExpectation *snapshotCompletion = [self expectationWithDescription:@"snapshot"];
  515. __block int callbacks = 0;
  516. id<FIRListenerRegistration> listenerRegistration =
  517. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *) {
  518. callbacks++;
  519. if (callbacks == 1) {
  520. XCTAssertNotNil(doc);
  521. XCTAssertFalse(doc.exists);
  522. [snapshotCompletion fulfill];
  523. } else {
  524. XCTFail("Should not have received this callback");
  525. }
  526. }];
  527. [self awaitExpectations];
  528. [listenerRegistration remove];
  529. }
  530. - (void)testDocumentSnapshotEvents_forAdd {
  531. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  532. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  533. __block XCTestExpectation *dataCompletion;
  534. __block int callbacks = 0;
  535. id<FIRListenerRegistration> listenerRegistration =
  536. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *) {
  537. callbacks++;
  538. if (callbacks == 1) {
  539. XCTAssertNotNil(doc);
  540. XCTAssertFalse(doc.exists);
  541. [emptyCompletion fulfill];
  542. } else if (callbacks == 2) {
  543. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  544. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  545. [dataCompletion fulfill];
  546. } else {
  547. XCTFail("Should not have received this callback");
  548. }
  549. }];
  550. [self awaitExpectations];
  551. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  552. [docRef setData:@{@"a" : @1}];
  553. [self awaitExpectations];
  554. [listenerRegistration remove];
  555. }
  556. - (void)testDocumentSnapshotEvents_forAddIncludingMetadata {
  557. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  558. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  559. __block XCTestExpectation *dataCompletion;
  560. __block int callbacks = 0;
  561. id<FIRListenerRegistration> listenerRegistration = [docRef
  562. addSnapshotListenerWithIncludeMetadataChanges:YES
  563. listener:^(FIRDocumentSnapshot *_Nullable doc,
  564. NSError *) {
  565. callbacks++;
  566. if (callbacks == 1) {
  567. XCTAssertNotNil(doc);
  568. XCTAssertFalse(doc.exists);
  569. [emptyCompletion fulfill];
  570. } else if (callbacks == 2) {
  571. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  572. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  573. } else if (callbacks == 3) {
  574. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  575. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  576. [dataCompletion fulfill];
  577. } else {
  578. XCTFail("Should not have received this callback");
  579. }
  580. }];
  581. [self awaitExpectations];
  582. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  583. [docRef setData:@{@"a" : @1}];
  584. [self awaitExpectations];
  585. [listenerRegistration remove];
  586. }
  587. - (void)testDocumentSnapshotEvents_forChange {
  588. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  589. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  590. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  591. [self writeDocumentRef:docRef data:initialData];
  592. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  593. __block XCTestExpectation *changeCompletion;
  594. __block int callbacks = 0;
  595. id<FIRListenerRegistration> listenerRegistration =
  596. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *) {
  597. callbacks++;
  598. if (callbacks == 1) {
  599. XCTAssertEqualObjects(doc.data, initialData);
  600. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  601. [initialCompletion fulfill];
  602. } else if (callbacks == 2) {
  603. XCTAssertEqualObjects(doc.data, changedData);
  604. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  605. [changeCompletion fulfill];
  606. } else {
  607. XCTFail("Should not have received this callback");
  608. }
  609. }];
  610. [self awaitExpectations];
  611. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  612. [docRef setData:changedData];
  613. [self awaitExpectations];
  614. [listenerRegistration remove];
  615. }
  616. - (void)testDocumentSnapshotEvents_forChangeIncludingMetadata {
  617. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  618. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  619. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  620. [self writeDocumentRef:docRef data:initialData];
  621. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  622. __block XCTestExpectation *changeCompletion;
  623. __block int callbacks = 0;
  624. id<FIRListenerRegistration> listenerRegistration = [docRef
  625. addSnapshotListenerWithIncludeMetadataChanges:YES
  626. listener:^(FIRDocumentSnapshot *_Nullable doc,
  627. NSError *) {
  628. callbacks++;
  629. if (callbacks == 1) {
  630. XCTAssertEqualObjects(doc.data, initialData);
  631. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  632. XCTAssertEqual(doc.metadata.isFromCache, YES);
  633. } else if (callbacks == 2) {
  634. XCTAssertEqualObjects(doc.data, initialData);
  635. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  636. XCTAssertEqual(doc.metadata.isFromCache, NO);
  637. [initialCompletion fulfill];
  638. } else if (callbacks == 3) {
  639. XCTAssertEqualObjects(doc.data, changedData);
  640. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  641. XCTAssertEqual(doc.metadata.isFromCache, NO);
  642. } else if (callbacks == 4) {
  643. XCTAssertEqualObjects(doc.data, changedData);
  644. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  645. XCTAssertEqual(doc.metadata.isFromCache, NO);
  646. [changeCompletion fulfill];
  647. } else {
  648. XCTFail("Should not have received this callback");
  649. }
  650. }];
  651. [self awaitExpectations];
  652. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  653. [docRef setData:changedData];
  654. [self awaitExpectations];
  655. [listenerRegistration remove];
  656. }
  657. - (void)testDocumentSnapshotEvents_forDelete {
  658. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  659. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  660. [self writeDocumentRef:docRef data:initialData];
  661. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  662. __block XCTestExpectation *changeCompletion;
  663. __block int callbacks = 0;
  664. id<FIRListenerRegistration> listenerRegistration =
  665. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *) {
  666. callbacks++;
  667. if (callbacks == 1) {
  668. XCTAssertEqualObjects(doc.data, initialData);
  669. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  670. XCTAssertEqual(doc.metadata.isFromCache, YES);
  671. [initialCompletion fulfill];
  672. } else if (callbacks == 2) {
  673. XCTAssertFalse(doc.exists);
  674. [changeCompletion fulfill];
  675. } else {
  676. XCTFail("Should not have received this callback");
  677. }
  678. }];
  679. [self awaitExpectations];
  680. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  681. [docRef deleteDocument];
  682. [self awaitExpectations];
  683. [listenerRegistration remove];
  684. }
  685. - (void)testDocumentSnapshotEvents_forDeleteIncludingMetadata {
  686. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  687. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  688. [self writeDocumentRef:docRef data:initialData];
  689. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  690. __block XCTestExpectation *changeCompletion;
  691. __block int callbacks = 0;
  692. id<FIRListenerRegistration> listenerRegistration = [docRef
  693. addSnapshotListenerWithIncludeMetadataChanges:YES
  694. listener:^(FIRDocumentSnapshot *_Nullable doc,
  695. NSError *) {
  696. callbacks++;
  697. if (callbacks == 1) {
  698. XCTAssertEqualObjects(doc.data, initialData);
  699. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  700. XCTAssertEqual(doc.metadata.isFromCache, YES);
  701. } else if (callbacks == 2) {
  702. XCTAssertEqualObjects(doc.data, initialData);
  703. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  704. XCTAssertEqual(doc.metadata.isFromCache, NO);
  705. [initialCompletion fulfill];
  706. } else if (callbacks == 3) {
  707. XCTAssertFalse(doc.exists);
  708. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  709. XCTAssertEqual(doc.metadata.isFromCache, NO);
  710. [changeCompletion fulfill];
  711. } else {
  712. XCTFail("Should not have received this callback");
  713. }
  714. }];
  715. [self awaitExpectations];
  716. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  717. [docRef deleteDocument];
  718. [self awaitExpectations];
  719. [listenerRegistration remove];
  720. }
  721. - (void)testQuerySnapshotEvents_forAdd {
  722. FIRCollectionReference *roomsRef = [self collectionRef];
  723. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  724. NSDictionary<NSString *, id> *newData = @{@"a" : @1};
  725. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  726. __block XCTestExpectation *changeCompletion;
  727. __block int callbacks = 0;
  728. id<FIRListenerRegistration> listenerRegistration =
  729. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *) {
  730. callbacks++;
  731. if (callbacks == 1) {
  732. XCTAssertEqual(docSet.count, 0);
  733. [emptyCompletion fulfill];
  734. } else if (callbacks == 2) {
  735. XCTAssertEqual(docSet.count, 1);
  736. XCTAssertTrue([docSet.documents[0] isKindOfClass:[FIRQueryDocumentSnapshot class]]);
  737. XCTAssertEqualObjects(docSet.documents[0].data, newData);
  738. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  739. [changeCompletion fulfill];
  740. } else {
  741. XCTFail("Should not have received a third callback");
  742. }
  743. }];
  744. [self awaitExpectations];
  745. changeCompletion = [self expectationWithDescription:@"changed snapshot"];
  746. [docRef setData:newData];
  747. [self awaitExpectations];
  748. [listenerRegistration remove];
  749. }
  750. - (void)testQuerySnapshotEvents_forChange {
  751. FIRCollectionReference *roomsRef = [self collectionRef];
  752. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  753. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  754. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  755. [self writeDocumentRef:docRef data:initialData];
  756. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  757. __block XCTestExpectation *changeCompletion;
  758. __block int callbacks = 0;
  759. id<FIRListenerRegistration> listenerRegistration =
  760. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *) {
  761. callbacks++;
  762. if (callbacks == 1) {
  763. XCTAssertEqual(docSet.count, 1);
  764. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  765. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  766. [initialCompletion fulfill];
  767. } else if (callbacks == 2) {
  768. XCTAssertEqual(docSet.count, 1);
  769. XCTAssertEqualObjects(docSet.documents[0].data, changedData);
  770. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  771. [changeCompletion fulfill];
  772. } else {
  773. XCTFail("Should not have received a third callback");
  774. }
  775. }];
  776. [self awaitExpectations];
  777. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  778. [docRef setData:changedData];
  779. [self awaitExpectations];
  780. [listenerRegistration remove];
  781. }
  782. - (void)testQuerySnapshotEvents_forDelete {
  783. FIRCollectionReference *roomsRef = [self collectionRef];
  784. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  785. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  786. [self writeDocumentRef:docRef data:initialData];
  787. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  788. __block XCTestExpectation *changeCompletion;
  789. __block int callbacks = 0;
  790. id<FIRListenerRegistration> listenerRegistration =
  791. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *) {
  792. callbacks++;
  793. if (callbacks == 1) {
  794. XCTAssertEqual(docSet.count, 1);
  795. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  796. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  797. [initialCompletion fulfill];
  798. } else if (callbacks == 2) {
  799. XCTAssertEqual(docSet.count, 0);
  800. [changeCompletion fulfill];
  801. } else {
  802. XCTFail("Should not have received a third callback");
  803. }
  804. }];
  805. [self awaitExpectations];
  806. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  807. [docRef deleteDocument];
  808. [self awaitExpectations];
  809. [listenerRegistration remove];
  810. }
  811. - (void)testExposesFirestoreOnDocumentReferences {
  812. FIRDocumentReference *doc = [self.db documentWithPath:@"foo/bar"];
  813. XCTAssertEqual(doc.firestore, self.db);
  814. }
  815. - (void)testExposesFirestoreOnQueries {
  816. FIRQuery *q = [[self.db collectionWithPath:@"foo"] queryLimitedTo:5];
  817. XCTAssertEqual(q.firestore, self.db);
  818. }
  819. - (void)testDocumentReferenceEquality {
  820. FIRFirestore *firestore = self.db;
  821. FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
  822. XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
  823. XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);
  824. XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);
  825. FIRFirestore *otherFirestore = [self firestore];
  826. XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
  827. }
  828. - (void)testQueryReferenceEquality {
  829. FIRFirestore *firestore = self.db;
  830. FIRQuery *query =
  831. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  832. isEqualTo:@42];
  833. FIRQuery *query2 =
  834. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  835. isEqualTo:@42];
  836. XCTAssertEqualObjects(query, query2);
  837. FIRQuery *query3 =
  838. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
  839. isEqualTo:@42];
  840. XCTAssertNotEqualObjects(query, query3);
  841. FIRFirestore *otherFirestore = [self firestore];
  842. FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
  843. queryWhereField:@"baz"
  844. isEqualTo:@42];
  845. XCTAssertNotEqualObjects(query, query4);
  846. }
  847. - (void)testCanTraverseCollectionsAndDocuments {
  848. NSString *expected = @"a/b/c/d";
  849. // doc path from root Firestore.
  850. XCTAssertEqualObjects([self.db documentWithPath:@"a/b/c/d"].path, expected);
  851. // collection path from root Firestore.
  852. XCTAssertEqualObjects([[self.db collectionWithPath:@"a/b/c"] documentWithPath:@"d"].path,
  853. expected);
  854. // doc path from CollectionReference.
  855. XCTAssertEqualObjects([[self.db collectionWithPath:@"a"] documentWithPath:@"b/c/d"].path,
  856. expected);
  857. // collection path from DocumentReference.
  858. XCTAssertEqualObjects([[self.db documentWithPath:@"a/b"] collectionWithPath:@"c/d/e"].path,
  859. @"a/b/c/d/e");
  860. }
  861. - (void)testCanTraverseCollectionAndDocumentParents {
  862. FIRCollectionReference *collection = [self.db collectionWithPath:@"a/b/c"];
  863. XCTAssertEqualObjects(collection.path, @"a/b/c");
  864. FIRDocumentReference *doc = collection.parent;
  865. XCTAssertEqualObjects(doc.path, @"a/b");
  866. collection = doc.parent;
  867. XCTAssertEqualObjects(collection.path, @"a");
  868. FIRDocumentReference *nilDoc = collection.parent;
  869. XCTAssertNil(nilDoc);
  870. }
  871. - (void)testUpdateFieldsWithDots {
  872. FIRDocumentReference *doc = [self documentRef];
  873. [self writeDocumentRef:doc data:@{@"a.b" : @"old", @"c.d" : @"old"}];
  874. [self updateDocumentRef:doc
  875. data:@{(id)[[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"}];
  876. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  877. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  878. XCTAssertNil(error);
  879. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  880. [expectation fulfill];
  881. }];
  882. [self awaitExpectations];
  883. }
  884. - (void)testUpdateNestedFields {
  885. FIRDocumentReference *doc = [self documentRef];
  886. [self writeDocumentRef:doc
  887. data:@{
  888. @"a" : @{@"b" : @"old"},
  889. @"c" : @{@"d" : @"old"},
  890. @"e" : @{@"f" : @"old"}
  891. }];
  892. [self updateDocumentRef:doc
  893. data:@{
  894. (id) @"a.b" : @"new",
  895. (id)[[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  896. }];
  897. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  898. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  899. XCTAssertNil(error);
  900. XCTAssertEqualObjects(snapshot.data, (@{
  901. @"a" : @{@"b" : @"new"},
  902. @"c" : @{@"d" : @"new"},
  903. @"e" : @{@"f" : @"old"}
  904. }));
  905. [expectation fulfill];
  906. }];
  907. [self awaitExpectations];
  908. }
  909. - (void)testCollectionID {
  910. XCTAssertEqualObjects([self.db collectionWithPath:@"foo"].collectionID, @"foo");
  911. XCTAssertEqualObjects([self.db collectionWithPath:@"foo/bar/baz"].collectionID, @"baz");
  912. }
  913. - (void)testDocumentID {
  914. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar"].documentID, @"bar");
  915. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux");
  916. }
  917. - (void)testCanQueueWritesWhileOffline {
  918. XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"];
  919. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"];
  920. FIRDocumentReference *doc = [self documentRef];
  921. FIRFirestore *firestore = doc.firestore;
  922. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  923. [firestore disableNetworkWithCompletion:^(NSError *error) {
  924. XCTAssertNil(error);
  925. [doc setData:data
  926. completion:^(NSError *error) {
  927. XCTAssertNil(error);
  928. [writeEpectation fulfill];
  929. }];
  930. [firestore enableNetworkWithCompletion:^(NSError *error) {
  931. XCTAssertNil(error);
  932. [networkExpectation fulfill];
  933. }];
  934. }];
  935. [self awaitExpectations];
  936. XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"];
  937. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  938. XCTAssertNil(error);
  939. XCTAssertEqualObjects(snapshot.data, data);
  940. XCTAssertFalse(snapshot.metadata.isFromCache);
  941. [getExpectation fulfill];
  942. }];
  943. [self awaitExpectations];
  944. }
  945. - (void)testCanGetDocumentsWhileOffline {
  946. FIRDocumentReference *doc = [self documentRef];
  947. FIRFirestore *firestore = doc.firestore;
  948. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  949. XCTestExpectation *failExpectation =
  950. [self expectationWithDescription:@"offline read with no cached data"];
  951. XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"];
  952. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"];
  953. __weak FIRDocumentReference *weakDoc = doc;
  954. [firestore disableNetworkWithCompletion:^(NSError *error) {
  955. XCTAssertNil(error);
  956. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *, NSError *error) {
  957. XCTAssertNotNil(error);
  958. [failExpectation fulfill];
  959. }];
  960. [doc setData:data
  961. completion:^(NSError *_Nullable error) {
  962. XCTAssertNil(error);
  963. [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  964. XCTAssertNil(error);
  965. // Verify that we are not reading from cache.
  966. XCTAssertFalse(snapshot.metadata.isFromCache);
  967. [onlineExpectation fulfill];
  968. }];
  969. }];
  970. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  971. XCTAssertNil(error);
  972. // Verify that we are reading from cache.
  973. XCTAssertTrue(snapshot.metadata.fromCache);
  974. XCTAssertEqualObjects(snapshot.data, data);
  975. [firestore enableNetworkWithCompletion:^(NSError *) {
  976. [networkExpectation fulfill];
  977. }];
  978. }];
  979. }];
  980. [self awaitExpectations];
  981. }
  982. - (void)testWriteStreamReconnectsAfterIdle {
  983. FIRDocumentReference *doc = [self documentRef];
  984. FIRFirestore *firestore = doc.firestore;
  985. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  986. [firestore workerQueue]->RunScheduledOperationsUntil(TimerId::WriteStreamIdle);
  987. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  988. }
  989. - (void)testWatchStreamReconnectsAfterIdle {
  990. FIRDocumentReference *doc = [self documentRef];
  991. FIRFirestore *firestore = doc.firestore;
  992. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  993. [firestore workerQueue]->RunScheduledOperationsUntil(TimerId::ListenStreamIdle);
  994. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  995. }
  996. - (void)testCanDisableNetwork {
  997. FIRDocumentReference *doc = [self documentRef];
  998. FIRFirestore *firestore = doc.firestore;
  999. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  1000. [self awaitExpectations];
  1001. [firestore
  1002. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network again"]];
  1003. [self awaitExpectations];
  1004. [firestore
  1005. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  1006. [self awaitExpectations];
  1007. [firestore
  1008. disableNetworkWithCompletion:[self
  1009. completionForExpectationWithName:@"Disable network again"]];
  1010. [self awaitExpectations];
  1011. [firestore
  1012. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Final enable network"]];
  1013. [self awaitExpectations];
  1014. }
  1015. - (void)testClientCallsAfterTerminationFail {
  1016. FIRDocumentReference *doc = [self documentRef];
  1017. FIRFirestore *firestore = doc.firestore;
  1018. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  1019. [self awaitExpectations];
  1020. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate"]];
  1021. [self awaitExpectations];
  1022. XCTAssertThrowsSpecific([firestore disableNetworkWithCompletion:^(NSError *){
  1023. }],
  1024. NSException, @"The client has already been terminated.");
  1025. }
  1026. - (void)testTransactionGetAfterTerminationFail {
  1027. FIRDocumentReference *doc = [self documentRef];
  1028. FIRFirestore *firestore = doc.firestore;
  1029. XCTestExpectation *expectation1 = [self expectationWithDescription:@"TransactionBlockStart"];
  1030. XCTestExpectation *expectation2 = [self expectationWithDescription:@"FirestoreTerminated"];
  1031. XCTestExpectation *expectation3 = [self expectationWithDescription:@"TransactionBlockDone"];
  1032. __block NSError *capturedError = nil;
  1033. [firestore
  1034. runTransactionWithBlock:^id _Nullable(FIRTransaction *transaction, NSError **error) {
  1035. // Tell the test thread that it can now call `terminateWithCompletion`.
  1036. [expectation1 fulfill];
  1037. // Wait for `terminateWithCompletion` to complete.
  1038. [self awaitExpectation:expectation2];
  1039. // Call `[transaction getDocument]` to make sure that it correctly reports the error.
  1040. [transaction getDocument:doc error:error];
  1041. // Save the `NSError` into a variable on which the test thread will perform assertions.
  1042. capturedError = *error;
  1043. // Tell the test thread that it can now perform assertions on the captured `NSError` object.
  1044. [expectation3 fulfill];
  1045. return @"I should have failed since the transaction was terminated";
  1046. }
  1047. completion:^(id, NSError *){
  1048. }];
  1049. // Wait for the transaction callback to start.
  1050. [self awaitExpectation:expectation1];
  1051. // Terminate the Firestore instance.
  1052. [firestore terminateWithCompletion:[self completionForExpectation:expectation2]];
  1053. // Wait for the transaction callback to "publish" the `NSError` from `[transaction getDocument]`.
  1054. [self awaitExpectation:expectation3];
  1055. // Verify that `[transaction getDocument]` set its `NSError` argument to a non-nil error.
  1056. XCTAssertNotNil(capturedError);
  1057. XCTAssertEqual(capturedError.code, FIRFirestoreErrorCodeFailedPrecondition);
  1058. XCTAssertEqualObjects(capturedError.userInfo[NSLocalizedDescriptionKey],
  1059. @"The client has already been terminated.");
  1060. }
  1061. - (void)testMaintainsPersistenceAfterRestarting {
  1062. FIRDocumentReference *doc = [self documentRef];
  1063. FIRFirestore *firestore = doc.firestore;
  1064. FIRApp *app = firestore.app;
  1065. NSString *appName = app.name;
  1066. FIROptions *options = app.options;
  1067. NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
  1068. [self writeDocumentRef:doc data:initialData];
  1069. // -clearPersistence() requires Firestore to be terminated. Shutdown FIRApp and remove the
  1070. // firestore instance to emulate the way an end user would do this.
  1071. [self terminateFirestore:firestore];
  1072. [self.firestores removeObject:firestore];
  1073. [self deleteApp:app];
  1074. // We restart the app with the same name and options to check that the previous instance's
  1075. // persistent storage persists its data after restarting. Calling [self firestore] here would
  1076. // create a new instance of firestore, which defeats the purpose of this test.
  1077. [FIRApp configureWithName:appName options:options];
  1078. FIRApp *app2 = [FIRApp appNamed:appName];
  1079. FIRFirestore *firestore2 = [self firestoreWithApp:app2];
  1080. FIRDocumentReference *docRef2 = [firestore2 documentWithPath:doc.path];
  1081. FIRDocumentSnapshot *snap = [self readDocumentForRef:docRef2 source:FIRFirestoreSourceCache];
  1082. XCTAssertTrue(snap.exists);
  1083. }
  1084. - (void)testCanClearPersistenceAfterRestarting {
  1085. FIRDocumentReference *doc = [self documentRef];
  1086. FIRFirestore *firestore = doc.firestore;
  1087. FIRApp *app = firestore.app;
  1088. NSString *appName = app.name;
  1089. FIROptions *options = app.options;
  1090. NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
  1091. [self writeDocumentRef:doc data:initialData];
  1092. // -clearPersistence() requires Firestore to be terminated. Shutdown FIRApp and remove the
  1093. // firestore instance to emulate the way an end user would do this.
  1094. [self terminateFirestore:firestore];
  1095. [self.firestores removeObject:firestore];
  1096. [firestore
  1097. clearPersistenceWithCompletion:[self completionForExpectationWithName:@"ClearPersistence"]];
  1098. [self awaitExpectations];
  1099. [self deleteApp:app];
  1100. // We restart the app with the same name and options to check that the previous instance's
  1101. // persistent storage is actually cleared after the restart. Calling [self firestore] here would
  1102. // create a new instance of firestore, which defeats the purpose of this test.
  1103. [FIRApp configureWithName:appName options:options];
  1104. FIRApp *app2 = [FIRApp appNamed:appName];
  1105. FIRFirestore *firestore2 = [self firestoreWithApp:app2];
  1106. FIRDocumentReference *docRef2 = [firestore2 documentWithPath:doc.path];
  1107. XCTestExpectation *expectation2 = [self expectationWithDescription:@"getData"];
  1108. [docRef2 getDocumentWithSource:FIRFirestoreSourceCache
  1109. completion:^(FIRDocumentSnapshot *, NSError *_Nullable error) {
  1110. XCTAssertNotNil(error);
  1111. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1112. XCTAssertEqual(error.code, FIRFirestoreErrorCodeUnavailable);
  1113. [expectation2 fulfill];
  1114. }];
  1115. [self awaitExpectations];
  1116. }
  1117. - (void)testCanClearPersistenceOnANewFirestoreInstance {
  1118. FIRDocumentReference *doc = [self documentRef];
  1119. FIRFirestore *firestore = doc.firestore;
  1120. FIRApp *app = firestore.app;
  1121. NSString *appName = app.name;
  1122. FIROptions *options = app.options;
  1123. NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
  1124. [self writeDocumentRef:doc data:initialData];
  1125. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate"]];
  1126. [self.firestores removeObject:firestore];
  1127. [self awaitExpectations];
  1128. [self deleteApp:app];
  1129. // We restart the app with the same name and options to check that the previous instance's
  1130. // persistent storage is actually cleared after the restart. Calling [self firestore] here would
  1131. // create a new instance of firestore, which defeats the purpose of this test.
  1132. [FIRApp configureWithName:appName options:options];
  1133. FIRApp *app2 = [FIRApp appNamed:appName];
  1134. FIRFirestore *firestore2 = [self firestoreWithApp:app2];
  1135. [firestore2
  1136. clearPersistenceWithCompletion:[self completionForExpectationWithName:@"ClearPersistence"]];
  1137. [self awaitExpectations];
  1138. FIRDocumentReference *docRef2 = [firestore2 documentWithPath:doc.path];
  1139. XCTestExpectation *expectation2 = [self expectationWithDescription:@"getData"];
  1140. [docRef2 getDocumentWithSource:FIRFirestoreSourceCache
  1141. completion:^(FIRDocumentSnapshot *, NSError *_Nullable error) {
  1142. XCTAssertNotNil(error);
  1143. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1144. XCTAssertEqual(error.code, FIRFirestoreErrorCodeUnavailable);
  1145. [expectation2 fulfill];
  1146. }];
  1147. [self awaitExpectations];
  1148. }
  1149. - (void)testClearPersistenceWhileRunningFails {
  1150. FIRDocumentReference *doc = [self documentRef];
  1151. FIRFirestore *firestore = doc.firestore;
  1152. [self enableNetwork];
  1153. XCTestExpectation *expectation = [self expectationWithDescription:@"clearPersistence"];
  1154. [firestore clearPersistenceWithCompletion:^(NSError *_Nullable error) {
  1155. XCTAssertNotNil(error);
  1156. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1157. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  1158. [expectation fulfill];
  1159. }];
  1160. [self awaitExpectations];
  1161. }
  1162. - (void)testRestartFirestoreLeadsToNewInstance {
  1163. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1164. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1165. FIRFirestore *sameInstance = [FIRFirestore firestoreForApp:app];
  1166. firestore.settings = [FSTIntegrationTestCase settings];
  1167. XCTAssertEqual(firestore, sameInstance);
  1168. NSDictionary<NSString *, id> *data =
  1169. @{@"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  1170. [self writeDocumentRef:[firestore documentWithPath:@"abc/123"] data:data];
  1171. [self terminateFirestore:firestore];
  1172. // Create a new instance, check it's a different instance.
  1173. FIRFirestore *newInstance = [FIRFirestore firestoreForApp:app];
  1174. newInstance.settings = [FSTIntegrationTestCase settings];
  1175. XCTAssertNotEqual(firestore, newInstance);
  1176. // New instance still functions.
  1177. FIRDocumentSnapshot *snapshot =
  1178. [self readDocumentForRef:[newInstance documentWithPath:@"abc/123"]];
  1179. XCTAssertTrue([data isEqualToDictionary:[snapshot data]]);
  1180. }
  1181. - (void)testAppDeleteLeadsToFirestoreTermination {
  1182. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1183. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1184. firestore.settings = [FSTIntegrationTestCase settings];
  1185. NSDictionary<NSString *, id> *data =
  1186. @{@"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  1187. [self writeDocumentRef:[firestore documentWithPath:@"abc/123"] data:data];
  1188. [self deleteApp:app];
  1189. XCTAssertTrue(firestore.wrapped->client()->is_terminated());
  1190. }
  1191. // Ensures b/172958106 doesn't regress.
  1192. - (void)testDeleteAppWorksWhenLastReferenceToFirestoreIsInListener {
  1193. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1194. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1195. FIRDocumentReference *doc = [firestore documentWithPath:@"abc/123"];
  1196. // Make sure there is a listener.
  1197. [doc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *){
  1198. }];
  1199. XCTestExpectation *expectation = [self expectationWithDescription:@"App is deleted"];
  1200. [app deleteApp:^(BOOL) {
  1201. [expectation fulfill];
  1202. }];
  1203. // Let go of the last app reference.
  1204. app = nil;
  1205. [self awaitExpectations];
  1206. }
  1207. - (void)testTerminateCanBeCalledMultipleTimes {
  1208. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1209. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1210. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate1"]];
  1211. [self awaitExpectations];
  1212. XCTAssertThrowsSpecific([firestore disableNetworkWithCompletion:^(NSError *){
  1213. }],
  1214. NSException, @"The client has already been terminated.");
  1215. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate2"]];
  1216. [self awaitExpectations];
  1217. XCTAssertThrowsSpecific([firestore enableNetworkWithCompletion:^(NSError *){
  1218. }],
  1219. NSException, @"The client has already been terminated.");
  1220. }
  1221. - (void)testCanRemoveListenerAfterTermination {
  1222. FIRApp *app = AppForUnitTesting(MakeString([FSTIntegrationTestCase projectID]));
  1223. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1224. firestore.settings = [FSTIntegrationTestCase settings];
  1225. FIRDocumentReference *doc = [[firestore collectionWithPath:@"rooms"] documentWithAutoID];
  1226. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  1227. [self writeDocumentRef:doc data:@{}];
  1228. id<FIRListenerRegistration> listenerRegistration =
  1229. [doc addSnapshotListener:[accumulator valueEventHandler]];
  1230. [accumulator awaitEventWithName:@"Snapshot"];
  1231. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"terminate"]];
  1232. [self awaitExpectations];
  1233. // This should proceed without error.
  1234. [listenerRegistration remove];
  1235. // Multiple calls should proceed as well.
  1236. [listenerRegistration remove];
  1237. }
  1238. - (void)testListenerCallbackBlocksRemove {
  1239. // This tests a guarantee required for C++ that doesn't strictly matter for Objective-C and has no
  1240. // equivalent on other platforms.
  1241. //
  1242. // The problem for C++ is that users can register a listener that refers to some state, then call
  1243. // `ListenerRegistration::Remove()` and expect to be able to immediately delete that state. The
  1244. // trouble is that there may be a callback in progress against that listener so the implementation
  1245. // now blocks the remove call until the callback is complete.
  1246. //
  1247. // To make this work, user callbacks can't be on the main thread because the main thread is
  1248. // blocked waiting for the test to complete (that is, you can't await expectations on the main
  1249. // thread and then have the user callback additionally await expectations).
  1250. dispatch_queue_t userQueue = dispatch_queue_create("firestore.test.user", DISPATCH_QUEUE_SERIAL);
  1251. FIRFirestoreSettings *settings = self.db.settings;
  1252. settings.dispatchQueue = userQueue;
  1253. self.db.settings = settings;
  1254. XCTestExpectation *running = [self expectationWithDescription:@"listener running"];
  1255. XCTestExpectation *allowCompletion =
  1256. [self expectationWithDescription:@"allow listener to complete"];
  1257. XCTestExpectation *removing = [self expectationWithDescription:@"attempting to remove listener"];
  1258. XCTestExpectation *removed = [self expectationWithDescription:@"listener removed"];
  1259. NSMutableString *steps = [NSMutableString string];
  1260. FIRDocumentReference *doc = [self documentRef];
  1261. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  1262. __block bool firstTime = true;
  1263. id<FIRListenerRegistration> listener =
  1264. [doc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *) {
  1265. @synchronized(self) {
  1266. if (!firstTime) {
  1267. return;
  1268. }
  1269. firstTime = false;
  1270. }
  1271. [steps appendString:@"1"];
  1272. [running fulfill];
  1273. [self awaitExpectation:allowCompletion];
  1274. [steps appendString:@"3"];
  1275. }];
  1276. // Call remove asynchronously to avoid blocking the main test thread.
  1277. dispatch_queue_t async = dispatch_queue_create("firestore.async", DISPATCH_QUEUE_SERIAL);
  1278. dispatch_async(async, ^{
  1279. [self awaitExpectation:running];
  1280. [steps appendString:@"2"];
  1281. [removing fulfill];
  1282. [listener remove];
  1283. [steps appendString:@"4"];
  1284. [removed fulfill];
  1285. });
  1286. // Perform a write to `doc` which will trigger the listener callback. Don't wait for completion
  1287. // though because that completion handler is in line behind the listener callback that the test
  1288. // is blocking.
  1289. XCTestExpectation *setData = [self expectationWithDescription:@"setData"];
  1290. [doc setData:@{@"foo" : @"bar"} completion:[self completionForExpectation:setData]];
  1291. [self awaitExpectation:removing];
  1292. [allowCompletion fulfill];
  1293. [self awaitExpectation:removed];
  1294. XCTAssertEqualObjects(steps, @"1234");
  1295. [self awaitExpectation:setData];
  1296. }
  1297. - (void)testListenerCallbackCanCallRemoveWithoutBlocking {
  1298. // This tests a guarantee required for C++ that doesn't strictly matter for Objective-C and has no
  1299. // equivalent on other platforms. See `testListenerCallbackBlocksRemove` for background.
  1300. XCTestExpectation *removed = [self expectationWithDescription:@"listener removed"];
  1301. NSMutableString *steps = [NSMutableString string];
  1302. FIRDocumentReference *doc = [self documentRef];
  1303. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  1304. __block id<FIRListenerRegistration> listener = nil;
  1305. @synchronized(self) {
  1306. listener = [doc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *) {
  1307. [steps appendString:@"1"];
  1308. @synchronized(self) {
  1309. // This test is successful if this method does not block.
  1310. [listener remove];
  1311. }
  1312. [steps appendString:@"2"];
  1313. [removed fulfill];
  1314. }];
  1315. }
  1316. // Perform a write to `doc` which will trigger the listener callback.
  1317. [self writeDocumentRef:doc data:@{@"foo" : @"bar2"}];
  1318. [self awaitExpectation:removed];
  1319. XCTAssertEqualObjects(steps, @"12");
  1320. }
  1321. - (void)testListenerCallbacksHappenOnMainThread {
  1322. // Verify that callbacks occur on the main thread if settings.dispatchQueue is not specified.
  1323. XCTestExpectation *invoked = [self expectationWithDescription:@"listener invoked"];
  1324. invoked.assertForOverFulfill = false;
  1325. FIRDocumentReference *doc = [self documentRef];
  1326. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  1327. __block bool callbackThreadIsMainThread;
  1328. __block NSString *callbackThreadDescription;
  1329. [doc addSnapshotListener:^(FIRDocumentSnapshot *, NSError *) {
  1330. callbackThreadIsMainThread = NSThread.isMainThread;
  1331. callbackThreadDescription = [NSString stringWithFormat:@"%@", NSThread.currentThread];
  1332. [invoked fulfill];
  1333. }];
  1334. [self awaitExpectation:invoked];
  1335. XCTAssertTrue(callbackThreadIsMainThread,
  1336. @"The listener callback was expected to occur on the main thread, but instead it "
  1337. @"occurred on the thread %@",
  1338. callbackThreadDescription);
  1339. }
  1340. - (void)testWaitForPendingWritesCompletes {
  1341. FIRDocumentReference *doc = [self documentRef];
  1342. FIRFirestore *firestore = doc.firestore;
  1343. [self disableNetwork];
  1344. [doc setData:@{@"foo" : @"bar"}];
  1345. [firestore waitForPendingWritesWithCompletion:
  1346. [self completionForExpectationWithName:@"Wait for pending writes"]];
  1347. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  1348. [self awaitExpectations];
  1349. }
  1350. - (void)testWaitForPendingWritesFailsWhenUserChanges {
  1351. FIRFirestore *firestore = self.db;
  1352. [self disableNetwork];
  1353. // Writes to local to prevent immediate call to the completion of waitForPendingWrites.
  1354. NSDictionary<NSString *, id> *data =
  1355. @{@"owner" : @{@"name" : @"Andy", @"email" : @"abc@example.com"}};
  1356. [[self documentRef] setData:data];
  1357. XCTestExpectation *expectation = [self expectationWithDescription:@"waitForPendingWrites"];
  1358. [firestore waitForPendingWritesWithCompletion:^(NSError *_Nullable error) {
  1359. XCTAssertNotNil(error);
  1360. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1361. XCTAssertEqual(error.code, FIRFirestoreErrorCodeCancelled);
  1362. [expectation fulfill];
  1363. }];
  1364. [self triggerUserChangeWithUid:@"user-to-fail-pending-writes"];
  1365. [self awaitExpectations];
  1366. }
  1367. - (void)testWaitForPendingWritesCompletesWhenOfflineIfNoPending {
  1368. FIRFirestore *firestore = self.db;
  1369. [self disableNetwork];
  1370. [firestore waitForPendingWritesWithCompletion:
  1371. [self completionForExpectationWithName:@"Wait for pending writes"]];
  1372. [self awaitExpectations];
  1373. }
  1374. - (void)testDefaultNamedDbIsSame {
  1375. [FIRApp configure];
  1376. FIRApp *app = [FIRApp defaultApp];
  1377. FIRFirestore *db1 = [FIRFirestore firestore];
  1378. FIRFirestore *db2 = [FIRFirestore firestoreForApp:app];
  1379. FIRFirestore *db3 = [FIRFirestore firestoreForApp:app database:@"(default)"];
  1380. FIRFirestore *db4 = [FIRFirestore firestoreForDatabase:@"(default)"];
  1381. XCTAssertIdentical(db1, db2);
  1382. XCTAssertIdentical(db1, db3);
  1383. XCTAssertIdentical(db1, db4);
  1384. }
  1385. - (void)testSameNamedDbIsSame {
  1386. [FIRApp configure];
  1387. FIRApp *app = [FIRApp defaultApp];
  1388. FIRFirestore *db1 = [FIRFirestore firestoreForApp:app database:@"myDb"];
  1389. FIRFirestore *db2 = [FIRFirestore firestoreForDatabase:@"myDb"];
  1390. XCTAssertIdentical(db1, db2);
  1391. }
  1392. - (void)testNamedDbHaveDifferentInstance {
  1393. [FIRApp configure];
  1394. FIRFirestore *db1 = [FIRFirestore firestore];
  1395. FIRFirestore *db2 = [FIRFirestore firestoreForDatabase:@"db1"];
  1396. FIRFirestore *db3 = [FIRFirestore firestoreForDatabase:@"db2"];
  1397. XCTAssertNotIdentical(db1, db2);
  1398. XCTAssertNotIdentical(db1, db3);
  1399. XCTAssertNotIdentical(db2, db3);
  1400. }
  1401. - (void)testCannotMixCacheConfigAPIs {
  1402. [FIRApp configure];
  1403. FIRFirestore *db1 = [FIRFirestore firestore];
  1404. FIRFirestoreSettings *settings = db1.settings;
  1405. settings.cacheSizeBytes = 10000000;
  1406. settings.cacheSettings = [[FIRPersistentCacheSettings alloc] init];
  1407. XCTAssertThrowsSpecific(db1.settings = settings, NSException);
  1408. FIRFirestore *db2 = [FIRFirestore firestoreForDatabase:@"db2"];
  1409. settings = db2.settings;
  1410. settings.cacheSettings = [[FIRMemoryCacheSettings alloc] init];
  1411. settings.persistenceEnabled = NO;
  1412. XCTAssertThrowsSpecific(db2.settings = settings, NSException);
  1413. }
  1414. - (void)testMinimumCacheSize {
  1415. XCTAssertThrowsSpecific([[FIRPersistentCacheSettings alloc] initWithSizeBytes:@(1024 * 1024 - 1)],
  1416. NSException);
  1417. }
  1418. - (void)testUnlimitedCacheSize {
  1419. FIRPersistentCacheSettings *cacheSettings =
  1420. [[FIRPersistentCacheSettings alloc] initWithSizeBytes:@(Settings::CacheSizeUnlimited)];
  1421. XCTAssertEqual(cacheSettings.internalSettings.size_bytes(), Settings::CacheSizeUnlimited);
  1422. self.db.settings.cacheSettings = cacheSettings;
  1423. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  1424. NSDictionary<NSString *, id> *data = @{@"value" : @"foo"};
  1425. [self writeDocumentRef:doc data:data];
  1426. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  1427. XCTAssertEqualObjects(result.data, data);
  1428. }
  1429. - (void)testGetValidPersistentCacheIndexManager {
  1430. [FIRApp configure];
  1431. FIRFirestore *db1 = [FIRFirestore firestoreForDatabase:@"PersistentCacheIndexManagerDB1"];
  1432. FIRFirestoreSettings *settings1 = [db1 settings];
  1433. [settings1 setCacheSettings:[[FIRPersistentCacheSettings alloc] init]];
  1434. [db1 setSettings:settings1];
  1435. XCTAssertNotNil(db1.persistentCacheIndexManager);
  1436. // Use persistent disk cache (default)
  1437. FIRFirestore *db2 = [FIRFirestore firestoreForDatabase:@"PersistentCacheIndexManagerDB2"];
  1438. XCTAssertNotNil(db2.persistentCacheIndexManager);
  1439. // Disable persistent disk cache
  1440. FIRFirestore *db3 = [FIRFirestore firestoreForDatabase:@"MemoryCacheIndexManagerDB1"];
  1441. FIRFirestoreSettings *settings3 = [db3 settings];
  1442. [settings3 setCacheSettings:[[FIRMemoryCacheSettings alloc] init]];
  1443. [db3 setSettings:settings3];
  1444. XCTAssertNil(db3.persistentCacheIndexManager);
  1445. // Disable persistent disk cache (deprecated)
  1446. FIRFirestore *db4 = [FIRFirestore firestoreForDatabase:@"PersistentCacheIndexManagerDB4"];
  1447. FIRFirestoreSettings *settings4 = [db4 settings];
  1448. settings4.persistenceEnabled = NO;
  1449. [db4 setSettings:settings4];
  1450. XCTAssertNil(db4.persistentCacheIndexManager);
  1451. }
  1452. - (void)testCanGetSameOrDifferentPersistentCacheIndexManager {
  1453. [FIRApp configure];
  1454. // Use persistent disk cache (explicit)
  1455. FIRFirestore *db1 = [FIRFirestore firestoreForDatabase:@"PersistentCacheIndexManagerDB5"];
  1456. FIRFirestoreSettings *settings1 = [db1 settings];
  1457. [settings1 setCacheSettings:[[FIRPersistentCacheSettings alloc] init]];
  1458. [db1 setSettings:settings1];
  1459. XCTAssertEqual(db1.persistentCacheIndexManager, db1.persistentCacheIndexManager);
  1460. // Use persistent disk cache (default)
  1461. FIRFirestore *db2 = [FIRFirestore firestoreForDatabase:@"PersistentCacheIndexManagerDB6"];
  1462. XCTAssertEqual(db2.persistentCacheIndexManager, db2.persistentCacheIndexManager);
  1463. XCTAssertNotEqual(db1.persistentCacheIndexManager, db2.persistentCacheIndexManager);
  1464. FIRFirestore *db3 = [FIRFirestore firestoreForDatabase:@"PersistentCacheIndexManagerDB7"];
  1465. FIRFirestoreSettings *settings3 = [db3 settings];
  1466. [settings3 setCacheSettings:[[FIRPersistentCacheSettings alloc] init]];
  1467. [db3 setSettings:settings3];
  1468. XCTAssertNotEqual(db1.persistentCacheIndexManager, db3.persistentCacheIndexManager);
  1469. XCTAssertNotEqual(db2.persistentCacheIndexManager, db3.persistentCacheIndexManager);
  1470. // Use persistent disk cache (default)
  1471. FIRFirestore *db4 = [FIRFirestore firestoreForDatabase:@"PersistentCacheIndexManagerDB8"];
  1472. XCTAssertNotEqual(db1.persistentCacheIndexManager, db4.persistentCacheIndexManager);
  1473. XCTAssertNotEqual(db2.persistentCacheIndexManager, db4.persistentCacheIndexManager);
  1474. XCTAssertNotEqual(db3.persistentCacheIndexManager, db4.persistentCacheIndexManager);
  1475. }
  1476. @end