Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Correct number of clauses when filtering with an array #113

Merged
merged 4 commits into from
Jul 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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"]]
djones6 marked this conversation as resolved.
Show resolved Hide resolved
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