forked from vapor/postgres-nio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPostgresErrorTests.swift
137 lines (124 loc) · 6.37 KB
/
PostgresErrorTests.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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
@testable import PostgresNIO
import XCTest
import NIOCore
final class PSQLErrorTests: XCTestCase {
func testPostgresBindingsDescription() {
let testBinds1 = PostgresBindings(capacity: 0)
var testBinds2 = PostgresBindings(capacity: 1)
var testBinds3 = PostgresBindings(capacity: 2)
testBinds2.append(1, context: .default)
testBinds3.appendUnprotected(1, context: .default)
testBinds3.appendUnprotected("foo", context: .default)
testBinds3.append("secret", context: .default)
XCTAssertEqual(String(describing: testBinds1), "[]")
XCTAssertEqual(String(reflecting: testBinds1), "[]")
XCTAssertEqual(String(describing: testBinds2), "[****]")
XCTAssertEqual(String(reflecting: testBinds2), "[(****; BIGINT; format: binary)]")
XCTAssertEqual(String(describing: testBinds3), #"[1, "foo", ****]"#)
XCTAssertEqual(String(reflecting: testBinds3), #"[(1; BIGINT; format: binary), ("foo"; TEXT; format: binary), (****; TEXT; format: binary)]"#)
}
func testPostgresQueryDescription() {
let testBinds1 = PostgresBindings(capacity: 0)
var testBinds2 = PostgresBindings(capacity: 1)
testBinds2.append(1, context: .default)
let testQuery1 = PostgresQuery(unsafeSQL: "TEST QUERY")
let testQuery2 = PostgresQuery(unsafeSQL: "TEST QUERY", binds: testBinds1)
let testQuery3 = PostgresQuery(unsafeSQL: "TEST QUERY", binds: testBinds2)
XCTAssertEqual(String(describing: testQuery1), "TEST QUERY []")
XCTAssertEqual(String(reflecting: testQuery1), "PostgresQuery(sql: TEST QUERY, binds: [])")
XCTAssertEqual(String(describing: testQuery2), "TEST QUERY []")
XCTAssertEqual(String(reflecting: testQuery2), "PostgresQuery(sql: TEST QUERY, binds: [])")
XCTAssertEqual(String(describing: testQuery3), "TEST QUERY [****]")
XCTAssertEqual(String(reflecting: testQuery3), "PostgresQuery(sql: TEST QUERY, binds: [(****; BIGINT; format: binary)])")
}
func testPSQLErrorDescription() {
var error1 = PSQLError.server(.init(fields: [.localizedSeverity: "ERROR", .severity: "ERROR", .sqlState: "00000", .message: "Test message", .detail: "More test message", .hint: "It's a test, that's your hint", .position: "1", .schemaName: "testsch", .tableName: "testtab", .columnName: "testcol", .dataTypeName: "testtyp", .constraintName: "testcon", .file: #fileID, .line: "0", .routine: #function]))
var testBinds = PostgresBindings(capacity: 1)
testBinds.append(1, context: .default)
error1.query = .init(unsafeSQL: "TEST QUERY", binds: testBinds)
XCTAssertEqual(String(describing: error1), """
PSQLError – Generic description to prevent accidental leakage of sensitive data. For debugging details, use `String(reflecting: error)`.
""")
XCTAssertEqual(String(reflecting: error1), """
PSQLError(code: server, serverInfo: [sqlState: 00000, detail: More test message, file: PostgresNIOTests/PostgresErrorTests.swift, hint: It's a test, that's your hint, line: 0, message: Test message, position: 1, routine: testPSQLErrorDescription(), localizedSeverity: ERROR, severity: ERROR, columnName: testcol, dataTypeName: testtyp, constraintName: testcon, schemaName: testsch, tableName: testtab], query: PostgresQuery(sql: TEST QUERY, binds: [(****; BIGINT; format: binary)]))
""")
}
}
final class PostgresDecodingErrorTests: XCTestCase {
func testPostgresDecodingErrorEquality() {
let error1 = PostgresDecodingError(
code: .typeMismatch,
columnName: "column",
columnIndex: 0,
targetType: String.self,
postgresType: .text,
postgresFormat: .binary,
postgresData: ByteBuffer(string: "hello world"),
file: "foo.swift",
line: 123
)
let error2 = PostgresDecodingError(
code: .typeMismatch,
columnName: "column",
columnIndex: 0,
targetType: Int.self,
postgresType: .text,
postgresFormat: .binary,
postgresData: ByteBuffer(string: "hello world"),
file: "foo.swift",
line: 123
)
XCTAssertNotEqual(error1, error2)
let error3 = error1
XCTAssertEqual(error1, error3)
}
func testPostgresDecodingErrorDescription() {
let error1 = PostgresDecodingError(
code: .typeMismatch,
columnName: "column",
columnIndex: 0,
targetType: String.self,
postgresType: .text,
postgresFormat: .binary,
postgresData: ByteBuffer(string: "hello world"),
file: "foo.swift",
line: 123
)
let error2 = PostgresDecodingError(
code: .missingData,
columnName: "column",
columnIndex: 0,
targetType: [[String: String]].self,
postgresType: .jsonbArray,
postgresFormat: .binary,
postgresData: nil,
file: "bar.swift",
line: 123
)
// Plain description
XCTAssertEqual(String(describing: error1), """
PostgresDecodingError – Generic description to prevent accidental leakage of sensitive data. For debugging details, use `String(reflecting: error)`.
""")
XCTAssertEqual(String(describing: error2), """
PostgresDecodingError – Generic description to prevent accidental leakage of sensitive data. For debugging details, use `String(reflecting: error)`.
""")
// Extended debugDescription
XCTAssertEqual(String(reflecting: error1), """
PostgresDecodingError(code: typeMismatch,\
columnName: "column", columnIndex: 0,\
targetType: Swift.String,\
postgresType: TEXT, postgresFormat: binary,\
postgresData: \(error1.postgresData?.debugDescription ?? "nil"),\
file: foo.swift, line: 123\
)
""")
XCTAssertEqual(String(reflecting: error2), """
PostgresDecodingError(code: missingData,\
columnName: "column", columnIndex: 0,\
targetType: Swift.Array<Swift.Dictionary<Swift.String, Swift.String>>,\
postgresType: JSONB[], postgresFormat: binary,\
file: bar.swift, line: 123\
)
""")
}
}