Skip to content

Commit ceb6e43

Browse files
committed
Convert AuthorControllerTests
1 parent 720141c commit ceb6e43

File tree

1 file changed

+64
-55
lines changed

1 file changed

+64
-55
lines changed

Tests/AppTests/AuthorControllerTests.swift

Lines changed: 64 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -12,89 +12,98 @@
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
18+
import Testing
2019
import Vapor
2120

2221

23-
class AuthorControllerTests: AppTestCase {
24-
25-
func test_query() async throws {
26-
// setup
27-
let p = try await savePackage(on: app.db, "1")
28-
try await Repository(package: p, owner: "owner").save(on: app.db)
29-
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
22+
@Suite struct AuthorControllerTests {
3023

31-
// MUT
32-
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
33-
34-
// validate
35-
XCTAssertEqual(pkg.map(\.model.id), [p.id])
36-
}
24+
@Test func query() async throws {
25+
try await withApp { app in
26+
// setup
27+
let p = try await savePackage(on: app.db, "1")
28+
try await Repository(package: p, owner: "owner").save(on: app.db)
29+
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
3730

38-
func test_query_no_version() async throws {
39-
// setup
40-
let p = try await savePackage(on: app.db, "1")
41-
try await Repository(package: p, owner: "owner").save(on: app.db)
31+
// MUT
32+
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
4233

43-
// MUT
44-
do {
45-
_ = try await AuthorController.query(on: app.db, owner: "owner")
46-
XCTFail("Expected Abort.notFound")
47-
} catch let error as Abort {
4834
// validate
49-
XCTAssertEqual(error.status, .notFound)
50-
} catch {
51-
XCTFail("Unexpected error: \(error)")
35+
#expect(pkg.map(\.model.id) == [p.id])
5236
}
5337
}
5438

55-
func test_query_sort_alphabetically() async throws {
56-
// setup
57-
for packageName in ["gamma", "alpha", "beta"] {
58-
let p = Package(url: "\(packageName)".url)
59-
try await p.save(on: app.db)
39+
@Test func query_no_version() async throws {
40+
try await withApp { app in
41+
// setup
42+
let p = try await savePackage(on: app.db, "1")
6043
try await Repository(package: p, owner: "owner").save(on: app.db)
61-
try await Version(package: p, latest: .defaultBranch, packageName: packageName).save(on: app.db)
44+
45+
// MUT
46+
do {
47+
_ = try await AuthorController.query(on: app.db, owner: "owner")
48+
Issue.record("Expected Abort.notFound")
49+
} catch let error as Abort {
50+
// validate
51+
#expect(error.status == .notFound)
52+
} catch {
53+
Issue.record("Unexpected error: \(error)")
54+
}
6255
}
56+
}
57+
58+
@Test func query_sort_alphabetically() async throws {
59+
try await withApp { app in
60+
// setup
61+
for packageName in ["gamma", "alpha", "beta"] {
62+
let p = Package(url: "\(packageName)".url)
63+
try await p.save(on: app.db)
64+
try await Repository(package: p, owner: "owner").save(on: app.db)
65+
try await Version(package: p, latest: .defaultBranch, packageName: packageName).save(on: app.db)
66+
}
6367

64-
// MUT
65-
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
68+
// MUT
69+
let pkg = try await AuthorController.query(on: app.db, owner: "owner")
6670

67-
// validate
68-
XCTAssertEqual(pkg.map(\.model.url), ["alpha", "beta", "gamma"])
71+
// validate
72+
#expect(pkg.map(\.model.url) == ["alpha", "beta", "gamma"])
73+
}
6974
}
7075

71-
func test_show_owner() async throws {
76+
@Test func show_owner() async throws {
7277
try await withDependencies {
7378
$0.environment.dbId = { nil }
7479
} operation: {
75-
let p = try await savePackage(on: app.db, "1")
76-
try await Repository(package: p, owner: "owner").save(on: app.db)
77-
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
78-
79-
// MUT
80-
try await app.test(.GET, "/owner", afterResponse: { response async in
81-
XCTAssertEqual(response.status, .ok)
82-
})
80+
try await withApp { app in
81+
let p = try await savePackage(on: app.db, "1")
82+
try await Repository(package: p, owner: "owner").save(on: app.db)
83+
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
84+
85+
// MUT
86+
try await app.test(.GET, "/owner", afterResponse: { response async in
87+
#expect(response.status == .ok)
88+
})
89+
}
8390
}
8491
}
8592

86-
func test_show_owner_empty() async throws {
93+
@Test func show_owner_empty() async throws {
8794
try await withDependencies {
8895
$0.environment.dbId = { nil }
8996
} operation: {
90-
let p = try await savePackage(on: app.db, "1")
91-
try await Repository(package: p, owner: "owner").save(on: app.db)
92-
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
93-
94-
// MUT
95-
try await app.test(.GET, "/fake-owner", afterResponse: { response async in
96-
XCTAssertEqual(response.status, .notFound)
97-
})
97+
try await withApp { app in
98+
let p = try await savePackage(on: app.db, "1")
99+
try await Repository(package: p, owner: "owner").save(on: app.db)
100+
try await Version(package: p, latest: .defaultBranch).save(on: app.db)
101+
102+
// MUT
103+
try await app.test(.GET, "/fake-owner", afterResponse: { response async in
104+
#expect(response.status == .notFound)
105+
})
106+
}
98107
}
99108
}
100109

0 commit comments

Comments
 (0)