FIRDatabaseTests.mm 51 KB

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