Ejemplo n.º 1
0
    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)
Ejemplo n.º 2
0
    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)
Ejemplo n.º 3
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)
Ejemplo n.º 4
0
    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)
Ejemplo n.º 5
0
    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)
Ejemplo n.º 6
0
    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)
Ejemplo n.º 7
0
    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)
Ejemplo n.º 8
0
    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)
Ejemplo n.º 9
0
    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'
Ejemplo n.º 10
0
    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)
Ejemplo n.º 11
0
    def get_id(self):
        """get vertex id, if the space vid_type is int, you can use get_id().as_int(),
        if the space vid_type is fixed_string, you can use get_id().as_string()

        :return: ValueWrapper
        """
        if len(self._row.values) < 1:
            raise RuntimeError('The row value is bad format, '
                               'get vertex id failed: len is {}'.format(
                                   len(self._row.values)))
        return ValueWrapper(self._row.values[0], self._decode_type)
Ejemplo n.º 12
0
    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]
Ejemplo n.º 13
0
    def get_prop_values(self):
        """get all prop values from the vertex data

        :return: list<ValueWrapper>
        """
        index = self.PROP_START_INDEX_with_vid
        prop_values = []
        while index < len(self._row.values):
            prop_values.append(
                ValueWrapper(self._row.values[index],
                             decode_type=self._decode_type))
            index = index + 1
        return prop_values
Ejemplo n.º 14
0
def executing_query_with_params(query, indices, keys, graph_spaces, session,
                                request):
    indices_list = [int(v) for v in indices.split(",")]
    key_list = [request.node.name + key for key in keys.split(",")]
    assert len(indices_list) == len(
        key_list
    ), f"Length not match for keys and indices: {keys} <=> {indices}"
    vals = []
    register_lock.acquire()
    for (key, index) in zip(key_list, indices_list):
        vals.append(ValueWrapper(register_dict[key][index]))
    register_lock.release()
    ngql = combine_query(query).format(*vals)
    exec_query(request, ngql, session, graph_spaces)
Ejemplo n.º 15
0
    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
Ejemplo n.º 16
0
    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
Ejemplo n.º 17
0
    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
Ejemplo n.º 18
0
    def test_all_interface(self):
        result = self.get_result_set()
        assert result.space_name() == "test"
        assert result.comment() == "Permission"
        assert result.error_msg() == "Permission"
        assert result.error_code() == ErrorCode.E_BAD_PERMISSION
        assert result.plan_desc() is None
        assert result.latency() == 100
        assert not result.is_empty()
        assert not result.is_succeeded()
        expect_keys = [
            "col1_empty",
            "col2_null",
            "col3_bool",
            "col4_int",
            "col5_double",
            "col6_string",
            "col7_list",
            "col8_set",
            "col9_map",
            "col10_time",
            "col11_date",
            "col12_datetime",
            "col13_vertex",
            "col14_edge",
            "col15_path",
            "col16_geography",
            "col17_duration",
        ]
        assert result.keys() == expect_keys
        assert result.col_size() == 17
        assert result.row_size() == 2

        # test column_values
        assert len(result.column_values("col6_string")) == 2
        assert result.column_values("col6_string")[0].is_string()
        assert result.column_values(
            "col6_string")[0].as_string() == "hello world"
        # test row_values
        assert len(result.row_values(0)) == 17
        assert result.row_values(0)[5].is_string()
        assert result.row_values(0)[5].as_string() == "hello world"

        # test rows
        assert len(result.rows()) == 2
        assert len(result.rows()[0].values) == 17
        assert isinstance(result.rows()[0].values[0], Value)
        assert isinstance(result.get_row_types(), list)

        # test get_row_types
        assert result.get_row_types() == [
            ttypes.Value.__EMPTY__,
            ttypes.Value.NVAL,
            ttypes.Value.BVAL,
            ttypes.Value.IVAL,
            ttypes.Value.FVAL,
            ttypes.Value.SVAL,
            ttypes.Value.LVAL,
            ttypes.Value.UVAL,
            ttypes.Value.MVAL,
            ttypes.Value.TVAL,
            ttypes.Value.DVAL,
            ttypes.Value.DTVAL,
            ttypes.Value.VVAL,
            ttypes.Value.EVAL,
            ttypes.Value.PVAL,
            ttypes.Value.GGVAL,
            ttypes.Value.DUVAL,
        ]

        # test record
        in_use = False
        for record in result:
            in_use = True
            record.size() == 17

            # test keys()
            assert record.keys() == expect_keys
            # test values()
            values = record.values()
            assert len(record.values()) == 17
            assert record.values()[0].is_empty()
            assert record.values()[5].is_string()
            assert record.values()[5].is_string()
            assert record.values()[5].as_string() == "hello world"

            # test get_value()
            assert record.get_value(0).is_empty()
            assert values[0].is_empty()
            assert record.get_value(1).is_null()
            assert record.get_value(1).as_null() == Null(Null.BAD_DATA)
            null_value = Value(nVal=Null.BAD_DATA)
            assert record.get_value(1) == ValueWrapper(null_value)
            assert str(record.get_value(1).as_null()) == 'BAD_DATA'

            # test get_value_by_key()
            assert record.get_value_by_key('col2_null').is_null()
            assert record.get_value_by_key('col3_bool').is_bool()
            assert not record.get_value_by_key('col3_bool').as_bool()

            # get_value_by_key with not exited key
            try:
                record.get_value_by_key('not existed')
                assert False, 'Not expect here'
            except InvalidKeyException as e:
                assert True
                assert e.message == "KeyError: `not existed'"
            assert values[1].is_null()
            assert record.get_value(2).is_bool()
            assert not record.get_value(2).as_bool()
            assert record.get_value(2).is_bool()
            assert record.get_value(3).is_int()
            assert record.get_value(3).as_int() == 100
            assert record.get_value(4).is_double()
            assert record.get_value(4).as_double() == 10.01
            assert record.get_value(5).is_string()
            assert record.get_value(5).as_string() == "hello world"
            assert record.get_value(6).is_list()
            assert record.get_value(7).is_set()
            assert record.get_value(8).is_map()
            assert record.get_value(9).is_time()
            assert record.get_value(10).is_date()
            assert record.get_value(11).is_datetime()
            assert record.get_value(12).is_vertex()
            assert record.get_value(13).is_edge()
            assert record.get_value(14).is_path()
            assert record.get_value(15).is_geography()
            assert record.get_value(16).is_duration()
        assert in_use

        # test use iterator again
        in_use = False
        for record in result:
            in_use = True
            record.size() == 17
        assert in_use
Ejemplo n.º 19
0
    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