Skip to content

Commit

Permalink
Added tests, changed exception
Browse files Browse the repository at this point in the history
  • Loading branch information
svats0001 committed Nov 29, 2024
1 parent 9d05008 commit 2186982
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import io.asyncer.r2dbc.mysql.constant.MySqlType;
import io.netty.buffer.ByteBuf;
import io.netty.buffer.ByteBufAllocator;
import io.r2dbc.spi.R2dbcNonTransientResourceException;
import reactor.core.publisher.Mono;

/**
Expand Down Expand Up @@ -55,15 +56,17 @@ public Boolean decode(ByteBuf value, MySqlReadableMetadata metadata, Class<?> ta
} else if (s.equalsIgnoreCase("N") || s.equalsIgnoreCase("no") ||
s.equalsIgnoreCase("F") || s.equalsIgnoreCase("false")) {
return createFromLong(0);
} else if (s.matches("-?\\d*\\.\\d*") || s.matches("-?\\d*\\.\\d+[eE]-?\\d+")) {
} else if (s.matches("-?\\d*\\.\\d*") || s.matches("-?\\d*\\.\\d+[eE]-?\\d+")
|| s.matches("-?\\d*[eE]-?\\d+")) {
return createFromDouble(Double.parseDouble(s));
} else if (s.matches("-?\\d+")) {
if (!CodecUtils.isGreaterThanLongMax(s)) {
return createFromLong(CodecUtils.parseLong(value));
}
return createFromBigInteger(new BigInteger(s));
}
throw new IllegalArgumentException("Unable to interpret string: " + s);
throw new R2dbcNonTransientResourceException("The value '" + s + "' of type '" + dataType +
"' cannot be encoded into a Boolean.", "22018");
}

return binary || dataType == MySqlType.BIT ? value.readBoolean() : value.readByte() != '0';
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,9 @@ void decodeString() {
Decoding d14 = new Decoding(Unpooled.copyDouble(26.57d), 26.57d, MySqlType.DOUBLE);
Decoding d15 = new Decoding(Unpooled.copiedBuffer(bOne), bOne, MySqlType.TINYINT);
Decoding d16 = new Decoding(Unpooled.copiedBuffer(bZero), bZero, MySqlType.TINYINT);
Decoding d17 = new Decoding(Unpooled.copiedBuffer("1e4", c), "1e4", MySqlType.VARCHAR);
Decoding d18 = new Decoding(Unpooled.copiedBuffer("-1.34e10", c), "-1.34e10", MySqlType.VARCHAR);
Decoding d19 = new Decoding(Unpooled.copiedBuffer("-0", c), "-0", MySqlType.VARCHAR);

assertThat(codec.decode(d1.content(), d1.metadata(), Boolean.class, false, ConnectionContextTest.mock()))
.as("Decode failed, %s", d1)
Expand Down Expand Up @@ -151,7 +154,19 @@ void decodeString() {
.isEqualTo(true);

assertThat(codec.decode(d16.content(), d16.metadata(), Boolean.class, true, ConnectionContextTest.mock()))
.as("Decode failed, %s", d14)
.as("Decode failed, %s", d16)
.isEqualTo(false);

assertThat(codec.decode(d17.content(), d17.metadata(), Boolean.class, false, ConnectionContextTest.mock()))
.as("Decode failed, %s", d17)
.isEqualTo(true);

assertThat(codec.decode(d18.content(), d18.metadata(), Boolean.class, false, ConnectionContextTest.mock()))
.as("Decode failed, %s", d18)
.isEqualTo(false);

assertThat(codec.decode(d19.content(), d19.metadata(), Boolean.class, false, ConnectionContextTest.mock()))
.as("Decode failed, %s", d19)
.isEqualTo(false);
}
}

0 comments on commit 2186982

Please sign in to comment.