forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathString+PSQLCodableTests.swift
67 lines (54 loc) · 2.16 KB
/
String+PSQLCodableTests.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
59
60
61
62
63
64
65
66
67
import XCTest
import NIOCore
@testable import PostgresNIO
class String_PSQLCodableTests: XCTestCase {
func testEncode() {
let value = "Hello World"
var buffer = ByteBuffer()
value.encode(into: &buffer, context: .default)
XCTAssertEqual(String.psqlType, .text)
XCTAssertEqual(buffer.readString(length: buffer.readableBytes), value)
}
func testDecodeStringFromTextVarchar() {
let expected = "Hello World"
var buffer = ByteBuffer()
buffer.writeString(expected)
let dataTypes: [PostgresDataType] = [
.text, .varchar, .name, .bpchar
]
for dataType in dataTypes {
var loopBuffer = buffer
var result: String?
XCTAssertNoThrow(result = try String(from: &loopBuffer, type: dataType, format: .binary, context: .default))
XCTAssertEqual(result, expected)
}
}
func testDecodeFailureFromInvalidType() {
let buffer = ByteBuffer()
let dataTypes: [PostgresDataType] = [.bool, .float4Array, .float8Array]
for dataType in dataTypes {
var loopBuffer = buffer
XCTAssertThrowsError(try String(from: &loopBuffer, type: dataType, format: .binary, context: .default)) {
XCTAssertEqual($0 as? PostgresDecodingError.Code, .typeMismatch)
}
}
}
func testDecodeFromUUID() {
let uuid = UUID()
var buffer = ByteBuffer()
uuid.encode(into: &buffer, context: .default)
var decoded: String?
XCTAssertNoThrow(decoded = try String(from: &buffer, type: .uuid, format: .binary, context: .default))
XCTAssertEqual(decoded, uuid.uuidString)
}
func testDecodeFailureFromInvalidUUID() {
let uuid = UUID()
var buffer = ByteBuffer()
uuid.encode(into: &buffer, context: .default)
// this makes only 15 bytes readable. this should lead to an error
buffer.moveReaderIndex(forwardBy: 1)
XCTAssertThrowsError(try String(from: &buffer, type: .uuid, format: .binary, context: .default)) {
XCTAssertEqual($0 as? PostgresDecodingError.Code, .failure)
}
}
}