Skip to content

Commit d6278ad

Browse files
Merge pull request #3688 from SwiftPackageIndex/issue-3655-swift-testing-part-11
Issue 3655 swift testing part 11
2 parents a1f489f + e4c7939 commit d6278ad

10 files changed

+778
-726
lines changed

Tests/AppTests/CustomCollectionControllerTests.swift

Lines changed: 82 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -12,111 +12,118 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import XCTest
16-
1715
@testable import App
1816

1917
import Dependencies
2018
import Fluent
19+
import Testing
2120
import Vapor
2221

2322

24-
class CustomCollectionControllerTests: AppTestCase {
25-
26-
func test_query() async throws {
27-
// setup
28-
try await CustomCollection.save(
29-
on: app.db,
30-
key: "list",
31-
name: "List",
32-
url: "https://github.com/foo/bar/list.json",
33-
packages: [( id: .id0, url: "https://github.com/foo/1", owner: "foo", name: "1" )]
34-
)
35-
36-
// MUT
37-
let page = try await CustomCollectionsController.query(on: app.db,
38-
key: "list",
39-
page: 1,
40-
pageSize: 10)
23+
@Suite struct CustomCollectionControllerTests {
4124

42-
// validation
43-
XCTAssertEqual(page.results.map(\.repository.name), ["1"])
44-
XCTAssertEqual(page.hasMoreResults, false)
45-
}
25+
@Test func query() async throws {
26+
try await withApp { app in
27+
// setup
28+
try await CustomCollection.save(
29+
on: app.db,
30+
key: "list",
31+
name: "List",
32+
url: "https://github.com/foo/bar/list.json",
33+
packages: [( id: .id0, url: "https://github.com/foo/1", owner: "foo", name: "1" )]
34+
)
4635

47-
func test_query_pagination() async throws {
48-
// setup
49-
let pkgInfo = [UUID.id0, .id1, .id2, .id3, .id4].enumerated().shuffled().map { (idx, id) in
50-
(id, URL(string: "https://github.com/foo/\(idx)")!, "foo", "\(idx)")
51-
}
52-
try await CustomCollection.save(
53-
on: app.db,
54-
key: "list",
55-
name: "List",
56-
url: "https://github.com/foo/bar/list.json",
57-
packages: pkgInfo
58-
)
59-
60-
do { // first page
61-
// MUT
36+
// MUT
6237
let page = try await CustomCollectionsController.query(on: app.db,
6338
key: "list",
6439
page: 1,
65-
pageSize: 2)
66-
// validate
67-
XCTAssertEqual(page.results.map(\.repository.name), ["0", "1"])
68-
XCTAssertEqual(page.hasMoreResults, true)
69-
}
70-
71-
do { // second page
72-
// MUT
73-
let page = try await CustomCollectionsController.query(on: app.db,
74-
key: "list",
75-
page: 2,
76-
pageSize: 2)
77-
// validate
78-
XCTAssertEqual(page.results.map(\.repository.name), ["2", "3"])
79-
XCTAssertEqual(page.hasMoreResults, true)
80-
}
40+
pageSize: 10)
8141

82-
do { // third page
83-
// MUT
84-
let page = try await CustomCollectionsController.query(on: app.db,
85-
key: "list",
86-
page: 3,
87-
pageSize: 2)
88-
// validate
89-
XCTAssertEqual(page.results.map(\.repository.name), ["4"])
90-
XCTAssertEqual(page.hasMoreResults, false)
42+
// validation
43+
#expect(page.results.map(\.repository.name) == ["1"])
44+
#expect(page.hasMoreResults == false)
9145
}
9246
}
9347

94-
func test_show_collection() async throws {
95-
try await withDependencies {
96-
$0.environment.dbId = { nil }
97-
} operation: {
48+
@Test func query_pagination() async throws {
49+
try await withApp { app in
50+
// setup
51+
let pkgInfo = [UUID.id0, .id1, .id2, .id3, .id4].enumerated().shuffled().map { (idx, id) in
52+
(id, URL(string: "https://github.com/foo/\(idx)")!, "foo", "\(idx)")
53+
}
9854
try await CustomCollection.save(
9955
on: app.db,
10056
key: "list",
10157
name: "List",
10258
url: "https://github.com/foo/bar/list.json",
103-
packages: [( id: .id0, url: "https://github.com/foo/1", owner: "foo", name: "1" )]
59+
packages: pkgInfo
10460
)
10561

106-
// MUT
107-
try await app.test(.GET, "/collections/list") { req async in
62+
do { // first page
63+
// MUT
64+
let page = try await CustomCollectionsController.query(on: app.db,
65+
key: "list",
66+
page: 1,
67+
pageSize: 2)
68+
// validate
69+
#expect(page.results.map(\.repository.name) == ["0", "1"])
70+
#expect(page.hasMoreResults == true)
71+
}
72+
73+
do { // second page
74+
// MUT
75+
let page = try await CustomCollectionsController.query(on: app.db,
76+
key: "list",
77+
page: 2,
78+
pageSize: 2)
79+
// validate
80+
#expect(page.results.map(\.repository.name) == ["2", "3"])
81+
#expect(page.hasMoreResults == true)
82+
}
83+
84+
do { // third page
85+
// MUT
86+
let page = try await CustomCollectionsController.query(on: app.db,
87+
key: "list",
88+
page: 3,
89+
pageSize: 2)
10890
// validate
109-
XCTAssertEqual(req.status, .ok)
91+
#expect(page.results.map(\.repository.name) == ["4"])
92+
#expect(page.hasMoreResults == false)
93+
}
94+
}
95+
}
96+
97+
@Test func show_collection() async throws {
98+
try await withDependencies {
99+
$0.environment.dbId = { nil }
100+
} operation: {
101+
try await withApp { app in
102+
try await CustomCollection.save(
103+
on: app.db,
104+
key: "list",
105+
name: "List",
106+
url: "https://github.com/foo/bar/list.json",
107+
packages: [( id: .id0, url: "https://github.com/foo/1", owner: "foo", name: "1" )]
108+
)
109+
110+
// MUT
111+
try await app.test(.GET, "/collections/list") { req async in
112+
// validate
113+
#expect(req.status == .ok)
114+
}
110115
}
111116
}
112117
}
113118

114-
func test_not_found() throws {
115-
try withDependencies {
119+
@Test func not_found() async throws {
120+
try await withDependencies {
116121
$0.environment.dbId = { nil }
117122
} operation: {
118-
try app.test(.GET, "/collections/list") {
119-
XCTAssertEqual($0.status, .notFound)
123+
try await withApp { app in
124+
try await app.test(.GET, "/collections/list") { res async in
125+
#expect(res.status == .notFound)
126+
}
120127
}
121128
}
122129
}

0 commit comments

Comments
 (0)