Skip to content

Commit 26dc631

Browse files
committed
tests passing
1 parent 848679b commit 26dc631

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/// A type that is compatible with PostgreSQL schema and data.
2+
public protocol PostgreSQLType: PostgreSQLColumnStaticRepresentable, PostgreSQLDataCustomConvertible { }
3+
4+
/// A type that is supports being represented as JSONB in a PostgreSQL database.
5+
public protocol PostgreSQLJSONType: PostgreSQLType, PostgreSQLJSONCustomConvertible { }
6+
7+
extension PostgreSQLJSONType {
8+
/// The `PostgreSQLColumn` type that best represents this type.
9+
public static var postgreSQLColumn: PostgreSQLColumn { return .init(type: .jsonb) }
10+
}

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 30 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -42,20 +42,19 @@ class FluentPostgreSQLTests: XCTestCase {
4242
try benchmarker.benchmarkAutoincrement_withSchema()
4343
}
4444

45-
// func testNestedStruct() throws {
46-
// database.logger = DatabaseLogger { print($0) }
47-
// let conn = try database.makeConnection(using: .init(), on: eventLoop).await(on: eventLoop)
48-
// try! User.prepare(on: conn).await(on: eventLoop)
49-
//
50-
// let user = try! User(id: nil, name: "Tanner", pet: Pet(name: "Zizek"))
51-
// .save(on: conn).await(on: eventLoop)
52-
//
53-
// let fetched = try! User.query(on: conn).first().await(on: eventLoop)
54-
// print(fetched?.pet)
55-
//
56-
// try User.revert(on: conn).await(on: eventLoop)
57-
// conn.close()
58-
// }
45+
func testNestedStruct() throws {
46+
let conn = try database.makeConnection(using: .init(), on: eventLoop).await(on: eventLoop)
47+
try User.prepare(on: conn).await(on: eventLoop)
48+
49+
let user = try User(id: nil, name: "Tanner", pet: Pet(name: "Zizek"))
50+
.save(on: conn).await(on: eventLoop)
51+
52+
let fetched = try User.query(on: conn).first().await(on: eventLoop)
53+
54+
XCTAssertEqual(user.id, fetched?.id)
55+
try User.revert(on: conn).await(on: eventLoop)
56+
conn.close()
57+
}
5958

6059
static let allTests = [
6160
("testSchema", testSchema),
@@ -67,33 +66,21 @@ class FluentPostgreSQLTests: XCTestCase {
6766
("testAutoincrement", testAutoincrement),
6867
]
6968
}
70-
//
71-
//struct Pet: Codable, PostgreSQLColumnStaticRepresentable, PostgreSQLDataCustomConvertible {
72-
// static let postgreSQLColumn = PostgreSQLColumn(type: .jsonb)
73-
// var name: String
74-
//
75-
// func convertToPostgreSQLData() throws -> PostgreSQLData {
76-
// return try .data(JSONEncoder().encode(self))
77-
// }
78-
//
79-
// static func convertFromPostgreSQLData(from data: PostgreSQLData) throws -> Pet {
80-
// switch data {
81-
// case .data(let data): return try JSONDecoder().decode(Pet.self, from: data)
82-
// default: fatalError()
83-
// }
84-
// }
85-
//}
86-
//
87-
//final class User: PostgreSQLModel, Migration {
88-
// static let idKey = \User.id
89-
// var id: Int?
90-
// var name: String
91-
// var pet: Pet
92-
//
93-
// init(id: Int? = nil, name: String, pet: Pet) {
94-
// self.id = id
95-
// self.name = name
96-
// self.pet = pet
97-
// }
98-
//}
69+
70+
struct Pet: PostgreSQLJSONType {
71+
var name: String
72+
}
73+
74+
final class User: PostgreSQLModel, Migration {
75+
static let idKey = \User.id
76+
var id: Int?
77+
var name: String
78+
var pet: Pet
79+
80+
init(id: Int? = nil, name: String, pet: Pet) {
81+
self.id = id
82+
self.name = name
83+
self.pet = pet
84+
}
85+
}
9986

0 commit comments

Comments
 (0)