|
12 | 12 | // See the License for the specific language governing permissions and |
13 | 13 | // limitations under the License. |
14 | 14 |
|
| 15 | +import Foundation |
| 16 | + |
15 | 17 | @testable import App |
16 | 18 |
|
17 | | -import XCTVapor |
| 19 | +import Testing |
18 | 20 |
|
19 | 21 |
|
20 | | -class ProductTests: AppTestCase { |
| 22 | +@Suite struct ProductTests { |
21 | 23 |
|
22 | | - func test_ProductType_Codable() throws { |
| 24 | + @Test func ProductType_Codable() throws { |
23 | 25 | // Ensure ProductType is Codable in a way that's forward compatible with Swift 5.5's Codable synthesis for enums with associated types (SE-0295) |
24 | 26 | let exe: ProductType = .executable |
25 | 27 | let lib: ProductType = .library(.automatic) |
26 | 28 | let test: ProductType = .test |
27 | 29 | do { // encoding |
28 | | - XCTAssertEqual( |
29 | | - String(decoding: try JSONEncoder().encode(exe), as: UTF8.self), |
30 | | - #"{"executable":{}}"# |
| 30 | + #expect( |
| 31 | + String(decoding: try JSONEncoder().encode(exe), as: UTF8.self) == #"{"executable":{}}"# |
31 | 32 | ) |
32 | | - XCTAssertEqual( |
33 | | - String(decoding: try JSONEncoder().encode(lib), as: UTF8.self), |
34 | | - #"{"library":{"_0":"automatic"}}"# |
| 33 | + #expect( |
| 34 | + String(decoding: try JSONEncoder().encode(lib), as: UTF8.self) == #"{"library":{"_0":"automatic"}}"# |
35 | 35 | ) |
36 | | - XCTAssertEqual( |
37 | | - String(decoding: try JSONEncoder().encode(test), as: UTF8.self), |
38 | | - #"{"test":{}}"# |
| 36 | + #expect( |
| 37 | + String(decoding: try JSONEncoder().encode(test), as: UTF8.self) == #"{"test":{}}"# |
39 | 38 | ) |
40 | 39 | } |
41 | 40 | do { // decoding |
42 | | - XCTAssertEqual( |
| 41 | + #expect( |
43 | 42 | try JSONDecoder().decode( |
44 | 43 | ProductType.self, |
45 | | - from: Data(#"{"executable":{}}"#.utf8)), |
46 | | - exe) |
47 | | - XCTAssertEqual( |
| 44 | + from: Data(#"{"executable":{}}"#.utf8)) == exe) |
| 45 | + #expect( |
48 | 46 | try JSONDecoder().decode( |
49 | 47 | ProductType.self, |
50 | | - from: Data(#"{"library":{"_0":"automatic"}}"#.utf8)), |
51 | | - lib |
| 48 | + from: Data(#"{"library":{"_0":"automatic"}}"#.utf8)) == lib |
52 | 49 | ) |
53 | | - XCTAssertEqual( |
| 50 | + #expect( |
54 | 51 | try JSONDecoder().decode( |
55 | 52 | ProductType.self, |
56 | | - from: Data(#"{"test":{}}"#.utf8)), |
57 | | - test) |
| 53 | + from: Data(#"{"test":{}}"#.utf8)) == test) |
58 | 54 | } |
59 | 55 | } |
60 | 56 |
|
61 | | - func test_Product_save() async throws { |
62 | | - let pkg = Package(id: UUID(), url: "1") |
63 | | - let ver = try Version(id: UUID(), package: pkg) |
64 | | - let prod = try Product(id: UUID(), |
65 | | - version: ver, |
66 | | - type: .library(.automatic), |
67 | | - name: "p1", |
68 | | - targets: ["t1", "t2"]) |
69 | | - try await pkg.save(on: app.db) |
70 | | - try await ver.save(on: app.db) |
71 | | - try await prod.save(on: app.db) |
72 | | - do { |
73 | | - let p = try await XCTUnwrapAsync(try await Product.find(prod.id, on: app.db)) |
74 | | - XCTAssertEqual(p.$version.id, ver.id) |
75 | | - XCTAssertEqual(p.type, .library(.automatic)) |
76 | | - XCTAssertEqual(p.name, "p1") |
77 | | - XCTAssertEqual(p.targets, ["t1", "t2"]) |
| 57 | + @Test func Product_save() async throws { |
| 58 | + try await withApp { app in |
| 59 | + let pkg = Package(id: UUID(), url: "1") |
| 60 | + let ver = try Version(id: UUID(), package: pkg) |
| 61 | + let prod = try Product(id: UUID(), |
| 62 | + version: ver, |
| 63 | + type: .library(.automatic), |
| 64 | + name: "p1", |
| 65 | + targets: ["t1", "t2"]) |
| 66 | + try await pkg.save(on: app.db) |
| 67 | + try await ver.save(on: app.db) |
| 68 | + try await prod.save(on: app.db) |
| 69 | + do { |
| 70 | + let p = try #require(try await Product.find(prod.id, on: app.db)) |
| 71 | + #expect(p.$version.id == ver.id) |
| 72 | + #expect(p.type == .library(.automatic)) |
| 73 | + #expect(p.name == "p1") |
| 74 | + #expect(p.targets == ["t1", "t2"]) |
| 75 | + } |
78 | 76 | } |
79 | 77 | } |
80 | 78 |
|
81 | | - func test_delete_cascade() async throws { |
| 79 | + @Test func delete_cascade() async throws { |
82 | 80 | // delete version must delete products |
83 | | - let pkg = Package(id: UUID(), url: "1") |
84 | | - let ver = try Version(id: UUID(), package: pkg) |
85 | | - let prod = try Product(id: UUID(), version: ver, type: .library(.automatic), name: "p1") |
86 | | - try await pkg.save(on: app.db) |
87 | | - try await ver.save(on: app.db) |
88 | | - try await prod.save(on: app.db) |
89 | | - let db = app.db |
| 81 | + try await withApp { app in |
| 82 | + let pkg = Package(id: UUID(), url: "1") |
| 83 | + let ver = try Version(id: UUID(), package: pkg) |
| 84 | + let prod = try Product(id: UUID(), version: ver, type: .library(.automatic), name: "p1") |
| 85 | + try await pkg.save(on: app.db) |
| 86 | + try await ver.save(on: app.db) |
| 87 | + try await prod.save(on: app.db) |
| 88 | + let db = app.db |
90 | 89 |
|
91 | | - try await XCTAssertEqualAsync(try await Package.query(on: db).count(), 1) |
92 | | - try await XCTAssertEqualAsync(try await Version.query(on: db).count(), 1) |
93 | | - try await XCTAssertEqualAsync(try await Product.query(on: db).count(), 1) |
| 90 | + try await XCTAssertEqualAsync(try await Package.query(on: db).count(), 1) |
| 91 | + try await XCTAssertEqualAsync(try await Version.query(on: db).count(), 1) |
| 92 | + try await XCTAssertEqualAsync(try await Product.query(on: db).count(), 1) |
94 | 93 |
|
95 | | - // MUT |
96 | | - try await ver.delete(on: app.db) |
| 94 | + // MUT |
| 95 | + try await ver.delete(on: app.db) |
97 | 96 |
|
98 | | - // version and product should be deleted |
99 | | - try await XCTAssertEqualAsync(try await Package.query(on: db).count(), 1) |
100 | | - try await XCTAssertEqualAsync(try await Version.query(on: db).count(), 0) |
101 | | - try await XCTAssertEqualAsync(try await Product.query(on: db).count(), 0) |
| 97 | + // version and product should be deleted |
| 98 | + try await XCTAssertEqualAsync(try await Package.query(on: db).count(), 1) |
| 99 | + try await XCTAssertEqualAsync(try await Version.query(on: db).count(), 0) |
| 100 | + try await XCTAssertEqualAsync(try await Product.query(on: db).count(), 0) |
| 101 | + } |
102 | 102 | } |
103 | 103 |
|
104 | 104 | } |
0 commit comments