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


class CheckFunc:
紫晴 已提交
7
    def __init__(self, res, func_name, check_res, check_params, check_res_result=True, **kwargs):
紫晴 已提交
8 9 10
        self.res = res  # response of api request
        self.func_name = func_name
        self.check_res = check_res
紫晴 已提交
11
        self.check_params = check_params
紫晴 已提交
12
        self.check_res_result = check_res_result
紫晴 已提交
13 14 15 16 17 18 19 20
        self.params = {}

        for key, value in kwargs.items():
            self.params[key] = value

        self.keys = self.params.keys()

    def run(self):
21
        # log.debug("[Run CheckFunc] Start checking res...")
紫晴 已提交
22
        check_result = True
紫晴 已提交
23 24

        if self.check_res is None:
25 26 27 28 29
            check_result = self.check_response(self.check_res_result, True)

        elif self.check_res == CheckParams.err_res:
            check_result = self.check_response(self.check_res_result, False)

紫晴 已提交
30 31
        elif self.check_res == CheckParams.list_count and self.check_params is not None:
            check_result = self.check_list_count(self.res, self.func_name, self.check_params)
32

T
ThreadDao 已提交
33 34
        elif self.check_res == CheckParams.collection_property_check:
            check_result = self.req_collection_property_check(self.res, self.func_name, self.params)
35

Y
yanliang567 已提交
36 37
        elif self.check_res == CheckParams.partition_property_check:
            check_result = self.partition_property_check(self.res, self.func_name, self.params)
紫晴 已提交
38

紫晴 已提交
39 40
        return check_result

41 42 43 44 45
    @staticmethod
    def check_response(check_res_result, expect_result):
        assert check_res_result == expect_result
        return True

紫晴 已提交
46
    @staticmethod
紫晴 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61
    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 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74
    @staticmethod
    def req_collection_property_check(collection, func_name, params):
        '''
        :param collection
        :return:
        '''
        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")
        assert collection.name == params["name"]
        assert collection.description == params["schema"].description
Y
yanliang567 已提交
75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
        assert collection.schema == params["schema"]
        return True

    @staticmethod
    def partition_property_check(partition, func_name, params):
        exp_func_name = "partition_init"
        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):
            raise Exception("The result to check isn't collection type object")
        assert partition.name == params["name"]
        assert partition.description == params["description"]
        assert partition.is_empty == params["is_empty"]
        assert partition.num_entities == params["num_entities"]
        return True