|
14 | 14 |
|
15 | 15 | @testable import App |
16 | 16 |
|
17 | | -import XCTest |
| 17 | +import Testing |
18 | 18 |
|
19 | | -class JoinedTests: AppTestCase { |
| 19 | + |
| 20 | +@Suite struct JoinedTests { |
20 | 21 | typealias JPR = Joined<Package, Repository> |
21 | 22 |
|
22 | | - func test_query_owner_repository() async throws { |
23 | | - // setup |
24 | | - let pkg = Package(url: "1") |
25 | | - try await pkg.save(on: app.db) |
26 | | - try await Repository(package: pkg, name: "bar", owner: "foo") |
27 | | - .save(on: app.db) |
28 | | - do { // inselected package |
29 | | - let pkg = Package(url: "2") |
| 23 | + @Test func query_owner_repository() async throws { |
| 24 | + try await withApp { app in |
| 25 | + // setup |
| 26 | + let pkg = Package(url: "1") |
30 | 27 | try await pkg.save(on: app.db) |
31 | | - try await Repository(package: pkg, name: "bar2", owner: "foo") |
| 28 | + try await Repository(package: pkg, name: "bar", owner: "foo") |
32 | 29 | .save(on: app.db) |
33 | | - } |
| 30 | + do { // inselected package |
| 31 | + let pkg = Package(url: "2") |
| 32 | + try await pkg.save(on: app.db) |
| 33 | + try await Repository(package: pkg, name: "bar2", owner: "foo") |
| 34 | + .save(on: app.db) |
| 35 | + } |
34 | 36 |
|
35 | | - // MUT |
36 | | - let jpr = try await JPR.query(on: app.db, owner: "foo", repository: "bar") |
| 37 | + // MUT |
| 38 | + let jpr = try await JPR.query(on: app.db, owner: "foo", repository: "bar") |
37 | 39 |
|
38 | | - // validate |
39 | | - XCTAssertEqual(jpr.package.id, pkg.id) |
40 | | - XCTAssertEqual(jpr.repository?.owner, "foo") |
41 | | - XCTAssertEqual(jpr.repository?.name, "bar") |
| 40 | + // validate |
| 41 | + #expect(jpr.package.id == pkg.id) |
| 42 | + #expect(jpr.repository?.owner == "foo") |
| 43 | + #expect(jpr.repository?.name == "bar") |
| 44 | + } |
42 | 45 | } |
43 | 46 |
|
44 | | - func test_repository_access() async throws { |
| 47 | + @Test func repository_access() async throws { |
45 | 48 | // Test accessing repository through the join vs through the package relation |
46 | | - // setup |
47 | | - let p = try await savePackage(on: app.db, "1") |
48 | | - try await Repository(package: p).save(on: app.db) |
| 49 | + try await withApp { app in |
| 50 | + // setup |
| 51 | + let p = try await savePackage(on: app.db, "1") |
| 52 | + try await Repository(package: p).save(on: app.db) |
49 | 53 |
|
50 | | - // MUT |
51 | | - let jpr = try await XCTUnwrapAsync(try await JPR.query(on: app.db).first()) |
| 54 | + // MUT |
| 55 | + let jpr = try await XCTUnwrapAsync(try await JPR.query(on: app.db).first()) |
52 | 56 |
|
53 | | - // validate |
54 | | - XCTAssertNotNil(jpr.repository) |
55 | | - // Assert the relationship is not loaded - that's the point of the join |
56 | | - // In particular, this means that |
57 | | - // let repos = jpr.model.repositories |
58 | | - // will fatalError. (This risk has always been there, it's just handled a |
59 | | - // bit better now via `Joined<...>`.) |
60 | | - // There is unfortunately no simple way to make this safe other that replacing/ |
61 | | - // wrapping all of the types involved. |
62 | | - XCTAssertNil(jpr.model.$repositories.value) |
| 57 | + // validate |
| 58 | + #expect(jpr.repository != nil) |
| 59 | + // Assert the relationship is not loaded - that's the point of the join |
| 60 | + // In particular, this means that |
| 61 | + // let repos = jpr.model.repositories |
| 62 | + // will fatalError. (This risk has always been there, it's just handled a |
| 63 | + // bit better now via `Joined<...>`.) |
| 64 | + // There is unfortunately no simple way to make this safe other that replacing/ |
| 65 | + // wrapping all of the types involved. |
| 66 | + #expect(jpr.model.$repositories.value == nil) |
| 67 | + } |
63 | 68 | } |
64 | 69 |
|
65 | | - func test_repository_update() async throws { |
| 70 | + @Test func repository_update() async throws { |
66 | 71 | // Test updating the repository through the join |
67 | | - // setup |
68 | | - let p = try await savePackage(on: app.db, "1") |
69 | | - try await Repository(package: p).save(on: app.db) |
| 72 | + try await withApp { app in |
| 73 | + // setup |
| 74 | + let p = try await savePackage(on: app.db, "1") |
| 75 | + try await Repository(package: p).save(on: app.db) |
70 | 76 |
|
71 | | - let jpr = try await XCTUnwrapAsync(try await JPR.query(on: app.db).first()) |
72 | | - let repo = try XCTUnwrap(jpr.repository) |
73 | | - XCTAssertEqual(repo.name, nil) |
74 | | - repo.name = "foo" |
| 77 | + let jpr = try await XCTUnwrapAsync(try await JPR.query(on: app.db).first()) |
| 78 | + let repo = try #require(jpr.repository) |
| 79 | + #expect(repo.name == nil) |
| 80 | + repo.name = "foo" |
75 | 81 |
|
76 | | - // MUT |
77 | | - try await repo.update(on: app.db) |
| 82 | + // MUT |
| 83 | + try await repo.update(on: app.db) |
78 | 84 |
|
79 | | - // validate |
80 | | - do { // test in-place updates |
81 | | - XCTAssertEqual(repo.name, "foo") |
82 | | - XCTAssertEqual(jpr.repository?.name, "foo") |
83 | | - } |
84 | | - do { // ensure value is persisted |
85 | | - let r = try await XCTUnwrapAsync(try await Repository.query(on: app.db).first()) |
86 | | - XCTAssertEqual(r.name, "foo") |
87 | | - let reloadedJPR = try await XCTUnwrapAsync(try await JPR.query(on: app.db).first()) |
88 | | - XCTAssertEqual(reloadedJPR.repository?.name, "foo") |
| 85 | + // validate |
| 86 | + do { // test in-place updates |
| 87 | + #expect(repo.name == "foo") |
| 88 | + #expect(jpr.repository?.name == "foo") |
| 89 | + } |
| 90 | + do { // ensure value is persisted |
| 91 | + let r = try await XCTUnwrapAsync(try await Repository.query(on: app.db).first()) |
| 92 | + #expect(r.name == "foo") |
| 93 | + let reloadedJPR = try await XCTUnwrapAsync(try await JPR.query(on: app.db).first()) |
| 94 | + #expect(reloadedJPR.repository?.name == "foo") |
| 95 | + } |
89 | 96 | } |
90 | 97 | } |
91 | 98 |
|
|
0 commit comments