From 752ee280c8ec5f06d66e1043f411a0855615e21e Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Mon, 1 Jul 2019 13:08:42 +0100 Subject: [PATCH 1/4] Add test to check filtering with arrays returns correct number of clauses --- Tests/SwiftKueryORMTests/TestFind.swift | 46 +++++++++++++++++++++++++ 1 file changed, 46 insertions(+) diff --git a/Tests/SwiftKueryORMTests/TestFind.swift b/Tests/SwiftKueryORMTests/TestFind.swift index 4a7fab1..2059eea 100644 --- a/Tests/SwiftKueryORMTests/TestFind.swift +++ b/Tests/SwiftKueryORMTests/TestFind.swift @@ -10,6 +10,7 @@ class TestFind: XCTestCase { ("testFind", testFind), ("testFindAll", testFindAll), ("testFindAllMatching", testFindAllMatching), + ("testFindAllMatchingArrayFilter", testFindAllMatchingArrayFilter), ] } @@ -140,6 +141,51 @@ class TestFind: XCTestCase { }) } + struct AgeFilter: QueryParams { + let age: [Int] + } + + /** + Testing that the correct SQL Query is created to retrieve all the models. + Testing that correct amount of models are retrieved + */ + func testFindAllMatchingArrayFilter() { + let connection: TestConnection = createConnection(.returnThreeRows) + Database.default = Database(single: connection) + let filterArray = [38,28,36] + let filter = AgeFilter(age: filterArray) + let numberOfFilters = filterArray.count + performTest(asyncTasks: { expectation in + Person.findAll(matching: filter) { array, error in + XCTAssertNil(error, "Find Failed: \(String(describing: error))") + XCTAssertNotNil(connection.query, "Find Failed: Query is nil") + if let query = connection.query { + let expectedPrefix = "SELECT * FROM \"People\" WHERE" + let expectedClauses = [["\"People\".\"age\" = ?1", "\"People\".\"age\" = ?2", "\"People\".\"age\" = ?3"]] + let expectedOperator = "OR" + let resultQuery = connection.descriptionOf(query: query) + XCTAssertTrue(resultQuery.hasPrefix(expectedPrefix)) + var numberOfClauses = 0 + for whereClauses in expectedClauses { + var success = false + for whereClause in whereClauses where resultQuery.contains(whereClause) { + success = true + } + XCTAssertTrue(success) + numberOfClauses += 1 + } + XCTAssertTrue(resultQuery.contains(expectedOperator)) + XCTAssertEqual(numberOfFilters, numberOfClauses, "Incorrect number of where clauses in query") + } + XCTAssertNotNil(array, "Find Failed: No array of models returned") + if let array = array { + XCTAssertEqual(array.count, 3, "Find Failed: \(String(describing: array.count)) is not equal to 1") + } + expectation.fulfill() + } + }) + } + struct Order: Model { static var tableName = "Orders" var item: Int From 172b225317ee7b7bc9e3ca4c8e704c2ba2923cd7 Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Mon, 1 Jul 2019 13:19:31 +0100 Subject: [PATCH 2/4] Fix test issue --- Tests/SwiftKueryORMTests/TestFind.swift | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Tests/SwiftKueryORMTests/TestFind.swift b/Tests/SwiftKueryORMTests/TestFind.swift index 2059eea..d9c7158 100644 --- a/Tests/SwiftKueryORMTests/TestFind.swift +++ b/Tests/SwiftKueryORMTests/TestFind.swift @@ -170,9 +170,9 @@ class TestFind: XCTestCase { var success = false for whereClause in whereClauses where resultQuery.contains(whereClause) { success = true + numberOfClauses += 1 } XCTAssertTrue(success) - numberOfClauses += 1 } XCTAssertTrue(resultQuery.contains(expectedOperator)) XCTAssertEqual(numberOfFilters, numberOfClauses, "Incorrect number of where clauses in query") From 942d2cc9b5a8f8ba89fcfb6dae6cadb7562494cb Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Mon, 1 Jul 2019 13:20:26 +0100 Subject: [PATCH 3/4] Ensure correct number of clauses are created --- Sources/SwiftKueryORM/Model.swift | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index bd77ca8..c904297 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -901,10 +901,12 @@ public extension Model { } case .or: let array = value.split(separator: ",") - if array.count > 1 { + var requiredFilters = array.count + if requiredFilters > 1 { var newFilter: Filter = (column == Parameter()) - for _ in array { + while (requiredFilters - 1) > 0 { newFilter = newFilter || (column == Parameter()) + requiredFilters -= 1 } filter = newFilter parameters = array.map { String($0) } From 641c9cca8ab47667b9fb18c68ef3cdad71f51611 Mon Sep 17 00:00:00 2001 From: "kilnerm@uk.ibm.com" Date: Mon, 1 Jul 2019 15:40:03 +0100 Subject: [PATCH 4/4] Rework core fix --- Sources/SwiftKueryORM/Model.swift | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/Sources/SwiftKueryORM/Model.swift b/Sources/SwiftKueryORM/Model.swift index c904297..912f43a 100644 --- a/Sources/SwiftKueryORM/Model.swift +++ b/Sources/SwiftKueryORM/Model.swift @@ -901,20 +901,15 @@ public extension Model { } case .or: let array = value.split(separator: ",") - var requiredFilters = array.count - if requiredFilters > 1 { - var newFilter: Filter = (column == Parameter()) - while (requiredFilters - 1) > 0 { - newFilter = newFilter || (column == Parameter()) - requiredFilters -= 1 - } - filter = newFilter - parameters = array.map { String($0) } - } else { - filter = (column == Parameter()) + + var newFilter: Filter = (column == Parameter()) + // For every additional element we need to add an OR clause for the next parameter. + for _ in 1 ..< array.count { + newFilter = newFilter || (column == Parameter()) } + filter = newFilter + parameters = array.map { String($0) } } - return (filter, parameters) }