use of org.knime.core.data.v2.value.ListValueFactory in project knime-core by knime.
the class ValueFactoryUtilsTest method testGetTraitsOnCollectionValueFactory.
@Test
void testGetTraitsOnCollectionValueFactory() {
var valueFactory = new ListValueFactory();
valueFactory.initialize(new IntValueFactory(), IntCell.TYPE);
//
assertThat(ValueFactoryUtils.getTraits(valueFactory)).isInstanceOf(//
ListDataTraits.class).matches(//
this::hasLogicalTypeTrait).matches(this::isListTypeTrait).extracting(//
ListDataTraits.class::cast).extracting(//
ListDataTraits::getInner).matches(//
this::hasLogicalTypeTrait).extracting(//
ValueFactoryUtilsTest::extractValueFactoryClassName).isEqualTo(IntValueFactory.class.getName());
// collection of data cell value factories
var listOfDataCells = createListValueFactory(createDataCellValueFactory(), XMLCell.TYPE);
//
assertThat(ValueFactoryUtils.getTraits(listOfDataCells)).isInstanceOf(//
ListDataTraits.class).matches(//
this::hasLogicalTypeTrait).matches(//
this::isListTypeTrait).extracting(//
ListDataTraits.class::cast).extracting(//
ListDataTraits::getInner).extracting(//
ValueFactoryUtilsTest::extractLogicalTypeJson).matches(j -> j.get("value_factory_class").asText().equals(DictEncodedDataCellValueFactory.class.getName())).extracting(//
j -> j.get("data_type")).extracting(//
j -> j.get("cell_class").asText()).isEqualTo(XMLCell.class.getName());
// nested collection
var nestedCollection = createListValueFactory(createListValueFactory(IntValueFactory.INSTANCE, IntCell.TYPE), DataType.getType(ListCell.class, IntCell.TYPE));
//
assertThat(ValueFactoryUtils.getTraits(nestedCollection)).isInstanceOf(//
ListDataTraits.class).matches(//
this::hasLogicalTypeTrait).matches(//
this::isListTypeTrait).extracting(//
ListDataTraits.class::cast).extracting(//
ListDataTraits::getInner).matches(//
this::hasLogicalTypeTrait).matches(//
this::isListTypeTrait).extracting(//
ListDataTraits.class::cast).extracting(//
ListDataTraits::getInner).matches(//
this::hasLogicalTypeTrait).extracting(//
ValueFactoryUtilsTest::extractValueFactoryClassName).isEqualTo(IntValueFactory.class.getName());
// SparseListValueFactory (uses StructDataTraits)
var sparseList = new SparseListValueFactory();
sparseList.initialize(IntValueFactory.INSTANCE, IntCell.TYPE);
//
assertThat(ValueFactoryUtils.getTraits(sparseList)).isInstanceOf(//
StructDataTraits.class).extracting(//
StructDataTraits.class::cast).matches(//
this::hasLogicalTypeTrait).matches(//
t -> extractValueFactoryClassName(t).equals(SparseListValueFactory.class.getName())).extracting(//
t -> t.getDataTraits(0)).matches(//
this::hasLogicalTypeTrait).extracting(//
ValueFactoryUtilsTest::extractValueFactoryClassName).isEqualTo(IntValueFactory.class.getName());
}
use of org.knime.core.data.v2.value.ListValueFactory in project knime-core by knime.
the class ValueFactoryUtilsTest method testLoadValueFactory.
@Test
void testLoadValueFactory() {
var dataRepo = NotInWorkflowDataRepository.newInstance();
// no logical type trait
assertThrows(IllegalArgumentException.class, () -> ValueFactoryUtils.loadValueFactory(DefaultDataTraits.EMPTY, dataRepo));
// void
var voidTrait = ValueFactoryUtils.getTraits(VoidValueFactory.INSTANCE);
assertThat(ValueFactoryUtils.loadValueFactory(voidTrait, dataRepo)).isEqualTo(VoidValueFactory.INSTANCE);
// ordinary
DataTraits traits = ValueFactoryUtils.getTraits(IntValueFactory.INSTANCE);
//
assertThat(ValueFactoryUtils.loadValueFactory(traits, dataRepo)).isInstanceOf(IntValueFactory.class);
// specific collection
traits = ValueFactoryUtils.getTraits(IntListValueFactory.INSTANCE);
//
assertThat(ValueFactoryUtils.loadValueFactory(traits, dataRepo)).isEqualTo(IntListValueFactory.INSTANCE);
// nested collection
var nestedListValueFactory = new ListValueFactory();
nestedListValueFactory.initialize(IntListValueFactory.INSTANCE, DataType.getType(ListCell.class, DataType.getType(ListCell.class, IntCell.TYPE)));
traits = ValueFactoryUtils.getTraits(nestedListValueFactory);
//
assertThat(ValueFactoryUtils.loadValueFactory(traits, dataRepo)).isInstanceOf(//
ListValueFactory.class).extracting(//
ListValueFactory.class::cast).extracting(//
ListValueFactory::getElementValueFactory).isEqualTo(IntListValueFactory.INSTANCE);
// data cell value factory
var dictEncodedDataCellValueFactory = createDataCellValueFactory();
traits = ValueFactoryUtils.getTraits(dictEncodedDataCellValueFactory);
//
assertThat(ValueFactoryUtils.loadValueFactory(traits, dataRepo)).isInstanceOf(//
DictEncodedDataCellValueFactory.class).extracting(//
DictEncodedDataCellValueFactory.class::cast).extracting(//
DictEncodedDataCellValueFactory::getType).isEqualTo(XMLCell.TYPE);
// try to load an unregistered value factory
final var finalTraits = createSimpleTypeTraits(DummyDataValueFactory.class.getName());
assertThrows(IllegalArgumentException.class, () -> ValueFactoryUtils.loadValueFactory(finalTraits, dataRepo));
}
Aggregations