Skip to content

Commit 549b17f

Browse files
authored
Move pre 1.5.0 unit tests into the unit test target (#196)
To make sure the unit test codecov report is correct, move pre 1.5.0 unit tests into the unit test target.
1 parent 8115741 commit 549b17f

File tree

4 files changed

+118
-104
lines changed

4 files changed

+118
-104
lines changed

Tests/IntegrationTests/PostgresNIOTests.swift

Lines changed: 0 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -659,22 +659,6 @@ final class PostgresNIOTests: XCTestCase {
659659
}
660660
}
661661

662-
func testJSONBConvertible() {
663-
struct Object: PostgresJSONBCodable {
664-
let foo: Int
665-
let bar: Int
666-
}
667-
668-
XCTAssertEqual(Object.postgresDataType, .jsonb)
669-
670-
let postgresData = Object(foo: 1, bar: 2).postgresData
671-
XCTAssertEqual(postgresData?.type, .jsonb)
672-
673-
let object = Object(postgresData: postgresData!)
674-
XCTAssertEqual(object?.foo, 1)
675-
XCTAssertEqual(object?.bar, 2)
676-
}
677-
678662
func testRemoteTLSServer() {
679663
// postgres://uymgphwj:[email protected]:5432/uymgphwj
680664
var conn: PostgresConnection?
@@ -899,38 +883,6 @@ final class PostgresNIOTests: XCTestCase {
899883
XCTAssertEqual(rows?.first?.column("int")?.uint8, 5)
900884
}
901885

902-
func testMessageDecoder() {
903-
let sample: [UInt8] = [
904-
0x52, // R - authentication
905-
0x00, 0x00, 0x00, 0x0C, // length = 12
906-
0x00, 0x00, 0x00, 0x05, // md5
907-
0x01, 0x02, 0x03, 0x04, // salt
908-
0x4B, // B - backend key data
909-
0x00, 0x00, 0x00, 0x0C, // length = 12
910-
0x05, 0x05, 0x05, 0x05, // process id
911-
0x01, 0x01, 0x01, 0x01, // secret key
912-
]
913-
var input = ByteBufferAllocator().buffer(capacity: 0)
914-
input.writeBytes(sample)
915-
916-
let output: [PostgresMessage] = [
917-
PostgresMessage(identifier: .authentication, bytes: [
918-
0x00, 0x00, 0x00, 0x05,
919-
0x01, 0x02, 0x03, 0x04,
920-
]),
921-
PostgresMessage(identifier: .backendKeyData, bytes: [
922-
0x05, 0x05, 0x05, 0x05,
923-
0x01, 0x01, 0x01, 0x01,
924-
])
925-
]
926-
XCTAssertNoThrow(try XCTUnwrap(ByteToMessageDecoderVerifier.verifyDecoder(
927-
inputOutputPairs: [(input, output)],
928-
decoderFactory: {
929-
PostgresMessageDecoder()
930-
}
931-
)))
932-
}
933-
934886
func testPreparedQuery() {
935887
var conn: PostgresConnection?
936888
XCTAssertNoThrow(conn = try PostgresConnection.test(on: eventLoop).wait())
@@ -1135,62 +1087,6 @@ final class PostgresNIOTests: XCTestCase {
11351087
XCTAssertEqual(rows?.first?.column("min64")?.int64, .min)
11361088
XCTAssertEqual(rows?.first?.column("max64")?.int64, .max)
11371089
}
1138-
1139-
// https://github.com/vapor/postgres-nio/issues/126
1140-
func testCustomJSONEncoder() {
1141-
let previousDefaultJSONEncoder = PostgresNIO._defaultJSONEncoder
1142-
defer {
1143-
PostgresNIO._defaultJSONEncoder = previousDefaultJSONEncoder
1144-
}
1145-
final class CustomJSONEncoder: PostgresJSONEncoder {
1146-
var didEncode = false
1147-
func encode<T>(_ value: T) throws -> Data where T : Encodable {
1148-
self.didEncode = true
1149-
return try JSONEncoder().encode(value)
1150-
}
1151-
}
1152-
struct Object: Codable {
1153-
var foo: Int
1154-
var bar: Int
1155-
}
1156-
let customJSONEncoder = CustomJSONEncoder()
1157-
PostgresNIO._defaultJSONEncoder = customJSONEncoder
1158-
XCTAssertNoThrow(try PostgresData(json: Object(foo: 1, bar: 2)))
1159-
XCTAssert(customJSONEncoder.didEncode)
1160-
1161-
let customJSONBEncoder = CustomJSONEncoder()
1162-
PostgresNIO._defaultJSONEncoder = customJSONBEncoder
1163-
XCTAssertNoThrow(try PostgresData(json: Object(foo: 1, bar: 2)))
1164-
XCTAssert(customJSONBEncoder.didEncode)
1165-
}
1166-
1167-
// https://github.com/vapor/postgres-nio/issues/126
1168-
func testCustomJSONDecoder() {
1169-
let previousDefaultJSONDecoder = PostgresNIO._defaultJSONDecoder
1170-
defer {
1171-
PostgresNIO._defaultJSONDecoder = previousDefaultJSONDecoder
1172-
}
1173-
final class CustomJSONDecoder: PostgresJSONDecoder {
1174-
var didDecode = false
1175-
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
1176-
self.didDecode = true
1177-
return try JSONDecoder().decode(type, from: data)
1178-
}
1179-
}
1180-
struct Object: Codable {
1181-
var foo: Int
1182-
var bar: Int
1183-
}
1184-
let customJSONDecoder = CustomJSONDecoder()
1185-
PostgresNIO._defaultJSONDecoder = customJSONDecoder
1186-
XCTAssertNoThrow(try PostgresData(json: Object(foo: 1, bar: 2)).json(as: Object.self))
1187-
XCTAssert(customJSONDecoder.didDecode)
1188-
1189-
let customJSONBDecoder = CustomJSONDecoder()
1190-
PostgresNIO._defaultJSONDecoder = customJSONBDecoder
1191-
XCTAssertNoThrow(try PostgresData(json: Object(foo: 1, bar: 2)).json(as: Object.self))
1192-
XCTAssert(customJSONBDecoder.didDecode)
1193-
}
11941090
}
11951091

