Skip to content

Commit bc8d802

Browse files
committed
add test for #21
1 parent c0a2d24 commit bc8d802

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

Sources/FluentPostgreSQL/PostgreSQLDatabase+SchemaSupporting.swift

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,9 @@ extension PostgreSQLDatabase: SchemaSupporting, IndexSupporting {
6161
var schemaQuery = schema.makeSchemaQuery(dataTypeFactory: dataType)
6262
schema.applyReferences(to: &schemaQuery)
6363
let sqlString = PostgreSQLSQLSerializer().serialize(schema: schemaQuery)
64+
if let logger = connection.logger {
65+
logger.log(query: sqlString, parameters: [])
66+
}
6467
return try connection.query(sqlString).map(to: Void.self) { rows in
6568
assert(rows.count == 0)
6669
}.flatMap(to: Void.self) {

Sources/FluentPostgreSQL/PostgreSQLType.swift

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,9 @@
1+
import CodableKit
12
import Foundation
23

34
/// A type that is compatible with PostgreSQL schema and data.
4-
public protocol PostgreSQLType: PostgreSQLColumnStaticRepresentable, PostgreSQLDataConvertible { }
5+
public protocol PostgreSQLType:
6+
PostgreSQLColumnStaticRepresentable, PostgreSQLDataConvertible { }
57

68
extension PostgreSQLColumnStaticRepresentable where Self: PostgreSQLDataConvertible {
79
/// The `PostgreSQLColumn` type that best represents this type.
@@ -14,6 +16,8 @@ public protocol PostgreSQLJSONType: PostgreSQLType, PostgreSQLJSONCustomConverti
1416
/// A type that is supports being represented as T[] in a PostgreSQL database.
1517
public protocol PostgreSQLArrayType: PostgreSQLType, PostgreSQLArrayCustomConvertible { }
1618

19+
public protocol PostgreSQLEnumType: PostgreSQLType, Codable, TupleKeyStringDecodable, RawRepresentable where Self.RawValue: PostgreSQLDataConvertible { }
20+
1721
/// MARK: Default Implementations
1822

1923
extension Data: PostgreSQLType { }

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -148,6 +148,37 @@ class FluentPostgreSQLTests: XCTestCase {
148148
XCTAssertEqual(stuff.first?.id, 2)
149149
}
150150

151+
func testGH21() throws {
152+
/// - types
153+
enum PetType: Int, PostgreSQLEnumType {
154+
static let keyString: TupleMap = (.cat, .dog)
155+
case cat = 1
156+
case dog = 2
157+
}
158+
struct Pet: PostgreSQLModel, Migration {
159+
static let entity = "pets"
160+
var id: Int?
161+
var type: PetType
162+
var name: String
163+
}
164+
165+
/// - prepare db
166+
benchmarker.database.enableLogging(using: .print)
167+
let conn = try benchmarker.pool.requestConnection().wait()
168+
try? Pet.revert(on: conn).wait()
169+
try Pet.prepare(on: conn).wait()
170+
171+
/// - tests
172+
_ = try Pet(id: nil, type: .cat, name: "Ziz").save(on: conn).wait()
173+
_ = try Pet(id: nil, type: .dog, name: "Spud").save(on: conn).wait()
174+
let cats = try Pet.query(on: conn).filter(\.type == .cat).all().wait()
175+
let dogs = try Pet.query(on: conn).filter(\.type == .dog).all().wait()
176+
XCTAssertEqual(cats.count, 1)
177+
XCTAssertEqual(cats.first?.name, "Ziz")
178+
XCTAssertEqual(dogs.count, 1)
179+
XCTAssertEqual(dogs.first?.name, "Spud")
180+
}
181+
151182
static let allTests = [
152183
("testSchema", testSchema),
153184
("testModels", testModels),
@@ -163,6 +194,7 @@ class FluentPostgreSQLTests: XCTestCase {
163194
("testIndexSupporting", testIndexSupporting),
164195
("testMinimumViableModelDeclaration", testMinimumViableModelDeclaration),
165196
("testGH24", testGH24),
197+
("testGH21", testGH21),
166198
]
167199
}
168200

0 commit comments

Comments
 (0)