Пример #1
0
class TestBaseResource(object):
    """
    Base resource test tool
    """
    def __init__(self, p_resource, p_version, *before):
        object.__init__(self)

        self._resource = OrcResource(p_resource, p_version, *before)

        # Id lib, removed these item in teardown
        self._ids = []

        # Result lib, shared in functions
        self._result = {}

    def get_result(self, p_key):
        """
        :param p_key:
        :return:
        """
        return self._result[p_key].data

    def record(self, p_name, p_result):
        """
        :param p_name:
        :param p_result:
        :return:
        """
        if isinstance(p_result.data, dict):
            self._ids.append(p_result.data["id"])
            self._result[p_name] = p_result

    def clean(self):
        """
        :return:
        """
        self.base_delete({"id": self._ids})

    @staticmethod
    def print_res(res):
        """
        :param res:
        :return:
        """
        print("Code: %s" % res.code)
        print("Message: %s" % res.message)
        print("Data: %s" % res.data)
        print()

    def base_help(self):
        """
        :return:
        """
        result = self._resource.fetch("help", {})
        self.print_res(result)
        assert 0 == result.code

    def base_add(self, p_data):
        """
        :param p_data:
        :return:
        """
        result = self._resource.add(p_data)
        self.print_res(result)
        assert 0 == result.code
        assert "id" in result.data

        return result

    def base_add_fail(self, p_data):
        """
        :param p_data:
        :return:
        """
        result = self._resource.add(p_data)
        self.print_res(result)
        assert 0 != result.code

        return result

    def base_delete(self, p_data):
        """
        :param p_data:
        :return:
        """
        result = self._resource.delete(p_data)
        self.print_res(result)
        assert 0 == result.code
        assert result.data

        return result

    def base_delete_fail(self, p_data):
        """
        :param p_data:
        :return:
        """
        result = self._resource.delete(p_data)
        self.print_res(result)
        assert 0 != result.code

        return result

    def base_delete_one(self, p_id):
        """
        :param p_id:
        :return:
        """
        result = self._resource.id(p_id).delete({})
        self.print_res(result)
        assert 0 == result.code
        assert result.data

        return result

    def base_delete_one_fail(self, p_id):
        """
        :param p_id:
        :return:
        """
        result = self._resource.id(p_id).delete({})
        self.print_res(result)
        assert 0 != result.code

        return result

    def base_update(self, p_id, p_data):
        """
        :param p_id:
        :param p_data:
        :return:
        """
        result = self._resource.id(p_id).update(p_data)
        self.print_res(result)
        assert 0 == result.code
        assert result.data
        return result

    def base_update_fail(self, p_id, p_data):
        """
        :param p_id:
        :param p_data:
        :return:
        """
        result = self._resource.id(p_id).update(p_data)
        self.print_res(result)
        assert 0 != result.code
        return result

    def base_query(self, p_data):
        """
        :param p_data:
        :return:
        """
        result = self._resource.query(p_data)
        self.print_res(result)
        assert 0 == result.code

        return result

    def base_query_fail(self, p_data):
        """
        :param p_data:
        :return:
        """
        result = self._resource.query(p_data)
        self.print_res(result)
        assert 0 == result.code
        assert not result.data

        return result

    def base_query_one(self, p_id, p_data=None):
        """
        :param p_data:
        :param p_id: id
        :return:
        """
        result = self._resource.id(p_id).query(p_data or {})
        self.print_res(result)
        assert 0 == result.code
        assert isinstance(result.data, dict)
        return result

    def base_query_one_fail(self, p_id, p_data=None):
        """
        :param p_data:
        :param p_id: id
        :return:
        """
        result = self._resource.id(p_id).query(p_data or {})
        self.print_res(result)
        assert 0 == result.code
        assert result.data is None
        return result

    def base_fetch(self, p_func, p_data):
        """
        :param p_func:
        :param p_data:
        :return:
        """
        result = self._resource.fetch(p_func, p_data)
        self.print_res(result)
        assert 0 == result.code

        return result

    def base_fetch_fail(self, p_func, p_data):
        """
        :param p_func:
        :param p_data:
        :return:
        """
        result = self._resource.fetch(p_func, p_data)
        self.print_res(result)
        assert 0 != result.code

        return result

    def base_fetch_one(self, p_func, p_id, p_data):
        """
        :param p_func:
        :param p_id:
        :param p_data:
        :return:
        """
        result = self._resource.id(p_id).fetch(p_func, p_data)
        self.print_res(result)
        assert 0 == result.code

        return result

    def base_fetch_one_fail(self, p_func, p_id, p_data):
        """
        :param p_func:
        :param p_id:
        :param p_data:
        :return:
        """
        result = self._resource.id(p_id).fetch(p_func, p_data)
        self.print_res(result)
        assert 0 != result.code

        return result
Пример #2
0
class OrcLocalParams(metaclass=SingletonType):
    """
    Parameters with a flag
    """
    def __init__(self):
        object.__init__(self)

        self._resource = OrcResource("util-runtime")
        self._flag = None

    def init(self, p_flag: str):
        """
        :param p_flag:
        :return:
        """
        self._flag = p_flag

    def _set(self, p_key: str, p_value):
        """
        :param p_key:
        :param p_value:
        :return:
        """
        if self._flag is None:
            raise OrcFrameworkLibException(0x1, "Parameter instance is not initialize.")

        if isinstance(p_value, int):
            data_type = "INT"
        elif isinstance(p_value, float):
            data_type = "FLOAT"
        elif isinstance(p_value, bool):
            data_type = "BOOL"
        elif isinstance(p_value, dict):
            data_type = "JSON"
        elif isinstance(p_value, list):
            data_type = "LIST"
        else:
            data_type = "STRING"

        # Query
        query_result = self._resource.query({"flag": self._flag, "key": p_key})
        if 0 != query_result.code:
            raise OrcFrameworkLibException(0x1, "Get local parameter for key %s failed." % p_key)

        if 1 < len(query_result.data):
            raise OrcFrameworkLibException(0x1, "Too many runtime data[%s.%s] founded." % (self._flag, p_key))

        # Data is not exists, add
        if not query_result.data:
            update_result = self._resource.add({"flag": self._flag, "key": p_key, "type": data_type, "value": p_value})

        # Update
        else:
            query_result = query_result.data[0]
            update_result = self._resource.id(query_result["id"]).update({"type": data_type, "value": p_value})

        if 0 != update_result.code:
            raise OrcFrameworkLibException(
                0x1, "Update data {flag: %s, key: %s, value: %s} failed." % (self._flag, p_key, p_value))

        return True

    def _get(self, p_key: str):
        """
        :param p_key:
        :return:
        """
        if self._flag is None:
            raise OrcFrameworkLibException(0x1, "Parameter instance is not initialize.")

        result = self._resource.fetch("get", {"flag": self._flag, "key": p_key})
        if 0 != result.code:
            raise OrcFrameworkLibException(0x1, "Get data %s.%s failed" % (self._flag, p_key))

        if 1 != len(result.data):
            raise OrcFrameworkLibException(0x1, "Data for %s.%s is wrong, %s." % (self._flag, p_key, result.data))

        return result[0]

    @staticmethod
    def set(p_key, p_value):
        """
        :param p_key:
        :param p_value:
        :return:
        """
        cls = OrcLocalParams()
        return cls.set(p_key, p_value)

    @staticmethod
    def get(p_key):
        """
        :param p_key:
        :return:
        """
        cls = OrcLocalParams()
        return cls.get(p_key)