Skip to content

Commit 64f400f

Browse files
authored
Merge pull request vapor#112 from vapor/zero-numeric
add failing test case for 0 numeric value
2 parents c8b01a3 + 76b12fe commit 64f400f

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

Sources/PostgreSQL/Data/PostgreSQLData+String.swift

+4
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ extension String: PostgreSQLDataConvertible {
2626
/// grab the numeric metadata from the beginning of the array
2727
let metadata = value.extract(PostgreSQLNumericMetadata.self)
2828

29+
guard metadata.ndigits.bigEndian > 0 else {
30+
return "0"
31+
}
32+
2933
var integer = ""
3034
var fractional = ""
3135
for offset in 0..<metadata.ndigits.bigEndian {

Tests/PostgreSQLTests/PostgreSQLConnectionTests.swift

+21
Original file line numberDiff line numberDiff line change
@@ -538,6 +538,26 @@ class PostgreSQLConnectionTests: XCTestCase {
538538
print(c)
539539
}
540540
}
541+
542+
// https://github.com/vapor/postgresql/pull/109
543+
func testZeroNumeric() throws {
544+
let conn = try PostgreSQLConnection.makeTest()
545+
struct ZeroNumeric: PostgreSQLTable, Equatable {
546+
static let sqlTableIdentifierString = "zeronumerictest"
547+
var foo: Int
548+
}
549+
550+
defer { try? conn.drop(table: ZeroNumeric.self).ifExists().run().wait() }
551+
try conn.create(table: ZeroNumeric.self).column(for: \ZeroNumeric.foo, type: .numeric).run().wait()
552+
553+
let a = ZeroNumeric(foo: 0)
554+
try conn.insert(into: ZeroNumeric.self).value(a).run().wait()
555+
let fetch: [ZeroNumeric] = try conn.select().all().from(ZeroNumeric.self).all(decoding: ZeroNumeric.self).wait()
556+
switch fetch.count {
557+
case 1: XCTAssertEqual(fetch[0].foo, 0)
558+
default: XCTFail("invalid row count")
559+
}
560+
}
541561

542562
static var allTests = [
543563
("testBenchmark", testBenchmark),
@@ -558,6 +578,7 @@ class PostgreSQLConnectionTests: XCTestCase {
558578
("testOrderBy", testOrderBy),
559579
("testInvalidDate", testInvalidDate),
560580
("testEmptyArray", testEmptyArray),
581+
("testZeroNumeric", testZeroNumeric),
561582
]
562583
}
563584

0 commit comments

Comments
 (0)