Skip to content

Commit 31473c2

Browse files
committed
add raw enum test, fixes #85, addresses #79
1 parent a99b651 commit 31473c2

File tree

2 files changed

+42
-0
lines changed

2 files changed

+42
-0
lines changed

Sources/FluentPostgreSQL/PostgreSQLEnum.swift

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,15 @@ public protocol PostgreSQLEnum: PostgreSQLExpressionRepresentable, CaseIterable,
22
static var postgreSQLEnumTypeName: String { get }
33
}
44

5+
public protocol PostgreSQLRawEnum: RawRepresentable, Codable, CaseIterable, ReflectionDecodable, PostgreSQLDataTypeStaticRepresentable { }
6+
7+
extension PostgreSQLRawEnum where Self.RawValue: PostgreSQLDataTypeStaticRepresentable {
8+
/// See `PostgreSQLDataTypeStaticRepresentable`.
9+
public static var postgreSQLDataType: PostgreSQLDataType {
10+
return RawValue.postgreSQLDataType
11+
}
12+
}
13+
514
extension PostgreSQLEnum {
615
/// See `PostgreSQLEnum`.
716
public static var postgreSQLEnumTypeName: String {

Tests/FluentPostgreSQLTests/FluentPostgreSQLTests.swift

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -459,6 +459,38 @@ class FluentPostgreSQLTests: XCTestCase {
459459
defer { try? C.revert(on: conn).wait() }
460460
}
461461

462+
// https://github.com/vapor/fluent-postgresql/issues/85
463+
func testGH85() throws {
464+
/// The Exact enum from my project
465+
enum Availability: UInt8, PostgreSQLRawEnum {
466+
static var allCases: [Availability] = [.everyday, .sunday, .monday, .tuesday, .wednesday, .thursday, .friday, .saturday]
467+
468+
case everyday
469+
case sunday
470+
case monday
471+
case tuesday
472+
case wednesday
473+
case thursday
474+
case friday
475+
case saturday
476+
}
477+
478+
struct Foo: PostgreSQLModel, Migration {
479+
var id: Int?
480+
var availability: Availability
481+
}
482+
483+
let conn = try benchmarker.pool.requestConnection().wait()
484+
conn.logger = DatabaseLogger(database: .psql, handler: PrintLogHandler())
485+
defer { benchmarker.pool.releaseConnection(conn) }
486+
487+
try Foo.prepare(on: conn).wait()
488+
defer { try? Foo.revert(on: conn).wait() }
489+
490+
let a = Foo(id: nil, availability: .everyday)
491+
_ = try a.save(on: conn).wait()
492+
}
493+
462494
static let allTests = [
463495
("testBenchmark", testBenchmark),
464496
("testNestedStruct", testNestedStruct),
@@ -475,6 +507,7 @@ class FluentPostgreSQLTests: XCTestCase {
475507
("testCustomFilter", testCustomFilter),
476508
("testCreateOrUpdate", testCreateOrUpdate),
477509
("testEnumArray", testEnumArray),
510+
("testGH85", testGH85),
478511
]
479512
}
480513

0 commit comments

Comments
 (0)