FIRDatabaseTests.mm 49 KB

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