forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresCellTests.swift
58 lines (51 loc) · 1.77 KB
/
PostgresCellTests.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
@testable import PostgresNIO
import XCTest
import NIOCore
final class PostgresCellTests: XCTestCase {
func testDecodingANonOptionalString() {
let cell = PostgresCell(
bytes: ByteBuffer(string: "Hello world"),
dataType: .text,
format: .binary,
columnName: "hello",
columnIndex: 1
)
var result: String?
XCTAssertNoThrow(result = try cell.decode(String.self, context: .default))
XCTAssertEqual(result, "Hello world")
}
func testDecodingAnOptionalString() {
let cell = PostgresCell(
bytes: nil,
dataType: .text,
format: .binary,
columnName: "hello",
columnIndex: 1
)
var result: String? = "test"
XCTAssertNoThrow(result = try cell.decode(String?.self, context: .default))
XCTAssertNil(result)
}
func testDecodingFailure() {
let cell = PostgresCell(
bytes: ByteBuffer(string: "Hello world"),
dataType: .text,
format: .binary,
columnName: "hello",
columnIndex: 1
)
XCTAssertThrowsError(try cell.decode(Int?.self, context: .default)) {
guard let error = $0 as? PostgresDecodingError else {
return XCTFail("Unexpected error")
}
XCTAssertEqual(error.file, #fileID)
XCTAssertEqual(error.line, #line - 6)
XCTAssertEqual(error.code, .typeMismatch)
XCTAssertEqual(error.columnName, "hello")
XCTAssertEqual(error.columnIndex, 1)
XCTAssert(error.targetType == Int?.self)
XCTAssertEqual(error.postgresType, .text)
XCTAssertEqual(error.postgresFormat, .binary)
}
}
}