FIRDatabaseTests.mm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  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. options:[FIRSetOptions merge]
  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. options:[FIRSetOptions merge]
  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:[NSDate 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. options:[FIRSetOptions merge]
  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. options:[FIRSetOptions merge]
  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. FIRDocumentListenOptions *options =
  296. [[FIRDocumentListenOptions options] includeMetadataChanges:YES];
  297. id<FIRListenerRegistration> listenerRegistration =
  298. [docRef addSnapshotListenerWithOptions:options
  299. listener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  300. callbacks++;
  301. if (callbacks == 1) {
  302. XCTAssertNotNil(doc);
  303. XCTAssertFalse(doc.exists);
  304. [emptyCompletion fulfill];
  305. } else if (callbacks == 2) {
  306. XCTAssertEqualObjects(doc.data, (@{ @"a" : @1 }));
  307. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  308. } else if (callbacks == 3) {
  309. XCTAssertEqualObjects(doc.data, (@{ @"a" : @1 }));
  310. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  311. [dataCompletion fulfill];
  312. } else if (callbacks == 4) {
  313. XCTFail("Should not have received this callback");
  314. }
  315. }];
  316. [self awaitExpectations];
  317. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  318. [docRef setData:@{ @"a" : @1 }];
  319. [self awaitExpectations];
  320. [listenerRegistration remove];
  321. }
  322. - (void)testDocumentSnapshotEvents_forChange {
  323. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  324. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  325. NSDictionary<NSString *, id> *changedData = @{ @"b" : @2 };
  326. [self writeDocumentRef:docRef data:initialData];
  327. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  328. __block XCTestExpectation *changeCompletion;
  329. __block int callbacks = 0;
  330. id<FIRListenerRegistration> listenerRegistration =
  331. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  332. callbacks++;
  333. if (callbacks == 1) {
  334. XCTAssertEqualObjects(doc.data, initialData);
  335. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  336. [initialCompletion fulfill];
  337. } else if (callbacks == 2) {
  338. XCTAssertEqualObjects(doc.data, changedData);
  339. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  340. [changeCompletion fulfill];
  341. } else if (callbacks == 3) {
  342. XCTFail("Should not have received this callback");
  343. }
  344. }];
  345. [self awaitExpectations];
  346. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  347. [docRef setData:changedData];
  348. [self awaitExpectations];
  349. [listenerRegistration remove];
  350. }
  351. - (void)testDocumentSnapshotEvents_forChangeIncludingMetadata {
  352. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  353. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  354. NSDictionary<NSString *, id> *changedData = @{ @"b" : @2 };
  355. [self writeDocumentRef:docRef data:initialData];
  356. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  357. __block XCTestExpectation *changeCompletion;
  358. __block int callbacks = 0;
  359. FIRDocumentListenOptions *options =
  360. [[FIRDocumentListenOptions options] includeMetadataChanges:YES];
  361. id<FIRListenerRegistration> listenerRegistration =
  362. [docRef addSnapshotListenerWithOptions:options
  363. listener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  364. callbacks++;
  365. if (callbacks == 1) {
  366. XCTAssertEqualObjects(doc.data, initialData);
  367. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  368. XCTAssertEqual(doc.metadata.isFromCache, YES);
  369. } else if (callbacks == 2) {
  370. XCTAssertEqualObjects(doc.data, initialData);
  371. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  372. XCTAssertEqual(doc.metadata.isFromCache, NO);
  373. [initialCompletion fulfill];
  374. } else if (callbacks == 3) {
  375. XCTAssertEqualObjects(doc.data, changedData);
  376. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  377. XCTAssertEqual(doc.metadata.isFromCache, NO);
  378. } else if (callbacks == 4) {
  379. XCTAssertEqualObjects(doc.data, changedData);
  380. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  381. XCTAssertEqual(doc.metadata.isFromCache, NO);
  382. [changeCompletion fulfill];
  383. } else if (callbacks == 5) {
  384. XCTFail("Should not have received this callback");
  385. }
  386. }];
  387. [self awaitExpectations];
  388. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  389. [docRef setData:changedData];
  390. [self awaitExpectations];
  391. [listenerRegistration remove];
  392. }
  393. - (void)testDocumentSnapshotEvents_forDelete {
  394. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  395. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  396. [self writeDocumentRef:docRef data:initialData];
  397. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  398. __block XCTestExpectation *changeCompletion;
  399. __block int callbacks = 0;
  400. id<FIRListenerRegistration> listenerRegistration =
  401. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  402. callbacks++;
  403. if (callbacks == 1) {
  404. XCTAssertEqualObjects(doc.data, initialData);
  405. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  406. XCTAssertEqual(doc.metadata.isFromCache, YES);
  407. [initialCompletion fulfill];
  408. } else if (callbacks == 2) {
  409. XCTAssertFalse(doc.exists);
  410. [changeCompletion fulfill];
  411. } else if (callbacks == 3) {
  412. XCTFail("Should not have received this callback");
  413. }
  414. }];
  415. [self awaitExpectations];
  416. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  417. [docRef deleteDocument];
  418. [self awaitExpectations];
  419. [listenerRegistration remove];
  420. }
  421. - (void)testDocumentSnapshotEvents_forDeleteIncludingMetadata {
  422. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  423. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  424. [self writeDocumentRef:docRef data:initialData];
  425. FIRDocumentListenOptions *options =
  426. [[FIRDocumentListenOptions options] includeMetadataChanges:YES];
  427. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  428. __block XCTestExpectation *changeCompletion;
  429. __block int callbacks = 0;
  430. id<FIRListenerRegistration> listenerRegistration =
  431. [docRef addSnapshotListenerWithOptions:options
  432. listener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  433. callbacks++;
  434. if (callbacks == 1) {
  435. XCTAssertEqualObjects(doc.data, initialData);
  436. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  437. XCTAssertEqual(doc.metadata.isFromCache, YES);
  438. } else if (callbacks == 2) {
  439. XCTAssertEqualObjects(doc.data, initialData);
  440. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  441. XCTAssertEqual(doc.metadata.isFromCache, NO);
  442. [initialCompletion fulfill];
  443. } else if (callbacks == 3) {
  444. XCTAssertFalse(doc.exists);
  445. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  446. XCTAssertEqual(doc.metadata.isFromCache, NO);
  447. [changeCompletion fulfill];
  448. } else if (callbacks == 4) {
  449. XCTFail("Should not have received this callback");
  450. }
  451. }];
  452. [self awaitExpectations];
  453. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  454. [docRef deleteDocument];
  455. [self awaitExpectations];
  456. [listenerRegistration remove];
  457. }
  458. - (void)testQuerySnapshotEvents_forAdd {
  459. FIRCollectionReference *roomsRef = [self collectionRef];
  460. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  461. NSDictionary<NSString *, id> *newData = @{ @"a" : @1 };
  462. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  463. __block XCTestExpectation *changeCompletion;
  464. __block int callbacks = 0;
  465. id<FIRListenerRegistration> listenerRegistration =
  466. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  467. callbacks++;
  468. if (callbacks == 1) {
  469. XCTAssertEqual(docSet.count, 0);
  470. [emptyCompletion fulfill];
  471. } else if (callbacks == 2) {
  472. XCTAssertEqual(docSet.count, 1);
  473. XCTAssertTrue([docSet.documents[0] isKindOfClass:[FIRQueryDocumentSnapshot class]]);
  474. XCTAssertEqualObjects(docSet.documents[0].data, newData);
  475. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  476. [changeCompletion fulfill];
  477. } else if (callbacks == 3) {
  478. XCTFail("Should not have received a third callback");
  479. }
  480. }];
  481. [self awaitExpectations];
  482. changeCompletion = [self expectationWithDescription:@"changed snapshot"];
  483. [docRef setData:newData];
  484. [self awaitExpectations];
  485. [listenerRegistration remove];
  486. }
  487. - (void)testQuerySnapshotEvents_forChange {
  488. FIRCollectionReference *roomsRef = [self collectionRef];
  489. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  490. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  491. NSDictionary<NSString *, id> *changedData = @{ @"b" : @2 };
  492. [self writeDocumentRef:docRef data:initialData];
  493. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  494. __block XCTestExpectation *changeCompletion;
  495. __block int callbacks = 0;
  496. id<FIRListenerRegistration> listenerRegistration =
  497. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  498. callbacks++;
  499. if (callbacks == 1) {
  500. XCTAssertEqual(docSet.count, 1);
  501. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  502. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  503. [initialCompletion fulfill];
  504. } else if (callbacks == 2) {
  505. XCTAssertEqual(docSet.count, 1);
  506. XCTAssertEqualObjects(docSet.documents[0].data, changedData);
  507. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  508. [changeCompletion fulfill];
  509. } else if (callbacks == 3) {
  510. XCTFail("Should not have received a third callback");
  511. }
  512. }];
  513. [self awaitExpectations];
  514. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  515. [docRef setData:changedData];
  516. [self awaitExpectations];
  517. [listenerRegistration remove];
  518. }
  519. - (void)testQuerySnapshotEvents_forDelete {
  520. FIRCollectionReference *roomsRef = [self collectionRef];
  521. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  522. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  523. [self writeDocumentRef:docRef data:initialData];
  524. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  525. __block XCTestExpectation *changeCompletion;
  526. __block int callbacks = 0;
  527. id<FIRListenerRegistration> listenerRegistration =
  528. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  529. callbacks++;
  530. if (callbacks == 1) {
  531. XCTAssertEqual(docSet.count, 1);
  532. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  533. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  534. [initialCompletion fulfill];
  535. } else if (callbacks == 2) {
  536. XCTAssertEqual(docSet.count, 0);
  537. [changeCompletion fulfill];
  538. } else if (callbacks == 4) {
  539. XCTFail("Should not have received a third callback");
  540. }
  541. }];
  542. [self awaitExpectations];
  543. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  544. [docRef deleteDocument];
  545. [self awaitExpectations];
  546. [listenerRegistration remove];
  547. }
  548. - (void)testExposesFirestoreOnDocumentReferences {
  549. FIRDocumentReference *doc = [self.db documentWithPath:@"foo/bar"];
  550. XCTAssertEqual(doc.firestore, self.db);
  551. }
  552. - (void)testExposesFirestoreOnQueries {
  553. FIRQuery *q = [[self.db collectionWithPath:@"foo"] queryLimitedTo:5];
  554. XCTAssertEqual(q.firestore, self.db);
  555. }
  556. - (void)testDocumentReferenceEquality {
  557. FIRFirestore *firestore = self.db;
  558. FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
  559. XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
  560. XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);
  561. XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);
  562. FIRFirestore *otherFirestore = [self firestore];
  563. XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
  564. }
  565. - (void)testQueryReferenceEquality {
  566. FIRFirestore *firestore = self.db;
  567. FIRQuery *query =
  568. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  569. isEqualTo:@42];
  570. FIRQuery *query2 =
  571. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  572. isEqualTo:@42];
  573. XCTAssertEqualObjects(query, query2);
  574. FIRQuery *query3 =
  575. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
  576. isEqualTo:@42];
  577. XCTAssertNotEqualObjects(query, query3);
  578. FIRFirestore *otherFirestore = [self firestore];
  579. FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
  580. queryWhereField:@"baz"
  581. isEqualTo:@42];
  582. XCTAssertNotEqualObjects(query, query4);
  583. }
  584. - (void)testCanTraverseCollectionsAndDocuments {
  585. NSString *expected = @"a/b/c/d";
  586. // doc path from root Firestore.
  587. XCTAssertEqualObjects([self.db documentWithPath:@"a/b/c/d"].path, expected);
  588. // collection path from root Firestore.
  589. XCTAssertEqualObjects([[self.db collectionWithPath:@"a/b/c"] documentWithPath:@"d"].path,
  590. expected);
  591. // doc path from CollectionReference.
  592. XCTAssertEqualObjects([[self.db collectionWithPath:@"a"] documentWithPath:@"b/c/d"].path,
  593. expected);
  594. // collection path from DocumentReference.
  595. XCTAssertEqualObjects([[self.db documentWithPath:@"a/b"] collectionWithPath:@"c/d/e"].path,
  596. @"a/b/c/d/e");
  597. }
  598. - (void)testCanTraverseCollectionAndDocumentParents {
  599. FIRCollectionReference *collection = [self.db collectionWithPath:@"a/b/c"];
  600. XCTAssertEqualObjects(collection.path, @"a/b/c");
  601. FIRDocumentReference *doc = collection.parent;
  602. XCTAssertEqualObjects(doc.path, @"a/b");
  603. collection = doc.parent;
  604. XCTAssertEqualObjects(collection.path, @"a");
  605. FIRDocumentReference *nilDoc = collection.parent;
  606. XCTAssertNil(nilDoc);
  607. }
  608. - (void)testUpdateFieldsWithDots {
  609. FIRDocumentReference *doc = [self documentRef];
  610. [self writeDocumentRef:doc data:@{@"a.b" : @"old", @"c.d" : @"old"}];
  611. [self updateDocumentRef:doc data:@{ [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new" }];
  612. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  613. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  614. XCTAssertNil(error);
  615. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  616. [expectation fulfill];
  617. }];
  618. [self awaitExpectations];
  619. }
  620. - (void)testUpdateNestedFields {
  621. FIRDocumentReference *doc = [self documentRef];
  622. [self writeDocumentRef:doc
  623. data:@{
  624. @"a" : @{@"b" : @"old"},
  625. @"c" : @{@"d" : @"old"},
  626. @"e" : @{@"f" : @"old"}
  627. }];
  628. [self updateDocumentRef:doc
  629. data:@{
  630. @"a.b" : @"new",
  631. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  632. }];
  633. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  634. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  635. XCTAssertNil(error);
  636. XCTAssertEqualObjects(snapshot.data, (@{
  637. @"a" : @{@"b" : @"new"},
  638. @"c" : @{@"d" : @"new"},
  639. @"e" : @{@"f" : @"old"}
  640. }));
  641. [expectation fulfill];
  642. }];
  643. [self awaitExpectations];
  644. }
  645. - (void)testCollectionID {
  646. XCTAssertEqualObjects([self.db collectionWithPath:@"foo"].collectionID, @"foo");
  647. XCTAssertEqualObjects([self.db collectionWithPath:@"foo/bar/baz"].collectionID, @"baz");
  648. }
  649. - (void)testDocumentID {
  650. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar"].documentID, @"bar");
  651. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux");
  652. }
  653. - (void)testCanQueueWritesWhileOffline {
  654. XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"];
  655. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"];
  656. FIRDocumentReference *doc = [self documentRef];
  657. FIRFirestore *firestore = doc.firestore;
  658. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  659. [firestore disableNetworkWithCompletion:^(NSError *error) {
  660. XCTAssertNil(error);
  661. [doc setData:data
  662. completion:^(NSError *error) {
  663. XCTAssertNil(error);
  664. [writeEpectation fulfill];
  665. }];
  666. [firestore enableNetworkWithCompletion:^(NSError *error) {
  667. XCTAssertNil(error);
  668. [networkExpectation fulfill];
  669. }];
  670. }];
  671. [self awaitExpectations];
  672. XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"];
  673. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  674. XCTAssertNil(error);
  675. XCTAssertEqualObjects(snapshot.data, data);
  676. XCTAssertFalse(snapshot.metadata.isFromCache);
  677. [getExpectation fulfill];
  678. }];
  679. [self awaitExpectations];
  680. }
  681. - (void)testCantGetDocumentsWhileOffline {
  682. FIRDocumentReference *doc = [self documentRef];
  683. FIRFirestore *firestore = doc.firestore;
  684. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  685. XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"];
  686. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"];
  687. __weak FIRDocumentReference *weakDoc = doc;
  688. [firestore disableNetworkWithCompletion:^(NSError *error) {
  689. XCTAssertNil(error);
  690. [doc setData:data
  691. completion:^(NSError *_Nullable error) {
  692. XCTAssertNil(error);
  693. [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  694. XCTAssertNil(error);
  695. // Verify that we are not reading from cache.
  696. XCTAssertFalse(snapshot.metadata.isFromCache);
  697. [onlineExpectation fulfill];
  698. }];
  699. }];
  700. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  701. XCTAssertNil(error);
  702. // Verify that we are reading from cache.
  703. XCTAssertTrue(snapshot.metadata.fromCache);
  704. XCTAssertEqualObjects(snapshot.data, data);
  705. [firestore enableNetworkWithCompletion:^(NSError *error) {
  706. [networkExpectation fulfill];
  707. }];
  708. }];
  709. }];
  710. [self awaitExpectations];
  711. }
  712. - (void)testWriteStreamReconnectsAfterIdle {
  713. FIRDocumentReference *doc = [self documentRef];
  714. FIRFirestore *firestore = doc.firestore;
  715. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  716. [[self queueForFirestore:firestore] runDelayedCallbacksUntil:FSTTimerIDWriteStreamIdle];
  717. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  718. }
  719. - (void)testWatchStreamReconnectsAfterIdle {
  720. FIRDocumentReference *doc = [self documentRef];
  721. FIRFirestore *firestore = doc.firestore;
  722. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  723. [[self queueForFirestore:firestore] runDelayedCallbacksUntil:FSTTimerIDListenStreamIdle];
  724. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  725. }
  726. - (void)testCanDisableNetwork {
  727. FIRDocumentReference *doc = [self documentRef];
  728. FIRFirestore *firestore = doc.firestore;
  729. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  730. [self awaitExpectations];
  731. [firestore
  732. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network again"]];
  733. [self awaitExpectations];
  734. [firestore
  735. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  736. [self awaitExpectations];
  737. [firestore
  738. disableNetworkWithCompletion:[self
  739. completionForExpectationWithName:@"Disable network again"]];
  740. [self awaitExpectations];
  741. [firestore
  742. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Final enable network"]];
  743. [self awaitExpectations];
  744. }
  745. @end