|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package io.r2dbc.postgresql.codec; |
| 18 | + |
| 19 | +import io.r2dbc.postgresql.AbstractIntegrationTests; |
| 20 | +import io.r2dbc.postgresql.PostgresqlConnectionConfiguration; |
| 21 | +import io.r2dbc.postgresql.PostgresqlConnectionFactory; |
| 22 | +import io.r2dbc.postgresql.api.PostgresqlConnection; |
| 23 | +import io.r2dbc.spi.Parameters; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | +import reactor.core.publisher.Mono; |
| 26 | +import reactor.test.StepVerifier; |
| 27 | + |
| 28 | +/** |
| 29 | + * Integration tests for {@link StringCodec} usage with CITEXT and customization options. |
| 30 | + */ |
| 31 | +class StringCodecIntegrationTests extends AbstractIntegrationTests { |
| 32 | + |
| 33 | + @Test |
| 34 | + void stringCodecShouldConsiderCIText() { |
| 35 | + |
| 36 | + SERVER.getJdbcOperations().execute("CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA public"); |
| 37 | + |
| 38 | + SERVER.getJdbcOperations().execute("DROP TABLE IF EXISTS test"); |
| 39 | + SERVER.getJdbcOperations().execute("CREATE TABLE test (ci CITEXT, cs VARCHAR)"); |
| 40 | + SERVER.getJdbcOperations().execute("INSERT INTO test VALUES('HeLlO', 'HeLlO')"); |
| 41 | + |
| 42 | + this.connection.createStatement("SELECT ci FROM test WHERE ci = $1::citext") |
| 43 | + .bind("$1", "Hello") |
| 44 | + .execute() |
| 45 | + .flatMap(it -> it.map(r -> r.get("ci"))) |
| 46 | + .as(StepVerifier::create) |
| 47 | + .expectNext("HeLlO") |
| 48 | + .verifyComplete(); |
| 49 | + |
| 50 | + this.connection.createStatement("SELECT ci FROM test WHERE ci = $1") |
| 51 | + .bind("$1", Parameters.in(PostgresqlObjectId.UNSPECIFIED, "Hello")) |
| 52 | + .execute() |
| 53 | + .flatMap(it -> it.map(r -> r.get("ci"))) |
| 54 | + .as(StepVerifier::create) |
| 55 | + .expectNext("HeLlO") |
| 56 | + .verifyComplete(); |
| 57 | + |
| 58 | + this.connection.createStatement("SELECT cs::citext = $1 FROM test") |
| 59 | + .bind("$1", Parameters.in(PostgresqlObjectId.UNSPECIFIED, "Hello")) |
| 60 | + .execute() |
| 61 | + .flatMap(it -> it.map(r -> r.get(0))) |
| 62 | + .as(StepVerifier::create) |
| 63 | + .expectNext(true) |
| 64 | + .verifyComplete(); |
| 65 | + |
| 66 | + this.connection.createStatement("SELECT cs::citext = $1 FROM test") |
| 67 | + .bind("$1", "Hello") |
| 68 | + .execute() |
| 69 | + .flatMap(it -> it.map(r -> r.get(0))) |
| 70 | + .as(StepVerifier::create) |
| 71 | + .expectNext(false) |
| 72 | + .verifyComplete(); |
| 73 | + |
| 74 | + SERVER.getJdbcOperations().execute("DROP TABLE test"); |
| 75 | + } |
| 76 | + |
| 77 | + @Test |
| 78 | + void shouldApplyCustomizedCodec() { |
| 79 | + |
| 80 | + SERVER.getJdbcOperations().execute("CREATE EXTENSION IF NOT EXISTS citext WITH SCHEMA public"); |
| 81 | + |
| 82 | + SERVER.getJdbcOperations().execute("DROP TABLE IF EXISTS test"); |
| 83 | + SERVER.getJdbcOperations().execute("CREATE TABLE test ( ci CITEXT, cs VARCHAR)"); |
| 84 | + SERVER.getJdbcOperations().execute("INSERT INTO test VALUES('HELLO', 'HELLO')"); |
| 85 | + |
| 86 | + PostgresqlConnectionFactory custom = getConnectionFactory(builder -> builder.codecRegistrar((connection1, allocator, registry) -> { |
| 87 | + registry.addFirst(new StringCodec(allocator, PostgresqlObjectId.UNSPECIFIED, PostgresqlObjectId.VARCHAR_ARRAY)); |
| 88 | + return Mono.empty(); |
| 89 | + })); |
| 90 | + |
| 91 | + PostgresqlConnection customizedConnection = custom.create().block(); |
| 92 | + |
| 93 | + customizedConnection.createStatement("SELECT cs::citext = $1 FROM test") |
| 94 | + .bind("$1", "Hello") |
| 95 | + .execute() |
| 96 | + .flatMap(it -> it.map(r -> r.get(0))) |
| 97 | + .as(StepVerifier::create) |
| 98 | + .expectNext(true) |
| 99 | + .verifyComplete(); |
| 100 | + |
| 101 | + customizedConnection.close().block(); |
| 102 | + |
| 103 | + SERVER.getJdbcOperations().execute("DROP TABLE test"); |
| 104 | + } |
| 105 | + |
| 106 | +} |
0 commit comments