Added tests for arrays

This commit is contained in:
Angus Muller committed 2018-08-14 11:20:02 +01:00
1 parent 8c06e7bff5
commit a08974ffa4
1 file changed
+99 -2
+99 -2
View File
@@ -907,8 +907,8 @@ extension PersistableRecordTests {
func testNonOptionalNestedStruct() throws {
struct NestedStruct : Codable {
let firstName: String?
let lastName: String?
let firstName: String
let lastName: String
}
struct StructWithNestedType : PersistableRecord, Codable {
@@ -945,4 +945,101 @@ extension PersistableRecordTests {
}
}
}
func testNonOptionalNestedArrayStruct() throws {
struct NestedStruct : Codable {
let firstName: String
let lastName: String
}
struct StructWithNestedType : PersistableRecord, Codable {
static let databaseTableName = "t1"
let nested: [NestedStruct]
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(table: "t1") { t in
t.column("nested", .text)
}
let nested = NestedStruct(firstName: "Bob", lastName: "Dylan")
let value = StructWithNestedType(nested: [nested])
try value.insert(db)
let dbValue = try DatabaseValue.fetchOne(db, "SELECT nested FROM t1")!
// Encodable has a default implementation which encodes a model to JSON as String.
// We expect here JSON in the form of a String
XCTAssert(dbValue.storage.value is String)
let string = dbValue.storage.value as! String
if let data = string.data(using: .utf8) {
do {
let decoded = try JSONDecoder().decode([NestedStruct].self, from: data)
XCTAssertEqual(nested.firstName, decoded.first!.firstName)
XCTAssertEqual(nested.lastName, decoded.first!.lastName)
} catch {
XCTFail(error.localizedDescription)
}
} else {
XCTFail("Failed to convert " + string)
}
}
}
func testStringStoredInArray() throws {
struct TestStruct : PersistableRecord, FetchableRecord, Codable {
static let databaseTableName = "t1"
let numbers: [String]
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(table: "t1") { t in
t.column("numbers", .text)
}
let model = TestStruct(numbers: ["test1", "test2", "test3"])
try model.insert(db)
// Encodable has a default implementation which encodes a model to JSON as String.
// We expect here JSON in the form of a String
guard let fetchModel = try TestStruct.fetchOne(db) else {
XCTFail("Could not find record in db")
return
}
print(fetchModel.numbers.first!)
XCTAssertEqual(model.numbers, fetchModel.numbers)
}
}
func testOptionalStringStoredInArray() throws {
struct TestStruct : PersistableRecord, FetchableRecord, Codable {
static let databaseTableName = "t1"
let numbers: [String]?
}
let dbQueue = try makeDatabaseQueue()
try dbQueue.inDatabase { db in
try db.create(table: "t1") { t in
t.column("numbers", .text)
}
let model = TestStruct(numbers: ["test1", "test2", "test3"])
try model.insert(db)
// Encodable has a default implementation which encodes a model to JSON as String.
// We expect here JSON in the form of a String
guard let fetchModel = try TestStruct.fetchOne(db) else {
XCTFail("Could not find record in db")
return
}
XCTAssertEqual(model.numbers, fetchModel.numbers)
}
}
}