forked from vapor-community/mongo-driver
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
added test cases for count and or/and groups and fixed the previous i…
…mplementation. Avoid inserting an empty filter before all queries.
- Loading branch information
1 parent
a614353
commit 31afa0b
Showing
4 changed files
with
83 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -23,7 +23,10 @@ class DriverTests: XCTestCase { | |
("testDeleteLimit0Implicit", testDeleteLimit0Implicit), | ||
("testDeleteLimit0Explicit", testDeleteLimit0Explicit), | ||
("testDeleteLimit1", testDeleteLimit1), | ||
("testDeleteLimitInvalid", testDeleteLimitInvalid) | ||
("testDeleteLimitInvalid", testDeleteLimitInvalid), | ||
("testCount", testCount), | ||
("testGroupOr", testGroupOr), | ||
("testGroupAnd", testGroupAnd), | ||
] | ||
} | ||
|
||
|
@@ -221,4 +224,60 @@ class DriverTests: XCTestCase { | |
// this should fail | ||
} | ||
} | ||
|
||
func testCount() throws { | ||
// Insert dummy users Vapor0, Vapor1, Vapor0, ..., Vapor1 | ||
for i in (0..<10) { | ||
_ = createUser(suffix: "\(i%2)") | ||
} | ||
let count = try User.query().count() | ||
XCTAssertEqual(10, count) | ||
} | ||
|
||
func testGroupOr() throws { | ||
// Insert dummy users Vapor0, Vapor1, ... | ||
for i in (0..<10) { | ||
_ = createUser(suffix: "\(i)") | ||
} | ||
|
||
let query = try User.query().or({ query in | ||
try query.filter("name", "Vapor3") | ||
try query.filter("name", "Vapor4") | ||
try query.filter("name", "Vapor5") | ||
try query.filter("name", "Vapor6") | ||
try query.filter("name", "Vapor7") | ||
}) | ||
query.limit = Limit(count: 3, offset: 1) | ||
let result = try query.all() | ||
XCTAssertEqual(["Vapor4", "Vapor5", "Vapor6"], result.map { $0.name }) | ||
} | ||
|
||
func testGroupAnd() throws { | ||
// Insert dummy users Vapor0, Vapor1, ... | ||
for i in (0..<10) { | ||
var user = createUser(suffix: "\(i)") | ||
if i%2 == 0 { | ||
user.email = "[email protected]" | ||
try user.save() | ||
} | ||
} | ||
|
||
let query = try User.query() | ||
.or{ query in | ||
try query.filter("name", "Vapor1") | ||
try query.and{ query in | ||
try query.filter("name", "Vapor2") | ||
try query.filter("email", "[email protected]") | ||
} | ||
try query.and{ query in | ||
try query.filter("name", "Vapor3") | ||
try query.filter("email", "[email protected]") | ||
} | ||
try query.filter("name", "Vapor4") | ||
try query.filter("name", "Vapor5") | ||
} | ||
query.limit = Limit(count: 2, offset: 1) | ||
let result = try query.all() | ||
XCTAssertEqual(["Vapor2", "Vapor4"], result.map { $0.name }) | ||
} | ||
} |