|
15 | 15 | @testable import App |
16 | 16 |
|
17 | 17 | import FluentKit |
18 | | -import XCTest |
| 18 | +import Testing |
19 | 19 |
|
20 | 20 |
|
21 | 21 | /// `JoinedQueryBuilder` is essentially a "pass-through" class that wraps a `QueryBuilder` |
22 | 22 | /// and forwards method calls to it. This test class tests this behaviour in principle for `sort` |
23 | 23 | /// but not for any other methods at this time, because the instrumentation is quite mechanical |
24 | 24 | /// and essentially compiler checked. |
25 | | -class JoinedQueryBuilderTests: AppTestCase { |
| 25 | +@Suite struct JoinedQueryBuilderTests { |
26 | 26 |
|
27 | | - func test_sort() async throws { |
28 | | - // setup |
29 | | - for idx in (0..<3).shuffled() { |
30 | | - try await Package(url: "\(idx)".url).save(on: app.db) |
31 | | - } |
32 | | - // query helper |
33 | | - func query() -> JoinedQueryBuilder<Package> { |
34 | | - JoinedQueryBuilder<Package>( |
35 | | - queryBuilder: Package.query(on: app.db) |
36 | | - ) |
37 | | - } |
38 | | - |
39 | | - do { // test sort(_ sort: DatabaseQuery.Sort) |
40 | | - // MUT |
41 | | - let res = try await query() |
42 | | - .sort(DatabaseQuery.Sort.sort(.sql(unsafeRaw: "url"), .descending)) |
43 | | - .all() |
44 | | - |
45 | | - // validate |
46 | | - XCTAssertEqual(res.map(\.url), ["2", "1", "0"]) |
47 | | - } |
48 | | - |
49 | | - do { // test sort<Field>(_ field: KeyPath<...>, _ direction:) |
50 | | - // MUT |
51 | | - let res = try await query() |
52 | | - .sort(\.$url, .descending) |
53 | | - .all() |
54 | | - |
55 | | - // validate |
56 | | - XCTAssertEqual(res.map(\.url), ["2", "1", "0"]) |
57 | | - } |
58 | | - |
59 | | - do { // test sort(_ field: DatabaseQuery.Field, _ direction:) |
60 | | - // MUT |
61 | | - let res = try await query() |
62 | | - .sort(DatabaseQuery.Field.sql(unsafeRaw: "url"), .descending) |
63 | | - .all() |
64 | | - |
65 | | - // validate |
66 | | - XCTAssertEqual(res.map(\.url), ["2", "1", "0"]) |
| 27 | + @Test func sort() async throws { |
| 28 | + try await withApp { app in |
| 29 | + // setup |
| 30 | + for idx in (0..<3).shuffled() { |
| 31 | + try await Package(url: "\(idx)".url).save(on: app.db) |
| 32 | + } |
| 33 | + // query helper |
| 34 | + func query() -> JoinedQueryBuilder<Package> { |
| 35 | + JoinedQueryBuilder<Package>( |
| 36 | + queryBuilder: Package.query(on: app.db) |
| 37 | + ) |
| 38 | + } |
| 39 | + |
| 40 | + do { // test sort(_ sort: DatabaseQuery.Sort) |
| 41 | + // MUT |
| 42 | + let res = try await query() |
| 43 | + .sort(DatabaseQuery.Sort.sort(.sql(unsafeRaw: "url"), .descending)) |
| 44 | + .all() |
| 45 | + |
| 46 | + // validate |
| 47 | + #expect(res.map(\.url) == ["2", "1", "0"]) |
| 48 | + } |
| 49 | + |
| 50 | + do { // test sort<Field>(_ field: KeyPath<...>, _ direction:) |
| 51 | + // MUT |
| 52 | + let res = try await query() |
| 53 | + .sort(\.$url, .descending) |
| 54 | + .all() |
| 55 | + |
| 56 | + // validate |
| 57 | + #expect(res.map(\.url) == ["2", "1", "0"]) |
| 58 | + } |
| 59 | + |
| 60 | + do { // test sort(_ field: DatabaseQuery.Field, _ direction:) |
| 61 | + // MUT |
| 62 | + let res = try await query() |
| 63 | + .sort(DatabaseQuery.Field.sql(unsafeRaw: "url"), .descending) |
| 64 | + .all() |
| 65 | + |
| 66 | + // validate |
| 67 | + #expect(res.map(\.url) == ["2", "1", "0"]) |
| 68 | + } |
67 | 69 | } |
68 | 70 | } |
69 | 71 |
|
|
0 commit comments