FIRDatabaseTests.mm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958
  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. [self writeDocumentRef:doc data:initialData];
  139. XCTestExpectation *completed =
  140. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  141. [doc setData:mergeData
  142. merge:YES
  143. completion:^(NSError *error) {
  144. XCTAssertNil(error);
  145. [completed fulfill];
  146. }];
  147. [self awaitExpectations];
  148. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  149. XCTAssertEqual(document[@"updated"], @NO);
  150. XCTAssertTrue([document[@"time"] isKindOfClass:[FIRTimestamp class]]);
  151. }
  152. - (void)testCanDeleteFieldUsingMerge {
  153. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  154. NSDictionary<NSString *, id> *initialData = @{
  155. @"untouched" : @YES,
  156. @"foo" : @"bar",
  157. @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}
  158. };
  159. NSDictionary<NSString *, id> *mergeData = @{
  160. @"foo" : [FIRFieldValue fieldValueForDelete],
  161. @"nested" : @{@"foo" : [FIRFieldValue fieldValueForDelete]}
  162. };
  163. [self writeDocumentRef:doc data:initialData];
  164. XCTestExpectation *completed =
  165. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  166. [doc setData:mergeData
  167. merge:YES
  168. completion:^(NSError *error) {
  169. XCTAssertNil(error);
  170. [completed fulfill];
  171. }];
  172. [self awaitExpectations];
  173. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  174. XCTAssertEqual(document[@"untouched"], @YES);
  175. XCTAssertNil(document[@"foo"]);
  176. XCTAssertEqual(document[@"nested.untouched"], @YES);
  177. XCTAssertNil(document[@"nested.foo"]);
  178. }
  179. - (void)testMergeReplacesArrays {
  180. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  181. NSDictionary<NSString *, id> *initialData = @{
  182. @"untouched" : @YES,
  183. @"data" : @"old",
  184. @"topLevel" : @[ @"old", @"old" ],
  185. @"mapInArray" : @[ @{@"data" : @"old"} ]
  186. };
  187. NSDictionary<NSString *, id> *mergeData =
  188. @{ @"data" : @"new",
  189. @"topLevel" : @[ @"new" ],
  190. @"mapInArray" : @[ @{@"data" : @"new"} ] };
  191. NSDictionary<NSString *, id> *finalData = @{
  192. @"untouched" : @YES,
  193. @"data" : @"new",
  194. @"topLevel" : @[ @"new" ],
  195. @"mapInArray" : @[ @{@"data" : @"new"} ]
  196. };
  197. [self writeDocumentRef:doc data:initialData];
  198. XCTestExpectation *completed =
  199. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  200. [doc setData:mergeData
  201. merge:YES
  202. completion:^(NSError *error) {
  203. XCTAssertNil(error);
  204. [completed fulfill];
  205. }];
  206. [self awaitExpectations];
  207. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  208. XCTAssertEqualObjects(document.data, finalData);
  209. }
  210. - (void)testAddingToACollectionYieldsTheCorrectDocumentReference {
  211. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  212. FIRDocumentReference *ref = [coll addDocumentWithData:@{ @"foo" : @1 }];
  213. XCTestExpectation *getCompletion = [self expectationWithDescription:@"getData"];
  214. [ref getDocumentWithCompletion:^(FIRDocumentSnapshot *_Nullable document,
  215. NSError *_Nullable error) {
  216. XCTAssertNil(error);
  217. XCTAssertEqualObjects(document.data, (@{ @"foo" : @1 }));
  218. [getCompletion fulfill];
  219. }];
  220. [self awaitExpectations];
  221. }
  222. - (void)testListenCanBeCalledMultipleTimes {
  223. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  224. FIRDocumentReference *doc = [coll documentWithAutoID];
  225. XCTestExpectation *completed = [self expectationWithDescription:@"multiple addSnapshotListeners"];
  226. __block NSDictionary<NSString *, id> *resultingData;
  227. // Shut the compiler up about strong references to doc.
  228. FIRDocumentReference *__weak weakDoc = doc;
  229. [doc setData:@{@"foo" : @"bar"}
  230. completion:^(NSError *error1) {
  231. XCTAssertNil(error1);
  232. FIRDocumentReference *strongDoc = weakDoc;
  233. [strongDoc addSnapshotListener:^(FIRDocumentSnapshot *snapshot2, NSError *error2) {
  234. XCTAssertNil(error2);
  235. FIRDocumentReference *strongDoc2 = weakDoc;
  236. [strongDoc2 addSnapshotListener:^(FIRDocumentSnapshot *snapshot3, NSError *error3) {
  237. XCTAssertNil(error3);
  238. resultingData = snapshot3.data;
  239. [completed fulfill];
  240. }];
  241. }];
  242. }];
  243. [self awaitExpectations];
  244. XCTAssertEqualObjects(resultingData, @{@"foo" : @"bar"});
  245. }
  246. - (void)testDocumentSnapshotEvents_nonExistent {
  247. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  248. XCTestExpectation *snapshotCompletion = [self expectationWithDescription:@"snapshot"];
  249. __block int callbacks = 0;
  250. id<FIRListenerRegistration> listenerRegistration =
  251. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  252. callbacks++;
  253. if (callbacks == 1) {
  254. XCTAssertNotNil(doc);
  255. XCTAssertFalse(doc.exists);
  256. [snapshotCompletion fulfill];
  257. } else if (callbacks == 2) {
  258. XCTFail("Should not have received this callback");
  259. }
  260. }];
  261. [self awaitExpectations];
  262. [listenerRegistration remove];
  263. }
  264. - (void)testDocumentSnapshotEvents_forAdd {
  265. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  266. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  267. __block XCTestExpectation *dataCompletion;
  268. __block int callbacks = 0;
  269. id<FIRListenerRegistration> listenerRegistration =
  270. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  271. callbacks++;
  272. if (callbacks == 1) {
  273. XCTAssertNotNil(doc);
  274. XCTAssertFalse(doc.exists);
  275. [emptyCompletion fulfill];
  276. } else if (callbacks == 2) {
  277. XCTAssertEqualObjects(doc.data, (@{ @"a" : @1 }));
  278. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  279. [dataCompletion fulfill];
  280. } else if (callbacks == 3) {
  281. XCTFail("Should not have received this callback");
  282. }
  283. }];
  284. [self awaitExpectations];
  285. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  286. [docRef setData:@{ @"a" : @1 }];
  287. [self awaitExpectations];
  288. [listenerRegistration remove];
  289. }
  290. - (void)testDocumentSnapshotEvents_forAddIncludingMetadata {
  291. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  292. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  293. __block XCTestExpectation *dataCompletion;
  294. __block int callbacks = 0;
  295. id<FIRListenerRegistration> listenerRegistration = [docRef
  296. addSnapshotListenerWithIncludeMetadataChanges:YES
  297. listener:^(FIRDocumentSnapshot *_Nullable doc,
  298. NSError *error) {
  299. callbacks++;
  300. if (callbacks == 1) {
  301. XCTAssertNotNil(doc);
  302. XCTAssertFalse(doc.exists);
  303. [emptyCompletion fulfill];
  304. } else if (callbacks == 2) {
  305. XCTAssertEqualObjects(doc.data, (@{ @"a" : @1 }));
  306. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  307. } else if (callbacks == 3) {
  308. XCTAssertEqualObjects(doc.data, (@{ @"a" : @1 }));
  309. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  310. [dataCompletion fulfill];
  311. } else if (callbacks == 4) {
  312. XCTFail("Should not have received this callback");
  313. }
  314. }];
  315. [self awaitExpectations];
  316. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  317. [docRef setData:@{ @"a" : @1 }];
  318. [self awaitExpectations];
  319. [listenerRegistration remove];
  320. }
  321. - (void)testDocumentSnapshotEvents_forChange {
  322. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  323. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  324. NSDictionary<NSString *, id> *changedData = @{ @"b" : @2 };
  325. [self writeDocumentRef:docRef data:initialData];
  326. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  327. __block XCTestExpectation *changeCompletion;
  328. __block int callbacks = 0;
  329. id<FIRListenerRegistration> listenerRegistration =
  330. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  331. callbacks++;
  332. if (callbacks == 1) {
  333. XCTAssertEqualObjects(doc.data, initialData);
  334. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  335. [initialCompletion fulfill];
  336. } else if (callbacks == 2) {
  337. XCTAssertEqualObjects(doc.data, changedData);
  338. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  339. [changeCompletion fulfill];
  340. } else if (callbacks == 3) {
  341. XCTFail("Should not have received this callback");
  342. }
  343. }];
  344. [self awaitExpectations];
  345. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  346. [docRef setData:changedData];
  347. [self awaitExpectations];
  348. [listenerRegistration remove];
  349. }
  350. - (void)testDocumentSnapshotEvents_forChangeIncludingMetadata {
  351. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  352. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  353. NSDictionary<NSString *, id> *changedData = @{ @"b" : @2 };
  354. [self writeDocumentRef:docRef data:initialData];
  355. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  356. __block XCTestExpectation *changeCompletion;
  357. __block int callbacks = 0;
  358. id<FIRListenerRegistration> listenerRegistration = [docRef
  359. addSnapshotListenerWithIncludeMetadataChanges:YES
  360. listener:^(FIRDocumentSnapshot *_Nullable doc,
  361. NSError *error) {
  362. callbacks++;
  363. if (callbacks == 1) {
  364. XCTAssertEqualObjects(doc.data, initialData);
  365. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  366. XCTAssertEqual(doc.metadata.isFromCache, YES);
  367. } else if (callbacks == 2) {
  368. XCTAssertEqualObjects(doc.data, initialData);
  369. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  370. XCTAssertEqual(doc.metadata.isFromCache, NO);
  371. [initialCompletion fulfill];
  372. } else if (callbacks == 3) {
  373. XCTAssertEqualObjects(doc.data, changedData);
  374. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  375. XCTAssertEqual(doc.metadata.isFromCache, NO);
  376. } else if (callbacks == 4) {
  377. XCTAssertEqualObjects(doc.data, changedData);
  378. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  379. XCTAssertEqual(doc.metadata.isFromCache, NO);
  380. [changeCompletion fulfill];
  381. } else if (callbacks == 5) {
  382. XCTFail("Should not have received this callback");
  383. }
  384. }];
  385. [self awaitExpectations];
  386. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  387. [docRef setData:changedData];
  388. [self awaitExpectations];
  389. [listenerRegistration remove];
  390. }
  391. - (void)testDocumentSnapshotEvents_forDelete {
  392. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  393. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  394. [self writeDocumentRef:docRef data:initialData];
  395. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  396. __block XCTestExpectation *changeCompletion;
  397. __block int callbacks = 0;
  398. id<FIRListenerRegistration> listenerRegistration =
  399. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  400. callbacks++;
  401. if (callbacks == 1) {
  402. XCTAssertEqualObjects(doc.data, initialData);
  403. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  404. XCTAssertEqual(doc.metadata.isFromCache, YES);
  405. [initialCompletion fulfill];
  406. } else if (callbacks == 2) {
  407. XCTAssertFalse(doc.exists);
  408. [changeCompletion fulfill];
  409. } else if (callbacks == 3) {
  410. XCTFail("Should not have received this callback");
  411. }
  412. }];
  413. [self awaitExpectations];
  414. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  415. [docRef deleteDocument];
  416. [self awaitExpectations];
  417. [listenerRegistration remove];
  418. }
  419. - (void)testDocumentSnapshotEvents_forDeleteIncludingMetadata {
  420. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  421. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  422. [self writeDocumentRef:docRef data:initialData];
  423. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  424. __block XCTestExpectation *changeCompletion;
  425. __block int callbacks = 0;
  426. id<FIRListenerRegistration> listenerRegistration = [docRef
  427. addSnapshotListenerWithIncludeMetadataChanges:YES
  428. listener:^(FIRDocumentSnapshot *_Nullable doc,
  429. NSError *error) {
  430. callbacks++;
  431. if (callbacks == 1) {
  432. XCTAssertEqualObjects(doc.data, initialData);
  433. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  434. XCTAssertEqual(doc.metadata.isFromCache, YES);
  435. } else if (callbacks == 2) {
  436. XCTAssertEqualObjects(doc.data, initialData);
  437. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  438. XCTAssertEqual(doc.metadata.isFromCache, NO);
  439. [initialCompletion fulfill];
  440. } else if (callbacks == 3) {
  441. XCTAssertFalse(doc.exists);
  442. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  443. XCTAssertEqual(doc.metadata.isFromCache, NO);
  444. [changeCompletion fulfill];
  445. } else if (callbacks == 4) {
  446. XCTFail("Should not have received this callback");
  447. }
  448. }];
  449. [self awaitExpectations];
  450. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  451. [docRef deleteDocument];
  452. [self awaitExpectations];
  453. [listenerRegistration remove];
  454. }
  455. - (void)testQuerySnapshotEvents_forAdd {
  456. FIRCollectionReference *roomsRef = [self collectionRef];
  457. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  458. NSDictionary<NSString *, id> *newData = @{ @"a" : @1 };
  459. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  460. __block XCTestExpectation *changeCompletion;
  461. __block int callbacks = 0;
  462. id<FIRListenerRegistration> listenerRegistration =
  463. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  464. callbacks++;
  465. if (callbacks == 1) {
  466. XCTAssertEqual(docSet.count, 0);
  467. [emptyCompletion fulfill];
  468. } else if (callbacks == 2) {
  469. XCTAssertEqual(docSet.count, 1);
  470. XCTAssertTrue([docSet.documents[0] isKindOfClass:[FIRQueryDocumentSnapshot class]]);
  471. XCTAssertEqualObjects(docSet.documents[0].data, newData);
  472. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  473. [changeCompletion fulfill];
  474. } else if (callbacks == 3) {
  475. XCTFail("Should not have received a third callback");
  476. }
  477. }];
  478. [self awaitExpectations];
  479. changeCompletion = [self expectationWithDescription:@"changed snapshot"];
  480. [docRef setData:newData];
  481. [self awaitExpectations];
  482. [listenerRegistration remove];
  483. }
  484. - (void)testQuerySnapshotEvents_forChange {
  485. FIRCollectionReference *roomsRef = [self collectionRef];
  486. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  487. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  488. NSDictionary<NSString *, id> *changedData = @{ @"b" : @2 };
  489. [self writeDocumentRef:docRef data:initialData];
  490. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  491. __block XCTestExpectation *changeCompletion;
  492. __block int callbacks = 0;
  493. id<FIRListenerRegistration> listenerRegistration =
  494. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  495. callbacks++;
  496. if (callbacks == 1) {
  497. XCTAssertEqual(docSet.count, 1);
  498. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  499. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  500. [initialCompletion fulfill];
  501. } else if (callbacks == 2) {
  502. XCTAssertEqual(docSet.count, 1);
  503. XCTAssertEqualObjects(docSet.documents[0].data, changedData);
  504. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  505. [changeCompletion fulfill];
  506. } else if (callbacks == 3) {
  507. XCTFail("Should not have received a third callback");
  508. }
  509. }];
  510. [self awaitExpectations];
  511. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  512. [docRef setData:changedData];
  513. [self awaitExpectations];
  514. [listenerRegistration remove];
  515. }
  516. - (void)testQuerySnapshotEvents_forDelete {
  517. FIRCollectionReference *roomsRef = [self collectionRef];
  518. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  519. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  520. [self writeDocumentRef:docRef data:initialData];
  521. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  522. __block XCTestExpectation *changeCompletion;
  523. __block int callbacks = 0;
  524. id<FIRListenerRegistration> listenerRegistration =
  525. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  526. callbacks++;
  527. if (callbacks == 1) {
  528. XCTAssertEqual(docSet.count, 1);
  529. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  530. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  531. [initialCompletion fulfill];
  532. } else if (callbacks == 2) {
  533. XCTAssertEqual(docSet.count, 0);
  534. [changeCompletion fulfill];
  535. } else if (callbacks == 4) {
  536. XCTFail("Should not have received a third callback");
  537. }
  538. }];
  539. [self awaitExpectations];
  540. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  541. [docRef deleteDocument];
  542. [self awaitExpectations];
  543. [listenerRegistration remove];
  544. }
  545. - (void)testExposesFirestoreOnDocumentReferences {
  546. FIRDocumentReference *doc = [self.db documentWithPath:@"foo/bar"];
  547. XCTAssertEqual(doc.firestore, self.db);
  548. }
  549. - (void)testExposesFirestoreOnQueries {
  550. FIRQuery *q = [[self.db collectionWithPath:@"foo"] queryLimitedTo:5];
  551. XCTAssertEqual(q.firestore, self.db);
  552. }
  553. - (void)testDocumentReferenceEquality {
  554. FIRFirestore *firestore = self.db;
  555. FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
  556. XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
  557. XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);
  558. XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);
  559. FIRFirestore *otherFirestore = [self firestore];
  560. XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
  561. }
  562. - (void)testQueryReferenceEquality {
  563. FIRFirestore *firestore = self.db;
  564. FIRQuery *query =
  565. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  566. isEqualTo:@42];
  567. FIRQuery *query2 =
  568. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  569. isEqualTo:@42];
  570. XCTAssertEqualObjects(query, query2);
  571. FIRQuery *query3 =
  572. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
  573. isEqualTo:@42];
  574. XCTAssertNotEqualObjects(query, query3);
  575. FIRFirestore *otherFirestore = [self firestore];
  576. FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
  577. queryWhereField:@"baz"
  578. isEqualTo:@42];
  579. XCTAssertNotEqualObjects(query, query4);
  580. }
  581. - (void)testCanTraverseCollectionsAndDocuments {
  582. NSString *expected = @"a/b/c/d";
  583. // doc path from root Firestore.
  584. XCTAssertEqualObjects([self.db documentWithPath:@"a/b/c/d"].path, expected);
  585. // collection path from root Firestore.
  586. XCTAssertEqualObjects([[self.db collectionWithPath:@"a/b/c"] documentWithPath:@"d"].path,
  587. expected);
  588. // doc path from CollectionReference.
  589. XCTAssertEqualObjects([[self.db collectionWithPath:@"a"] documentWithPath:@"b/c/d"].path,
  590. expected);
  591. // collection path from DocumentReference.
  592. XCTAssertEqualObjects([[self.db documentWithPath:@"a/b"] collectionWithPath:@"c/d/e"].path,
  593. @"a/b/c/d/e");
  594. }
  595. - (void)testCanTraverseCollectionAndDocumentParents {
  596. FIRCollectionReference *collection = [self.db collectionWithPath:@"a/b/c"];
  597. XCTAssertEqualObjects(collection.path, @"a/b/c");
  598. FIRDocumentReference *doc = collection.parent;
  599. XCTAssertEqualObjects(doc.path, @"a/b");
  600. collection = doc.parent;
  601. XCTAssertEqualObjects(collection.path, @"a");
  602. FIRDocumentReference *nilDoc = collection.parent;
  603. XCTAssertNil(nilDoc);
  604. }
  605. - (void)testUpdateFieldsWithDots {
  606. FIRDocumentReference *doc = [self documentRef];
  607. [self writeDocumentRef:doc data:@{@"a.b" : @"old", @"c.d" : @"old"}];
  608. [self updateDocumentRef:doc data:@{ [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new" }];
  609. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  610. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  611. XCTAssertNil(error);
  612. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  613. [expectation fulfill];
  614. }];
  615. [self awaitExpectations];
  616. }
  617. - (void)testUpdateNestedFields {
  618. FIRDocumentReference *doc = [self documentRef];
  619. [self writeDocumentRef:doc
  620. data:@{
  621. @"a" : @{@"b" : @"old"},
  622. @"c" : @{@"d" : @"old"},
  623. @"e" : @{@"f" : @"old"}
  624. }];
  625. [self updateDocumentRef:doc
  626. data:@{
  627. @"a.b" : @"new",
  628. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  629. }];
  630. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  631. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  632. XCTAssertNil(error);
  633. XCTAssertEqualObjects(snapshot.data, (@{
  634. @"a" : @{@"b" : @"new"},
  635. @"c" : @{@"d" : @"new"},
  636. @"e" : @{@"f" : @"old"}
  637. }));
  638. [expectation fulfill];
  639. }];
  640. [self awaitExpectations];
  641. }
  642. - (void)testCollectionID {
  643. XCTAssertEqualObjects([self.db collectionWithPath:@"foo"].collectionID, @"foo");
  644. XCTAssertEqualObjects([self.db collectionWithPath:@"foo/bar/baz"].collectionID, @"baz");
  645. }
  646. - (void)testDocumentID {
  647. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar"].documentID, @"bar");
  648. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux");
  649. }
  650. - (void)testCanQueueWritesWhileOffline {
  651. XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"];
  652. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"];
  653. FIRDocumentReference *doc = [self documentRef];
  654. FIRFirestore *firestore = doc.firestore;
  655. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  656. [firestore disableNetworkWithCompletion:^(NSError *error) {
  657. XCTAssertNil(error);
  658. [doc setData:data
  659. completion:^(NSError *error) {
  660. XCTAssertNil(error);
  661. [writeEpectation fulfill];
  662. }];
  663. [firestore enableNetworkWithCompletion:^(NSError *error) {
  664. XCTAssertNil(error);
  665. [networkExpectation fulfill];
  666. }];
  667. }];
  668. [self awaitExpectations];
  669. XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"];
  670. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  671. XCTAssertNil(error);
  672. XCTAssertEqualObjects(snapshot.data, data);
  673. XCTAssertFalse(snapshot.metadata.isFromCache);
  674. [getExpectation fulfill];
  675. }];
  676. [self awaitExpectations];
  677. }
  678. - (void)testCantGetDocumentsWhileOffline {
  679. FIRDocumentReference *doc = [self documentRef];
  680. FIRFirestore *firestore = doc.firestore;
  681. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  682. XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"];
  683. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"];
  684. __weak FIRDocumentReference *weakDoc = doc;
  685. [firestore disableNetworkWithCompletion:^(NSError *error) {
  686. XCTAssertNil(error);
  687. [doc setData:data
  688. completion:^(NSError *_Nullable error) {
  689. XCTAssertNil(error);
  690. [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  691. XCTAssertNil(error);
  692. // Verify that we are not reading from cache.
  693. XCTAssertFalse(snapshot.metadata.isFromCache);
  694. [onlineExpectation fulfill];
  695. }];
  696. }];
  697. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  698. XCTAssertNil(error);
  699. // Verify that we are reading from cache.
  700. XCTAssertTrue(snapshot.metadata.fromCache);
  701. XCTAssertEqualObjects(snapshot.data, data);
  702. [firestore enableNetworkWithCompletion:^(NSError *error) {
  703. [networkExpectation fulfill];
  704. }];
  705. }];
  706. }];
  707. [self awaitExpectations];
  708. }
  709. - (void)testWriteStreamReconnectsAfterIdle {
  710. FIRDocumentReference *doc = [self documentRef];
  711. FIRFirestore *firestore = doc.firestore;
  712. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  713. [[self queueForFirestore:firestore] runDelayedCallbacksUntil:FSTTimerIDWriteStreamIdle];
  714. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  715. }
  716. - (void)testWatchStreamReconnectsAfterIdle {
  717. FIRDocumentReference *doc = [self documentRef];
  718. FIRFirestore *firestore = doc.firestore;
  719. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  720. [[self queueForFirestore:firestore] runDelayedCallbacksUntil:FSTTimerIDListenStreamIdle];
  721. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  722. }
  723. - (void)testCanDisableNetwork {
  724. FIRDocumentReference *doc = [self documentRef];
  725. FIRFirestore *firestore = doc.firestore;
  726. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  727. [self awaitExpectations];
  728. [firestore
  729. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network again"]];
  730. [self awaitExpectations];
  731. [firestore
  732. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  733. [self awaitExpectations];
  734. [firestore
  735. disableNetworkWithCompletion:[self
  736. completionForExpectationWithName:@"Disable network again"]];
  737. [self awaitExpectations];
  738. [firestore
  739. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Final enable network"]];
  740. [self awaitExpectations];
  741. }
  742. @end