|
1 | 1 | @testable import App
|
2 | 2 | import XCTVapor
|
| 3 | +import Testing |
3 | 4 | {{#fluent}}import Fluent
|
4 | 5 | {{/fluent}}
|
5 | 6 |
|
6 |
| -final class AppTests: XCTestCase { |
7 |
| - var app: Application! |
8 |
| - |
9 |
| - override func setUp() async throws { |
10 |
| - self.app = try await Application.make(.testing) |
11 |
| - try await configure(app){{#fluent}} |
12 |
| - try await app.autoMigrate(){{/fluent}} |
13 |
| - } |
14 |
| - |
15 |
| - override func tearDown() async throws { {{#fluent}} |
16 |
| - try await app.autoRevert(){{/fluent}} |
17 |
| - try await self.app.asyncShutdown() |
18 |
| - self.app = nil |
| 7 | +{{#fluent}}@Suite("App Tests with DB", .serialized) |
| 8 | +{{/fluent}}{{^fluent}}@Suite("App Tests") |
| 9 | +{{/fluent}} |
| 10 | +struct AppTests { |
| 11 | + private func withApp(_ test: (Application) async throws -> ()) async throws { |
| 12 | + let app = try await Application.make(.testing) |
| 13 | + try await configure(app) |
| 14 | + {{#fluent}}try await app.autoMigrate() |
| 15 | +{{/fluent}} try await test(app) |
| 16 | + {{#fluent}}try await app.autoRevert() |
| 17 | +{{/fluent}} try await app.asyncShutdown() |
19 | 18 | }
|
20 | 19 |
|
21 |
| - func testHelloWorld() async throws { |
22 |
| - try await self.app.test(.GET, "hello", afterResponse: { res async in |
23 |
| - XCTAssertEqual(res.status, .ok) |
24 |
| - XCTAssertEqual(res.body.string, "Hello, world!") |
25 |
| - }) |
| 20 | + @Test("Test Hello World Route") |
| 21 | + func helloWorld() async throws { |
| 22 | + try await withApp { app in |
| 23 | + try await app.test(.GET, "hello", afterResponse: { res async in |
| 24 | + #expect(res.status == .ok) |
| 25 | + #expect(res.body.string == "Hello, world!") |
| 26 | + }) |
| 27 | + } |
26 | 28 | }{{#fluent}}
|
27 | 29 |
|
28 |
| - func testTodoIndex() async throws { |
29 |
| - let sampleTodos = [Todo(title: "sample1"), Todo(title: "sample2")] |
30 |
| - try await sampleTodos.create(on: self.app.db) |
31 |
| - |
32 |
| - try await self.app.test(.GET, "todos", afterResponse: { res async throws in |
33 |
| - XCTAssertEqual(res.status, .ok) |
34 |
| - XCTAssertEqual( |
35 |
| - try res.content.decode([TodoDTO].self).sorted(by: { $0.title ?? "" < $1.title ?? "" }), |
36 |
| - sampleTodos.map { $0.toDTO() }.sorted(by: { $0.title ?? "" < $1.title ?? "" }) |
37 |
| - ) |
38 |
| - }) |
| 30 | + @Test("Getting all the Todos") |
| 31 | + func getAllTodos() async throws { |
| 32 | + try await withApp { app in |
| 33 | + let sampleTodos = [Todo(title: "sample1"), Todo(title: "sample2")] |
| 34 | + try await sampleTodos.create(on: app.db) |
| 35 | + |
| 36 | + try await app.test(.GET, "todos", afterResponse: { res async throws in |
| 37 | + #expect(res.status == .ok) |
| 38 | + #expect(try res.content.decode([TodoDTO].self) == sampleTodos.map { $0.toDTO()} ) |
| 39 | + }) |
| 40 | + } |
39 | 41 | }
|
40 | 42 |
|
41 |
| - func testTodoCreate() async throws { |
| 43 | + @Test("Creating a Todo") |
| 44 | + func createTodo() async throws { |
42 | 45 | let newDTO = TodoDTO(id: nil, title: "test")
|
43 | 46 |
|
44 |
| - try await self.app.test(.POST, "todos", beforeRequest: { req in |
45 |
| - try req.content.encode(newDTO) |
46 |
| - }, afterResponse: { res async throws in |
47 |
| - XCTAssertEqual(res.status, .ok) |
48 |
| - let models = try await Todo.query(on: self.app.db).all() |
49 |
| - XCTAssertEqual(models.map { $0.toDTO().title }, [newDTO.title]) |
50 |
| - }) |
| 47 | + try await withApp { app in |
| 48 | + try await app.test(.POST, "todos", beforeRequest: { req in |
| 49 | + try req.content.encode(newDTO) |
| 50 | + }, afterResponse: { res async throws in |
| 51 | + #expect(res.status == .ok) |
| 52 | + let models = try await Todo.query(on: app.db).all() |
| 53 | + #expect(models.map({ $0.toDTO().title }) == [newDTO.title]) |
| 54 | + XCTAssertEqual(models.map { $0.toDTO() }, [newDTO]) |
| 55 | + }) |
| 56 | + } |
51 | 57 | }
|
52 | 58 |
|
53 |
| - func testTodoDelete() async throws { |
| 59 | + @Test("Deleting a Todo") |
| 60 | + func deleteTodo() async throws { |
54 | 61 | let testTodos = [Todo(title: "test1"), Todo(title: "test2")]
|
55 |
| - try await testTodos.create(on: app.db) |
56 | 62 |
|
57 |
| - try await self.app.test(.DELETE, "todos/\(testTodos[0].requireID())", afterResponse: { res async throws in |
58 |
| - XCTAssertEqual(res.status, .noContent) |
59 |
| - let model = try await Todo.find(testTodos[0].id, on: self.app.db) |
60 |
| - XCTAssertNil(model) |
61 |
| - }) |
| 63 | + try await withApp { app in |
| 64 | + try await testTodos.create(on: app.db) |
| 65 | + |
| 66 | + try await app.test(.DELETE, "todos/\(testTodos[0].requireID())", afterResponse: { res async throws in |
| 67 | + #expect(res.status == .noContent) |
| 68 | + let model = try await Todo.find(testTodos[0].id, on: app.db) |
| 69 | + #expect(model == nil) |
| 70 | + }) |
| 71 | + } |
62 | 72 | }{{/fluent}}
|
63 | 73 | }
|
64 | 74 | {{#fluent}}
|
|
0 commit comments