|
| 1 | +package io.r2dbc.postgresql.codec; |
| 2 | + |
| 3 | +import io.netty.buffer.ByteBuf; |
| 4 | +import io.r2dbc.postgresql.client.Parameter; |
| 5 | +import io.r2dbc.postgresql.client.ParameterAssert; |
| 6 | +import org.junit.jupiter.api.Test; |
| 7 | + |
| 8 | +import static io.r2dbc.postgresql.client.Parameter.NULL_VALUE; |
| 9 | +import static io.r2dbc.postgresql.message.Format.FORMAT_BINARY; |
| 10 | +import static io.r2dbc.postgresql.message.Format.FORMAT_TEXT; |
| 11 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.LINE; |
| 12 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.POINT; |
| 13 | +import static io.r2dbc.postgresql.type.PostgresqlObjectId.VARCHAR; |
| 14 | +import static io.r2dbc.postgresql.util.ByteBufUtils.encode; |
| 15 | +import static io.r2dbc.postgresql.util.TestByteBufAllocator.TEST; |
| 16 | +import static org.assertj.core.api.Assertions.assertThat; |
| 17 | +import static org.assertj.core.api.Assertions.assertThatIllegalArgumentException; |
| 18 | + |
| 19 | +/** |
| 20 | + * Unit tests for {@link LineCodec}. |
| 21 | + */ |
| 22 | +public class LineCodecUnitTest { |
| 23 | + |
| 24 | + private static final int dataType = LINE.getObjectId(); |
| 25 | + |
| 26 | + @Test |
| 27 | + void constructorNoByteBufAllocator() { |
| 28 | + assertThatIllegalArgumentException().isThrownBy(() -> new LineCodec(null)) |
| 29 | + .withMessage("byteBufAllocator must not be null"); |
| 30 | + } |
| 31 | + |
| 32 | + @Test |
| 33 | + void decode() { |
| 34 | + Line line = Line.of(-10.42, 3.14, 5.24); |
| 35 | + |
| 36 | + ByteBuf lineTextFormat = encode(TEST, "{-10.42,3.14,5.24}"); |
| 37 | + assertThat(new LineCodec(TEST).decode(lineTextFormat, dataType, FORMAT_TEXT, Line.class)) |
| 38 | + .isEqualTo(line); |
| 39 | + |
| 40 | + ByteBuf lineByteFormat = TEST.buffer(24) |
| 41 | + .writeDouble(-10.42).writeDouble(3.14).writeDouble(5.24); |
| 42 | + assertThat(new LineCodec(TEST).decode(lineByteFormat, dataType, FORMAT_BINARY, Line.class)) |
| 43 | + .isEqualTo(line); |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + void decodeNoByteBuf() { |
| 48 | + assertThat(new LineCodec(TEST).decode(null, dataType, FORMAT_TEXT, Line.class)).isNull(); |
| 49 | + } |
| 50 | + |
| 51 | + @Test |
| 52 | + void doCanDecode() { |
| 53 | + LineCodec codec = new LineCodec(TEST); |
| 54 | + |
| 55 | + assertThat(codec.doCanDecode(LINE, FORMAT_BINARY)).isTrue(); |
| 56 | + assertThat(codec.doCanDecode(VARCHAR, FORMAT_TEXT)).isFalse(); |
| 57 | + assertThat(codec.doCanDecode(POINT, FORMAT_TEXT)).isFalse(); |
| 58 | + } |
| 59 | + |
| 60 | + @Test |
| 61 | + void doCanDecodeNoFormat() { |
| 62 | + assertThatIllegalArgumentException().isThrownBy(() -> new LineCodec(TEST).doCanDecode(LINE, null)) |
| 63 | + .withMessage("format must not be null"); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + void doCanDecodeNoType() { |
| 68 | + assertThatIllegalArgumentException().isThrownBy(() -> new LineCodec(TEST).doCanDecode(null, FORMAT_TEXT)) |
| 69 | + .withMessage("type must not be null"); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void doEncode() { |
| 74 | + Line line = Line.of(-10.42, 3.14, 5.24); |
| 75 | + |
| 76 | + ParameterAssert.assertThat(new LineCodec(TEST).doEncode(line)) |
| 77 | + .hasFormat(FORMAT_BINARY) |
| 78 | + .hasType(dataType) |
| 79 | + .hasValue(TEST.buffer(24) |
| 80 | + .writeDouble(-10.42) |
| 81 | + .writeDouble(3.14) |
| 82 | + .writeDouble(5.24) |
| 83 | + ); |
| 84 | + } |
| 85 | + |
| 86 | + @Test |
| 87 | + void doEncodeNoValue() { |
| 88 | + assertThatIllegalArgumentException().isThrownBy(() -> new LineCodec(TEST).doEncode(null)) |
| 89 | + .withMessage("value must not be null"); |
| 90 | + } |
| 91 | + |
| 92 | + @Test |
| 93 | + void encodeNull() { |
| 94 | + ParameterAssert.assertThat(new LineCodec(TEST).encodeNull()) |
| 95 | + .isEqualTo(new Parameter(FORMAT_BINARY, dataType, NULL_VALUE)); |
| 96 | + } |
| 97 | + |
| 98 | +} |
0 commit comments