-
-
Notifications
You must be signed in to change notification settings - Fork 119
Expand file tree
/
Copy pathFluentBenchmarker.swift
More file actions
115 lines (103 loc) · 3.42 KB
/
Copy pathFluentBenchmarker.swift
File metadata and controls
115 lines (103 loc) · 3.42 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
import FluentKit
import Foundation
import XCTest
public final class FluentBenchmarker {
public let databases: Databases
public var database: any Database
public init(databases: Databases) {
precondition(databases.ids().count >= 2, "FluentBenchmarker Databases instance must have 2 or more registered databases")
self.databases = databases
self.database = self.databases.database(
logger: .init(label: "codes.vapor.fluent.benchmarker"),
on: self.databases.eventLoopGroup.any()
)!
}
public func testAll() throws {
try self.testAggregate()
try self.testArray()
try self.testBatch()
try self.testChild()
try self.testChildren()
try self.testCodable()
try self.testChunk()
try self.testCompositeID()
try self.testCRUD()
try self.testEagerLoad()
try self.testEnum()
try self.testFilter()
try self.testGroup()
try self.testID()
try self.testJoin()
try self.testMiddleware()
try self.testMigrator()
try self.testModel()
try self.testOptionalParent()
try self.testPagination()
try self.testParent()
try self.testPerformance()
try self.testRange()
try self.testSchema()
try self.testSet()
try self.testSiblings()
try self.testSoftDelete()
try self.testSort()
try self.testSQL()
try self.testTimestamp()
try self.testTransaction()
try self.testUnique()
}
// MARK: Utilities
func runTest(
_ name: String,
_ migrations: [any Migration],
_ test: () throws -> Void
) throws {
try self.runTest(name, migrations, { _ in try test() })
}
func runTest(
_ name: String,
_ migrations: [any Migration],
_ test: (any Database) throws -> Void
) throws {
// This re-initialization is required to make the middleware tests work thanks to ridiculous design flaws
self.database = self.databases.database(
logger: .init(label: "codes.vapor.fluent.benchmarker"),
on: self.databases.eventLoopGroup.any()
)!
try self.runTest(name, migrations, on: self.database, test)
}
func runTest(
_ name: String,
_ migrations: [any Migration],
on database: any Database,
_ test: (any Database) throws -> Void
) throws {
database.logger.notice("Running \(name)...")
// Prepare migrations.
do {
for migration in migrations {
try migration.prepare(on: database).wait()
}
} catch {
database.logger.error("\(name): Error: \(String(reflecting: error))")
throw error
}
let result = Result { try test(database) }
// Revert migrations
do {
for migration in migrations.reversed() {
try migration.revert(on: database).wait()
}
} catch {
// ignore revert errors if the test itself failed
guard case .failure(_) = result else {
database.logger.error("\(name): Error: \(String(reflecting: error))")
throw error
}
}
if case .failure(let error) = result {
database.logger.error("\(name): Error: \(String(reflecting: error))")
throw error
}
}
}