-
-
Notifications
You must be signed in to change notification settings - Fork 25
/
Copy pathFluentSQLiteDriverTests.swift
165 lines (149 loc) · 6.5 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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
import FluentBenchmark
import FluentSQLiteDriver
import XCTest
import Logging
import NIO
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 testCodable() throws { try self.benchmarker.testCodable() }
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 testSchema() throws { try self.benchmarker.testSchema() }
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 testSQL() throws { try self.benchmarker.testSQL() }
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)")
}
}
// https://github.com/vapor/fluent-sqlite-driver/issues/62
func testUnsupportedUpdateMigration() throws {
struct UserMigration_v1_0_0: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("users")
.id()
.field("email", .string, .required)
.field("password", .string, .required)
.unique(on: "email")
.create()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("users").delete()
}
}
struct UserMigration_v1_2_0: Migration {
func prepare(on database: Database) -> EventLoopFuture<Void> {
database.schema("users")
.field("apple_id", .string)
.unique(on: "apple_id")
.update()
}
func revert(on database: Database) -> EventLoopFuture<Void> {
database.schema("users")
.deleteField("apple_id")
.update()
}
}
try UserMigration_v1_0_0().prepare(on: self.database).wait()
do {
try UserMigration_v1_2_0().prepare(on: self.database).wait()
try UserMigration_v1_2_0().revert(on: self.database).wait()
} catch {
print(error)
XCTAssertTrue("\(error)".contains("adding columns"))
}
try UserMigration_v1_0_0().revert(on: self.database).wait()
}
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: System.coreCount)
self.threadPool = .init(numberOfThreads: System.coreCount)
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()
}
}
func env(_ name: String) -> String? {
return ProcessInfo.processInfo.environment[name]
}
let isLoggingConfigured: Bool = {
LoggingSystem.bootstrap { label in
var handler = StreamLogHandler.standardOutput(label: label)
handler.logLevel = env("LOG_LEVEL").flatMap { Logger.Level(rawValue: $0) } ?? .debug
return handler
}
return true
}()
extension DatabaseID {
static let benchmark = DatabaseID(string: "benchmark")
}