FIRDatabaseTests.mm 43 KB

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