mirror of
https://github.com/groue/GRDB.swift
synced 2026-11-25 04:07:47 +00:00
Fix JSON column tests so that they don't run when .sortedKeys option is not available
This commit is contained in:
1 parent
ba7cd4f61d
commit
e9b7a67c73
1 file changed
+104
-95
@@ -480,7 +480,6 @@ extension MutablePersistableRecordEncodableTests {
|
||||
private let testKeyRoot = CodingUserInfoKey(rawValue: "test1")!
|
||||
private let testKeyNested = CodingUserInfoKey(rawValue: "test2")!
|
||||
|
||||
@available(OSX 10.13, iOS 11.0, *)
|
||||
extension MutablePersistableRecordEncodableTests {
|
||||
struct NestedKeyed: Encodable {
|
||||
var name: String
|
||||
@@ -551,6 +550,7 @@ extension MutablePersistableRecordEncodableTests {
|
||||
}
|
||||
}
|
||||
|
||||
@available(OSX 10.13, iOS 11.0, *)
|
||||
struct CustomizedRecord: Encodable, MutablePersistableRecord {
|
||||
var nestedKeyed: NestedKeyed
|
||||
var nestedSingle: NestedSingle
|
||||
@@ -591,111 +591,120 @@ extension MutablePersistableRecordEncodableTests {
|
||||
|
||||
// Used as a reference
|
||||
func testFoundationBehavior() throws {
|
||||
do {
|
||||
let record = Record(
|
||||
nestedKeyed: NestedKeyed(name: "foo"),
|
||||
nestedSingle: NestedSingle(name: "bar"),
|
||||
nestedUnkeyed: NestedUnkeyed(name: "baz"))
|
||||
// This test relies on .sortedKeys option
|
||||
if #available(OSX 10.13, iOS 11.0, *) {
|
||||
do {
|
||||
let record = Record(
|
||||
nestedKeyed: NestedKeyed(name: "foo"),
|
||||
nestedSingle: NestedSingle(name: "bar"),
|
||||
nestedUnkeyed: NestedUnkeyed(name: "baz"))
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
|
||||
let json = try String(data: encoder.encode(record), encoding: .utf8)!
|
||||
XCTAssertEqual(json, """
|
||||
{
|
||||
"nestedKeyed" : {
|
||||
"key" : "nestedKeyed",
|
||||
"name" : "foo"
|
||||
},
|
||||
"nestedSingle" : "bar,key:nestedSingle,context:nil",
|
||||
"nestedUnkeyed" : [
|
||||
"baz",
|
||||
"nestedUnkeyed",
|
||||
null
|
||||
]
|
||||
}
|
||||
""")
|
||||
}
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
|
||||
let json = try String(data: encoder.encode(record), encoding: .utf8)!
|
||||
XCTAssertEqual(json, """
|
||||
{
|
||||
"nestedKeyed" : {
|
||||
"key" : "nestedKeyed",
|
||||
"name" : "foo"
|
||||
},
|
||||
"nestedSingle" : "bar,key:nestedSingle,context:nil",
|
||||
"nestedUnkeyed" : [
|
||||
"baz",
|
||||
"nestedUnkeyed",
|
||||
null
|
||||
]
|
||||
}
|
||||
""")
|
||||
}
|
||||
|
||||
do {
|
||||
let record = Record(
|
||||
nestedKeyed: NestedKeyed(name: "foo"),
|
||||
nestedSingle: NestedSingle(name: "bar"),
|
||||
nestedUnkeyed: NestedUnkeyed(name: "baz"))
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.userInfo = [testKeyRoot: "root", testKeyNested: "nested"]
|
||||
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
|
||||
let json = try String(data: encoder.encode(record), encoding: .utf8)
|
||||
XCTAssertEqual(json, """
|
||||
{
|
||||
"context" : "root",
|
||||
"nestedKeyed" : {
|
||||
"context" : "nested",
|
||||
"key" : "nestedKeyed",
|
||||
"name" : "foo"
|
||||
},
|
||||
"nestedSingle" : "bar,key:nestedSingle,context:nested",
|
||||
"nestedUnkeyed" : [
|
||||
"baz",
|
||||
"nestedUnkeyed",
|
||||
"nested"
|
||||
]
|
||||
}
|
||||
""")
|
||||
do {
|
||||
let record = Record(
|
||||
nestedKeyed: NestedKeyed(name: "foo"),
|
||||
nestedSingle: NestedSingle(name: "bar"),
|
||||
nestedUnkeyed: NestedUnkeyed(name: "baz"))
|
||||
|
||||
let encoder = JSONEncoder()
|
||||
encoder.userInfo = [testKeyRoot: "root", testKeyNested: "nested"]
|
||||
encoder.outputFormatting = [.sortedKeys, .prettyPrinted]
|
||||
let json = try String(data: encoder.encode(record), encoding: .utf8)
|
||||
XCTAssertEqual(json, """
|
||||
{
|
||||
"context" : "root",
|
||||
"nestedKeyed" : {
|
||||
"context" : "nested",
|
||||
"key" : "nestedKeyed",
|
||||
"name" : "foo"
|
||||
},
|
||||
"nestedSingle" : "bar,key:nestedSingle,context:nested",
|
||||
"nestedUnkeyed" : [
|
||||
"baz",
|
||||
"nestedUnkeyed",
|
||||
"nested"
|
||||
]
|
||||
}
|
||||
""")
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func testRecord() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "record") { t in
|
||||
t.column("context")
|
||||
t.column("key")
|
||||
t.column("nestedKeyed")
|
||||
t.column("nestedSingle")
|
||||
t.column("nestedUnkeyed")
|
||||
// This test relies on .sortedKeys option
|
||||
if #available(OSX 10.13, iOS 11.0, *) {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "record") { t in
|
||||
t.column("context")
|
||||
t.column("key")
|
||||
t.column("nestedKeyed")
|
||||
t.column("nestedSingle")
|
||||
t.column("nestedUnkeyed")
|
||||
}
|
||||
|
||||
var record = Record(
|
||||
nestedKeyed: NestedKeyed(name: "foo"),
|
||||
nestedSingle: NestedSingle(name: "bar"),
|
||||
nestedUnkeyed: NestedUnkeyed(name: "baz"))
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, Record.all())!
|
||||
XCTAssertEqual(row, [
|
||||
"context": nil,
|
||||
"key": nil,
|
||||
"nestedKeyed": "{\"name\":\"foo\"}",
|
||||
"nestedSingle": "bar,key:nestedSingle,context:nil",
|
||||
"nestedUnkeyed": "[\"baz\",null,null]"])
|
||||
}
|
||||
|
||||
var record = Record(
|
||||
nestedKeyed: NestedKeyed(name: "foo"),
|
||||
nestedSingle: NestedSingle(name: "bar"),
|
||||
nestedUnkeyed: NestedUnkeyed(name: "baz"))
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, Record.all())!
|
||||
XCTAssertEqual(row, [
|
||||
"context": nil,
|
||||
"key": nil,
|
||||
"nestedKeyed": "{\"name\":\"foo\"}",
|
||||
"nestedSingle": "bar,key:nestedSingle,context:nil",
|
||||
"nestedUnkeyed": "[\"baz\",null,null]"])
|
||||
}
|
||||
}
|
||||
|
||||
func testCustomizedRecord() throws {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "customizedRecord") { t in
|
||||
t.column("context")
|
||||
t.column("key")
|
||||
t.column("nestedKeyed")
|
||||
t.column("nestedSingle")
|
||||
t.column("nestedUnkeyed")
|
||||
// This test relies on .sortedKeys option
|
||||
if #available(OSX 10.13, iOS 11.0, *) {
|
||||
let dbQueue = try makeDatabaseQueue()
|
||||
try dbQueue.write { db in
|
||||
try db.create(table: "customizedRecord") { t in
|
||||
t.column("context")
|
||||
t.column("key")
|
||||
t.column("nestedKeyed")
|
||||
t.column("nestedSingle")
|
||||
t.column("nestedUnkeyed")
|
||||
}
|
||||
|
||||
var record = CustomizedRecord(
|
||||
nestedKeyed: NestedKeyed(name: "foo"),
|
||||
nestedSingle: NestedSingle(name: "bar"),
|
||||
nestedUnkeyed: NestedUnkeyed(name: "baz"))
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, CustomizedRecord.all())!
|
||||
XCTAssertEqual(row, [
|
||||
"context": "GRDB root",
|
||||
"key": nil,
|
||||
"nestedKeyed": "{\"context\":\"JSON nested: nestedKeyed\",\"name\":\"foo\"}",
|
||||
"nestedSingle": "bar,key:nestedSingle,context:GRDB nested",
|
||||
"nestedUnkeyed": "[\"baz\",null,\"JSON nested: nestedUnkeyed\"]"])
|
||||
}
|
||||
|
||||
var record = CustomizedRecord(
|
||||
nestedKeyed: NestedKeyed(name: "foo"),
|
||||
nestedSingle: NestedSingle(name: "bar"),
|
||||
nestedUnkeyed: NestedUnkeyed(name: "baz"))
|
||||
try record.insert(db)
|
||||
|
||||
let row = try Row.fetchOne(db, CustomizedRecord.all())!
|
||||
XCTAssertEqual(row, [
|
||||
"context": "GRDB root",
|
||||
"key": nil,
|
||||
"nestedKeyed": "{\"context\":\"JSON nested: nestedKeyed\",\"name\":\"foo\"}",
|
||||
"nestedSingle": "bar,key:nestedSingle,context:GRDB nested",
|
||||
"nestedUnkeyed": "[\"baz\",null,\"JSON nested: nestedUnkeyed\"]"])
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user