use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.
the class TupleCodecTest method should_format_tuple.
@Test
public void should_format_tuple() {
TupleValue tuple = tupleType.newValue();
tuple = tuple.setInt(0, 1);
tuple = tuple.setToNull(1);
tuple = tuple.setString(2, "a");
assertThat(format(tuple)).isEqualTo("(1,NULL,'a')");
verify(intCodec).format(1);
verify(doubleCodec).format(null);
verify(textCodec).format("a");
}
use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.
the class TupleCodecTest method should_parse_partial_tuple.
@Test
public void should_parse_partial_tuple() {
TupleValue tuple = parse("(1,NULL)");
assertThat(tuple.getInt(0)).isEqualTo(1);
assertThat(tuple.isNull(1)).isTrue();
assertThat(tuple.isNull(2)).isTrue();
verify(intCodec).parse("1");
verify(doubleCodec).parse("NULL");
verifyNoMoreInteractions(textCodec);
}
use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.
the class TupleCodecTest method should_parse_empty_tuple.
@Test
public void should_parse_empty_tuple() {
TupleValue tuple = parse("()");
assertThat(tuple.isNull(0)).isTrue();
assertThat(tuple.isNull(1)).isTrue();
assertThat(tuple.isNull(2)).isTrue();
verifyNoMoreInteractions(intCodec);
verifyNoMoreInteractions(doubleCodec);
verifyNoMoreInteractions(textCodec);
}
use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.
the class TupleCodecTest method should_encode_tuple.
@Test
public void should_encode_tuple() {
TupleValue tuple = tupleType.newValue();
tuple = tuple.setInt(0, 1);
tuple = tuple.setToNull(1);
tuple = tuple.setString(2, "a");
assertThat(encode(tuple)).isEqualTo("0x" + // size and contents of field 0
("00000004" + "00000001") + // null field 1
"ffffffff" + // size and contents of field 2
("00000001" + "61"));
verify(intCodec).encodePrimitive(1, ProtocolVersion.DEFAULT);
// null values are handled directly in the tuple codec, without calling the child codec:
verifyZeroInteractions(doubleCodec);
verify(textCodec).encode("a", ProtocolVersion.DEFAULT);
}
use of com.datastax.oss.driver.api.core.data.TupleValue in project java-driver by datastax.
the class DataTypeIT method readValue.
private <K> void readValue(Statement<?> select, DataType dataType, K value, K expectedPrimitiveValue) {
TypeCodec<Object> codec = SESSION_RULE.session().getContext().getCodecRegistry().codecFor(dataType);
ResultSet result = SESSION_RULE.session().execute(select);
String columnName = columnNameFor(dataType);
List<Row> rows = result.all();
assertThat(rows).hasSize(1);
Row row = rows.iterator().next();
K expectedValue = expectedPrimitiveValue != null ? expectedPrimitiveValue : value;
switch(dataType.getProtocolCode()) {
case ProtocolConstants.DataType.ASCII:
case ProtocolConstants.DataType.VARCHAR:
assertThat(row.getString(columnName)).isEqualTo(expectedValue);
assertThat(row.getString(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.BIGINT:
assertThat(row.getLong(columnName)).isEqualTo(expectedValue);
assertThat(row.getLong(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.BLOB:
assertThat(row.getByteBuffer(columnName)).isEqualTo(expectedValue);
assertThat(row.getByteBuffer(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.BOOLEAN:
assertThat(row.getBoolean(columnName)).isEqualTo(expectedValue);
assertThat(row.getBoolean(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.DECIMAL:
assertThat(row.getBigDecimal(columnName)).isEqualTo(expectedValue);
assertThat(row.getBigDecimal(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.DOUBLE:
assertThat(row.getDouble(columnName)).isEqualTo(expectedValue);
assertThat(row.getDouble(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.FLOAT:
assertThat(row.getFloat(columnName)).isEqualTo(expectedValue);
assertThat(row.getFloat(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.INET:
assertThat(row.getInetAddress(columnName)).isEqualTo(expectedValue);
assertThat(row.getInetAddress(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TINYINT:
assertThat(row.getByte(columnName)).isEqualTo(expectedValue);
assertThat(row.getByte(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.SMALLINT:
assertThat(row.getShort(columnName)).isEqualTo(expectedValue);
assertThat(row.getShort(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.INT:
assertThat(row.getInt(columnName)).isEqualTo(expectedValue);
assertThat(row.getInt(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.DURATION:
assertThat(row.getCqlDuration(columnName)).isEqualTo(expectedValue);
assertThat(row.getCqlDuration(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TIMESTAMP:
assertThat(row.getInstant(columnName)).isEqualTo(expectedValue);
assertThat(row.getInstant(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.DATE:
assertThat(row.getLocalDate(columnName)).isEqualTo(expectedValue);
assertThat(row.getLocalDate(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TIME:
assertThat(row.getLocalTime(columnName)).isEqualTo(expectedValue);
assertThat(row.getLocalTime(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TIMEUUID:
case ProtocolConstants.DataType.UUID:
assertThat(row.getUuid(columnName)).isEqualTo(expectedValue);
assertThat(row.getUuid(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.VARINT:
assertThat(row.getBigInteger(columnName)).isEqualTo(expectedValue);
assertThat(row.getBigInteger(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.CUSTOM:
if (((CustomType) dataType).getClassName().equals("org.apache.cassandra.db.marshal.DurationType")) {
assertThat(row.getCqlDuration(columnName)).isEqualTo(expectedValue);
assertThat(row.getCqlDuration(0)).isEqualTo(expectedValue);
break;
}
// fall through
case ProtocolConstants.DataType.LIST:
case ProtocolConstants.DataType.MAP:
case ProtocolConstants.DataType.SET:
assertThat(row.get(columnName, codec)).isEqualTo(expectedValue);
assertThat(row.get(0, codec)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.TUPLE:
TupleValue returnedValue = row.getTupleValue(columnName);
TupleValue exValue = (TupleValue) expectedValue;
assertThat(returnedValue.getType()).isEqualTo(exValue.getType());
assertThat(row.getTupleValue(columnName)).isEqualTo(expectedValue);
assertThat(row.getTupleValue(0)).isEqualTo(expectedValue);
break;
case ProtocolConstants.DataType.UDT:
UdtValue returnedUdtValue = row.getUdtValue(columnName);
UdtValue exUdtValue = (UdtValue) expectedValue;
assertThat(returnedUdtValue.getType()).isEqualTo(exUdtValue.getType());
assertThat(row.getUdtValue(columnName)).isEqualTo(expectedValue);
assertThat(row.getUdtValue(0)).isEqualTo(expectedValue);
break;
default:
fail("Unhandled DataType " + dataType);
}
if (value == null) {
assertThat(row.isNull(columnName)).isTrue();
assertThat(row.isNull(0)).isTrue();
}
// Decode directly using the codec
ProtocolVersion protocolVersion = SESSION_RULE.session().getContext().getProtocolVersion();
assertThat(codec.decode(row.getBytesUnsafe(columnName), protocolVersion)).isEqualTo(value);
assertThat(codec.decode(row.getBytesUnsafe(0), protocolVersion)).isEqualTo(value);
}
Aggregations