mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Test the stable order of views with a primary key
This commit is contained in:
1 parent
99ecf76a77
commit
ca27f75760
2 files changed
+69
-1
No files matched your search
@@ -286,7 +286,6 @@ extension SQLRelation: Refinable {
|
||||
with { relation in
|
||||
relation.ordering = relation.ordering.appending(Ordering(orderings: { [relation] db in
|
||||
do {
|
||||
#warning("TODO: test with views")
|
||||
// Order by primary key. Don't order by rowid because those are
|
||||
// not stable: rowids can change after a vacuum.
|
||||
return try db.primaryKey(source.tableName).columns.map { SQLExpression.column($0).sqlOrdering }
|
||||
|
||||
@@ -22,6 +22,11 @@ private struct Player: Codable, MutablePersistableRecord {
|
||||
mutating func didInsert(_ inserted: InsertionSuccess) {
|
||||
id = inserted.rowID
|
||||
}
|
||||
|
||||
enum Columns {
|
||||
static let name = Column(CodingKeys.name)
|
||||
static let teamId = Column(CodingKeys.teamId)
|
||||
}
|
||||
}
|
||||
|
||||
private struct Team: Codable, PersistableRecord {
|
||||
@@ -1141,6 +1146,70 @@ final class DatabaseDumpTests: GRDBTestCase {
|
||||
}
|
||||
}
|
||||
|
||||
func test_dumpTables_single_view_with_schema_source() throws {
|
||||
struct SchemaSource: DatabaseSchemaSource {
|
||||
func columnsForPrimaryKey(_ db: Database, inView view: DatabaseObjectID) throws -> [String]? {
|
||||
["teamId", "name"]
|
||||
}
|
||||
}
|
||||
dbConfiguration.schemaSource = SchemaSource()
|
||||
|
||||
try makeRugbyDatabase().write { db in
|
||||
try db.create(view: "playerName", as: Player
|
||||
.orderByPrimaryKey()
|
||||
.filter { $0.teamId != nil }
|
||||
.select { [$0.teamId, $0.name] })
|
||||
|
||||
do {
|
||||
// Default order: use the view ordering
|
||||
do {
|
||||
// Default format
|
||||
let stream = TestStream()
|
||||
try db.dumpTables(["playerName"], to: stream)
|
||||
XCTAssertEqual(stream.output, """
|
||||
FRA|Antoine Dupond
|
||||
ENG|Owen Farrell
|
||||
|
||||
""")
|
||||
}
|
||||
do {
|
||||
// Custom format
|
||||
let stream = TestStream()
|
||||
try db.dumpTables(["playerName"], format: .json(), to: stream)
|
||||
XCTAssertEqual(stream.output, """
|
||||
[{"teamId":"FRA","name":"Antoine Dupond"},
|
||||
{"teamId":"ENG","name":"Owen Farrell"}]
|
||||
|
||||
""")
|
||||
}
|
||||
}
|
||||
|
||||
do {
|
||||
// Stable order (primary key)
|
||||
do {
|
||||
// Default format
|
||||
let stream = TestStream()
|
||||
try db.dumpTables(["playerName"], stableOrder: true, to: stream)
|
||||
XCTAssertEqual(stream.output, """
|
||||
ENG|Owen Farrell
|
||||
FRA|Antoine Dupond
|
||||
|
||||
""")
|
||||
}
|
||||
do {
|
||||
// Custom format
|
||||
let stream = TestStream()
|
||||
try db.dumpTables(["playerName"], format: .json(), stableOrder: true, to: stream)
|
||||
XCTAssertEqual(stream.output, """
|
||||
[{"teamId":"ENG","name":"Owen Farrell"},
|
||||
{"teamId":"FRA","name":"Antoine Dupond"}]
|
||||
|
||||
""")
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func test_dumpTables_multiple() throws {
|
||||
try makeRugbyDatabase().read { db in
|
||||
do {
|
||||
|
||||
Reference in new issue
Block a user