FIRDatabaseTests.mm 42 KB

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