-
-
Notifications
You must be signed in to change notification settings - Fork 54
/
Copy pathFluentPostgresDriverTests.swift
416 lines (355 loc) · 16.4 KB
/
FluentPostgresDriverTests.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
import Logging
import FluentKit
import FluentBenchmark
import FluentPostgresDriver
import XCTest
import PostgresKit
import SQLKit
func XCTAssertThrowsErrorAsync<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath, line: UInt = #line,
_ callback: (any Error) -> Void = { _ in }
) async {
do {
_ = try await expression()
XCTAssertThrowsError({}(), message(), file: file, line: line, callback)
} catch {
XCTAssertThrowsError(try { throw error }(), message(), file: file, line: line, callback)
}
}
func XCTAssertNoThrowAsync<T>(
_ expression: @autoclosure () async throws -> T,
_ message: @autoclosure () -> String = "",
file: StaticString = #filePath, line: UInt = #line
) async {
do {
_ = try await expression()
} catch {
XCTAssertNoThrow(try { throw error }(), message(), file: file, line: line)
}
}
final class FluentPostgresDriverTests: XCTestCase {
func testAggregate() throws { try self.benchmarker.testAggregate() }
func testArray() throws { try self.benchmarker.testArray() }
func testBatch() throws { try self.benchmarker.testBatch() }
func testChild() throws { try self.benchmarker.testChildren() }
func testChildren() throws { try self.benchmarker.testChildren() }
func testChunk() throws { try self.benchmarker.testChunk() }
func testCodable() throws { try self.benchmarker.testCodable() }
func testCompositeID() throws { try self.benchmarker.testCompositeID() }
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() async throws {
let sql1 = (self.db as! any SQLDatabase)
await XCTAssertThrowsErrorAsync(try await sql1.raw("asdf").run()) {
XCTAssertTrue(($0 as? any DatabaseError)?.isSyntaxError ?? false, "\(String(reflecting: $0))")
XCTAssertFalse(($0 as? any DatabaseError)?.isConstraintFailure ?? true, "\(String(reflecting: $0))")
XCTAssertFalse(($0 as? any DatabaseError)?.isConnectionClosed ?? true, "\(String(reflecting: $0))")
}
let sql2 = (self.dbs.database(.a, logger: .init(label: "test.fluent.a"), on: self.eventLoopGroup.any())!) as! any SQLDatabase
try await sql2.drop(table: "foo").ifExists().run()
try await sql2.create(table: "foo").column("name", type: .text, .unique).run()
try await sql2.insert(into: "foo").columns("name").values("bar").run()
await XCTAssertThrowsErrorAsync(try await sql2.insert(into: "foo").columns("name").values("bar").run()) {
XCTAssertTrue(($0 as? any DatabaseError)?.isConstraintFailure ?? false, "\(String(reflecting: $0))")
XCTAssertFalse(($0 as? any DatabaseError)?.isSyntaxError ?? true, "\(String(reflecting: $0))")
XCTAssertFalse(($0 as? any DatabaseError)?.isConnectionClosed ?? true, "\(String(reflecting: $0))")
}
// Disabled until we figure out why it hangs instead of throwing an error.
//let postgres = (self.dbs.database(.a, logger: .init(label: "test.fluent.a"), on: self.eventLoopGroup.any())!) as! any PostgresDatabase
//await XCTAssertThrowsErrorAsync(try await postgres.withConnection { conn in
// conn.close().flatMap {
// conn.sql().insert(into: "foo").columns("name").values("bar").run()
// }
//}.get()) {
// XCTAssertTrue(($0 as? any DatabaseError)?.isConnectionClosed ?? false, "\(String(reflecting: $0))")
// XCTAssertFalse(($0 as? any DatabaseError)?.isSyntaxError ?? true, "\(String(reflecting: $0))")
// XCTAssertFalse(($0 as? any DatabaseError)?.isConstraintFailure ?? true, "\(String(reflecting: $0))")
//}
}
func testBlob() async throws {
struct CreateFoo: AsyncMigration {
func prepare(on database: any Database) async throws {
try await database.schema("foos")
.field("id", .int, .identifier(auto: true))
.field("data", .data, .required)
.create()
}
func revert(on database: any Database) async throws {
try await database.schema("foos").delete()
}
}
try await CreateFoo().prepare(on: self.db)
try await CreateFoo().revert(on: self.db)
}
func testSaveModelWithBool() async throws {
final class Organization: Model, @unchecked Sendable {
static let schema = "orgs"
@ID(custom: "id", generatedBy: .database) var id: Int?
@Field(key: "disabled") var disabled: Bool
init() {}
}
struct CreateOrganization: AsyncMigration {
func prepare(on database: any Database) async throws {
try await database.schema("orgs")
.field("id", .int, .identifier(auto: true))
.field("disabled", .bool, .required)
.create()
}
func revert(on database: any Database) async throws {
try await database.schema("orgs").delete()
}
}
try await CreateOrganization().prepare(on: self.db)
do {
let new = Organization()
new.disabled = false
try await new.save(on: self.db)
} catch {
try? await CreateOrganization().revert(on: self.db)
throw error
}
try await CreateOrganization().revert(on: self.db)
}
func testCustomJSON() async throws {
let jsonEncoder = JSONEncoder()
jsonEncoder.dateEncodingStrategy = .iso8601
let jsonDecoder = JSONDecoder()
jsonDecoder.dateDecodingStrategy = .iso8601
self.dbs.use(.testPostgres(subconfig: "A",
encodingContext: .init(jsonEncoder: jsonEncoder),
decodingContext: .init(jsonDecoder: jsonDecoder)
), as: .iso8601)
let db = self.dbs.database(
.iso8601,
logger: .init(label: "test"),
on: self.eventLoopGroup.any()
)!
try await EventMigration().prepare(on: db)
do {
let date = Date()
let event = Event()
event.id = 1
event.metadata = Metadata(createdAt: date)
try await event.save(on: db)
let rows = try await EventStringlyTyped.query(on: db).filter(\.$id == 1).all()
let expected = ISO8601DateFormatter().string(from: date)
XCTAssertEqual(rows[0].metadata["createdAt"], expected)
} catch {
try? await EventMigration().revert(on: db)
throw error
}
try await EventMigration().revert(on: db)
}
func testEnumAddingMultipleCases() async throws {
try await EnumMigration().prepare(on: self.db)
do {
try await EventWithFooMigration().prepare(on: self.db)
do {
let event = EventWithFoo()
event.foobar = .foo
try await event.save(on: self.db)
await XCTAssertNoThrowAsync(try await EnumAddMultipleCasesMigration().prepare(on: self.db))
event.foobar = .baz
await XCTAssertNoThrowAsync(try await event.update(on: self.db))
event.foobar = .qux
await XCTAssertNoThrowAsync(try await event.update(on: self.db))
await XCTAssertNoThrowAsync(try await EnumAddMultipleCasesMigration().revert(on: self.db))
} catch {
try? await EventWithFooMigration().revert(on: self.db)
throw error
}
} catch {
try? await EnumMigration().revert(on: self.db)
throw error
}
}
func testEncodingArrayOfModels() async throws {
final class Elem: Model, ExpressibleByIntegerLiteral, @unchecked Sendable {
static let schema = ""
@ID(custom: .id) var id: Int?
init() {}; init(integerLiteral l: Int) { self.id = l }
}
final class Seq: Model, ExpressibleByNilLiteral, ExpressibleByArrayLiteral, @unchecked Sendable {
static let schema = "seqs"
@ID(custom: .id) var id: Int?; @OptionalField(key: "list") var list: [Elem]?
init() {}; init(nilLiteral: ()) { self.list = nil }; init(arrayLiteral el: Elem...) { self.list = el }
}
do {
try await self.db.schema(Seq.schema).field(.id, .int, .identifier(auto: true)).field("list", .sql(embed: "JSONB[]")).create()
let s1: Seq = [1, 2], s2: Seq = nil; try [s1, s2].forEach { try $0.create(on: self.db).wait() }
// Make sure it went into the DB as "array of jsonb" rather than as "array of one jsonb containing array" or such.
let raws = try await (self.db as! any SQLDatabase).raw("SELECT array_to_json(list)::text t FROM seqs").all().map { try $0.decode(column: "t", as: String?.self) }
XCTAssertEqual(raws, [#"[{"id": 1},{"id": 2}]"#, nil])
// Make sure it round-trips through Fluent.
let seqs = try await Seq.query(on: self.db).all()
XCTAssertEqual(seqs.count, 2)
XCTAssertEqual(seqs.dropFirst(0).first?.id, s1.id)
XCTAssertEqual(seqs.dropFirst(0).first?.list?.map(\.id), s1.list?.map(\.id))
XCTAssertEqual(seqs.dropFirst(1).first?.id, s2.id)
XCTAssertEqual(seqs.dropFirst(1).first?.list?.map(\.id), s2.list?.map(\.id))
} catch let error {
XCTFail("caught error: \(String(reflecting: error))")
}
try await db.schema(Seq.schema).delete()
}
var benchmarker: FluentBenchmarker { .init(databases: self.dbs) }
var eventLoopGroup: any EventLoopGroup { MultiThreadedEventLoopGroup.singleton }
var threadPool: NIOThreadPool { NIOThreadPool.singleton }
var dbs: Databases!
var db: (any Database)!
var postgres: any PostgresDatabase { self.db as! any PostgresDatabase }
override func setUp() async throws {
try await super.setUp()
XCTAssert(isLoggingConfigured)
self.dbs = Databases(threadPool: self.threadPool, on: self.eventLoopGroup)
self.dbs.use(.testPostgres(subconfig: "A"), as: .a)
self.dbs.use(.testPostgres(subconfig: "B"), as: .b)
let a = self.dbs.database(.a, logger: .init(label: "test.fluent.a"), on: self.eventLoopGroup.any())
_ = try await (a as! any PostgresDatabase).query("drop schema public cascade").get()
_ = try await (a as! any PostgresDatabase).query("create schema public").get()
let b = self.dbs.database(.b, logger: .init(label: "test.fluent.b"), on: self.eventLoopGroup.any())
_ = try await (b as! any PostgresDatabase).query("drop schema public cascade").get()
_ = try await (b as! any PostgresDatabase).query("create schema public").get()
self.db = a
}
override func tearDown() async throws {
await self.dbs.shutdownAsync()
try await super.tearDown()
}
}
extension DatabaseConfigurationFactory {
static func testPostgres(
subconfig: String,
encodingContext: PostgresEncodingContext<some PostgresJSONEncoder> = .default,
decodingContext: PostgresDecodingContext<some PostgresJSONDecoder> = .default
) -> DatabaseConfigurationFactory {
let baseSubconfig = SQLPostgresConfiguration(
hostname: env("POSTGRES_HOSTNAME_\(subconfig)") ?? "localhost",
port: env("POSTGRES_PORT_\(subconfig)").flatMap(Int.init) ?? SQLPostgresConfiguration.ianaPortNumber,
username: env("POSTGRES_USER_\(subconfig)") ?? "test_username",
password: env("POSTGRES_PASSWORD_\(subconfig)") ?? "test_password",
database: env("POSTGRES_DB_\(subconfig)") ?? "test_database",
tls: try! .prefer(.init(configuration: .makeClientConfiguration()))
)
return .postgres(configuration: baseSubconfig, connectionPoolTimeout: .seconds(30), encodingContext: encodingContext, decodingContext: decodingContext)
}
}
extension DatabaseID {
static let iso8601 = DatabaseID(string: "iso8601")
static let a = DatabaseID(string: "a")
static let b = DatabaseID(string: "b")
}
struct Metadata: Codable {
let createdAt: Date
}
final class Event: Model, @unchecked Sendable {
static let schema = "events"
@ID(custom: "id", generatedBy: .database)
var id: Int?
@Field(key: "metadata")
var metadata: Metadata
}
final class EventStringlyTyped: Model, @unchecked Sendable {
static let schema = "events"
@ID(custom: "id", generatedBy: .database)
var id: Int?
@Field(key: "metadata")
var metadata: [String: String]
}
struct EventMigration: AsyncMigration {
func prepare(on database: any Database) async throws {
try await database.schema(Event.schema)
.field("id", .int, .identifier(auto: true))
.field("metadata", .json, .required)
.create()
}
func revert(on database: any Database) async throws {
try await database.schema(Event.schema).delete()
}
}
final class EventWithFoo: Model, @unchecked Sendable {
static let schema = "foobar_events"
@ID
var id: UUID?
@Enum(key: "foo")
var foobar: Foobar
}
enum Foobar: String, Codable {
static let schema = "foobars"
case foo
case bar
case baz
case qux
}
struct EventWithFooMigration: AsyncMigration {
func prepare(on database: any Database) async throws {
let foobar = try await database.enum(Foobar.schema).read()
try await database.schema(EventWithFoo.schema)
.id()
.field("foo", foobar, .required)
.create()
}
func revert(on database: any Database) async throws {
try await database.schema(EventWithFoo.schema).delete()
}
}
struct EnumMigration: AsyncMigration {
func prepare(on database: any Database) async throws {
_ = try await database.enum(Foobar.schema)
.case("foo")
.case("bar")
.create()
}
func revert(on database: any Database) async throws {
try await database.enum(Foobar.schema).delete()
}
}
struct EnumAddMultipleCasesMigration: AsyncMigration {
func prepare(on database: any Database) async throws {
_ = try await database.enum(Foobar.schema)
.case("baz")
.case("qux")
.update()
}
func revert(on database: any Database) async throws {
_ = try await database.enum(Foobar.schema)
.deleteCase("baz")
.deleteCase("qux")
.update()
}
}
func env(_ name: String) -> String? {
ProcessInfo.processInfo.environment[name]
}
let isLoggingConfigured: Bool = {
LoggingSystem.bootstrap { label in
var handler = StreamLogHandler.standardOutput(label: label)
handler.logLevel = env("LOG_LEVEL").flatMap { .init(rawValue: $0) } ?? .info
return handler
}
return true
}()