|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | | -import XCTest |
16 | | - |
17 | 15 | @testable import App |
18 | 16 |
|
19 | 17 | import Dependencies |
| 18 | +import Testing |
20 | 19 | import Vapor |
21 | 20 |
|
22 | 21 |
|
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 { |
30 | 23 |
|
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) |
37 | 30 |
|
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") |
42 | 33 |
|
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 { |
48 | 34 | // validate |
49 | | - XCTAssertEqual(error.status, .notFound) |
50 | | - } catch { |
51 | | - XCTFail("Unexpected error: \(error)") |
| 35 | + #expect(pkg.map(\.model.id) == [p.id]) |
52 | 36 | } |
53 | 37 | } |
54 | 38 |
|
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") |
60 | 43 | 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 | + } |
62 | 55 | } |
| 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 | + } |
63 | 67 |
|
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") |
66 | 70 |
|
67 | | - // validate |
68 | | - XCTAssertEqual(pkg.map(\.model.url), ["alpha", "beta", "gamma"]) |
| 71 | + // validate |
| 72 | + #expect(pkg.map(\.model.url) == ["alpha", "beta", "gamma"]) |
| 73 | + } |
69 | 74 | } |
70 | 75 |
|
71 | | - func test_show_owner() async throws { |
| 76 | + @Test func show_owner() async throws { |
72 | 77 | try await withDependencies { |
73 | 78 | $0.environment.dbId = { nil } |
74 | 79 | } 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 | + } |
83 | 90 | } |
84 | 91 | } |
85 | 92 |
|
86 | | - func test_show_owner_empty() async throws { |
| 93 | + @Test func show_owner_empty() async throws { |
87 | 94 | try await withDependencies { |
88 | 95 | $0.environment.dbId = { nil } |
89 | 96 | } 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 | + } |
98 | 107 | } |
99 | 108 | } |
100 | 109 |
|
|
0 commit comments