forked from pgjdbc/r2dbc-postgresql
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
adding support for Month data type - pgjdbcgh-591
- Loading branch information
1 parent
626efb9
commit 986ca22
Showing
3 changed files
with
139 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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
109
src/test/java/io/r2dbc/postgresql/codec/MonthCodecTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} | ||
} |