From ad45dcce04f59c371cf11b18a5b84b5daedf0c95 Mon Sep 17 00:00:00 2001 From: kilnerm <38245247+kilnerm@users.noreply.github.com> Date: Thu, 21 Mar 2019 14:11:23 +0000 Subject: [PATCH] Ensure a provided db is used to execute Model operations (#106) --- Sources/SwiftKueryORM/Model.swift | 18 +++++------ Tests/SwiftKueryORMTests/TestFind.swift | 26 ++++++++++++++++ Tests/SwiftKueryORMTests/TestSave.swift | 31 +++++++++++++++++++ Tests/SwiftKueryORMTests/TestTable.swift | 39 ++++++++++++++++++++++++ 4 files changed, 105 insertions(+), 9 deletions(-) diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index a6d68a0..a990603 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -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")) @@ -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")) @@ -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")) @@ -418,7 +418,7 @@ public extension Model { } private func executeQuery(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")) @@ -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")) @@ -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(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")) @@ -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")) @@ -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(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")) @@ -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")) diff --git a/Tests/SwiftKueryORMTests/TestFind.swift b/Tests/SwiftKueryORMTests/TestFind.swift index e13a8f4..4a7fab1 100644 --- a/Tests/SwiftKueryORMTests/TestFind.swift +++ b/Tests/SwiftKueryORMTests/TestFind.swift @@ -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 diff --git a/Tests/SwiftKueryORMTests/TestSave.swift b/Tests/SwiftKueryORMTests/TestSave.swift index bb52874..8daffdd 100644 --- a/Tests/SwiftKueryORMTests/TestSave.swift +++ b/Tests/SwiftKueryORMTests/TestSave.swift @@ -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 diff --git a/Tests/SwiftKueryORMTests/TestTable.swift b/Tests/SwiftKueryORMTests/TestTable.swift index 44761b2..63bcaca 100644 --- a/Tests/SwiftKueryORMTests/TestTable.swift +++ b/Tests/SwiftKueryORMTests/TestTable.swift @@ -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 */ @@ -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