Search in sources :

Example 6 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class DataTypeCqlNameParserTest method should_parse_top_level_user_type_as_shallow.

@Test
public void should_parse_top_level_user_type_as_shallow() {
    UserDefinedType addressType = (UserDefinedType) parse("address");
    assertThat(addressType).isInstanceOf(ShallowUserDefinedType.class);
    assertThat(addressType.getKeyspace()).isEqualTo(KEYSPACE_ID);
    assertThat(addressType.getName().asInternal()).isEqualTo("address");
    assertThat(addressType.isFrozen()).isFalse();
    UserDefinedType frozenAddressType = (UserDefinedType) parse("frozen<address>");
    assertThat(frozenAddressType).isInstanceOf(ShallowUserDefinedType.class);
    assertThat(frozenAddressType.getKeyspace()).isEqualTo(KEYSPACE_ID);
    assertThat(frozenAddressType.getName().asInternal()).isEqualTo("address");
    assertThat(frozenAddressType.isFrozen()).isTrue();
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) ShallowUserDefinedType(com.datastax.oss.driver.internal.core.metadata.schema.ShallowUserDefinedType) Test(org.junit.Test)

Example 7 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class DataTypeClassNameParserTest method should_make_a_frozen_copy_user_type_when_definition_already_available.

@Test
@UseDataProvider(location = TestDataProviders.class, value = "locales")
public void should_make_a_frozen_copy_user_type_when_definition_already_available(Locale locale) {
    Locale def = Locale.getDefault();
    try {
        Locale.setDefault(locale);
        UserDefinedType existing = mock(UserDefinedType.class);
        parse("org.apache.cassandra.db.marshal.UserType(foo,70686f6e65," + "6e616d65:org.apache.cassandra.db.marshal.UTF8Type," + "6e756d626572:org.apache.cassandra.db.marshal.UTF8Type)", ImmutableMap.of(CqlIdentifier.fromInternal("phone"), existing));
        verify(existing).copy(true);
    } finally {
        Locale.setDefault(def);
    }
}
Also used : Locale(java.util.Locale) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) Test(org.junit.Test) UseDataProvider(com.tngtech.java.junit.dataprovider.UseDataProvider)

Example 8 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UserDefinedTypeListParserTest method should_resolve_map_dependency.

@Test
public void should_resolve_map_dependency() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "a1", ImmutableList.of("bs"), ImmutableList.of("frozen<map<int, frozen<b>>>")), mockTypeRow("ks", "a2", ImmutableList.of("bs"), ImmutableList.of("frozen<map<frozen<b>, int>>")), mockTypeRow("ks", "b", ImmutableList.of("i"), ImmutableList.of("int")));
    assertThat(types).hasSize(3);
    UserDefinedType a1Type = types.get(CqlIdentifier.fromInternal("a1"));
    UserDefinedType a2Type = types.get(CqlIdentifier.fromInternal("a2"));
    UserDefinedType bType = types.get(CqlIdentifier.fromInternal("b"));
    assertThat(((MapType) a1Type.getFieldTypes().get(0)).getValueType()).isEqualTo(bType);
    assertThat(((MapType) a2Type.getFieldTypes().get(0)).getKeyType()).isEqualTo(bType);
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) MapType(com.datastax.oss.driver.api.core.type.MapType) Test(org.junit.Test)

Example 9 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UserDefinedTypeListParserTest method should_parse_legacy_table.

@Test
public void should_parse_legacy_table() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeClassNameParser(), context);
    // no need to add a column for the address type, because in 2.2 UDTs are always fully redefined
    // in column and field types (instead of referencing an existing type)
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, PERSON_ROW_2_2);
    assertThat(types).hasSize(1);
    UserDefinedType personType = types.get(CqlIdentifier.fromInternal("person"));
    assertThat(personType.getKeyspace().asInternal()).isEqualTo("ks");
    assertThat(personType.getName().asInternal()).isEqualTo("person");
    assertThat(personType.getFieldNames()).containsExactly(CqlIdentifier.fromInternal("first_name"), CqlIdentifier.fromInternal("last_name"), CqlIdentifier.fromInternal("address"));
    assertThat(personType.getFieldTypes().get(0)).isEqualTo(DataTypes.TEXT);
    assertThat(personType.getFieldTypes().get(1)).isEqualTo(DataTypes.TEXT);
    UserDefinedType addressType = ((UserDefinedType) personType.getFieldTypes().get(2));
    assertThat(addressType.getKeyspace().asInternal()).isEqualTo("ks");
    assertThat(addressType.getName().asInternal()).isEqualTo("address");
    assertThat(addressType.getFieldNames()).containsExactly(CqlIdentifier.fromInternal("street"), CqlIdentifier.fromInternal("zipcode"));
}
Also used : UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Example 10 with UserDefinedType

use of com.datastax.oss.driver.api.core.type.UserDefinedType in project java-driver by datastax.

the class UserDefinedTypeListParserTest method should_resolve_list_dependency.

@Test
public void should_resolve_list_dependency() {
    UserDefinedTypeParser parser = new UserDefinedTypeParser(new DataTypeCqlNameParser(), context);
    Map<CqlIdentifier, UserDefinedType> types = parser.parse(KEYSPACE_ID, mockTypeRow("ks", "a", ImmutableList.of("bs"), ImmutableList.of("frozen<list<frozen<b>>>")), mockTypeRow("ks", "b", ImmutableList.of("i"), ImmutableList.of("int")));
    assertThat(types).hasSize(2);
    UserDefinedType aType = types.get(CqlIdentifier.fromInternal("a"));
    UserDefinedType bType = types.get(CqlIdentifier.fromInternal("b"));
    assertThat(((ListType) aType.getFieldTypes().get(0)).getElementType()).isEqualTo(bType);
}
Also used : ListType(com.datastax.oss.driver.api.core.type.ListType) UserDefinedType(com.datastax.oss.driver.api.core.type.UserDefinedType) CqlIdentifier(com.datastax.oss.driver.api.core.CqlIdentifier) Test(org.junit.Test)

Aggregations

UserDefinedType (com.datastax.oss.driver.api.core.type.UserDefinedType)53 Test (org.junit.Test)33 CqlIdentifier (com.datastax.oss.driver.api.core.CqlIdentifier)21 UdtValue (com.datastax.oss.driver.api.core.data.UdtValue)20 UserDefinedTypeBuilder (com.datastax.oss.driver.internal.core.type.UserDefinedTypeBuilder)15 TupleType (com.datastax.oss.driver.api.core.type.TupleType)13 CqlSession (com.datastax.oss.driver.api.core.CqlSession)9 DataType (com.datastax.oss.driver.api.core.type.DataType)9 Assertions.assertThat (org.assertj.core.api.Assertions.assertThat)7 ListType (com.datastax.oss.driver.api.core.type.ListType)6 SetType (com.datastax.oss.driver.api.core.type.SetType)6 SessionRule (com.datastax.oss.driver.api.testinfra.session.SessionRule)6 ParallelizableTests (com.datastax.oss.driver.categories.ParallelizableTests)6 Category (org.junit.experimental.categories.Category)6 Row (com.datastax.oss.driver.api.core.cql.Row)5 SimpleStatement (com.datastax.oss.driver.api.core.cql.SimpleStatement)5 TupleValue (com.datastax.oss.driver.api.core.data.TupleValue)5 TypeCodec (com.datastax.oss.driver.api.core.type.codec.TypeCodec)5 CcmRule (com.datastax.oss.driver.api.testinfra.ccm.CcmRule)5 ResultSet (com.datastax.oss.driver.api.core.cql.ResultSet)4