11961092
let isLoggingConfigured: Bool = {
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import PostgresNIO
2+
import XCTest
3+
4+
class PostgresData_JSONTests: XCTestCase {
5+
func testJSONBConvertible() {
6+
struct Object: PostgresJSONBCodable {
7+
let foo: Int
8+
let bar: Int
9+
}
10+
11+
XCTAssertEqual(Object.postgresDataType, .jsonb)
12+
13+
let postgresData = Object(foo: 1, bar: 2).postgresData
14+
XCTAssertEqual(postgresData?.type, .jsonb)
15+
16+
let object = Object(postgresData: postgresData!)
17+
XCTAssertEqual(object?.foo, 1)
18+
XCTAssertEqual(object?.bar, 2)
19+
}
20+
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import PostgresNIO
2+
import XCTest
3+
import NIOTestUtils
4+
5+
class PostgresMessageDecoderTests: XCTestCase {
6+
func testMessageDecoder() {
7+
let sample: [UInt8] = [
8+
0x52, // R - authentication
9+
0x00, 0x00, 0x00, 0x0C, // length = 12
10+
0x00, 0x00, 0x00, 0x05, // md5
11+
0x01, 0x02, 0x03, 0x04, // salt
12+
0x4B, // B - backend key data
13+
0x00, 0x00, 0x00, 0x0C, // length = 12
14+
0x05, 0x05, 0x05, 0x05, // process id
15+
0x01, 0x01, 0x01, 0x01, // secret key
16+
]
17+
var input = ByteBufferAllocator().buffer(capacity: 0)
18+
input.writeBytes(sample)
19+
20+
let output: [PostgresMessage] = [
21+
PostgresMessage(identifier: .authentication, bytes: [
22+
0x00, 0x00, 0x00, 0x05,
23+
0x01, 0x02, 0x03, 0x04,
24+
]),
25+
PostgresMessage(identifier: .backendKeyData, bytes: [
26+
0x05, 0x05, 0x05, 0x05,
27+
0x01, 0x01, 0x01, 0x01,
28+
])
29+
]
30+
XCTAssertNoThrow(try XCTUnwrap(ByteToMessageDecoderVerifier.verifyDecoder(
31+
inputOutputPairs: [(input, output)],
32+
decoderFactory: {
33+
PostgresMessageDecoder()
34+
}
35+
)))
36+
}
37+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
import NIOCore
2+
import XCTest
3+
import PostgresNIO
4+
5+
class PostgresJSONCodingTests: XCTestCase {
6+
// https://github.com/vapor/postgres-nio/issues/126
7+
func testCustomJSONEncoder() {
8+
let previousDefaultJSONEncoder = PostgresNIO._defaultJSONEncoder
9+
defer {
10+
PostgresNIO._defaultJSONEncoder = previousDefaultJSONEncoder
11+
}
12+
final class CustomJSONEncoder: PostgresJSONEncoder {
13+
var didEncode = false
14+
func encode<T>(_ value: T) throws -> Data where T : Encodable {
15+
self.didEncode = true
16+
return try JSONEncoder().encode(value)
17+
}
18+
}
19+
struct Object: Codable {
20+
var foo: Int
21+
var bar: Int
22+
}
23+
let customJSONEncoder = CustomJSONEncoder()
24+
PostgresNIO._defaultJSONEncoder = customJSONEncoder
25+
XCTAssertNoThrow(try PostgresData(json: Object(foo: 1, bar: 2)))
26+
XCTAssert(customJSONEncoder.didEncode)
27+
28+
let customJSONBEncoder = CustomJSONEncoder()
29+
PostgresNIO._defaultJSONEncoder = customJSONBEncoder
30+
XCTAssertNoThrow(try PostgresData(json: Object(foo: 1, bar: 2)))
31+
XCTAssert(customJSONBEncoder.didEncode)
32+
}
33+
34+
// https://github.com/vapor/postgres-nio/issues/126
35+
func testCustomJSONDecoder() {
36+
let previousDefaultJSONDecoder = PostgresNIO._defaultJSONDecoder
37+
defer {
38+
PostgresNIO._defaultJSONDecoder = previousDefaultJSONDecoder
39+
}
40+
final class CustomJSONDecoder: PostgresJSONDecoder {
41+
var didDecode = false
42+
func decode<T>(_ type: T.Type, from data: Data) throws -> T where T : Decodable {
43+
self.didDecode = true
44+
return try JSONDecoder().decode(type, from: data)
45+
}
46+
}
47+
struct Object: Codable {
48+
var foo: Int
49+
var bar: Int
50+
}
51+
let customJSONDecoder = CustomJSONDecoder()
52+
PostgresNIO._defaultJSONDecoder = customJSONDecoder
53+
XCTAssertNoThrow(try PostgresData(json: Object(foo: 1, bar: 2)).json(as: Object.self))
54+
XCTAssert(customJSONDecoder.didDecode)
55+
56+
let customJSONBDecoder = CustomJSONDecoder()
57+
PostgresNIO._defaultJSONDecoder = customJSONBDecoder
58+
XCTAssertNoThrow(try PostgresData(json: Object(foo: 1, bar: 2)).json(as: Object.self))
59+
XCTAssert(customJSONBDecoder.didDecode)
60+
}
61+
}

0 commit comments

Comments
 (0)