Skip to content

Commit db290ee

Browse files
author
sgr-ksmt
committed
add collection group test.
1 parent d1ea6c9 commit db290ee

File tree

4 files changed

+103
-1
lines changed

4 files changed

+103
-1
lines changed

FireSnapshot.xcodeproj/project.pbxproj

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
1694DF6D234DEC8300B7807A /* BatchTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1694DF6C234DEC8300B7807A /* BatchTests.swift */; };
5353
1694DF6F234DF02E00B7807A /* DocumentTimestampsTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1694DF6E234DF02E00B7807A /* DocumentTimestampsTests.swift */; };
5454
1694DF71234E32DC00B7807A /* ReadTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1694DF70234E32DC00B7807A /* ReadTests.swift */; };
55+
1694DF73234ED43900B7807A /* CollectionGroupTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 1694DF72234ED43900B7807A /* CollectionGroupTests.swift */; };
5556
16B699E4234636DD005877A9 /* CollectionGroup.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16B699E3234636DD005877A9 /* CollectionGroup.swift */; };
5657
16B699E82348CA80005877A9 /* PathTests.swift in Sources */ = {isa = PBXBuildFile; fileRef = 16B699E72348CA80005877A9 /* PathTests.swift */; };
5758
C7AC0728461714610F570CB5 /* Pods_FireSnapshotTests.framework in Frameworks */ = {isa = PBXBuildFile; fileRef = BA9C3F24DAB7C0106A5A122D /* Pods_FireSnapshotTests.framework */; };
@@ -117,6 +118,7 @@
117118
1694DF6C234DEC8300B7807A /* BatchTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BatchTests.swift; sourceTree = "<group>"; };
118119
1694DF6E234DF02E00B7807A /* DocumentTimestampsTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = DocumentTimestampsTests.swift; sourceTree = "<group>"; };
119120
1694DF70234E32DC00B7807A /* ReadTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ReadTests.swift; sourceTree = "<group>"; };
121+
1694DF72234ED43900B7807A /* CollectionGroupTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionGroupTests.swift; sourceTree = "<group>"; };
120122
16B699E3234636DD005877A9 /* CollectionGroup.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = CollectionGroup.swift; sourceTree = "<group>"; };
121123
16B699E72348CA80005877A9 /* PathTests.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = PathTests.swift; sourceTree = "<group>"; };
122124
25B42645485A6D1C6682724A /* Pods-FireSnapshot.debug.xcconfig */ = {isa = PBXFileReference; includeInIndex = 1; lastKnownFileType = text.xcconfig; name = "Pods-FireSnapshot.debug.xcconfig"; path = "Target Support Files/Pods-FireSnapshot/Pods-FireSnapshot.debug.xcconfig"; sourceTree = "<group>"; };
@@ -220,6 +222,7 @@
220222
1694DF6C234DEC8300B7807A /* BatchTests.swift */,
221223
1694DF6E234DF02E00B7807A /* DocumentTimestampsTests.swift */,
222224
1694DF70234E32DC00B7807A /* ReadTests.swift */,
225+
1694DF72234ED43900B7807A /* CollectionGroupTests.swift */,
223226
);
224227
path = FireSnapshotTests;
225228
sourceTree = "<group>";
@@ -485,6 +488,7 @@
485488
isa = PBXSourcesBuildPhase;
486489
buildActionMask = 2147483647;
487490
files = (
491+
1694DF73234ED43900B7807A /* CollectionGroupTests.swift in Sources */,
488492
1694DF6D234DEC8300B7807A /* BatchTests.swift in Sources */,
489493
1694DF67234DD29E00B7807A /* ReferenceTests.swift in Sources */,
490494
1694DF5F234D78E800B7807A /* ReplicatedTests.swift in Sources */,

FireSnapshot/Sources/Snapshot+Read.swift

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ public extension Snapshot {
3939
)
4040
}
4141

42-
static func get(_ collectionGroup: CollectionGroup<Data>,
42+
static func get(collectionGroup: CollectionGroup<Data>,
4343
queryBuilder: QueryBuilder = { $0 },
4444
source: FirestoreSource = .default,
4545
completion: @escaping CollectionReadResultBlock<Data>) {
Lines changed: 93 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
//
2+
// Copyright © Suguru Kishimoto. All rights reserved.
3+
//
4+
5+
import XCTest
6+
import FirebaseFirestore
7+
8+
@testable import FireSnapshot
9+
10+
private extension CollectionPaths {
11+
static let users = CollectionPath<User>("users")
12+
static let tasks = CollectionPath<Task>("tasks")
13+
static func groups(for user: DocumentPath<User>) -> CollectionPath<Group> {
14+
user.collection("groups")
15+
}
16+
17+
static func groups(for task: DocumentPath<Task>) -> CollectionPath<Group> {
18+
task.collection("groups")
19+
}
20+
}
21+
22+
private extension CollectionGroups {
23+
static let groups = CollectionGroup<Group>("groups")
24+
}
25+
26+
private struct User: SnapshotData {
27+
var name: String = "Mike"
28+
}
29+
30+
private struct Task: SnapshotData {
31+
var title: String = "task"
32+
}
33+
34+
private struct Group: SnapshotData {
35+
var name: String = "group"
36+
}
37+
38+
class CollectionGroupTests: XCTestCase {
39+
40+
override func setUp() {
41+
super.setUp()
42+
FirebaseTestHelper.setupFirebaseApp()
43+
}
44+
45+
override func tearDown() {
46+
super.tearDown()
47+
FirebaseTestHelper.deleteFirebaseApp()
48+
}
49+
50+
func testCollectionGroup() {
51+
let exp = expectation(description: #function)
52+
53+
let user = Snapshot(data: .init(), path: .users)
54+
let task = Snapshot(data: .init(), path: .tasks)
55+
56+
let batch = Firestore.firestore().batch()
57+
try! batch.create(user)
58+
try! batch.create(task)
59+
batch.commit { error in
60+
if let error = error {
61+
XCTFail("\(error)")
62+
exp.fulfill()
63+
return
64+
}
65+
let g1 = Snapshot(data: .init(), path: .groups(for: user.path))
66+
g1.name = "from user"
67+
let g2 = Snapshot(data: .init(), path: .groups(for: task.path))
68+
g2.name = "from task"
69+
let batch = Firestore.firestore().batch()
70+
try! batch.create(g1)
71+
try! batch.create(g2)
72+
73+
batch.commit { error in
74+
if let error = error {
75+
XCTFail("\(error)")
76+
exp.fulfill()
77+
return
78+
}
79+
Snapshot.get(collectionGroup: .groups) { result in
80+
switch result {
81+
case let .success(groups):
82+
XCTAssertEqual(groups.count, 2)
83+
exp.fulfill()
84+
case let .failure(error):
85+
XCTFail("\(error)")
86+
exp.fulfill()
87+
}
88+
}
89+
}
90+
}
91+
wait(for: [exp], timeout: 10.0)
92+
}
93+
}

firebase/firestore.rules

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
rules_version = '2';
12
service cloud.firestore {
23
match /databases/{database}/documents {
34
match /mocks/{mockID} {
@@ -15,5 +16,9 @@ service cloud.firestore {
1516
match /tasks/{taskID} {
1617
allow read, write;
1718
}
19+
20+
match /{path=**}/groups/{groupId} {
21+
allow read, write;
22+
}
1823
}
1924
}

0 commit comments

Comments
 (0)