-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFluentSQLiteDriverTests.swift
116 lines (104 loc) · 4.56 KB
/
FluentSQLiteDriverTests.swift
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
116
import FluentBenchmark
import FluentSQLiteDriver
import XCTest
import Logging
final class FluentSQLiteDriverTests: XCTestCase {
func testAll() throws { try self.benchmarker.testAll() }
func testAggregate() throws { try self.benchmarker.testAggregate() }
func testArray() throws { try self.benchmarker.testArray() }
func testBatch() throws { try self.benchmarker.testBatch() }
func testChildren() throws { try self.benchmarker.testChildren() }
func testChunk() throws { try self.benchmarker.testChunk() }
func testCRUD() throws { try self.benchmarker.testCRUD() }
func testEagerLoad() throws { try self.benchmarker.testEagerLoad() }
func testEnum() throws { try self.benchmarker.testEnum() }
func testFilter() throws { try self.benchmarker.testFilter() }
func testGroup() throws { try self.benchmarker.testGroup() }
func testID() throws { try self.benchmarker.testID() }
func testJoin() throws { try self.benchmarker.testJoin() }
func testMiddleware() throws { try self.benchmarker.testMiddleware() }
func testMigrator() throws { try self.benchmarker.testMigrator() }
func testModel() throws { try self.benchmarker.testModel() }
func testOptionalParent() throws { try self.benchmarker.testOptionalParent() }
func testPagination() throws { try self.benchmarker.testPagination() }
func testParent() throws { try self.benchmarker.testParent() }
func testPerformance() throws { try self.benchmarker.testPerformance() }
func testRange() throws { try self.benchmarker.testRange() }
func testSet() throws { try self.benchmarker.testSet() }
func testSiblings() throws { try self.benchmarker.testSiblings() }
func testSoftDelete() throws { try self.benchmarker.testSoftDelete() }
func testSort() throws { try self.benchmarker.testSort() }
func testTimestamp() throws { try self.benchmarker.testTimestamp() }
func testTransaction() throws { try self.benchmarker.testTransaction() }
func testUnique() throws { try self.benchmarker.testUnique() }
func testDatabaseError() throws {
let sql = (self.database as! SQLDatabase)
do {
try sql.raw("asdf").run().wait()
} catch let error as DatabaseError where error.isSyntaxError {
// pass
} catch {
XCTFail("\(error)")
}
do {
try sql.raw("CREATE TABLE foo (name TEXT UNIQUE)").run().wait()
try sql.raw("INSERT INTO foo (name) VALUES ('bar')").run().wait()
try sql.raw("INSERT INTO foo (name) VALUES ('bar')").run().wait()
} catch let error as DatabaseError where error.isConstraintFailure {
// pass
} catch {
XCTFail("\(error)")
}
do {
try (sql as! SQLiteDatabase).withConnection { conn in
conn.close().flatMap {
conn.sql().raw("INSERT INTO foo (name) VALUES ('bar')").run()
}
}.wait()
} catch let error as DatabaseError where error.isConnectionClosed {
// pass
} catch {
XCTFail("\(error)")
}
}
var benchmarker: FluentBenchmarker {
return .init(databases: self.dbs)
}
var database: Database {
self.benchmarker.database
}
var threadPool: NIOThreadPool!
var eventLoopGroup: EventLoopGroup!
var dbs: Databases!
let benchmarkPath = FileManager.default.temporaryDirectory.appendingPathComponent("benchmark.sqlite").absoluteString
override func setUpWithError() throws {
try super.setUpWithError()
XCTAssert(isLoggingConfigured)
self.eventLoopGroup = MultiThreadedEventLoopGroup(numberOfThreads: 1)
self.threadPool = .init(numberOfThreads: 2)
self.threadPool.start()
self.dbs = Databases(threadPool: self.threadPool, on: self.eventLoopGroup)
self.dbs.use(.sqlite(.memory), as: .sqlite)
self.dbs.use(.sqlite(.file(self.benchmarkPath)), as: .benchmark)
}
override func tearDownWithError() throws {
self.dbs.shutdown()
self.dbs = nil
try self.threadPool.syncShutdownGracefully()
self.threadPool = nil
try self.eventLoopGroup.syncShutdownGracefully()
self.eventLoopGroup = nil
try super.tearDownWithError()
}
}
let isLoggingConfigured: Bool = {
LoggingSystem.bootstrap { label in
var handler = StreamLogHandler.standardOutput(label: label)
handler.logLevel = .debug
return handler
}
return true
}()
extension DatabaseID {
static let benchmark = DatabaseID(string: "benchmark")
}