Skip to content

Commit 5f15685

Browse files
committed
Convert HomeIndexModelTests
1 parent 8c123e1 commit 5f15685

File tree

1 file changed

+60
-57
lines changed

1 file changed

+60
-57
lines changed

Tests/AppTests/HomeIndexModelTests.swift

Lines changed: 60 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -12,73 +12,76 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15+
import Foundation
16+
1517
@testable import App
1618

1719
import Dependencies
18-
import XCTVapor
20+
import Testing
1921

2022

21-
class HomeIndexModelTests: AppTestCase {
23+
@Suite struct HomeIndexModelTests {
2224

23-
func test_query() async throws {
24-
// setup
25-
let pkgId = UUID()
26-
let pkg = Package(id: pkgId, url: "1".url)
27-
try await pkg.save(on: app.db)
28-
try await Repository(package: pkg,
29-
name: "1",
30-
owner: "foo").save(on: app.db)
31-
try await App.Version(package: pkg,
32-
commitDate: Date(timeIntervalSince1970: 0),
33-
packageName: "Package",
34-
reference: .tag(.init(1, 2, 3))).save(on: app.db)
35-
try await RecentPackage.refresh(on: app.db)
36-
try await RecentRelease.refresh(on: app.db)
37-
// Sleep for 1ms to ensure we can detect a difference between update times.
38-
try await Task.sleep(nanoseconds: UInt64(1e6))
25+
@Test func query() async throws {
26+
try await withApp { app in
27+
// setup
28+
let pkg = Package(url: "1".url)
29+
try await pkg.save(on: app.db)
30+
try await Repository(package: pkg,
31+
name: "1",
32+
owner: "foo").save(on: app.db)
33+
try await App.Version(package: pkg,
34+
commitDate: .t0,
35+
packageName: "Package",
36+
reference: .tag(.init(1, 2, 3))).save(on: app.db)
37+
try await RecentPackage.refresh(on: app.db)
38+
try await RecentRelease.refresh(on: app.db)
39+
// Sleep for 1ms to ensure we can detect a difference between update times.
40+
try await Task.sleep(nanoseconds: UInt64(1e6))
3941

40-
try await withDependencies {
41-
$0.date.now = .now
42-
} operation: {
43-
// MUT
44-
let m = try await HomeIndex.Model.query(database: app.db)
42+
try await withDependencies {
43+
$0.date.now = .now
44+
} operation: {
45+
// MUT
46+
let m = try await HomeIndex.Model.query(database: app.db)
4547

46-
// validate
47-
let createdAt = try XCTUnwrap(pkg.createdAt)
48+
// validate
49+
let createdAt = try #require(pkg.createdAt)
4850
#if os(Linux)
49-
if m.recentPackages == [
50-
.init(
51-
date: createdAt,
52-
link: .init(label: "Package", url: "/foo/1")
53-
)
54-
] {
55-
logWarning()
56-
// When this triggers, remove Task.sleep above and the validtion below until // TEMPORARY - END
57-
// and replace with original assert:
58-
// XCTAssertEqual(m.recentPackages, [
59-
// .init(
60-
// date: createdAt,
61-
// link: .init(label: "Package", url: "/foo/1")
62-
// )
63-
// ])
64-
}
51+
if m.recentPackages == [
52+
.init(
53+
date: createdAt,
54+
link: .init(label: "Package", url: "/foo/1")
55+
)
56+
] {
57+
logWarning()
58+
// When this triggers, remove Task.sleep above and the validtion below until // TEMPORARY - END
59+
// and replace with original assert:
60+
// #expect(m.recentPackages == [
61+
// .init(
62+
// date: createdAt,
63+
// link: .init(label: "Package", url: "/foo/1")
64+
// )
65+
// ])
66+
}
6567
#endif
66-
XCTAssertEqual(m.recentPackages.count, 1)
67-
let recent = try XCTUnwrap(m.recentPackages.first)
68-
// Comaring the dates directly fails due to tiny rounding differences with the new swift-foundation types on Linux
69-
// E.g.
70-
// 1724071056.5824609
71-
// 1724071056.5824614
72-
// By testing only to accuracy 10e-5 and delaying by 10e-3 we ensure we properly detect if the value was changed.
73-
XCTAssertEqual(recent.date.timeIntervalSince1970, createdAt.timeIntervalSince1970, accuracy: 10e-5)
74-
XCTAssertEqual(recent.link, .init(label: "Package", url: "/foo/1"))
75-
XCTAssertEqual(m.recentReleases, [
76-
.init(packageName: "Package",
77-
version: "1.2.3",
78-
date: "\(date: Date(timeIntervalSince1970: 0), relativeTo: Date.now)",
79-
url: "/foo/1"),
80-
])
81-
// TEMPORARY - END
68+
#expect(m.recentPackages.count == 1)
69+
let recent = try #require(m.recentPackages.first)
70+
// Comaring the dates directly fails due to tiny rounding differences with the new swift-foundation types on Linux
71+
// E.g.
72+
// 1724071056.5824609
73+
// 1724071056.5824614
74+
// By testing only to accuracy 10e-5 and delaying by 10e-3 we ensure we properly detect if the value was changed.
75+
#expect(fabs(recent.date.timeIntervalSince1970 - createdAt.timeIntervalSince1970) <= 10e-5)
76+
#expect(recent.link == .init(label: "Package", url: "/foo/1"))
77+
#expect(m.recentReleases == [
78+
.init(packageName: "Package",
79+
version: "1.2.3",
80+
date: "\(date: Date(timeIntervalSince1970: 0), relativeTo: Date.now)",
81+
url: "/foo/1"),
82+
])
83+
// TEMPORARY - END
84+
}
8285
}
8386
}
8487

0 commit comments

Comments
 (0)