def get_vertex_value(cls, vid, empty_props=False): vertex = ttypes.Vertex() vertex.vid = ttypes.Value(sVal=vid) vertex.tags = list() for i in range(0, 3): tag = ttypes.Tag() tag.name = ('tag{}'.format(i)).encode('utf-8') if not empty_props: tag.props = dict() for j in range(0, 5): value = ttypes.Value() value.set_iVal(j) tag.props[('prop{}'.format(j)).encode('utf-8')] = value vertex.tags.append(tag) return vertex
def test_as_time(self): time = Time() time.hour = 10 time.minute = 20 time.sec = 10 time.microsec = 100 value = ttypes.Value(tVal=time) value_wrapper = ValueWrapper(value) assert value_wrapper.is_time() time_val = value_wrapper.as_time() time_val.set_timezone_offset(28800) assert isinstance(time_val, TimeWrapper) assert time_val.get_hour() == 10 assert time_val.get_minute() == 20 assert time_val.get_sec() == 10 assert time_val.get_microsec() == 100 assert 'utc time: 10:20:10.000100, timezone_offset: 28800' == str( time_val) assert '18:20:10.000100' == time_val.get_local_time_str() new_time = copy.deepcopy(time) new_time.hour = 18 assert new_time == time_val.get_local_time() new_time_2 = copy.deepcopy(time) new_time_2.hour = 12 assert new_time_2 == time_val.get_local_time_by_timezone_offset(7200)
def test_as_datetime(self): datetime = DateTime() datetime.year = 123 datetime.month = 2 datetime.day = 1 datetime.hour = 10 datetime.minute = 20 datetime.sec = 10 datetime.microsec = 100 value = ttypes.Value(dtVal=datetime) value_wrapper = ValueWrapper(value) assert value_wrapper.is_datetime() datetime_val = value_wrapper.as_datetime() datetime_val.set_timezone_offset(28800) assert isinstance(datetime_val, DateTimeWrapper) assert datetime_val.get_hour() == 10 assert datetime_val.get_minute() == 20 assert datetime_val.get_sec() == 10 assert datetime_val.get_microsec() == 100 assert 'utc datetime: 123-02-01T10:20:10.000100, timezone_offset: 28800' == str( datetime_val) assert '123-02-01T18:20:10.000100' == datetime_val.get_local_datetime_str( ) new_datetime = copy.deepcopy(datetime) new_datetime.hour = 18 assert new_datetime == datetime_val.get_local_datetime() new_datetime_2 = copy.deepcopy(datetime) new_datetime_2.hour = 12 assert new_datetime_2 == datetime_val.get_local_datetime_by_timezone_offset( 7200)
def test_as_relationship(self): value = ttypes.Value(eVal=self.get_edge_value(b'Tom', b'Lily')) value_wrapper = ValueWrapper(value) assert value_wrapper.is_edge() relationship = value_wrapper.as_relationship() assert isinstance(relationship, Relationship) # test with reversely reversely_value = ttypes.Value( eVal=self.get_edge_value(b'Lily', b'Tom', True)) reversely_value_wrapper = ValueWrapper(reversely_value) reversely_relationship = reversely_value_wrapper.as_relationship() assert isinstance(reversely_relationship, Relationship) assert reversely_relationship == relationship # test with reversely no equal reversely_value = ttypes.Value( eVal=self.get_edge_value(b'Tom', b'Lily', True)) reversely_value_wrapper = ValueWrapper(reversely_value) reversely_relationship = reversely_value_wrapper.as_relationship() assert isinstance(reversely_relationship, Relationship) assert reversely_relationship != relationship relationship.ranking() == 100 relationship.edge_name() == 'classmate' relationship.start_vertex_id().as_string() == 'Lily' relationship.start_vertex_id().as_string() == 'Tom' assert relationship.keys() == [ 'prop0', 'prop1', 'prop2', 'prop3', 'prop4' ] expect_values = [(v.as_int()) for v in relationship.values()] assert expect_values == [0, 1, 2, 3, 4] assert (list(relationship.properties().keys()).sort() == [ 'prop0', 'prop1', 'prop2', 'prop3', 'prop4' ].sort()) expect_values = [(v.as_int()) for v in relationship.properties().values()] assert expect_values == [0, 1, 2, 3, 4] # test empty props value = ttypes.Value( eVal=self.get_edge_value(b'Tom', b'Lily', empty_props=True)) relationship = ValueWrapper(value).as_relationship() assert relationship.keys() == [] assert relationship.values() == [] assert len(relationship.properties()) == 0
def test_as_path(self): value = ttypes.Value() value.set_pVal(self.get_path_value(b'Tom')) value_wrapper = ValueWrapper(value) assert value_wrapper.is_path() node = value_wrapper.as_path() assert isinstance(node, PathWrapper)
def test_as_geography(self): value = ttypes.Value() value.set_ggVal(self.get_geography_value(3.0, 5.2)) value_wrapper = ValueWrapper(value) assert value_wrapper.is_geography() geog = value_wrapper.as_geography() assert isinstance(geog, GeographyWrapper)
def test_as_string(self): value = ttypes.Value() value.set_sVal(b'Tom') value_wrapper = ValueWrapper(value) assert value_wrapper.is_string() str_val = value_wrapper.as_string() assert isinstance(str_val, str)
def test_as_double(self): value = ttypes.Value() value.set_fVal(10.10) value_wrapper = ValueWrapper(value) assert value_wrapper.is_double() node = value_wrapper.as_double() assert isinstance(node, float)
def test_as_int(self): value = ttypes.Value() value.set_iVal(100) value_wrapper = ValueWrapper(value) assert value_wrapper.is_int() node = value_wrapper.as_int() assert isinstance(node, int)
def test_as_bool(self): value = ttypes.Value() value.set_bVal(False) value_wrapper = ValueWrapper(value) assert value_wrapper.is_bool() node = value_wrapper.as_bool() assert isinstance(node, bool)
def test_as_map(self): value = ttypes.Value() str_val1 = ttypes.Value() str_val1.set_sVal(b"word") str_val2 = ttypes.Value() str_val2.set_sVal(b"car") map_val = NMap() map_val.kvs = {b"a": str_val1, b"b": str_val2} value.set_mVal(map_val) value_wrapper = ValueWrapper(value) assert value_wrapper.is_map() map_val = value_wrapper.as_map() assert isinstance(map_val, dict) expect_result = dict() expect_result["a"] = ValueWrapper(ttypes.Value(sVal=b"word")) expect_result["b"] = ValueWrapper(ttypes.Value(sVal=b"car")) assert map_val == expect_result
def test_as_duration(self): value = ttypes.Value() value.set_duVal(Duration(86400, 3000, 12)) value_wrapper = ValueWrapper(value) assert value_wrapper.is_duration() duration = value_wrapper.as_duration() assert isinstance(duration, DurationWrapper) assert str(duration) == 'P12MT86400.003000000S'
def test_as_list(self): value = ttypes.Value() str_val1 = ttypes.Value() str_val1.set_sVal(b"word") str_val2 = ttypes.Value() str_val2.set_sVal(b"car") val_list = NList() val_list.values = [str_val1, str_val2] value.set_lVal(val_list) value_wrapper = ValueWrapper(value) assert value_wrapper.is_list() list_val = value_wrapper.as_list() assert isinstance(list_val, list) expect_result = [ ValueWrapper(ttypes.Value(sVal=b"word")), ValueWrapper(ttypes.Value(sVal=b"car")), ] assert list_val == expect_result
def test_as_set(self): value = ttypes.Value() str_val1 = ttypes.Value() str_val1.set_sVal(b"word") str_val2 = ttypes.Value() str_val2.set_sVal(b"car") set_val = NSet() set_val.values = set() set_val.values.add(str_val1) set_val.values.add(str_val2) value.set_uVal(set_val) value_wrapper = ValueWrapper(value) assert value_wrapper.is_set() set_val = value_wrapper.as_set() assert isinstance(set_val, set) expect_result = set() expect_result.add(ValueWrapper(ttypes.Value(sVal=b"word"))) expect_result.add(ValueWrapper(ttypes.Value(sVal=b"car"))) assert set_val == expect_result
def get_edge_value(cls, src_id, dst_id, is_reverse=False, empty_props=False): edge = ttypes.Edge() if not is_reverse: edge.src = ttypes.Value(sVal=src_id) edge.dst = ttypes.Value(sVal=dst_id) else: edge.src = ttypes.Value(sVal=dst_id) edge.dst = ttypes.Value(sVal=src_id) edge.type = 1 edge.name = b'classmate' edge.ranking = 100 if not empty_props: edge.props = dict() for i in range(0, 5): value = ttypes.Value() value.set_iVal(i) edge.props[('prop{}'.format(i)).encode('utf-8')] = value return edge
def test_as_date(self): date = Date() date.year = 220 date.month = 2 date.day = 10 value = ttypes.Value(dVal=date) value_wrapper = ValueWrapper(value) assert value_wrapper.is_date() date_val = value_wrapper.as_date() assert isinstance(date_val, DateWrapper) assert date_val.get_year() == 220 assert date_val.get_month() == 2 assert date_val.get_day() == 10 assert '220-02-10' == str(date_val)
def get_path_value(cls, start_id, steps=5): path = ttypes.Path() path.src = cls.get_vertex_value(start_id) path.steps = list() for i in range(0, steps): step = ttypes.Step() step.dst = cls.get_vertex_value( ('vertex{}'.format(i)).encode('utf-8')) step.type = 1 if i % 2 == 0 else -1 step.name = b'classmate' step.ranking = 100 step.props = dict() for i in range(0, 5): value = ttypes.Value() value.set_iVal(i) step.props[('prop{}'.format(i)).encode('utf-8')] = value path.steps.append(step) return path
def to_value(col): if isinstance(col, CommonTtypes.Value): return col value = CommonTtypes.Value() if type(col) is bool: value.set_bVal(col) elif type(col) is int: value.set_iVal(col) elif type(col) is float: value.set_fVal(col) elif type(col) is str: value.set_sVal(col.encode('utf-8')) elif isinstance(col, CommonTtypes.Date): value.set_dVal(col) elif isinstance(col, CommonTtypes.Time): value.set_tVal(col) elif isinstance(col, CommonTtypes.DateTime): value.set_dtVal(col) elif type(col) is dict: map_val = CommonTtypes.NMap() map_val.kvs = {k.encode('utf-8'): to_value(v) for k, v in col.items()} value.set_mVal(map_val) elif type(col) is list: list_val = CommonTtypes.NList() list_val.values = list(map(to_value, col)) value.set_lVal(list_val) elif type(col) is set: set_val = CommonTtypes.NSet() set_val.values = set(map(to_value, col)) value.set_uVal(set_val) elif isinstance(col, CommonTtypes.Edge): value.set_eVal(col) elif isinstance(col, CommonTtypes.Vertex): value.set_vVal(col) elif isinstance(col, CommonTtypes.Path): value.set_pVal(col) elif isinstance(col, PathVal): value.set_pVal(col.to_value()) else: raise ValueError(f'Wrong val type: {str(col)}') return value
def test_as_node(self): value = ttypes.Value() value.set_vVal(self.get_vertex_value(b'Tom')) value_wrapper = ValueWrapper(value) assert value_wrapper.is_vertex() node = value_wrapper.as_node() assert isinstance(node, Node) assert node.get_id().as_string() == 'Tom' assert node.has_tag('tag1') assert (node.prop_names('tag1').sort() == [ 'prop0', 'prop1', 'prop2', 'prop3', 'prop4' ].sort()) expect_values = [(v.as_int()) for v in node.prop_values('tag1')] assert expect_values == [0, 1, 2, 3, 4] assert node.tags() == ['tag0', 'tag1', 'tag2'] assert (list(node.properties('tag1').keys()).sort() == [ 'prop0', 'prop1', 'prop2', 'prop3', 'prop4' ].sort()) expect_values = [(v.as_int()) for v in node.properties('tag1').values()] assert expect_values == [0, 1, 2, 3, 4]
from nebula3.common import ttypes as CommonTtypes # from nebula3.gclient.net import ConnectionPool # from nebula3.Config import Config from nebula3.graph import ttypes from tests.common.configs import get_delay_time from tests.common.utils import ( compare_value, row_to_string, to_value, value_to_string, find_in_rows, ) T_EMPTY = CommonTtypes.Value() T_NULL = CommonTtypes.Value() T_NULL.set_nVal(CommonTtypes.NullType.__NULL__) T_NULL_NaN = CommonTtypes.Value() T_NULL_NaN.set_nVal(CommonTtypes.NullType.NaN) T_NULL_BAD_DATA = CommonTtypes.Value() T_NULL_BAD_DATA.set_nVal(CommonTtypes.NullType.BAD_DATA) T_NULL_BAD_TYPE = CommonTtypes.Value() T_NULL_BAD_TYPE.set_nVal(CommonTtypes.NullType.BAD_TYPE) T_NULL_UNKNOWN_PROP = CommonTtypes.Value() T_NULL_UNKNOWN_PROP.set_nVal(CommonTtypes.NullType.UNKNOWN_PROP) T_NULL_UNKNOWN_DIV_BY_ZERO = CommonTtypes.Value() T_NULL_UNKNOWN_DIV_BY_ZERO.set_nVal(CommonTtypes.NullType.DIV_BY_ZERO) @pytest.mark.usefixtures("workarround_for_class")
def get_data_set(cls): data_set = ttypes.DataSet() data_set.column_names = [ b"col1_empty", b"col2_null", b"col3_bool", b"col4_int", b"col5_double", b"col6_string", b"col7_list", b"col8_set", b"col9_map", b"col10_time", b"col11_date", b"col12_datetime", b"col13_vertex", b"col14_edge", b"col15_path", b"col16_geography", b"col17_duration", ] row = ttypes.Row() row.values = [] value1 = ttypes.Value() row.values.append(value1) value2 = ttypes.Value() value2.set_nVal(NullType.BAD_DATA) row.values.append(value2) value3 = ttypes.Value() value3.set_bVal(False) row.values.append(value3) value4 = ttypes.Value() value4.set_iVal(100) row.values.append(value4) value5 = ttypes.Value() value5.set_fVal(10.01) row.values.append(value5) value6 = ttypes.Value() value6.set_sVal(b"hello world") row.values.append(value6) value7 = ttypes.Value() str_val1 = ttypes.Value() str_val1.set_sVal(b"word") str_val2 = ttypes.Value() str_val2.set_sVal(b"car") list_val = NList() list_val.values = [str_val1, str_val2] value7.set_lVal(list_val) row.values.append(value7) value8 = ttypes.Value() set_val = NSet() set_val.values = set() set_val.values.add(str_val1) set_val.values.add(str_val2) value8.set_uVal(set_val) row.values.append(value8) value9 = ttypes.Value() map = NMap() map.kvs = {b"a": str_val1, b"b": str_val2} value9.set_mVal(map) row.values.append(value9) value10 = ttypes.Value() value10.set_tVal(Time(10, 10, 10, 10000)) row.values.append(value10) value11 = ttypes.Value() value11.set_dVal(date(2020, 10, 1)) row.values.append(value11) value12 = ttypes.Value() value12.set_dtVal(DateTime(2020, 10, 1, 10, 10, 10, 10000)) row.values.append(value12) value13 = ttypes.Value() value13.set_vVal(cls.get_vertex_value(b"Tom")) row.values.append(value13) value14 = ttypes.Value() value14.set_eVal(cls.get_edge_value(b"Tom", b"Lily")) row.values.append(value14) value15 = ttypes.Value() value15.set_pVal(cls.get_path_value(b"Tom", 3)) row.values.append(value15) value16 = ttypes.Value() value16.set_ggVal(cls.get_geography_value(4.8, 5.2)) row.values.append(value16) value17 = ttypes.Value() value17.set_duVal(Duration(86400, 3000, 12)) row.values.append(value17) data_set.rows = [] data_set.rows.append(row) data_set.rows.append(row) return data_set