Skip to content

Commit

Permalink
adding support for Month data type - pgjdbcgh-591
Browse files Browse the repository at this point in the history
  • Loading branch information
steverigney committed Aug 13, 2023
1 parent 626efb9 commit 986ca22
Show file tree
Hide file tree
Showing 3 changed files with 139 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/main/java/io/r2dbc/postgresql/codec/DefaultCodecs.java
Expand Up @@ -253,14 +253,15 @@ private static List<Codec<?>> getDefaultCodecs(ByteBufAllocator byteBufAllocator
new LocalDateTimeCodec(byteBufAllocator, configuration::getZoneId),
new LocalTimeCodec(byteBufAllocator),
new LongCodec(byteBufAllocator),
new MonthCodec(byteBufAllocator),
new OffsetDateTimeCodec(byteBufAllocator),
new OffsetTimeCodec(byteBufAllocator),
new ShortCodec(byteBufAllocator),
new UriCodec(byteBufAllocator),
new UrlCodec(byteBufAllocator),
new UuidCodec(byteBufAllocator),
new ZoneIdCodec(byteBufAllocator),
new YearCodec(byteBufAllocator),
new ZoneIdCodec(byteBufAllocator),

// JSON
new JsonCodec(byteBufAllocator, preferAttachedBuffers),
Expand Down
28 changes: 28 additions & 0 deletions src/main/java/io/r2dbc/postgresql/codec/MonthCodec.java
@@ -0,0 +1,28 @@
/*
* Copyright 2017 the original author or authors.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* https://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package io.r2dbc.postgresql.codec;

import io.netty.buffer.ByteBufAllocator;

import java.time.Month;

final class MonthCodec extends IntegerCodecDelegate<Month> {

MonthCodec(ByteBufAllocator byteBufAllocator) {
super(Month.class, byteBufAllocator, Month::getValue, Month::of);
}
}
109 changes: 109 additions & 0 deletions src/test/java/io/r2dbc/postgresql/codec/MonthCodecTest.java
@@ -0,0 +1,109 @@
package io.r2dbc.postgresql.codec;

import io.r2dbc.postgresql.client.EncodedParameter;
import io.r2dbc.postgresql.client.ParameterAssert;
import org.junit.jupiter.api.Test;

import java.time.Month;
import java.util.Arrays;
import java.util.function.Consumer;

import static io.r2dbc.postgresql.client.EncodedParameter.NULL_VALUE;
import static io.r2dbc.postgresql.codec.PostgresqlObjectId.*;
import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY;
import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT;
import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException;

class MonthCodecTest {

@Test
void constructorNoByteBufAllocator() {
assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(null))
.withMessage("byteBufAllocator must not be null");
}


@Test
void decode() {
forEveryMonth(m ->
assertThat(new MonthCodec(TEST).decode(TEST.buffer().writeInt(m.getValue()), INT4, FORMAT_BINARY, Month.class)).isEqualTo(m));
}

@Test
void decodeNoByteBuf() {
assertThat(new MonthCodec(TEST).decode(null, INT4.getObjectId(), FORMAT_BINARY, Month.class)).isNull();
}

@Test
void doCanDecode() {
MonthCodec codec = new MonthCodec(TEST);

assertThat(codec.doCanDecode(INT4, FORMAT_BINARY)).isTrue();
assertThat(codec.doCanDecode(INT2, FORMAT_BINARY)).isTrue();
assertThat(codec.doCanDecode(INT8, FORMAT_BINARY)).isTrue();
assertThat(codec.doCanDecode(NUMERIC, FORMAT_TEXT)).isTrue();
assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isFalse();
}

@Test
void doCanDecodeNoType() {
assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).doCanDecode(null, FORMAT_BINARY))
.withMessage("type must not be null");
}

@Test
void doEncodeInt() {

forEveryMonth(m -> {
ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m))
.hasFormat(FORMAT_BINARY)
.hasType(INT4.getObjectId())
.hasValue(TEST.buffer().writeInt(m.getValue()));
});
}

@Test
void doEncodeShort() {
forEveryMonth(m -> {
ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m, INT2))
.hasFormat(FORMAT_BINARY)
.hasType(INT2.getObjectId())
.hasValue(TEST.buffer().writeShort(m.getValue()));
});
}

@Test
void doEncodeLong() {

forEveryMonth(m -> {
ParameterAssert.assertThat(new MonthCodec(TEST).doEncode(m, INT8))
.hasFormat(FORMAT_BINARY)
.hasType(INT8.getObjectId())
.hasValue(TEST.buffer().writeLong(m.getValue()));
});
}

@Test
void doEncodeNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).doEncode(null))
.withMessage("value must not be null");
}

@Test
void encodeItemNoValue() {
assertThatIllegalArgumentException().isThrownBy(() -> new MonthCodec(TEST).encode(null))
.withMessage("value must not be null");
}

@Test
void encodeNull() {
ParameterAssert.assertThat(new MonthCodec(TEST).encodeNull())
.isEqualTo(new EncodedParameter(FORMAT_BINARY, INT4.getObjectId(), NULL_VALUE));
}

private void forEveryMonth(Consumer<Month> assertion) {
Arrays.stream(Month.values()).forEach(assertion);
}
}

0 comments on commit 986ca22

Please sign in to comment.