aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/Example/Tests/Integration/FSTSmokeTests.m
blob: 847474a37e35f3526a0e988b63b9ad5d9261eded (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
/*
 * Copyright 2017 Google
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

@import FirebaseFirestore;

#import <XCTest/XCTest.h>

#import "Firestore/Example/Tests/Util/FSTEventAccumulator.h"
#import "Firestore/Example/Tests/Util/FSTIntegrationTestCase.h"

@interface FSTSmokeTests : FSTIntegrationTestCase
@end

@implementation FSTSmokeTests

- (void)testCanWriteASingleDocument {
  FIRDocumentReference *ref = [self documentRef];
  [self writeDocumentRef:ref data:[self chatMessage]];
}

- (void)testCanReadAWrittenDocument {
  NSDictionary<NSString *, id> *data = [self chatMessage];

  FIRDocumentReference *ref = [self documentRef];
  [self writeDocumentRef:ref data:data];

  FIRDocumentSnapshot *doc = [self readDocumentForRef:ref];
  XCTAssertEqualObjects(doc.data, data);
}

- (void)testObservesExistingDocument {
  [self readerAndWriterOnDocumentRef:^(NSString *path, FIRDocumentReference *readerRef,
                                       FIRDocumentReference *writerRef) {
    NSDictionary<NSString *, id> *data = [self chatMessage];
    [self writeDocumentRef:writerRef data:data];

    id<FIRListenerRegistration> listenerRegistration =
        [readerRef addSnapshotListener:self.eventAccumulator.handler];

    FIRDocumentSnapshot *doc = [self.eventAccumulator awaitEventWithName:@"snapshot"];
    XCTAssertEqual([doc class], [FIRDocumentSnapshot class]);
    XCTAssertEqualObjects(doc.data, data);

    [listenerRegistration remove];
  }];
}

- (void)testObservesNewDocument {
  [self readerAndWriterOnDocumentRef:^(NSString *path, FIRDocumentReference *readerRef,
                                       FIRDocumentReference *writerRef) {
    id<FIRListenerRegistration> listenerRegistration =
        [readerRef addSnapshotListener:self.eventAccumulator.handler];

    FIRDocumentSnapshot *doc1 = [self.eventAccumulator awaitEventWithName:@"null snapshot"];
    XCTAssertFalse(doc1.exists);
    // TODO(b/36366944): add tests for doc1.path)

    NSDictionary<NSString *, id> *data = [self chatMessage];
    [self writeDocumentRef:writerRef data:data];

    FIRDocumentSnapshot *doc2 = [self.eventAccumulator awaitEventWithName:@"full snapshot"];
    XCTAssertEqual([doc2 class], [FIRDocumentSnapshot class]);
    XCTAssertEqualObjects(doc2.data, data);

    [listenerRegistration remove];
  }];
}

- (void)testWillFireValueEventsForEmptyCollections {
  FIRCollectionReference *collection = [self.db collectionWithPath:@"empty-collection"];
  id<FIRListenerRegistration> listenerRegistration =
      [collection addSnapshotListener:self.eventAccumulator.handler];

  FIRQuerySnapshot *snap = [self.eventAccumulator awaitEventWithName:@"empty query snapshot"];
  XCTAssertEqual([snap class], [FIRQuerySnapshot class]);
  XCTAssertEqual(snap.count, 0);

  [listenerRegistration remove];
}

- (void)testGetCollectionQuery {
  NSDictionary<NSString *, id> *testDocs = @{
    @"1" : @{@"name" : @"Patryk", @"message" : @"Real data, yo!"},
    @"2" : @{@"name" : @"Gil", @"message" : @"Yep!"},
    @"3" : @{@"name" : @"Jonny", @"message" : @"Back to work!"},
  };

  FIRCollectionReference *docs = [self collectionRefWithDocuments:testDocs];
  FIRQuerySnapshot *result = [self readDocumentSetForRef:docs];
  XCTAssertEqualObjects(FIRQuerySnapshotGetData(result),
                        (@[ testDocs[@"1"], testDocs[@"2"], testDocs[@"3"] ]));
}

// TODO(klimt): This test is disabled because we can't create compound indexes programmatically.
- (void)xtestQueryByFieldAndUseOrderBy {
  NSDictionary<NSString *, id> *testDocs = @{
    @"1" : @{@"sort" : @1, @"filter" : @YES, @"key" : @"1"},
    @"2" : @{@"sort" : @2, @"filter" : @YES, @"key" : @"2"},
    @"3" : @{@"sort" : @2, @"filter" : @YES, @"key" : @"3"},
    @"4" : @{@"sort" : @3, @"filter" : @NO, @"key" : @"4"}
  };

  FIRCollectionReference *coll = [self collectionRefWithDocuments:testDocs];

  FIRQuery *query =
      [[coll queryWhereField:@"filter" isEqualTo:@YES] queryOrderedByField:@"sort" descending:YES];
  FIRQuerySnapshot *result = [self readDocumentSetForRef:query];
  XCTAssertEqualObjects(FIRQuerySnapshotGetData(result),
                        (@[ testDocs[@"2"], testDocs[@"3"], testDocs[@"1"] ]));
}

- (NSDictionary<NSString *, id> *)chatMessage {
  return @{@"name" : @"Patryk", @"message" : @"We are actually writing data!"};
}

@end