FIRDatabaseTests.mm 35 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963
  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. @interface FIRDatabaseTests : FSTIntegrationTestCase
  22. @end
  23. @implementation FIRDatabaseTests
  24. - (void)testCanUpdateAnExistingDocument {
  25. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  26. NSDictionary<NSString *, id> *initialData =
  27. @{ @"desc" : @"Description",
  28. @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"} };
  29. NSDictionary<NSString *, id> *updateData =
  30. @{@"desc" : @"NewDescription", @"owner.email" : @"new@xyz.com"};
  31. NSDictionary<NSString *, id> *finalData =
  32. @{ @"desc" : @"NewDescription",
  33. @"owner" : @{@"name" : @"Jonny", @"email" : @"new@xyz.com"} };
  34. [self writeDocumentRef:doc data:initialData];
  35. XCTestExpectation *updateCompletion = [self expectationWithDescription:@"updateData"];
  36. [doc updateData:updateData
  37. completion:^(NSError *_Nullable error) {
  38. XCTAssertNil(error);
  39. [updateCompletion fulfill];
  40. }];
  41. [self awaitExpectations];
  42. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  43. XCTAssertTrue(result.exists);
  44. XCTAssertEqualObjects(result.data, finalData);
  45. }
  46. - (void)testCanDeleteAFieldWithAnUpdate {
  47. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  48. NSDictionary<NSString *, id> *initialData =
  49. @{ @"desc" : @"Description",
  50. @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"} };
  51. NSDictionary<NSString *, id> *updateData =
  52. @{@"owner.email" : [FIRFieldValue fieldValueForDelete]};
  53. NSDictionary<NSString *, id> *finalData =
  54. @{ @"desc" : @"Description",
  55. @"owner" : @{@"name" : @"Jonny"} };
  56. [self writeDocumentRef:doc data:initialData];
  57. [self updateDocumentRef:doc data:updateData];
  58. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  59. XCTAssertTrue(result.exists);
  60. XCTAssertEqualObjects(result.data, finalData);
  61. }
  62. - (void)testDeleteDocument {
  63. FIRDocumentReference *doc = [self.db documentWithPath:@"rooms/eros"];
  64. NSDictionary<NSString *, id> *data = @{@"value" : @"foo"};
  65. [self writeDocumentRef:doc data:data];
  66. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  67. XCTAssertEqualObjects(result.data, data);
  68. [self deleteDocumentRef:doc];
  69. result = [self readDocumentForRef:doc];
  70. XCTAssertFalse(result.exists);
  71. }
  72. - (void)testCanRetrieveDocumentThatDoesNotExist {
  73. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  74. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  75. XCTAssertNil(result.data);
  76. XCTAssertNil(result[@"foo"]);
  77. }
  78. - (void)testCannotUpdateNonexistentDocument {
  79. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  80. XCTestExpectation *setCompletion = [self expectationWithDescription:@"setData"];
  81. [doc updateData:@{@"owner" : @"abc"}
  82. completion:^(NSError *_Nullable error) {
  83. XCTAssertNotNil(error);
  84. XCTAssertEqualObjects(error.domain, FIRFirestoreErrorDomain);
  85. XCTAssertEqual(error.code, FIRFirestoreErrorCodeNotFound);
  86. [setCompletion fulfill];
  87. }];
  88. [self awaitExpectations];
  89. FIRDocumentSnapshot *result = [self readDocumentForRef:doc];
  90. XCTAssertFalse(result.exists);
  91. }
  92. - (void)testCanOverwriteDataAnExistingDocumentUsingSet {
  93. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  94. NSDictionary<NSString *, id> *initialData =
  95. @{ @"desc" : @"Description",
  96. @"owner" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"} };
  97. NSDictionary<NSString *, id> *udpateData = @{@"desc" : @"NewDescription"};
  98. [self writeDocumentRef:doc data:initialData];
  99. [self writeDocumentRef:doc data:udpateData];
  100. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  101. XCTAssertEqualObjects(document.data, udpateData);
  102. }
  103. - (void)testCanMergeDataWithAnExistingDocumentUsingSet {
  104. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  105. NSDictionary<NSString *, id> *initialData = @{
  106. @"desc" : @"Description",
  107. @"owner.data" : @{@"name" : @"Jonny", @"email" : @"abc@xyz.com"}
  108. };
  109. NSDictionary<NSString *, id> *mergeData =
  110. @{ @"updated" : @YES,
  111. @"owner.data" : @{@"name" : @"Sebastian"} };
  112. NSDictionary<NSString *, id> *finalData = @{
  113. @"desc" : @"Description",
  114. @"updated" : @YES,
  115. @"owner.data" : @{@"name" : @"Sebastian", @"email" : @"abc@xyz.com"}
  116. };
  117. [self writeDocumentRef:doc data:initialData];
  118. XCTestExpectation *completed =
  119. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  120. [doc setData:mergeData
  121. options:[FIRSetOptions merge]
  122. completion:^(NSError *error) {
  123. XCTAssertNil(error);
  124. [completed fulfill];
  125. }];
  126. [self awaitExpectations];
  127. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  128. XCTAssertEqualObjects(document.data, finalData);
  129. }
  130. - (void)testCanMergeServerTimestamps {
  131. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  132. NSDictionary<NSString *, id> *initialData = @{
  133. @"updated" : @NO,
  134. };
  135. NSDictionary<NSString *, id> *mergeData =
  136. @{@"time" : [FIRFieldValue fieldValueForServerTimestamp]};
  137. [self writeDocumentRef:doc data:initialData];
  138. XCTestExpectation *completed =
  139. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  140. [doc setData:mergeData
  141. options:[FIRSetOptions merge]
  142. completion:^(NSError *error) {
  143. XCTAssertNil(error);
  144. [completed fulfill];
  145. }];
  146. [self awaitExpectations];
  147. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  148. XCTAssertEqual(document[@"updated"], @NO);
  149. XCTAssertTrue([document[@"time"] isKindOfClass:[NSDate class]]);
  150. }
  151. - (void)testCanDeleteFieldUsingMerge {
  152. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  153. NSDictionary<NSString *, id> *initialData = @{
  154. @"untouched" : @YES,
  155. @"foo" : @"bar",
  156. @"nested" : @{@"untouched" : @YES, @"foo" : @"bar"}
  157. };
  158. NSDictionary<NSString *, id> *mergeData = @{
  159. @"foo" : [FIRFieldValue fieldValueForDelete],
  160. @"nested" : @{@"foo" : [FIRFieldValue fieldValueForDelete]}
  161. };
  162. [self writeDocumentRef:doc data:initialData];
  163. XCTestExpectation *completed =
  164. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  165. [doc setData:mergeData
  166. options:[FIRSetOptions merge]
  167. completion:^(NSError *error) {
  168. XCTAssertNil(error);
  169. [completed fulfill];
  170. }];
  171. [self awaitExpectations];
  172. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  173. XCTAssertEqual(document[@"untouched"], @YES);
  174. XCTAssertNil(document[@"foo"]);
  175. XCTAssertEqual(document[@"nested.untouched"], @YES);
  176. XCTAssertNil(document[@"nested.foo"]);
  177. }
  178. - (void)testMergeReplacesArrays {
  179. FIRDocumentReference *doc = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  180. NSDictionary<NSString *, id> *initialData = @{
  181. @"untouched" : @YES,
  182. @"data" : @"old",
  183. @"topLevel" : @[ @"old", @"old" ],
  184. @"mapInArray" : @[ @{@"data" : @"old"} ]
  185. };
  186. NSDictionary<NSString *, id> *mergeData =
  187. @{ @"data" : @"new",
  188. @"topLevel" : @[ @"new" ],
  189. @"mapInArray" : @[ @{@"data" : @"new"} ] };
  190. NSDictionary<NSString *, id> *finalData = @{
  191. @"untouched" : @YES,
  192. @"data" : @"new",
  193. @"topLevel" : @[ @"new" ],
  194. @"mapInArray" : @[ @{@"data" : @"new"} ]
  195. };
  196. [self writeDocumentRef:doc data:initialData];
  197. XCTestExpectation *completed =
  198. [self expectationWithDescription:@"testCanMergeDataWithAnExistingDocumentUsingSet"];
  199. [doc setData:mergeData
  200. options:[FIRSetOptions merge]
  201. completion:^(NSError *error) {
  202. XCTAssertNil(error);
  203. [completed fulfill];
  204. }];
  205. [self awaitExpectations];
  206. FIRDocumentSnapshot *document = [self readDocumentForRef:doc];
  207. XCTAssertEqualObjects(document.data, finalData);
  208. }
  209. - (void)testAddingToACollectionYieldsTheCorrectDocumentReference {
  210. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  211. FIRDocumentReference *ref = [coll addDocumentWithData:@{ @"foo" : @1 }];
  212. XCTestExpectation *getCompletion = [self expectationWithDescription:@"getData"];
  213. [ref getDocumentWithCompletion:^(FIRDocumentSnapshot *_Nullable document,
  214. NSError *_Nullable error) {
  215. XCTAssertNil(error);
  216. XCTAssertEqualObjects(document.data, (@{ @"foo" : @1 }));
  217. [getCompletion fulfill];
  218. }];
  219. [self awaitExpectations];
  220. }
  221. - (void)testListenCanBeCalledMultipleTimes {
  222. FIRCollectionReference *coll = [self.db collectionWithPath:@"collection"];
  223. FIRDocumentReference *doc = [coll documentWithAutoID];
  224. XCTestExpectation *completed = [self expectationWithDescription:@"multiple addSnapshotListeners"];
  225. __block NSDictionary<NSString *, id> *resultingData;
  226. // Shut the compiler up about strong references to doc.
  227. FIRDocumentReference *__weak weakDoc = doc;
  228. [doc setData:@{@"foo" : @"bar"}
  229. completion:^(NSError *error1) {
  230. XCTAssertNil(error1);
  231. FIRDocumentReference *strongDoc = weakDoc;
  232. [strongDoc addSnapshotListener:^(FIRDocumentSnapshot *snapshot2, NSError *error2) {
  233. XCTAssertNil(error2);
  234. FIRDocumentReference *strongDoc2 = weakDoc;
  235. [strongDoc2 addSnapshotListener:^(FIRDocumentSnapshot *snapshot3, NSError *error3) {
  236. XCTAssertNil(error3);
  237. resultingData = snapshot3.data;
  238. [completed fulfill];
  239. }];
  240. }];
  241. }];
  242. [self awaitExpectations];
  243. XCTAssertEqualObjects(resultingData, @{@"foo" : @"bar"});
  244. }
  245. - (void)testDocumentSnapshotEvents_nonExistent {
  246. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  247. XCTestExpectation *snapshotCompletion = [self expectationWithDescription:@"snapshot"];
  248. __block int callbacks = 0;
  249. id<FIRListenerRegistration> listenerRegistration =
  250. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  251. callbacks++;
  252. if (callbacks == 1) {
  253. XCTAssertNotNil(doc);
  254. XCTAssertFalse(doc.exists);
  255. [snapshotCompletion fulfill];
  256. } else if (callbacks == 2) {
  257. XCTFail("Should not have received this callback");
  258. }
  259. }];
  260. [self awaitExpectations];
  261. [listenerRegistration remove];
  262. }
  263. - (void)testDocumentSnapshotEvents_forAdd {
  264. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  265. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  266. __block XCTestExpectation *dataCompletion;
  267. __block int callbacks = 0;
  268. id<FIRListenerRegistration> listenerRegistration =
  269. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  270. callbacks++;
  271. if (callbacks == 1) {
  272. XCTAssertNotNil(doc);
  273. XCTAssertFalse(doc.exists);
  274. [emptyCompletion fulfill];
  275. } else if (callbacks == 2) {
  276. XCTAssertEqualObjects(doc.data, (@{ @"a" : @1 }));
  277. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  278. [dataCompletion fulfill];
  279. } else if (callbacks == 3) {
  280. XCTFail("Should not have received this callback");
  281. }
  282. }];
  283. [self awaitExpectations];
  284. dataCompletion = [self expectationWithDescription:@"data snapshot"];
  285. [docRef setData:@{ @"a" : @1 }];
  286. [self awaitExpectations];
  287. [listenerRegistration remove];
  288. }
  289. - (void)testDocumentSnapshotEvents_forAddIncludingMetadata {
  290. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  291. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  292. __block XCTestExpectation *dataCompletion;
  293. __block int callbacks = 0;
  294. FIRDocumentListenOptions *options =
  295. [[FIRDocumentListenOptions options] includeMetadataChanges:YES];
  296. id<FIRListenerRegistration> listenerRegistration =
  297. [docRef addSnapshotListenerWithOptions:options
  298. listener:^(FIRDocumentSnapshot *_Nullable doc, 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. FIRDocumentListenOptions *options =
  359. [[FIRDocumentListenOptions options] includeMetadataChanges:YES];
  360. id<FIRListenerRegistration> listenerRegistration =
  361. [docRef addSnapshotListenerWithOptions:options
  362. listener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  363. callbacks++;
  364. if (callbacks == 1) {
  365. XCTAssertEqualObjects(doc.data, initialData);
  366. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  367. XCTAssertEqual(doc.metadata.isFromCache, YES);
  368. } else if (callbacks == 2) {
  369. XCTAssertEqualObjects(doc.data, initialData);
  370. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  371. XCTAssertEqual(doc.metadata.isFromCache, NO);
  372. [initialCompletion fulfill];
  373. } else if (callbacks == 3) {
  374. XCTAssertEqualObjects(doc.data, changedData);
  375. XCTAssertEqual(doc.metadata.hasPendingWrites, YES);
  376. XCTAssertEqual(doc.metadata.isFromCache, NO);
  377. } else if (callbacks == 4) {
  378. XCTAssertEqualObjects(doc.data, changedData);
  379. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  380. XCTAssertEqual(doc.metadata.isFromCache, NO);
  381. [changeCompletion fulfill];
  382. } else if (callbacks == 5) {
  383. XCTFail("Should not have received this callback");
  384. }
  385. }];
  386. [self awaitExpectations];
  387. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  388. [docRef setData:changedData];
  389. [self awaitExpectations];
  390. [listenerRegistration remove];
  391. }
  392. - (void)testDocumentSnapshotEvents_forDelete {
  393. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  394. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  395. [self writeDocumentRef:docRef data:initialData];
  396. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  397. __block XCTestExpectation *changeCompletion;
  398. __block int callbacks = 0;
  399. id<FIRListenerRegistration> listenerRegistration =
  400. [docRef addSnapshotListener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  401. callbacks++;
  402. if (callbacks == 1) {
  403. XCTAssertEqualObjects(doc.data, initialData);
  404. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  405. XCTAssertEqual(doc.metadata.isFromCache, YES);
  406. [initialCompletion fulfill];
  407. } else if (callbacks == 2) {
  408. XCTAssertFalse(doc.exists);
  409. [changeCompletion fulfill];
  410. } else if (callbacks == 3) {
  411. XCTFail("Should not have received this callback");
  412. }
  413. }];
  414. [self awaitExpectations];
  415. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  416. [docRef deleteDocument];
  417. [self awaitExpectations];
  418. [listenerRegistration remove];
  419. }
  420. - (void)testDocumentSnapshotEvents_forDeleteIncludingMetadata {
  421. FIRDocumentReference *docRef = [[self.db collectionWithPath:@"rooms"] documentWithAutoID];
  422. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  423. [self writeDocumentRef:docRef data:initialData];
  424. FIRDocumentListenOptions *options =
  425. [[FIRDocumentListenOptions options] includeMetadataChanges:YES];
  426. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  427. __block XCTestExpectation *changeCompletion;
  428. __block int callbacks = 0;
  429. id<FIRListenerRegistration> listenerRegistration =
  430. [docRef addSnapshotListenerWithOptions:options
  431. listener:^(FIRDocumentSnapshot *_Nullable doc, NSError *error) {
  432. callbacks++;
  433. if (callbacks == 1) {
  434. XCTAssertEqualObjects(doc.data, initialData);
  435. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  436. XCTAssertEqual(doc.metadata.isFromCache, YES);
  437. } else if (callbacks == 2) {
  438. XCTAssertEqualObjects(doc.data, initialData);
  439. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  440. XCTAssertEqual(doc.metadata.isFromCache, NO);
  441. [initialCompletion fulfill];
  442. } else if (callbacks == 3) {
  443. XCTAssertFalse(doc.exists);
  444. XCTAssertEqual(doc.metadata.hasPendingWrites, NO);
  445. XCTAssertEqual(doc.metadata.isFromCache, NO);
  446. [changeCompletion fulfill];
  447. } else if (callbacks == 4) {
  448. XCTFail("Should not have received this callback");
  449. }
  450. }];
  451. [self awaitExpectations];
  452. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  453. [docRef deleteDocument];
  454. [self awaitExpectations];
  455. [listenerRegistration remove];
  456. }
  457. - (void)testQuerySnapshotEvents_forAdd {
  458. FIRCollectionReference *roomsRef = [self collectionRef];
  459. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  460. NSDictionary<NSString *, id> *newData = @{ @"a" : @1 };
  461. XCTestExpectation *emptyCompletion = [self expectationWithDescription:@"empty snapshot"];
  462. __block XCTestExpectation *changeCompletion;
  463. __block int callbacks = 0;
  464. id<FIRListenerRegistration> listenerRegistration =
  465. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  466. callbacks++;
  467. if (callbacks == 1) {
  468. XCTAssertEqual(docSet.count, 0);
  469. [emptyCompletion fulfill];
  470. } else if (callbacks == 2) {
  471. XCTAssertEqual(docSet.count, 1);
  472. XCTAssertTrue([docSet.documents[0] isKindOfClass:[FIRQueryDocumentSnapshot class]]);
  473. XCTAssertEqualObjects(docSet.documents[0].data, newData);
  474. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  475. [changeCompletion fulfill];
  476. } else if (callbacks == 3) {
  477. XCTFail("Should not have received a third callback");
  478. }
  479. }];
  480. [self awaitExpectations];
  481. changeCompletion = [self expectationWithDescription:@"changed snapshot"];
  482. [docRef setData:newData];
  483. [self awaitExpectations];
  484. [listenerRegistration remove];
  485. }
  486. - (void)testQuerySnapshotEvents_forChange {
  487. FIRCollectionReference *roomsRef = [self collectionRef];
  488. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  489. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  490. NSDictionary<NSString *, id> *changedData = @{ @"b" : @2 };
  491. [self writeDocumentRef:docRef data:initialData];
  492. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  493. __block XCTestExpectation *changeCompletion;
  494. __block int callbacks = 0;
  495. id<FIRListenerRegistration> listenerRegistration =
  496. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  497. callbacks++;
  498. if (callbacks == 1) {
  499. XCTAssertEqual(docSet.count, 1);
  500. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  501. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  502. [initialCompletion fulfill];
  503. } else if (callbacks == 2) {
  504. XCTAssertEqual(docSet.count, 1);
  505. XCTAssertEqualObjects(docSet.documents[0].data, changedData);
  506. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, YES);
  507. [changeCompletion fulfill];
  508. } else if (callbacks == 3) {
  509. XCTFail("Should not have received a third callback");
  510. }
  511. }];
  512. [self awaitExpectations];
  513. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  514. [docRef setData:changedData];
  515. [self awaitExpectations];
  516. [listenerRegistration remove];
  517. }
  518. - (void)testQuerySnapshotEvents_forDelete {
  519. FIRCollectionReference *roomsRef = [self collectionRef];
  520. FIRDocumentReference *docRef = [roomsRef documentWithAutoID];
  521. NSDictionary<NSString *, id> *initialData = @{ @"a" : @1 };
  522. [self writeDocumentRef:docRef data:initialData];
  523. XCTestExpectation *initialCompletion = [self expectationWithDescription:@"initial data"];
  524. __block XCTestExpectation *changeCompletion;
  525. __block int callbacks = 0;
  526. id<FIRListenerRegistration> listenerRegistration =
  527. [roomsRef addSnapshotListener:^(FIRQuerySnapshot *_Nullable docSet, NSError *error) {
  528. callbacks++;
  529. if (callbacks == 1) {
  530. XCTAssertEqual(docSet.count, 1);
  531. XCTAssertEqualObjects(docSet.documents[0].data, initialData);
  532. XCTAssertEqual(docSet.documents[0].metadata.hasPendingWrites, NO);
  533. [initialCompletion fulfill];
  534. } else if (callbacks == 2) {
  535. XCTAssertEqual(docSet.count, 0);
  536. [changeCompletion fulfill];
  537. } else if (callbacks == 4) {
  538. XCTFail("Should not have received a third callback");
  539. }
  540. }];
  541. [self awaitExpectations];
  542. changeCompletion = [self expectationWithDescription:@"listen for changed data"];
  543. [docRef deleteDocument];
  544. [self awaitExpectations];
  545. [listenerRegistration remove];
  546. }
  547. - (void)testExposesFirestoreOnDocumentReferences {
  548. FIRDocumentReference *doc = [self.db documentWithPath:@"foo/bar"];
  549. XCTAssertEqual(doc.firestore, self.db);
  550. }
  551. - (void)testExposesFirestoreOnQueries {
  552. FIRQuery *q = [[self.db collectionWithPath:@"foo"] queryLimitedTo:5];
  553. XCTAssertEqual(q.firestore, self.db);
  554. }
  555. - (void)testDocumentReferenceEquality {
  556. FIRFirestore *firestore = self.db;
  557. FIRDocumentReference *docRef = [firestore documentWithPath:@"foo/bar"];
  558. XCTAssertEqualObjects([firestore documentWithPath:@"foo/bar"], docRef);
  559. XCTAssertEqualObjects([docRef collectionWithPath:@"blah"].parent, docRef);
  560. XCTAssertNotEqualObjects([firestore documentWithPath:@"foo/BAR"], docRef);
  561. FIRFirestore *otherFirestore = [self firestore];
  562. XCTAssertNotEqualObjects([otherFirestore documentWithPath:@"foo/bar"], docRef);
  563. }
  564. - (void)testQueryReferenceEquality {
  565. FIRFirestore *firestore = self.db;
  566. FIRQuery *query =
  567. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  568. isEqualTo:@42];
  569. FIRQuery *query2 =
  570. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"] queryWhereField:@"baz"
  571. isEqualTo:@42];
  572. XCTAssertEqualObjects(query, query2);
  573. FIRQuery *query3 =
  574. [[[firestore collectionWithPath:@"foo"] queryOrderedByField:@"BAR"] queryWhereField:@"baz"
  575. isEqualTo:@42];
  576. XCTAssertNotEqualObjects(query, query3);
  577. FIRFirestore *otherFirestore = [self firestore];
  578. FIRQuery *query4 = [[[otherFirestore collectionWithPath:@"foo"] queryOrderedByField:@"bar"]
  579. queryWhereField:@"baz"
  580. isEqualTo:@42];
  581. XCTAssertNotEqualObjects(query, query4);
  582. }
  583. - (void)testCanTraverseCollectionsAndDocuments {
  584. NSString *expected = @"a/b/c/d";
  585. // doc path from root Firestore.
  586. XCTAssertEqualObjects([self.db documentWithPath:@"a/b/c/d"].path, expected);
  587. // collection path from root Firestore.
  588. XCTAssertEqualObjects([[self.db collectionWithPath:@"a/b/c"] documentWithPath:@"d"].path,
  589. expected);
  590. // doc path from CollectionReference.
  591. XCTAssertEqualObjects([[self.db collectionWithPath:@"a"] documentWithPath:@"b/c/d"].path,
  592. expected);
  593. // collection path from DocumentReference.
  594. XCTAssertEqualObjects([[self.db documentWithPath:@"a/b"] collectionWithPath:@"c/d/e"].path,
  595. @"a/b/c/d/e");
  596. }
  597. - (void)testCanTraverseCollectionAndDocumentParents {
  598. FIRCollectionReference *collection = [self.db collectionWithPath:@"a/b/c"];
  599. XCTAssertEqualObjects(collection.path, @"a/b/c");
  600. FIRDocumentReference *doc = collection.parent;
  601. XCTAssertEqualObjects(doc.path, @"a/b");
  602. collection = doc.parent;
  603. XCTAssertEqualObjects(collection.path, @"a");
  604. FIRDocumentReference *nilDoc = collection.parent;
  605. XCTAssertNil(nilDoc);
  606. }
  607. - (void)testUpdateFieldsWithDots {
  608. FIRDocumentReference *doc = [self documentRef];
  609. [self writeDocumentRef:doc data:@{@"a.b" : @"old", @"c.d" : @"old"}];
  610. [self updateDocumentRef:doc data:@{ [[FIRFieldPath alloc] initWithFields:@[ @"a.b" ]] : @"new" }];
  611. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateFieldsWithDots"];
  612. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  613. XCTAssertNil(error);
  614. XCTAssertEqualObjects(snapshot.data, (@{@"a.b" : @"new", @"c.d" : @"old"}));
  615. [expectation fulfill];
  616. }];
  617. [self awaitExpectations];
  618. }
  619. - (void)testUpdateNestedFields {
  620. FIRDocumentReference *doc = [self documentRef];
  621. [self writeDocumentRef:doc
  622. data:@{
  623. @"a" : @{@"b" : @"old"},
  624. @"c" : @{@"d" : @"old"},
  625. @"e" : @{@"f" : @"old"}
  626. }];
  627. [self updateDocumentRef:doc
  628. data:@{
  629. @"a.b" : @"new",
  630. [[FIRFieldPath alloc] initWithFields:@[ @"c", @"d" ]] : @"new"
  631. }];
  632. XCTestExpectation *expectation = [self expectationWithDescription:@"testUpdateNestedFields"];
  633. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  634. XCTAssertNil(error);
  635. XCTAssertEqualObjects(snapshot.data, (@{
  636. @"a" : @{@"b" : @"new"},
  637. @"c" : @{@"d" : @"new"},
  638. @"e" : @{@"f" : @"old"}
  639. }));
  640. [expectation fulfill];
  641. }];
  642. [self awaitExpectations];
  643. }
  644. - (void)testCollectionID {
  645. XCTAssertEqualObjects([self.db collectionWithPath:@"foo"].collectionID, @"foo");
  646. XCTAssertEqualObjects([self.db collectionWithPath:@"foo/bar/baz"].collectionID, @"baz");
  647. }
  648. - (void)testDocumentID {
  649. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar"].documentID, @"bar");
  650. XCTAssertEqualObjects([self.db documentWithPath:@"foo/bar/baz/qux"].documentID, @"qux");
  651. }
  652. - (void)testCanQueueWritesWhileOffline {
  653. XCTestExpectation *writeEpectation = [self expectationWithDescription:@"successfull write"];
  654. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"enable network"];
  655. FIRDocumentReference *doc = [self documentRef];
  656. FIRFirestore *firestore = doc.firestore;
  657. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  658. [firestore disableNetworkWithCompletion:^(NSError *error) {
  659. XCTAssertNil(error);
  660. [doc setData:data
  661. completion:^(NSError *error) {
  662. XCTAssertNil(error);
  663. [writeEpectation fulfill];
  664. }];
  665. [firestore enableNetworkWithCompletion:^(NSError *error) {
  666. XCTAssertNil(error);
  667. [networkExpectation fulfill];
  668. }];
  669. }];
  670. [self awaitExpectations];
  671. XCTestExpectation *getExpectation = [self expectationWithDescription:@"successfull get"];
  672. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  673. XCTAssertNil(error);
  674. XCTAssertEqualObjects(snapshot.data, data);
  675. XCTAssertFalse(snapshot.metadata.isFromCache);
  676. [getExpectation fulfill];
  677. }];
  678. [self awaitExpectations];
  679. }
  680. - (void)testCantGetDocumentsWhileOffline {
  681. FIRDocumentReference *doc = [self documentRef];
  682. FIRFirestore *firestore = doc.firestore;
  683. NSDictionary<NSString *, id> *data = @{@"a" : @"b"};
  684. XCTestExpectation *onlineExpectation = [self expectationWithDescription:@"online read"];
  685. XCTestExpectation *networkExpectation = [self expectationWithDescription:@"network online"];
  686. __weak FIRDocumentReference *weakDoc = doc;
  687. [firestore disableNetworkWithCompletion:^(NSError *error) {
  688. XCTAssertNil(error);
  689. [doc setData:data
  690. completion:^(NSError *_Nullable error) {
  691. XCTAssertNil(error);
  692. [weakDoc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  693. XCTAssertNil(error);
  694. // Verify that we are not reading from cache.
  695. XCTAssertFalse(snapshot.metadata.isFromCache);
  696. [onlineExpectation fulfill];
  697. }];
  698. }];
  699. [doc getDocumentWithCompletion:^(FIRDocumentSnapshot *snapshot, NSError *error) {
  700. XCTAssertNil(error);
  701. // Verify that we are reading from cache.
  702. XCTAssertTrue(snapshot.metadata.fromCache);
  703. XCTAssertEqualObjects(snapshot.data, data);
  704. [firestore enableNetworkWithCompletion:^(NSError *error) {
  705. [networkExpectation fulfill];
  706. }];
  707. }];
  708. }];
  709. [self awaitExpectations];
  710. }
  711. - (void)testWriteStreamReconnectsAfterIdle {
  712. FIRDocumentReference *doc = [self documentRef];
  713. FIRFirestore *firestore = doc.firestore;
  714. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  715. [self waitForIdleFirestore:firestore];
  716. [self writeDocumentRef:doc data:@{@"foo" : @"bar"}];
  717. }
  718. - (void)testWatchStreamReconnectsAfterIdle {
  719. FIRDocumentReference *doc = [self documentRef];
  720. FIRFirestore *firestore = doc.firestore;
  721. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  722. [self waitForIdleFirestore:firestore];
  723. [self readSnapshotForRef:[self documentRef] requireOnline:YES];
  724. }
  725. - (void)testCanDisableNetwork {
  726. FIRDocumentReference *doc = [self documentRef];
  727. FIRFirestore *firestore = doc.firestore;
  728. [firestore enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network"]];
  729. [self awaitExpectations];
  730. [firestore
  731. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Enable network again"]];
  732. [self awaitExpectations];
  733. [firestore
  734. disableNetworkWithCompletion:[self completionForExpectationWithName:@"Disable network"]];
  735. [self awaitExpectations];
  736. [firestore
  737. disableNetworkWithCompletion:[self
  738. completionForExpectationWithName:@"Disable network again"]];
  739. [self awaitExpectations];
  740. [firestore
  741. enableNetworkWithCompletion:[self completionForExpectationWithName:@"Final enable network"]];
  742. [self awaitExpectations];
  743. }
  744. @end