Skip to content

Commit db676a6

Browse files
committed
Convert MetricsTests
1 parent 488704c commit db676a6

File tree

1 file changed

+110
-100
lines changed

1 file changed

+110
-100
lines changed

Tests/AppTests/MetricsTests.swift

Lines changed: 110 additions & 100 deletions
Original file line numberDiff line numberDiff line change
@@ -12,150 +12,160 @@
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
1820
import Prometheus
19-
import XCTest
21+
import Testing
2022

2123

22-
class MetricsTests: AppTestCase {
24+
@Suite struct MetricsTests {
2325

24-
func test_basic() async throws {
26+
@Test func basic() async throws {
2527
try await withDependencies {
2628
$0.buildSystem.triggerBuild = { @Sendable _, _, _, _, _, _, _ in
2729
.init(status: .ok, webUrl: "")
2830
}
2931
$0.environment.builderToken = { "builder token" }
3032
$0.environment.gitlabPipelineToken = { "pipeline token" }
3133
} operation: {
32-
// setup - trigger build to increment counter
33-
let versionId = UUID()
34-
do { // save minimal package + version
35-
let p = Package(id: UUID(), url: "1")
36-
try await p.save(on: app.db)
37-
try await Version(id: versionId, package: p, reference: .branch("main")).save(on: app.db)
34+
try await withApp { app in
35+
// setup - trigger build to increment counter
36+
let versionId = UUID()
37+
do { // save minimal package + version
38+
let p = Package(id: UUID(), url: "1")
39+
try await p.save(on: app.db)
40+
try await Version(id: versionId, package: p, reference: .branch("main")).save(on: app.db)
41+
}
42+
try await triggerBuildsUnchecked(on: app.db,
43+
triggers: [
44+
.init(versionId: versionId,
45+
buildPairs: [.init(.macosSpm, .v3)])!
46+
])
47+
48+
// MUT
49+
try await app.test(.GET, "metrics", afterResponse: { res async in
50+
// validation
51+
#expect(res.status == .ok)
52+
let content = res.body.asString()
53+
#expect(content.contains(
54+
#"spi_build_trigger_count{swiftVersion="\#(SwiftVersion.v3)", platform="macos-spm"}"#
55+
), "was:\n\(content)")
56+
})
3857
}
39-
try await triggerBuildsUnchecked(on: app.db,
40-
triggers: [
41-
.init(versionId: versionId,
42-
buildPairs: [.init(.macosSpm, .v3)])!
43-
])
44-
45-
// MUT
46-
try await app.test(.GET, "metrics", afterResponse: { res async in
47-
// validation
48-
XCTAssertEqual(res.status, .ok)
49-
let content = res.body.asString()
50-
XCTAssertTrue(content.contains(
51-
#"spi_build_trigger_count{swiftVersion="\#(SwiftVersion.v3)", platform="macos-spm"}"#
52-
), "was:\n\(content)")
53-
})
5458
}
5559
}
5660

57-
func test_versions_added() async throws {
58-
// setup
59-
let initialAddedBranch = try XCTUnwrap(
60-
AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .branch))
61-
)
62-
let initialAddedTag = try XCTUnwrap(
63-
AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .tag))
64-
)
65-
let initialDeletedBranch = try XCTUnwrap(
66-
AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .branch))
67-
)
68-
let initialDeletedTag = try XCTUnwrap(
69-
AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .tag))
70-
)
71-
let pkg = try await savePackage(on: app.db, "1")
72-
let new = [
73-
try Version(package: pkg, reference: .branch("main")),
74-
try Version(package: pkg, reference: .tag(1, 2, 3)),
75-
try Version(package: pkg, reference: .tag(2, 0, 0)),
76-
]
77-
let del = [
78-
try Version(package: pkg, reference: .branch("main")),
79-
try Version(package: pkg, reference: .tag(1, 0, 0)),
80-
]
81-
try await del.save(on: app.db)
82-
83-
// MUT
84-
try await Analyze.applyVersionDelta(on: app.db,
85-
delta: .init(toAdd: new, toDelete: del))
86-
87-
// validation
88-
XCTAssertEqual(
89-
AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .branch)),
90-
initialAddedBranch + 1
91-
)
92-
XCTAssertEqual(
93-
AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .tag)),
94-
initialAddedTag + 2
95-
)
96-
XCTAssertEqual(
97-
AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .branch)),
98-
initialDeletedBranch + 1
99-
)
100-
XCTAssertEqual(
101-
AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .tag)),
102-
initialDeletedTag + 1
103-
)
61+
@Test func versions_added() async throws {
62+
try await withApp { app in
63+
// setup
64+
let initialAddedBranch = try #require(
65+
AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .branch))
66+
)
67+
let initialAddedTag = try #require(
68+
AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .tag))
69+
)
70+
let initialDeletedBranch = try #require(
71+
AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .branch))
72+
)
73+
let initialDeletedTag = try #require(
74+
AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .tag))
75+
)
76+
let pkg = try await savePackage(on: app.db, "1")
77+
let new = [
78+
try Version(package: pkg, reference: .branch("main")),
79+
try Version(package: pkg, reference: .tag(1, 2, 3)),
80+
try Version(package: pkg, reference: .tag(2, 0, 0)),
81+
]
82+
let del = [
83+
try Version(package: pkg, reference: .branch("main")),
84+
try Version(package: pkg, reference: .tag(1, 0, 0)),
85+
]
86+
try await del.save(on: app.db)
87+
88+
// MUT
89+
try await Analyze.applyVersionDelta(on: app.db,
90+
delta: .init(toAdd: new, toDelete: del))
91+
92+
// validation
93+
#expect(
94+
AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .branch)) == initialAddedBranch + 1
95+
)
96+
#expect(
97+
AppMetrics.analyzeVersionsAddedCount?.get(.versionLabels(kind: .tag)) == initialAddedTag + 2
98+
)
99+
#expect(
100+
AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .branch)) == initialDeletedBranch + 1
101+
)
102+
#expect(
103+
AppMetrics.analyzeVersionsDeletedCount?.get(.versionLabels(kind: .tag)) == initialDeletedTag + 1
104+
)
105+
}
104106
}
105107

