8000 Support Duration type in PrimitiveTypeSamples. (#828) · kecmu/java-driver@f7d47c9 · GitHub 8000
[go: up one dir, main page]

Skip to content

Commit f7d47c9

Browse files
authored
Support Duration type in PrimitiveTypeSamples. (apache#828)
* Support Duration type in PrimitiveTypeSamples. A number of tests use PrimitiveTypeSamples for getting sample values for given DataTypes. Duration was previously omitted from this because it is not supported in Set or Map keys. Special case tests that involve using these samples for Map and Sets so we get better Duration coverage. In addition, this fixes a bug in UserTypesTest where it attempts to resolve a sample for Duration but one is not found.
1 parent da865b1 commit f7d47c9

File tree

4 files changed

+26
-11
lines changed

4 files changed

+26
-11
lines changed

driver-core/src/test/java/com/datastax/driver/core/DataTypeIntegrationTest.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -217,7 +217,7 @@ private List<TestTable> tablesWithPrimitives() {
217217
private List<TestTable> tablesWithPrimitivesNull() {
218218
List<TestTable> tables = Lists.newArrayList();
219219
// Create a test table for each primitive type testing with null values. If the
220-
// type maps to a java primitive type it's value will by the default value instead of null.
220+
// type maps to a java primitive type it's value will be the default one specified here instead of null.
221221
for (DataType dataType : TestUtils.allPrimitiveTypes(ccm().getProtocolVersion())) {
222222
Object expectedPrimitiveValue = null;
223223
switch (dataType.getName()) {
@@ -243,10 +243,8 @@ private List<TestTable> tablesWithPrimitivesNull() {
243243
case BOOLEAN:
244244
expectedPrimitiveValue = false;
245245
break;
246-
case COUNTER:
247-
case DURATION:
248-
// Duration is handled separately in DurationIntegrationTest, because it has specific restrictions (e.g.
249-
// not allowed in collections).
246+
default:
247+
// not a Java primitive type
250248
continue;
251249
}
252250

@@ -264,15 +262,21 @@ private List<TestTable> tablesWithCollectionsOfPrimitives() {
264262
Object elementSample = entry.getValue();
265263

266264
tables.add(new TestTable(DataType.list(elementType), Lists.newArrayList(elementSample, elementSample), "1.2.0"));
267-
tables.add(new TestTable(DataType.set(elementType), Sets.newHashSet(elementSample), "1.2.0"));
265+
// Duration not supported in Set
266+
if (elementType != DataType.duration())
267+
tables.add(new TestTable(DataType.set(elementType), Sets.newHashSet(elementSample), "1.2.0"));
268268
}
269269
return tables;
270270
}
271271

272272
private List<TestTable> tablesWithMapsOfPrimitives() {
273273
List<TestTable> tables = Lists.newArrayList();
274274
for (Map.Entry<DataType, Object> keyEntry : samples.entrySet()) {
275+
// Duration not supported as Map key
275276
DataType keyType = keyEntry.getKey();
277+
if (keyType == DataType.duration())
278+
continue;
279+
276280
Object keySample = keyEntry.getValue();
277281
for (Map.Entry<DataType, Object> valueEntry : samples.entrySet()) {
278282
DataType valueType = valueEntry.getKey();
@@ -467,6 +471,8 @@ private Object getValue(GettableByIndexData data, DataType dataType) {
467471
return data.getMap(0,
468472
codecRegistry.codecFor(dataType.getTypeArguments().get(0)).getJavaType(),
469473
codecRegistry.codecFor(dataType.getTypeArguments().get(1)).getJavaType());
474+
case DURATION:
475+
return data.get(0, Duration.class);
470476
case CUSTOM:
471477
case COUNTER:
472478
default:

driver-core/src/test/java/com/datastax/driver/core/PrimitiveTypeSamples.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,7 @@ static Map<DataType, Object> samples(ProtocolVersion protocolVersion) {
5151
.put(DataType.tinyint(), Byte.MAX_VALUE)
5252
.put(DataType.smallint(), Short.MAX_VALUE)
5353
.put(DataType.cint(), Integer.MAX_VALUE)
54+
.put(DataType.duration(), Duration.from("PT30H20M"))
5455
.put(DataType.text(), "text")
5556
.put(DataType.timestamp(), new Date(872835240000L))
5657
.put(DataType.date(), LocalDate.fromDaysSinceEpoch(16071))
@@ -74,9 +75,7 @@ public boolean apply(DataType input) {
7475
List<DataType> tmp = Lists.newArrayList(primitiveTypes);
7576
tmp.removeAll(result.keySet());
7677

77-
List<DataType> expectedFilteredTypes = protocolVersion.compareTo(ProtocolVersion.V5) >= 0 ?
78-
Lists.newArrayList(DataType.counter(), DataType.duration()) :
79-
Lists.newArrayList(DataType.counter());
78+
List<DataType> expectedFilteredTypes = Lists.newArrayList(DataType.counter());
8079

8180
assertThat(tmp)
8281
.as("new datatype not covered in test")

driver-core/src/test/java/com/datastax/driver/core/TupleTest.java

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -277,12 +277,16 @@ public void tupleNonPrimitiveSubTypesTest() throws Exception {
277277

278278
// create set values
279279
for (DataType datatype : DATA_TYPE_PRIMITIVES) {
280-
values.add(String.format("v_%s frozen<tuple<set<%s>>>", values.size(), datatype));
280+
// Duration not supported in Set.
281+
if (datatype != DataType.duration())
282+
values.add(String.format("v_%s frozen<tuple<set<%s>>>", values.size(), datatype));
281283
}
282284

283285
// create map values
284286
for (DataType datatype : DATA_TYPE_PRIMITIVES) {
285-
values.add(String.format("v_%s frozen<tuple<map<%s, %s>>>", values.size(), datatype, datatype));
287+
// Duration not supported as Map key.
288+
if (datatype != DataType.duration())
289+
values.add(String.format("v_%s frozen<tuple<map<%s, %s>>>", values.size(), datatype, datatype));
286290
}
287291

288292
// create table
@@ -315,6 +319,9 @@ public void tupleNonPrimitiveSubTypesTest() throws Exception {
315319

316320
// test tuple<set<datatype>>
317321
for (DataType datatype : DATA_TYPE_PRIMITIVES) {
322+
if (datatype == DataType.duration())
323+
continue;
324+
318325
// create tuple
319326
ArrayList<DataType> dataTypes = new ArrayList<DataType>();
320327
ArrayList<Object> createdValues = new ArrayList<Object>();
@@ -338,6 +345,8 @@ public void tupleNonPrimitiveSubTypesTest() throws Exception {
338345

339346
// test tuple<map<datatype, datatype>>
340347
for (DataType datatype : DATA_TYPE_PRIMITIVES) {
348+
if (datatype == DataType.duration())
349+
continue;
341350
// create tuple
342351
ArrayList<DataType> dataTypes = new ArrayList<DataType>();
343352
ArrayList<Object> createdValues = new ArrayList<Object>();

driver-core/src/test/java/com/datastax/driver/core/UserTypesTest.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,6 +216,7 @@ public void should_store_and_retrieve_UDT_containing_any_primitive_type() throws
216216
break;
217217
case DURATION:
218218
alldatatypes.set(index, Duration.from(sampleData.toString()), Duration.class);
219+
break;
219220
case FLOAT:
220221
alldatatypes.setFloat(index, (Float) sampleData);
221222
break;

0 commit comments

Comments
 (0)
0