FIRDatabaseTests.mm 44 KB

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