Add tests for joining methods added on DerivableRequest

This commit is contained in:
Gwendal Roué committed 2019-05-29 09:00:28 +02:00
1 parent e7a590b82b
commit 03d9727f61
1 file changed
+83 -2
+83 -2
View File
@@ -60,25 +60,40 @@ private var libraryMigrator: DatabaseMigrator = {
// Define DerivableRequest extensions
extension DerivableRequest where RowDecoder == Author {
// SelectionRequest
func selectCountry() -> Self {
return select(Column("country"))
}
// FilteredRequest
func filter(country: String) -> Self {
return filter(Column("country") == country)
}
// OrderedRequest
func orderByFullName() -> Self {
return order(
Column("lastName").collating(.localizedCaseInsensitiveCompare),
Column("firstName").collating(.localizedCaseInsensitiveCompare))
}
// TODO: aggregate annotation with SelectionRequest & JoinableRequest
}
extension DerivableRequest where RowDecoder == Book {
// OrderedRequest
func orderByTitle() -> Self {
return order(Column("title").collating(.localizedCaseInsensitiveCompare))
}
// JoinableRequest
func filter(authorCountry: String) -> Self {
return joining(required: Book.author.filter(country: authorCountry))
}
}
class DerivableRequestTests: GRDBTestCase {
func testFilter() throws {
func testFilteredRequest() throws {
let dbQueue = try makeDatabaseQueue()
try libraryMigrator.migrate(dbQueue)
try dbQueue.inDatabase { db in
@@ -99,7 +114,7 @@ class DerivableRequestTests: GRDBTestCase {
}
}
func testOrder() throws {
func testOrderedRequest() throws {
let dbQueue = try makeDatabaseQueue()
try libraryMigrator.migrate(dbQueue)
try dbQueue.inDatabase { db in
@@ -184,4 +199,70 @@ class DerivableRequestTests: GRDBTestCase {
""")
}
}
func testSelectionRequest() throws {
let dbQueue = try makeDatabaseQueue()
try libraryMigrator.migrate(dbQueue)
try dbQueue.inDatabase { db in
do {
sqlQueries.removeAll()
let request = Author.all().selectCountry()
let authorCountries = try Set(String.fetchAll(db, request))
XCTAssertEqual(authorCountries, ["FR", "US"])
XCTAssertEqual(lastSQLQuery, """
SELECT "country" FROM "author"
""")
}
do {
sqlQueries.removeAll()
let request = Book.including(required: Book.author.selectCountry())
_ = try Row.fetchAll(db, request)
XCTAssertEqual(lastSQLQuery, """
SELECT "book".*, "author"."country" \
FROM "book" \
JOIN "author" ON "author"."id" = "book"."authorId"
""")
}
}
}
func testJoinableRequest() throws {
let dbQueue = try makeDatabaseQueue()
try libraryMigrator.migrate(dbQueue)
try dbQueue.inDatabase { db in
do {
sqlQueries.removeAll()
let frenchBookTitles = try Book.all()
.filter(authorCountry: "FR")
.order(Column("title"))
.fetchAll(db)
.map { $0.title }
XCTAssertEqual(frenchBookTitles, ["Du côté de chez Swann"])
XCTAssertEqual(lastSQLQuery, """
SELECT "book".* \
FROM "book" \
JOIN "author" ON ("author"."id" = "book"."authorId") AND ("author"."country" = 'FR') \
ORDER BY "book"."title"
""")
}
do {
sqlQueries.removeAll()
let frenchAuthorFullNames = try Author
.joining(required: Author.books.filter(authorCountry: "FR"))
.order(Column("firstName"))
.fetchAll(db)
.map { $0.fullName }
XCTAssertEqual(frenchAuthorFullNames, ["Marcel Proust"])
XCTAssertEqual(lastSQLQuery, """
SELECT "author1".* \
FROM "author" "author1" \
JOIN "book" ON "book"."authorId" = "author1"."id" \
JOIN "author" "author2" ON ("author2"."id" = "book"."authorId") AND ("author2"."country" = 'FR') \
ORDER BY "author1"."firstName"
""")
}
}
}
}