FIRDatabaseTests.mm 55 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487
  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. #include "Firestore/core/src/firebase/firestore/core/firestore_client.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)testSnapshotsInSyncListenerFiresAfterListenersInSync {
  413. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  414. FIRDocumentReference *ref = [coll addDocumentWithData:@{@"foo" : @1}];
  415. NSMutableArray<NSString *> *events = [NSMutableArray array];
  416. XCTestExpectation *gotInitialSnapshot = [self expectationWithDescription:@"gotInitialSnapshot"];
  417. __block bool setupComplete = false;
  418. [ref addSnapshotListener:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  419. XCTAssertNil(error);
  420. [events addObject:@"doc"];
  421. // Wait for the initial event from the backend so that we know we'll get exactly one snapshot
  422. // event for our local write below.
  423. if (!setupComplete) {
  424. setupComplete = true;
  425. [gotInitialSnapshot fulfill];
  426. }
  427. }];
  428. [self awaitExpectations];
  429. [events removeAllObjects];
  430. XCTestExpectation *done = [self expectationWithDescription:@"SnapshotsInSyncListenerDone"];
  431. [ref.firestore addSnapshotsInSyncListener:^() {
  432. [events addObject:@"snapshots-in-sync"];
  433. if ([events count] == 3) {
  434. // We should have an initial snapshots-in-sync event, then a snapshot event
  435. // for set(), then another event to indicate we're in sync again.
  436. NSArray<NSString *> *expected = @[ @"snapshots-in-sync", @"doc", @"snapshots-in-sync" ];
  437. XCTAssertEqualObjects(events, expected);
  438. [done fulfill];
  439. }
  440. }];
  441. [self writeDocumentRef:ref data:@{@"foo" : @3}];
  442. }
  443. - (void)testListenCanBeCalledMultipleTimes {
  444. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  445. FIRDocumentReference *doc = [coll documentWithAutoID];
  446. XCTestExpectation *completed = [self expectationWithDescription:@"multiple addSnapshotListeners"];
  447. __block NSDictionary<NSString *, id> *resultingData;
  448. // Shut the compiler up about strong references to doc.
  449. FIRDocumentReference *__weak weakDoc = doc;
  450. [doc setData:@{@"foo" : @"bar"}
  451. completion:^(NSError *error1) {
  452. XCTAssertNil(error1);
  453. FIRDocumentReference *strongDoc = weakDoc;
  454. [strongDoc addSnapshotListener:^(FIRDocumentSnapshot *snapshot2, NSError *error2) {
  455. XCTAssertNil(error2);
  456. FIRDocumentReference *strongDoc2 = weakDoc;
  457. [strongDoc2 addSnapshotListener:^(FIRDocumentSnapshot *snapshot3, NSError *error3) {
  458. XCTAssertNil(error3);
  459. resultingData = snapshot3.data;
  460. [completed fulfill];
  461. }];
  462. }];
  463. }];
  464. [self awaitExpectations];
  465. XCTAssertEqualObjects(resultingData, @{@"foo" : @"bar"});
  466. }
  467. - (void)testDocumentSnapshotEvents_nonExistent {
  468. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  469. XCTestExpectation *snapshotCompletion = [self expectationWithDescription:@"snapshot"];
  470. __block int callbacks = 0;
  471. id<FIRListenerRegistration> listenerRegistration =
  472. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  473. callbacks++;
  474. if (callbacks == 1) {
  475. XCTAssertNotNil(doc);
  476. XCTAssertFalse(doc.exists);
  477. [snapshotCompletion fulfill];
  478. } else {
  479. XCTFail("Should not have received this callback");
  480. }
  481. }];
  482. [self awaitExpectations];
  483. [listenerRegistration remove];
  484. }
  485. - (void)testDocumentSnapshotEvents_forAdd {
  486. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  487. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  488. __block XCTestExpectation *dataCompletion;
  489. __block int callbacks = 0;
  490. id<FIRListenerRegistration> listenerRegistration =
  491. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  492. callbacks++;
  493. if (callbacks == 1) {
  494. XCTAssertNotNil(doc);
  495. XCTAssertFalse(doc.exists);
  496. [emptyCompletion fulfill];
  497. } else if (callbacks == 2) {
  498. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  499. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  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_forAddIncludingMetadata {
  512. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  513. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  514. __block XCTestExpectation *dataCompletion;
  515. __block int callbacks = 0;
  516. id<FIRListenerRegistration> listenerRegistration = [docRef
  517. addSnapshotListenerWithIncludeMetadataChanges:YES
  518. listener:^(FIRDocumentSnapshot *_Nullable doc,
  519. NSError *error) {
  520. callbacks++;
  521. if (callbacks == 1) {
  522. XCTAssertNotNil(doc);
  523. XCTAssertFalse(doc.exists);
  524. [emptyCompletion fulfill];
  525. } else if (callbacks == 2) {
  526. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  527. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  528. } else if (callbacks == 3) {
  529. XCTAssertEqualObjects(doc.data, (@{@"a" : @1}));
  530. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  531. [dataCompletion fulfill];
  532. } else {
  533. XCTFail("Should not have received this callback");
  534. }
  535. }];
  536. [self awaitExpectations];
  537. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  538. [docRef setData:@{@"a" : @1}];
  539. [self awaitExpectations];
  540. [listenerRegistration remove];
  541. }
  542. - (void)testDocumentSnapshotEvents_forChange {
  543. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  544. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  545. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  546. [self writeDocumentRef:docRef data:initialData];
  547. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  548. __block XCTestExpectation *changeCompletion;
  549. __block int callbacks = 0;
  550. id<FIRListenerRegistration> listenerRegistration =
  551. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  552. callbacks++;
  553. if (callbacks == 1) {
  554. XCTAssertEqualObjects(doc.data, initialData);
  555. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  556. [initialCompletion fulfill];
  557. } else if (callbacks == 2) {
  558. XCTAssertEqualObjects(doc.data, changedData);
  559. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  560. [changeCompletion fulfill];
  561. } else {
  562. XCTFail("Should not have received this callback");
  563. }
  564. }];
  565. [self awaitExpectations];
  566. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  567. [docRef setData:changedData];
  568. [self awaitExpectations];
  569. [listenerRegistration remove];
  570. }
  571. - (void)testDocumentSnapshotEvents_forChangeIncludingMetadata {
  572. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  573. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  574. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  575. [self writeDocumentRef:docRef data:initialData];
  576. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  577. __block XCTestExpectation *changeCompletion;
  578. __block int callbacks = 0;
  579. id<FIRListenerRegistration> listenerRegistration = [docRef
  580. addSnapshotListenerWithIncludeMetadataChanges:YES
  581. listener:^(FIRDocumentSnapshot *_Nullable doc,
  582. NSError *error) {
  583. callbacks++;
  584. if (callbacks == 1) {
  585. XCTAssertEqualObjects(doc.data, initialData);
  586. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  587. XCTAssertEqual(doc.metadata.isFromCache, YES);
  588. } else if (callbacks == 2) {
  589. XCTAssertEqualObjects(doc.data, initialData);
  590. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  591. XCTAssertEqual(doc.metadata.isFromCache, NO);
  592. [initialCompletion fulfill];
  593. } else if (callbacks == 3) {
  594. XCTAssertEqualObjects(doc.data, changedData);
  595. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  596. XCTAssertEqual(doc.metadata.isFromCache, NO);
  597. } else if (callbacks == 4) {
  598. XCTAssertEqualObjects(doc.data, changedData);
  599. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  600. XCTAssertEqual(doc.metadata.isFromCache, NO);
  601. [changeCompletion fulfill];
  602. } else {
  603. XCTFail("Should not have received this callback");
  604. }
  605. }];
  606. [self awaitExpectations];
  607. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  608. [docRef setData:changedData];
  609. [self awaitExpectations];
  610. [listenerRegistration remove];
  611. }
  612. - (void)testDocumentSnapshotEvents_forDelete {
  613. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  614. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  615. [self writeDocumentRef:docRef data:initialData];
  616. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  617. __block XCTestExpectation *changeCompletion;
  618. __block int callbacks = 0;
  619. id<FIRListenerRegistration> listenerRegistration =
  620. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  621. callbacks++;
  622. if (callbacks == 1) {
  623. XCTAssertEqualObjects(doc.data, initialData);
  624. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  625. XCTAssertEqual(doc.metadata.isFromCache, YES);
  626. [initialCompletion fulfill];
  627. } else if (callbacks == 2) {
  628. XCTAssertFalse(doc.exists);
  629. [changeCompletion fulfill];
  630. } else {
  631. XCTFail("Should not have received this callback");
  632. }
  633. }];
  634. [self awaitExpectations];
  635. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  636. [docRef deleteDocument];
  637. [self awaitExpectations];
  638. [listenerRegistration remove];
  639. }
  640. - (void)testDocumentSnapshotEvents_forDeleteIncludingMetadata {
  641. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  642. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  643. [self writeDocumentRef:docRef data:initialData];
  644. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  645. __block XCTestExpectation *changeCompletion;
  646. __block int callbacks = 0;
  647. id<FIRListenerRegistration> listenerRegistration = [docRef
  648. addSnapshotListenerWithIncludeMetadataChanges:YES
  649. listener:^(FIRDocumentSnapshot *_Nullable doc,
  650. NSError *error) {
  651. callbacks++;
  652. if (callbacks == 1) {
  653. XCTAssertEqualObjects(doc.data, initialData);
  654. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  655. XCTAssertEqual(doc.metadata.isFromCache, YES);
  656. } else if (callbacks == 2) {
  657. XCTAssertEqualObjects(doc.data, initialData);
  658. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  659. XCTAssertEqual(doc.metadata.isFromCache, NO);
  660. [initialCompletion fulfill];
  661. } else if (callbacks == 3) {
  662. XCTAssertFalse(doc.exists);
  663. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  664. XCTAssertEqual(doc.metadata.isFromCache, NO);
  665. [changeCompletion fulfill];
  666. } else {
  667. XCTFail("Should not have received this callback");
  668. }
  669. }];
  670. [self awaitExpectations];
  671. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  672. [docRef deleteDocument];
  673. [self awaitExpectations];
  674. [listenerRegistration remove];
  675. }
  676. - (void)testQuerySnapshotEvents_forAdd {
  677. FIRCollectionReference *roomsRef = [self collectionRef];
  678. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  679. NSDictionary<NSString *, id> *newData = @{@"a" : @1};
  680. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  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, 0);
  688. [emptyCompletion fulfill];
  689. } else if (callbacks == 2) {
  690. XCTAssertEqual(docSet.count, 1);
  691. XCTAssertTrue([docSet.documents[0] isKindOfClass:[FIRQueryDocumentSnapshot class]]);
  692. XCTAssertEqualObjects(docSet.documents[0].data, newData);
  693. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  694. [changeCompletion fulfill];
  695. } else {
  696. XCTFail("Should not have received a third callback");
  697. }
  698. }];
  699. [self awaitExpectations];
  700. changeCompletion = [self expectationWithDescription:@"changed snapshot"];
  701. [docRef setData:newData];
  702. [self awaitExpectations];
  703. [listenerRegistration remove];
  704. }
  705. - (void)testQuerySnapshotEvents_forChange {
  706. FIRCollectionReference *roomsRef = [self collectionRef];
  707. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  708. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  709. NSDictionary<NSString *, id> *changedData = @{@"b" : @2};
  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, 1);
  724. XCTAssertEqualObjects(docSet.documents[0].data, changedData);
  725. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  726. [changeCompletion fulfill];
  727. } else {
  728. XCTFail("Should not have received a third callback");
  729. }
  730. }];
  731. [self awaitExpectations];
  732. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  733. [docRef setData:changedData];
  734. [self awaitExpectations];
  735. [listenerRegistration remove];
  736. }
  737. - (void)testQuerySnapshotEvents_forDelete {
  738. FIRCollectionReference *roomsRef = [self collectionRef];
  739. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  740. NSDictionary<NSString *, id> *initialData = @{@"a" : @1};
  741. [self writeDocumentRef:docRef data:initialData];
  742. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  743. __block XCTestExpectation *changeCompletion;
  744. __block int callbacks = 0;
  745. id<FIRListenerRegistration> listenerRegistration =
  746. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  747. callbacks++;
  748. if (callbacks == 1) {
  749. XCTAssertEqual(docSet.count, 1);
  750. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  751. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  752. [initialCompletion fulfill];
  753. } else if (callbacks == 2) {
  754. XCTAssertEqual(docSet.count, 0);
  755. [changeCompletion fulfill];
  756. } else {
  757. XCTFail("Should not have received a third callback");
  758. }
  759. }];
  760. [self awaitExpectations];
  761. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  762. [docRef deleteDocument];
  763. [self awaitExpectations];
  764. [listenerRegistration remove];
  765. }
  766. - (void)testExposesFirestoreOnDocumentReferences {
  767. FIRDocumentReference *doc = [self.db documentWithPath:@"foo/bar"];
  768. XCTAssertEqual(doc.firestore, self.db);
  769. }
  770. - (void)testExposesFirestoreOnQueries {
  771. FIRQuery *q = [[self.db collectionWithPath:@"foo"] queryLimitedTo:5];
  772. XCTAssertEqual(q.firestore, self.db);
  773. }
  774. - (void)testDocumentReferenceEquality {
  775. FIRFirestore *firestore = self.db;
  776. FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
  777. XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
  778. XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);
  779. XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);
  780. FIRFirestore *otherFirestore = [self firestore];
  781. XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
  782. }
  783. - (void)testQueryReferenceEquality {
  784. FIRFirestore *firestore = self.db;
  785. FIRQuery *query =
  786. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  787. isEqualTo:@42];
  788. FIRQuery *query2 =
  789. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  790. isEqualTo:@42];
  791. XCTAssertEqualObjects(query, query2);
  792. FIRQuery *query3 =
  793. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
  794. isEqualTo:@42];
  795. XCTAssertNotEqualObjects(query, query3);
  796. FIRFirestore *otherFirestore = [self firestore];
  797. FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
  798. queryWhereField:@"baz"
  799. isEqualTo:@42];
  800. XCTAssertNotEqualObjects(query, query4);
  801. }
  802. - (void)testCanTraverseCollectionsAndDocuments {
  803. NSString *expected = @"a/b/c/d";
  804. // doc path from root Firestore.
  805. XCTAssertEqualObjects([self.db documentWithPath:@"a/b/c/d"].path, expected);
  806. // collection path from root Firestore.
  807. XCTAssertEqualObjects([[self.db collectionWithPath:@"a/b/c"] documentWithPath:@"d"].path,
  808. expected);
  809. // doc path from CollectionReference.
  810. XCTAssertEqualObjects([[self.db collectionWithPath:@"a"] documentWithPath:@"b/c/d"].path,
  811. expected);
  812. // collection path from DocumentReference.
  813. XCTAssertEqualObjects([[self.db documentWithPath:@"a/b"] collectionWithPath:@"c/d/e"].path,
  814. @"a/b/c/d/e");
  815. }
  816. - (void)testCanTraverseCollectionAndDocumentParents {
  817. FIRCollectionReference *collection = [self.db collectionWithPath:@"a/b/c"];
  818. XCTAssertEqualObjects(collection.path, @"a/b/c");
  819. FIRDocumentReference *doc = collection.parent;
  820. XCTAssertEqualObjects(doc.path, @"a/b");
  821. collection = doc.parent;
  822. XCTAssertEqualObjects(collection.path, @"a");
  823. FIRDocumentReference *nilDoc = collection.parent;
  824. XCTAssertNil(nilDoc);
  825. }
  826. - (void)testUpdateFieldsWithDots {
  827. FIRDocumentReference *doc = [self documentRef];
  828. [self writeDocumentRef:doc data:@{@"a.b" : @"old", @"c.d" : @"old"}];
  829. [self updateDocumentRef:doc
  830. data:@{(id)[[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new"}];
  831. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  832. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  833. XCTAssertNil(error);
  834. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  835. [expectation fulfill];
  836. }];
  837. [self awaitExpectations];
  838. }
  839. - (void)testUpdateNestedFields {
  840. FIRDocumentReference *doc = [self documentRef];
  841. [self writeDocumentRef:doc
  842. data:@{
  843. @"a" : @{@"b" : @"old"},
  844. @"c" : @{@"d" : @"old"},
  845. @"e" : @{@"f" : @"old"}
  846. }];
  847. [self updateDocumentRef:doc
  848. data:@{
  849. (id) @"a.b" : @"new",
  850. (id)[[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  851. }];
  852. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  853. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  854. XCTAssertNil(error);
  855. XCTAssertEqualObjects(snapshot.data, (@{
  856. @"a" : @{@"b" : @"new"},
  857. @"c" : @{@"d" : @"new"},
  858. @"e" : @{@"f" : @"old"}
  859. }));
  860. [expectation fulfill];
  861. }];
  862. [self awaitExpectations];
  863. }
  864. - (void)testCollectionID {
  865. XCTAssertEqualObjects([self.db collectionWithPath:@"foo"].collectionID, @"foo");
  866. XCTAssertEqualObjects([self.db collectionWithPath:@"foo/bar/baz"].collectionID, @"baz");
  867. }
  868. - (void)testDocumentID {
  869. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar"].documentID, @"bar");
  870. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux");
  871. }
  872. - (void)testCanQueueWritesWhileOffline {
  873. XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"];
  874. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"];
  875. FIRDocumentReference *doc = [self documentRef];
  876. FIRFirestore *firestore = doc.firestore;
  877. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  878. [firestore disableNetworkWithCompletion:^(NSError *error) {
  879. XCTAssertNil(error);
  880. [doc setData:data
  881. completion:^(NSError *error) {
  882. XCTAssertNil(error);
  883. [writeEpectation fulfill];
  884. }];
  885. [firestore enableNetworkWithCompletion:^(NSError *error) {
  886. XCTAssertNil(error);
  887. [networkExpectation fulfill];
  888. }];
  889. }];
  890. [self awaitExpectations];
  891. XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"];
  892. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  893. XCTAssertNil(error);
  894. XCTAssertEqualObjects(snapshot.data, data);
  895. XCTAssertFalse(snapshot.metadata.isFromCache);
  896. [getExpectation fulfill];
  897. }];
  898. [self awaitExpectations];
  899. }
  900. - (void)testCanGetDocumentsWhileOffline {
  901. FIRDocumentReference *doc = [self documentRef];
  902. FIRFirestore *firestore = doc.firestore;
  903. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  904. XCTestExpectation *failExpectation =
  905. [self expectationWithDescription:@"offline read with no cached data"];
  906. XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"];
  907. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"];
  908. __weak FIRDocumentReference *weakDoc = doc;
  909. [firestore disableNetworkWithCompletion:^(NSError *error) {
  910. XCTAssertNil(error);
  911. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  912. XCTAssertNotNil(error);
  913. [failExpectation fulfill];
  914. }];
  915. [doc setData:data
  916. completion:^(NSError *_Nullable error) {
  917. XCTAssertNil(error);
  918. [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  919. XCTAssertNil(error);
  920. // Verify that we are not reading from cache.
  921. XCTAssertFalse(snapshot.metadata.isFromCache);
  922. [onlineExpectation fulfill];
  923. }];
  924. }];
  925. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  926. XCTAssertNil(error);
  927. // Verify that we are reading from cache.
  928. XCTAssertTrue(snapshot.metadata.fromCache);
  929. XCTAssertEqualObjects(snapshot.data, data);
  930. [firestore enableNetworkWithCompletion:^(NSError *error) {
  931. [networkExpectation fulfill];
  932. }];
  933. }];
  934. }];
  935. [self awaitExpectations];
  936. }
  937. - (void)testWriteStreamReconnectsAfterIdle {
  938. FIRDocumentReference *doc = [self documentRef];
  939. FIRFirestore *firestore = doc.firestore;
  940. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  941. [firestore workerQueue] -> RunScheduledOperationsUntil(TimerId::WriteStreamIdle);
  942. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  943. }
  944. - (void)testWatchStreamReconnectsAfterIdle {
  945. FIRDocumentReference *doc = [self documentRef];
  946. FIRFirestore *firestore = doc.firestore;
  947. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  948. [firestore workerQueue] -> RunScheduledOperationsUntil(TimerId::ListenStreamIdle);
  949. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  950. }
  951. - (void)testCanDisableNetwork {
  952. FIRDocumentReference *doc = [self documentRef];
  953. FIRFirestore *firestore = doc.firestore;
  954. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  955. [self awaitExpectations];
  956. [firestore
  957. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network again"]];
  958. [self awaitExpectations];
  959. [firestore
  960. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  961. [self awaitExpectations];
  962. [firestore
  963. disableNetworkWithCompletion:[self
  964. completionForExpectationWithName:@"Disable network again"]];
  965. [self awaitExpectations];
  966. [firestore
  967. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Final enable network"]];
  968. [self awaitExpectations];
  969. }
  970. - (void)testClientCallsAfterTerminationFail {
  971. FIRDocumentReference *doc = [self documentRef];
  972. FIRFirestore *firestore = doc.firestore;
  973. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  974. [self awaitExpectations];
  975. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate"]];
  976. [self awaitExpectations];
  977. XCTAssertThrowsSpecific(
  978. {
  979. [firestore disableNetworkWithCompletion:^(NSError *error){
  980. }];
  981. },
  982. NSException, @"The client has already been terminated.");
  983. }
  984. - (void)testMaintainsPersistenceAfterRestarting {
  985. FIRDocumentReference *doc = [self documentRef];
  986. FIRFirestore *firestore = doc.firestore;
  987. FIRApp *app = firestore.app;
  988. NSString *appName = app.name;
  989. FIROptions *options = app.options;
  990. NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
  991. [self writeDocumentRef:doc data:initialData];
  992. // -clearPersistence() requires Firestore to be terminated. Shutdown FIRApp and remove the
  993. // firestore instance to emulate the way an end user would do this.
  994. [self terminateFirestore:firestore];
  995. [self.firestores removeObject:firestore];
  996. [self deleteApp:app];
  997. // We restart the app with the same name and options to check that the previous instance's
  998. // persistent storage persists its data after restarting. Calling [self firestore] here would
  999. // create a new instance of firestore, which defeats the purpose of this test.
  1000. [FIRApp configureWithName:appName options:options];
  1001. FIRApp *app2 = [FIRApp appNamed:appName];
  1002. FIRFirestore *firestore2 = [self firestoreWithApp:app2];
  1003. FIRDocumentReference *docRef2 = [firestore2 documentWithPath:doc.path];
  1004. FIRDocumentSnapshot *snap = [self readDocumentForRef:docRef2 source:FIRFirestoreSourceCache];
  1005. XCTAssertTrue(snap.exists);
  1006. }
  1007. - (void)testCanClearPersistenceAfterRestarting {
  1008. FIRDocumentReference *doc = [self documentRef];
  1009. FIRFirestore *firestore = doc.firestore;
  1010. FIRApp *app = firestore.app;
  1011. NSString *appName = app.name;
  1012. FIROptions *options = app.options;
  1013. NSDictionary<NSString *, id> *initialData = @{@"foo" : @"42"};
  1014. [self writeDocumentRef:doc data:initialData];
  1015. // -clearPersistence() requires Firestore to be terminated. Shutdown FIRApp and remove the
  1016. // firestore instance to emulate the way an end user would do this.
  1017. [self terminateFirestore:firestore];
  1018. [self.firestores removeObject:firestore];
  1019. [firestore
  1020. clearPersistenceWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  1021. [self awaitExpectations];
  1022. [self deleteApp:app];
  1023. // We restart the app with the same name and options to check that the previous instance's
  1024. // persistent storage is actually cleared after the restart. Calling [self firestore] here would
  1025. // create a new instance of firestore, which defeats the purpose of this test.
  1026. [FIRApp configureWithName:appName options:options];
  1027. FIRApp *app2 = [FIRApp appNamed:appName];
  1028. FIRFirestore *firestore2 = [self firestoreWithApp:app2];
  1029. FIRDocumentReference *docRef2 = [firestore2 documentWithPath:doc.path];
  1030. XCTestExpectation *expectation2 = [self expectationWithDescription:@"getData"];
  1031. [docRef2 getDocumentWithSource:FIRFirestoreSourceCache
  1032. completion:^(FIRDocumentSnapshot *doc2, NSError *_Nullable error) {
  1033. XCTAssertNotNil(error);
  1034. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1035. XCTAssertEqual(error.code, FIRFirestoreErrorCodeUnavailable);
  1036. [expectation2 fulfill];
  1037. }];
  1038. [self awaitExpectations];
  1039. }
  1040. - (void)testClearPersistenceWhileRunningFails {
  1041. FIRDocumentReference *doc = [self documentRef];
  1042. FIRFirestore *firestore = doc.firestore;
  1043. [self enableNetwork];
  1044. XCTestExpectation *expectation = [self expectationWithDescription:@"clearPersistence"];
  1045. [firestore clearPersistenceWithCompletion:^(NSError *_Nullable error) {
  1046. XCTAssertNotNil(error);
  1047. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1048. XCTAssertEqual(error.code, FIRFirestoreErrorCodeFailedPrecondition);
  1049. [expectation fulfill];
  1050. }];
  1051. [self awaitExpectations];
  1052. }
  1053. - (void)testRestartFirestoreLeadsToNewInstance {
  1054. FIRApp *app = testutil::AppForUnitTesting(util::MakeString([FSTIntegrationTestCase projectID]));
  1055. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1056. FIRFirestore *sameInstance = [FIRFirestore firestoreForApp:app];
  1057. firestore.settings = [FSTIntegrationTestCase settings];
  1058. XCTAssertEqual(firestore, sameInstance);
  1059. NSDictionary<NSString *, id> *data =
  1060. @{@"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  1061. [self writeDocumentRef:[firestore documentWithPath:@"abc/123"] data:data];
  1062. [self terminateFirestore:firestore];
  1063. // Create a new instance, check it's a different instance.
  1064. FIRFirestore *newInstance = [FIRFirestore firestoreForApp:app];
  1065. newInstance.settings = [FSTIntegrationTestCase settings];
  1066. XCTAssertNotEqual(firestore, newInstance);
  1067. // New instance still functions.
  1068. FIRDocumentSnapshot *snapshot =
  1069. [self readDocumentForRef:[newInstance documentWithPath:@"abc/123"]];
  1070. XCTAssertTrue([data isEqualToDictionary:[snapshot data]]);
  1071. }
  1072. - (void)testAppDeleteLeadsToFirestoreTermination {
  1073. FIRApp *app = testutil::AppForUnitTesting(util::MakeString([FSTIntegrationTestCase projectID]));
  1074. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1075. firestore.settings = [FSTIntegrationTestCase settings];
  1076. NSDictionary<NSString *, id> *data =
  1077. @{@"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}};
  1078. [self writeDocumentRef:[firestore documentWithPath:@"abc/123"] data:data];
  1079. [self deleteApp:app];
  1080. XCTAssertTrue(firestore.wrapped->client()->is_terminated());
  1081. }
  1082. - (void)testTerminateCanBeCalledMultipleTimes {
  1083. FIRApp *app = testutil::AppForUnitTesting(util::MakeString([FSTIntegrationTestCase projectID]));
  1084. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1085. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate1"]];
  1086. [self awaitExpectations];
  1087. XCTAssertThrowsSpecific(
  1088. {
  1089. [firestore disableNetworkWithCompletion:^(NSError *error){
  1090. }];
  1091. },
  1092. NSException, @"The client has already been terminated.");
  1093. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"Terminate2"]];
  1094. [self awaitExpectations];
  1095. XCTAssertThrowsSpecific(
  1096. {
  1097. [firestore enableNetworkWithCompletion:^(NSError *error){
  1098. }];
  1099. },
  1100. NSException, @"The client has already been terminated.");
  1101. }
  1102. - (void)testCanRemoveListenerAfterTermination {
  1103. FIRApp *app = testutil::AppForUnitTesting(util::MakeString([FSTIntegrationTestCase projectID]));
  1104. FIRFirestore *firestore = [FIRFirestore firestoreForApp:app];
  1105. firestore.settings = [FSTIntegrationTestCase settings];
  1106. FIRDocumentReference *doc = [[firestore collectionWithPath:@"rooms"] documentWithAutoID];
  1107. FSTEventAccumulator *accumulator = [FSTEventAccumulator accumulatorForTest:self];
  1108. [self writeDocumentRef:doc data:@{}];
  1109. id<FIRListenerRegistration> listenerRegistration =
  1110. [doc addSnapshotListener:[accumulator valueEventHandler]];
  1111. [accumulator awaitEventWithName:@"Snapshot"];
  1112. [firestore terminateWithCompletion:[self completionForExpectationWithName:@"terminate"]];
  1113. [self awaitExpectations];
  1114. // This should proceed without error.
  1115. [listenerRegistration remove];
  1116. // Multiple calls should proceed as well.
  1117. [listenerRegistration remove];
  1118. }
  1119. - (void)testWaitForPendingWritesCompletes {
  1120. FIRDocumentReference *doc = [self documentRef];
  1121. FIRFirestore *firestore = doc.firestore;
  1122. [self disableNetwork];
  1123. [doc setData:@{@"foo" : @"bar"}];
  1124. [firestore waitForPendingWritesWithCompletion:
  1125. [self completionForExpectationWithName:@"Wait for pending writes"]];
  1126. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  1127. [self awaitExpectations];
  1128. }
  1129. - (void)testWaitForPendingWritesFailsWhenUserChanges {
  1130. FIRFirestore *firestore = self.db;
  1131. [self disableNetwork];
  1132. // Writes to local to prevent immediate call to the completion of waitForPendingWrites.
  1133. NSDictionary<NSString *, id> *data =
  1134. @{@"owner" : @{@"name" : @"Andy", @"email" : @"abc@example.com"}};
  1135. [[self documentRef] setData:data];
  1136. XCTestExpectation *expectation = [self expectationWithDescription:@"waitForPendingWrites"];
  1137. [firestore waitForPendingWritesWithCompletion:^(NSError *_Nullable error) {
  1138. XCTAssertNotNil(error);
  1139. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  1140. XCTAssertEqual(error.code, FIRFirestoreErrorCodeCancelled);
  1141. [expectation fulfill];
  1142. }];
  1143. [self triggerUserChangeWithUid:@"user-to-fail-pending-writes"];
  1144. [self awaitExpectations];
  1145. }
  1146. - (void)testWaitForPendingWritesCompletesWhenOfflineIfNoPending {
  1147. FIRFirestore *firestore = self.db;
  1148. [self disableNetwork];
  1149. [firestore waitForPendingWritesWithCompletion:
  1150. [self completionForExpectationWithName:@"Wait for pending writes"]];
  1151. [self awaitExpectations];
  1152. }
  1153. @end