106-
func test_reconcileDurationSeconds() async throws {
108+
@Test func reconcileDurationSeconds() async throws {
107109
try await withDependencies {
108110
$0.packageListRepository.fetchPackageList = { @Sendable _ in ["1", "2", "3"].asURLs }
109111
$0.packageListRepository.fetchPackageDenyList = { @Sendable _ in [] }
110112
$0.packageListRepository.fetchCustomCollections = { @Sendable _ in [] }
111113
} operation: {
112-
// MUT
113-
try await reconcile(client: app.client, database: app.db)
114+
try await withApp { app in
115+
// MUT
116+
try await reconcile(client: app.client, database: app.db)
114117

115-
// validation
116-
XCTAssert((AppMetrics.reconcileDurationSeconds?.get()) ?? 0 > 0)
118+
// validation
119+
#expect((AppMetrics.reconcileDurationSeconds?.get()) ?? 0 > 0)
120+
}
117121
}
118122
}
119123

120-
func test_ingestDurationSeconds() async throws {
121-
// setup
122-
let pkg = try await savePackage(on: app.db, "1")
124+
@Test func ingestDurationSeconds() async throws {
125+
try await withApp { app in
126+
// setup
127+
let pkg = try await savePackage(on: app.db, "1")
123128

124-
// MUT
125-
try await Ingestion.ingest(client: app.client, database: app.db, mode: .id(pkg.id!))
129+
// MUT
130+
try await Ingestion.ingest(client: app.client, database: app.db, mode: .id(pkg.id!))
126131

127-
// validation
128-
XCTAssert((AppMetrics.ingestDurationSeconds?.get()) ?? 0 > 0)
132+
// validation
133+
#expect((AppMetrics.ingestDurationSeconds?.get()) ?? 0 > 0)
134+
}
129135
}
130136

131-
func test_analyzeDurationSeconds() async throws {
137+
@Test func analyzeDurationSeconds() async throws {
132138
try await withDependencies {
133139
$0.fileManager.fileExists = { @Sendable _ in true }
134140
} operation: {
135-
// setup
136-
let pkg = try await savePackage(on: app.db, "1")
141+
try await withApp { app in
142+
// setup
143+
let pkg = try await savePackage(on: app.db, "1")
137144

138-
// MUT
139-
try await Analyze.analyze(client: app.client, database: app.db, mode: .id(pkg.id!))
145+
// MUT
146+
try await Analyze.analyze(client: app.client, database: app.db, mode: .id(pkg.id!))
140147

141-
// validation
142-
XCTAssert((AppMetrics.analyzeDurationSeconds?.get()) ?? 0 > 0)
148+
// validation
149+
#expect((AppMetrics.analyzeDurationSeconds?.get()) ?? 0 > 0)
150+
}
143151
}
144152
}
145153

146-
func test_triggerBuildsDurationSeconds() async throws {
154+
@Test func triggerBuildsDurationSeconds() async throws {
147155
try await withDependencies {
148156
$0.environment.allowBuildTriggers = { true }
149157
} operation: {
150-
// setup
151-
let pkg = try await savePackage(on: app.db, "1")
152-
153-
// MUT
154-
try await triggerBuilds(on: app.db, mode: .packageId(pkg.id!, force: true))
155-
156-
// validation
157-
XCTAssert((AppMetrics.buildTriggerDurationSeconds?.get()) ?? 0 > 0)
158-
print(AppMetrics.buildTriggerDurationSeconds!.get())
158+
try await withApp { app in
159+
// setup
160+
let pkg = try await savePackage(on: app.db, "1")
161+
162+
// MUT
163+
try await triggerBuilds(on: app.db, mode: .packageId(pkg.id!, force: true))
164+
165+
// validation
166+
#expect((AppMetrics.buildTriggerDurationSeconds?.get()) ?? 0 > 0)
167+
print(AppMetrics.buildTriggerDurationSeconds!.get())
168+
}
159169
}
160170
}
161171

0 commit comments

Comments
 (0)