Skip to content

Commit

Permalink
DatabaseError's isSyntaxError and isConnectionClosed properties n…
Browse files Browse the repository at this point in the history
…ow correctly respect `MySQLError.invalidSyntax` and `MySQLError.closed`. A unit tests for these behaviors is included. (#203)

Also avoids double-running the FluentBenchmark tests and adds the missing `testSQL()` benchmark test.
  • Loading branch information
gwynne authored Dec 13, 2021
1 parent 2d1cbdd commit f86bf9c
Show file tree
Hide file tree
Showing 2 changed files with 47 additions and 4 deletions.
15 changes: 12 additions & 3 deletions Sources/FluentMySQLDriver/MySQLError+Database.swift
Original file line number Diff line number Diff line change
@@ -1,19 +1,28 @@
extension MySQLError: DatabaseError {
public var isSyntaxError: Bool {
return false
switch self {
case .invalidSyntax(_):
return true
default:
return false
}
}

public var isConstraintFailure: Bool {
switch self {
case .duplicateEntry(_):
return true

default:
return false
}
}

public var isConnectionClosed: Bool {
return false
switch self {
case .closed:
return true
default:
return false
}
}
}
36 changes: 35 additions & 1 deletion Tests/FluentMySQLDriverTests/FluentMySQLDriverTests.swift
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
import NIO
import FluentBenchmark
import FluentMySQLDriver
import SQLKit
import XCTest
import Logging

final class FluentMySQLDriverTests: XCTestCase {
func testAll() throws { try self.benchmarker.testAll() }
// 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() }
Expand All @@ -32,10 +33,43 @@ final class FluentMySQLDriverTests: XCTestCase {
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.db as! SQLDatabase)
do {
try sql.raw("asdf").run().wait()
} catch let error as DatabaseError where error.isSyntaxError {
// pass
} catch {
XCTFail("\(error)")
}
do {
try sql.create(table: "foo").column("name", type: .text, .unique).run().wait()
try sql.insert(into: "foo").columns("name").values("bar").run().wait()
try sql.insert(into: "foo").columns("name").values("bar").run().wait()
} catch let error as DatabaseError where error.isConstraintFailure {
// pass
} catch {
XCTFail("\(error)")
}
do {
try self.mysql.withConnection { conn in
conn.close().flatMap {
conn.sql().insert(into: "foo").columns("name").values("bar").run()
}
}.wait()
} catch let error as DatabaseError where error.isConnectionClosed {
// pass
} catch let error {
let error = error
XCTFail("\(error)")
}
}

func testClarityModel() throws {
final class Clarity: Model {
static let schema = "clarities"
Expand Down

0 comments on commit f86bf9c

Please sign in to comment.