Skip to content

Commit

Permalink
Correct number of clauses when filtering with an array (#113)
Browse files Browse the repository at this point in the history
  • Loading branch information
kilnerm authored and djones6 committed Jul 1, 2019
1 parent c909e0e commit ce42319
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 10 deletions.
17 changes: 7 additions & 10 deletions Sources/SwiftKueryORM/Model.swift
Original file line number Diff line number Diff line change
Expand Up @@ -901,18 +901,15 @@ public extension Model {
}
case .or:
let array = value.split(separator: ",")
if array.count > 1 {
var newFilter: Filter = (column == Parameter())
for _ in array {
newFilter = newFilter || (column == Parameter())
}
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)
}

Expand Down
46 changes: 46 additions & 0 deletions Tests/SwiftKueryORMTests/TestFind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class TestFind: XCTestCase {
("testFind", testFind),
("testFindAll", testFindAll),
("testFindAllMatching", testFindAllMatching),
("testFindAllMatchingArrayFilter", testFindAllMatchingArrayFilter),
]
}

Expand Down Expand Up @@ -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
numberOfClauses += 1
}
XCTAssertTrue(success)
}
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
Expand Down

0 comments on commit ce42319

Please sign in to comment.