Skip to content

Commit

Permalink
Ensure a provided db is used to execute Model operations (#106)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilnerm authored Mar 21, 2019
1 parent bbfbb38 commit ad45dcc
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 9 deletions.
18 changes: 9 additions & 9 deletions Sources/SwiftKueryORM/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ public extension Model {
} catch let error {
return onCompletion(nil, Self.convertError(error))
}
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(nil, RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down Expand Up @@ -207,7 +207,7 @@ public extension Model {
} catch let error {
return onCompletion(nil, Self.convertError(error))
}
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(nil, RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down Expand Up @@ -360,7 +360,7 @@ public extension Model {

private func executeQuery(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (Self?, RequestError?) -> Void ) {
var dictionaryTitleToValue = [String: Any?]()
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(nil, RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down Expand Up @@ -418,7 +418,7 @@ public extension Model {
}

private func executeQuery<I: Identifier>(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (I?, Self?, RequestError?) -> Void ) {
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(nil, nil, RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down Expand Up @@ -473,7 +473,7 @@ public extension Model {
/// - Parameter using: Optional Database to use
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a tuple of (Self?, RequestError?), of which one will be nil, depending on whether the query was successful.
public static func executeQuery(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (Self?, RequestError?) -> Void ) {
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(nil, RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down Expand Up @@ -518,7 +518,7 @@ public extension Model {
/// - Parameter using: Optional Database to use
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a tuple of (Identifier?, Self?, RequestError?), of which some will be nil, depending on whether the query was successful.
public static func executeQuery<I: Identifier>(query: Query, parameters: [Any?], using db: Database? = nil, _ onCompletion: @escaping (I?, Self?, RequestError?) -> Void ) {
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(nil, nil, RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down Expand Up @@ -581,7 +581,7 @@ public extension Model {
/// - Parameter using: Optional Database to use
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a tuple of ([Self]?, RequestError?), of which one will be nil, depending on whether the query was successful.
public static func executeQuery(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping ([Self]?, RequestError?)-> Void ) {
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(nil, RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down Expand Up @@ -646,7 +646,7 @@ public extension Model {
/// - Parameter using: Optional Database to use
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a tuple of ([Identifier, Self]?, RequestError?), of which one will be nil, depending on whether the query was successful.
public static func executeQuery<I: Identifier>(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping ([(I, Self)]?, RequestError?) -> Void ) {
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(nil, RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down Expand Up @@ -725,7 +725,7 @@ public extension Model {
/// - Parameter using: Optional Database to use
/// - Parameter onCompletion: The function to be called when the execution of the query has completed. The function will be passed a RequestError? which may be nil, depending on whether the query was successful.
public static func executeQuery(query: Query, parameters: [Any?]? = nil, using db: Database? = nil, _ onCompletion: @escaping (RequestError?) -> Void ) {
Self.executeTask() { connection, error in
Self.executeTask(using: db) { connection, error in
guard let connection = connection else {
guard let error = error else {
return onCompletion(RequestError(.ormInternalError, reason: "Unknow error when getting connection"))
Expand Down
26 changes: 26 additions & 0 deletions Tests/SwiftKueryORMTests/TestFind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,32 @@ class TestFind: XCTestCase {
})
}

/**
Testing that the correct SQL Query is created to retrieve a specific model when using a non-default database.
Testing that the model can be retrieved
*/
func testFindUsingDB() {
let connection: TestConnection = createConnection(.returnOneRow)
let db = Database(single: connection)
performTest(asyncTasks: { expectation in
Person.find(id: 1, using: db) { p, error in
XCTAssertNil(error, "Find Failed: \(String(describing: error))")
XCTAssertNotNil(connection.query, "Find Failed: Query is nil")
if let query = connection.query {
let expectedQuery = "SELECT * FROM \"People\" WHERE \"People\".\"id\" = ?1"
let resultQuery = connection.descriptionOf(query: query)
XCTAssertEqual(resultQuery, expectedQuery, "Find Failed: Invalid query")
}
XCTAssertNotNil(p, "Find Failed: No model returned")
if let p = p {
XCTAssertEqual(p.name, "Joe", "Find Failed: \(String(describing: p.name)) is not equal to Joe")
XCTAssertEqual(p.age, 38, "Find Failed: \(String(describing: p.age)) is not equal to 38")
}
expectation.fulfill()
}
})
}

/**
Testing that the correct SQL Query is created to retrieve all the models.
Testing that correct amount of models are retrieved
Expand Down
31 changes: 31 additions & 0 deletions Tests/SwiftKueryORMTests/TestSave.swift
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,37 @@ class TestSave: XCTestCase {
})
}

/**
Testing that the correct SQL Query is created to save a Model when using a non-default database
*/
func testSaveUsingDB() {
let connection: TestConnection = createConnection()
let db = Database(single: connection)
performTest(asyncTasks: { expectation in
let person = Person(name: "Joe", age: 38)
person.save(using: db) { p, error in
XCTAssertNil(error, "Save Failed: \(String(describing: error))")
XCTAssertNotNil(connection.query, "Save Failed: Query is nil")
if let query = connection.query {
let expectedPrefix = "INSERT INTO \"People\""
let expectedSQLStatement = "VALUES"
let expectedDictionary = ["\"name\"": "?1,?2", "\"age\"": "?1,?2"]

let resultQuery = connection.descriptionOf(query: query)
XCTAssertTrue(resultQuery.hasPrefix(expectedPrefix))
XCTAssertTrue(resultQuery.contains(expectedSQLStatement))
self.verifyColumnsAndValues(resultQuery: resultQuery, expectedDictionary: expectedDictionary)
}
XCTAssertNotNil(p, "Save Failed: No model returned")
if let p = p {
XCTAssertEqual(p.name, person.name, "Save Failed: \(String(describing: p.name)) is not equal to \(String(describing: person.name))")
XCTAssertEqual(p.age, person.age, "Save Failed: \(String(describing: p.age)) is not equal to \(String(describing: person.age))")
}
expectation.fulfill()
}
})
}

/**
Testing that the correct SQL Query is created to save a Model
Testing that an id is correcly returned
Expand Down
39 changes: 39 additions & 0 deletions Tests/SwiftKueryORMTests/TestTable.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ class TestTable: XCTestCase {
})
}

/**
Testing that the correct SQL Query is created to create a table when using a non-default database
*/
func testCreateTableUsingDB() {
let connection: TestConnection = createConnection(.returnEmpty)
let db = Database(single: connection)
performTest(asyncTasks: { expectation in
User.createTable(using: db) { result, error in
XCTAssertNil(error, "Table Creation Failed: \(String(describing: error))")
XCTAssertNotNil(connection.raw, "Table Creation Failed: Query is nil")
if let raw = connection.raw {
let expectedQuery = "CREATE TABLE \"Users\" (\"username\" type NOT NULL, \"password\" type NOT NULL, \"id\" type AUTO_INCREMENT PRIMARY KEY)"
XCTAssertEqual(raw, expectedQuery, "Table Creation Failed: Invalid query")
}
expectation.fulfill()
}
})
}

/**
Testing that the correct SQL Query is created to drop a table
*/
Expand All @@ -58,6 +77,26 @@ class TestTable: XCTestCase {
})
}

/**
Testing that the correct SQL Query is created to drop a table when using a non-default database
*/
func testDropTableUsingDB() {
let connection: TestConnection = createConnection(.returnEmpty)
let db = Database(single: connection)
performTest(asyncTasks: { expectation in
User.dropTable(using: db) { result, error in
XCTAssertNil(error, "Table Drop Failed: \(String(describing: error))")
XCTAssertNotNil(connection.query, "Table Drop Failed: Query is nil")
if let query = connection.query {
let expectedQuery = "DROP TABLE \"Users\""
let resultQuery = connection.descriptionOf(query: query)
XCTAssertEqual(resultQuery, expectedQuery, "Table Drop Failed: Invalid query")
}
expectation.fulfill()
}
})
}

struct Meal: Model {
static var idColumnName = "name"
var name: String
Expand Down

0 comments on commit ad45dcc

Please sign in to comment.