func_check.py 4.6 KB
Newer Older
紫晴 已提交
1
from utils.util_log import test_log as log
紫晴 已提交
2
from common.common_type import *
紫晴 已提交
3
from common.code_mapping import ErrorCode, ErrorMessage
Y
yanliang567 已提交
4
from pymilvus_orm import Collection, Partition
Y
yanliang567 已提交
5
from utils.api_request import Error
紫晴 已提交
6 7


8
class ResponseChecker:
9
    def __init__(self, response, func_name, check_task, check_items, is_succ=True, **kwargs):
Y
yanliang567 已提交
10 11 12
        self.response = response            # response of api request
        self.func_name = func_name          # api function name
        self.check_task = check_task        # task to check response of the api request
13
        self.check_items = check_items    # check items and expectations that to be checked in check task
Y
yanliang567 已提交
14 15
        self.succ = is_succ                 # api responses successful or not

16
        self.kwargs_dict = {}       # not used for now, just for extension
紫晴 已提交
17
        for key, value in kwargs.items():
Y
yanliang567 已提交
18 19
            self.kwargs_dict[key] = value
        self.keys = self.kwargs_dict.keys()
紫晴 已提交
20 21

    def run(self):
22 23 24 25
        """
        Method: start response checking for milvus API call
        """
        result = True
Y
yanliang567 已提交
26 27
        if self.check_task is None:
            result = self.assert_succ(self.succ, True)
紫晴 已提交
28

Y
yanliang567 已提交
29
        elif self.check_task == CheckTasks.err_res:
30
            result = self.assert_exception(self.response, self.succ, self.check_items)
31

紫晴 已提交
32 33
        elif self.check_task == CheckTasks.check_list_count and self.check_items is not None:
            result = self.check_list_count(self.response, self.func_name, self.check_items)
34

Y
yanliang567 已提交
35
        elif self.check_task == CheckTasks.check_collection_property:
36
            result = self.check_collection_property(self.response, self.func_name, self.check_items)
37

Y
yanliang567 已提交
38
        elif self.check_task == CheckTasks.check_partition_property:
39
            result = self.check_partition_property(self.response, self.func_name, self.check_items)
40

41
        # Add check_items here if something new need verify
紫晴 已提交
42

43
        return result
紫晴 已提交
44

45
    @staticmethod
Y
yanliang567 已提交
46 47 48 49 50 51 52 53 54
    def assert_succ(actual, expect):
        assert actual is expect
        return True

    @staticmethod
    def assert_exception(res, actual=True, error_dict=None):
        assert actual is False
        assert len(error_dict) > 0
        if isinstance(res, Error):
紫晴 已提交
55 56 57
            err_code = error_dict["err_code"]
            assert res.code == err_code or ErrorMessage[err_code] in res.message
            # assert res.code == error_dict["err_code"] or error_dict["err_msg"] in res.message
Y
yanliang567 已提交
58 59 60
        else:
            log.error("[CheckFunc] Response of API is not an error: %s" % str(res))
            assert False
61 62
        return True

紫晴 已提交
63
    @staticmethod
紫晴 已提交
64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    def check_list_count(res, func_name, params):
        if not isinstance(res, list):
            log.error("[CheckFunc] Response of API is not a list: %s" % str(res))
            assert False

        if func_name == "list_connections":
            list_count = params.get("list_count", None)
            if not str(list_count).isdigit():
                log.error("[CheckFunc] Check param of list_count is not a number: %s" % str(list_count))
                assert False

            assert len(res) == int(list_count)

        return True

T
ThreadDao 已提交
79
    @staticmethod
80
    def check_collection_property(collection, func_name, check_items):
T
ThreadDao 已提交
81 82 83 84 85
        exp_func_name = "collection_init"
        if func_name != exp_func_name:
            log.warning("The function name is {} rather than {}".format(func_name, exp_func_name))
        if not isinstance(collection, Collection):
            raise Exception("The result to check isn't collection type object")
86 87 88
        assert collection.name == check_items["name"]
        assert collection.description == check_items["schema"].description
        assert collection.schema == check_items["schema"]
Y
yanliang567 已提交
89 90 91
        return True

    @staticmethod
92
    def check_partition_property(partition, func_name, check_items):
Y
yanliang567 已提交
93
        exp_func_name = "_init_partition"
Y
yanliang567 已提交
94 95 96
        if func_name != exp_func_name:
            log.warning("The function name is {} rather than {}".format(func_name, exp_func_name))
        if not isinstance(partition, Partition):
Y
yanliang567 已提交
97
            raise Exception("The result to check isn't partition type object")
98
        if len(check_items) == 0:
Y
yanliang567 已提交
99
            raise Exception("No expect values found in the check task")
100 101 102 103 104 105 106 107
        if check_items["name"]:
            assert partition.name == check_items["name"]
        if check_items["description"]:
            assert partition.description == check_items["description"]
        if check_items["is_empty"]:
            assert partition.is_empty == check_items["is_empty"]
        if check_items["num_entities"]:
            assert partition.num_entities == check_items["num_entities"]
Y
yanliang567 已提交
108 109
        return True