From e9b7a67c73b7a4e55b7fee43fa8be5a694b3e0a0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gwendal=20Roue=CC=81?= Date: Sat, 18 Aug 2018 15:43:18 +0200 Subject: [PATCH] Fix JSON column tests so that they don't run when .sortedKeys option is not available --- ...tablePersistableRecordEncodableTests.swift | 199 +++++++++--------- 1 file changed, 104 insertions(+), 95 deletions(-) diff --git a/Tests/GRDBTests/MutablePersistableRecordEncodableTests.swift b/Tests/GRDBTests/MutablePersistableRecordEncodableTests.swift index b664ed239..65ac337a6 100644 --- a/Tests/GRDBTests/MutablePersistableRecordEncodableTests.swift +++ b/Tests/GRDBTests/MutablePersistableRecordEncodableTests.swift @@ -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\"]"]) } } }