FSTSmokeTests.mm 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /*
  2. * Copyright 2017 Google
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #import <FirebaseFirestore/FirebaseFirestore.h>
  17. #import <XCTest/XCTest.h>
  18. #import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
  19. #import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"
  20. @interface FSTSmokeTests : FSTIntegrationTestCase
  21. @end
  22. @implementation FSTSmokeTests
  23. - (void)testCanWriteASingleDocument {
  24. FIRDocumentReference *ref = [self documentRef];
  25. [self writeDocumentRef:ref data:[self chatMessage]];
  26. }
  27. - (void)testCanReadAWrittenDocument {
  28. NSDictionary<NSString *, id> *data = [self chatMessage];
  29. FIRDocumentReference *ref = [self documentRef];
  30. [self writeDocumentRef:ref data:data];
  31. FIRDocumentSnapshot *doc = [self readDocumentForRef:ref];
  32. XCTAssertEqualObjects(doc.data, data);
  33. }
  34. - (void)testObservesExistingDocument {
  35. [self readerAndWriterOnDocumentRef:^(FIRDocumentReference *readerRef,
  36. FIRDocumentReference *writerRef) {
  37. NSDictionary<NSString *, id> *data = [self chatMessage];
  38. [self writeDocumentRef:writerRef data:data];
  39. id<FIRListenerRegistration> listenerRegistration =
  40. [readerRef addSnapshotListener:self.eventAccumulator.valueEventHandler];
  41. FIRDocumentSnapshot *doc = [self.eventAccumulator awaitEventWithName:@"snapshot"];
  42. XCTAssertEqual([doc class], [FIRDocumentSnapshot class]);
  43. XCTAssertEqualObjects(doc.data, data);
  44. [listenerRegistration remove];
  45. }];
  46. }
  47. - (void)testObservesNewDocument {
  48. [self readerAndWriterOnDocumentRef:^(FIRDocumentReference *readerRef,
  49. FIRDocumentReference *writerRef) {
  50. id<FIRListenerRegistration> listenerRegistration =
  51. [readerRef addSnapshotListener:self.eventAccumulator.valueEventHandler];
  52. FIRDocumentSnapshot *doc1 = [self.eventAccumulator awaitEventWithName:@"null snapshot"];
  53. XCTAssertFalse(doc1.exists);
  54. // TODO(b/36366944): add tests for doc1.path)
  55. NSDictionary<NSString *, id> *data = [self chatMessage];
  56. [self writeDocumentRef:writerRef data:data];
  57. FIRDocumentSnapshot *doc2 = [self.eventAccumulator awaitEventWithName:@"full snapshot"];
  58. XCTAssertEqual([doc2 class], [FIRDocumentSnapshot class]);
  59. XCTAssertEqualObjects(doc2.data, data);
  60. [listenerRegistration remove];
  61. }];
  62. }
  63. - (void)testWillFireValueEventsForEmptyCollections {
  64. FIRCollectionReference *collection = [self.db collectionWithPath:@"empty-collection"];
  65. id<FIRListenerRegistration> listenerRegistration =
  66. [collection addSnapshotListener:self.eventAccumulator.valueEventHandler];
  67. FIRQuerySnapshot *snap = [self.eventAccumulator awaitEventWithName:@"empty query snapshot"];
  68. XCTAssertEqual([snap class], [FIRQuerySnapshot class]);
  69. XCTAssertEqual(snap.count, 0);
  70. [listenerRegistration remove];
  71. }
  72. - (void)testGetCollectionQuery {
  73. NSDictionary<NSString *, id> *testDocs = @{
  74. @"1" : @{@"name" : @"Patryk", @"message" : @"Real data, yo!"},
  75. @"2" : @{@"name" : @"Gil", @"message" : @"Yep!"},
  76. @"3" : @{@"name" : @"Jonny", @"message" : @"Back to work!"},
  77. };
  78. FIRCollectionReference *docs = [self collectionRefWithDocuments:testDocs];
  79. FIRQuerySnapshot *result = [self readDocumentSetForRef:docs];
  80. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result),
  81. (@[ testDocs[@"1"], testDocs[@"2"], testDocs[@"3"] ]));
  82. }
  83. // TODO(klimt): This test is disabled because we can't create compound indexes programmatically.
  84. - (void)xtestQueryByFieldAndUseOrderBy {
  85. NSDictionary<NSString *, id> *testDocs = @{
  86. @"1" : @{@"sort" : @1, @"filter" : @YES, @"key" : @"1"},
  87. @"2" : @{@"sort" : @2, @"filter" : @YES, @"key" : @"2"},
  88. @"3" : @{@"sort" : @2, @"filter" : @YES, @"key" : @"3"},
  89. @"4" : @{@"sort" : @3, @"filter" : @NO, @"key" : @"4"}
  90. };
  91. FIRCollectionReference *coll = [self collectionRefWithDocuments:testDocs];
  92. FIRQuery *query = [[coll queryWhereField:@"filter" isEqualTo:@YES] queryOrderedByField:@"sort"
  93. descending:YES];
  94. FIRQuerySnapshot *result = [self readDocumentSetForRef:query];
  95. XCTAssertEqualObjects(FIRQuerySnapshotGetData(result),
  96. (@[ testDocs[@"2"], testDocs[@"3"], testDocs[@"1"] ]));
  97. }
  98. - (NSDictionary<NSString *, id> *)chatMessage {
  99. return @{@"name" : @"Patryk", @"message" : @"We are actually writing data!"};
  100. }
  101. @end