|
| 1 | +from typing import Dict, Any |
| 2 | +import asyncio |
| 3 | +import random |
| 4 | +import string |
| 5 | +import pytest |
| 6 | + |
| 7 | +from test.common.logger import logger |
| 8 | + |
| 9 | + |
| 10 | +class ResponseWrapper: |
| 11 | + def __init__(self, status: int, json_data: Dict): |
| 12 | + self.status_code = status |
| 13 | + self._json_data = json_data |
| 14 | + |
| 15 | + def json(self): |
| 16 | + return self._json_data |
| 17 | + |
| 18 | + |
| 19 | +def list_to_dict(data: list): |
| 20 | + d = {} |
| 21 | + for i in data: |
| 22 | + if isinstance(i, dict): |
| 23 | + for k, v in i.items(): |
| 24 | + d[k] = v |
| 25 | + else: |
| 26 | + d.update(i) |
| 27 | + return d |
| 28 | + |
| 29 | + |
| 30 | +def get_random_email(): |
| 31 | + domain = ["163.com", "126.com", "yeah.net", "vip.163.com", "vip.126.com", "188.com", "vip.188.com", "qq.com", |
| 32 | + "gmail.com", "yahoo.com", "sina.com", "sina.cn", "sohu.com", "sogou.com", "outlook.com", "189.com", |
| 33 | + "wo.cn", "139.com", "ailiyun.com", "icloud.com", "tianya.cn", "renren.com", "tom.com"] |
| 34 | + username_length = random.randint(5, 10) |
| 35 | + username = ''.join(random.choices(string.ascii_lowercase, k=username_length)) |
| 36 | + domain_name = random.choice(domain) |
| 37 | + email = username + "@" + domain_name |
| 38 | + return email |
| 39 | + |
| 40 | + |
| 41 | +def get_password(): |
| 42 | + return ''.join(random.choices(string.ascii_letters, k=7))+str(random.randint(0, 9)) |
| 43 | + |
| 44 | + |
| 45 | +def assume(res, except_dict: Dict[str, Any]): |
| 46 | + pytest.assume(res.status_code == int(except_dict["except_http_code"])) |
| 47 | + pytest.assume(res.json()["status"] == except_dict["except_status"]) |
| 48 | + |
| 49 | + |
| 50 | +def assume_success(res, except_dict: Dict[str, Any]): |
| 51 | + assume(res, except_dict) |
| 52 | + pytest.assume(res.json()["data"]["status"] == except_dict["except_data_status"]) |
| 53 | + |
| 54 | + |
| 55 | +def assume_error(res, except_dict: Dict[str, Any]): |
| 56 | + assume(res, except_dict) |
| 57 | + pytest.assume(res.json()["error"]["code"] == except_dict["except_error_code"]) |
| 58 | + |
| 59 | + |
| 60 | +def assume_count(res, except_dict: Dict[str, Any]): |
| 61 | + assume(res, except_dict) |
| 62 | + pytest.assume(res.json()["total_count"] >= 0) |
| 63 | + pytest.assume(res.json()["fetched_count"] >= 0) |
| 64 | + pytest.assume(len(res.json()["data"]) == res.json()["fetched_count"]) |
| 65 | + |
| 66 | + |
| 67 | +def assume_assistant(res, assistant_dict: Dict[str, Any]): |
| 68 | + for key, value in assistant_dict.items(): |
| 69 | + if key == "error": |
| 70 | + pass |
| 71 | + else: |
| 72 | + pytest.assume(res[key] == assistant_dict[key]) |
| 73 | + |
| 74 | + |
| 75 | +def assume_collection(res, collection_dict: Dict[str, Any]): |
| 76 | + for key, value in collection_dict.items(): |
| 77 | + if key == "configs": |
| 78 | + for k, v in value.items(): |
| 79 | + pytest.assume(res.json()["data"][key][k] == collection_dict[key][k]) |
| 80 | + elif key == "error": |
| 81 | + pass |
| 82 | + else: |
| 83 | + pytest.assume(res.json()["data"][key] == collection_dict[key]) |
| 84 | + |
| 85 | + |
| 86 | +def assume_record(res, record_dict: Dict[str, Any]): |
| 87 | + for key, value in record_dict.items(): |
| 88 | + if key == "type": |
| 89 | + pytest.assume(res.json()["data"][key] == record_dict[key]) |
| 90 | + elif key == "error": |
| 91 | + pass |
| 92 | + elif key == "text": |
| 93 | + pytest.assume(res.json()["data"]["content"][key] == record_dict[key]) |
| 94 | + |
| 95 | + |
| 96 | +def assume_chunk(res, chunk_dict: Dict[str, Any]): |
| 97 | + for key, value in chunk_dict.items(): |
| 98 | + if key == "error": |
| 99 | + pass |
| 100 | + else: |
| 101 | + pytest.assume(len(res.json()["data"]) == res.json()["fetched_count"] == chunk_dict["top_k"]) |
| 102 | + |
| 103 | + |
| 104 | +async def mq_execute_create_collection(n, func, *args, **kwargs): |
| 105 | + if n == 10: |
| 106 | + logger.info(func+"timeout") |
| 107 | + pytest.assume(False) |
| 108 | + else: |
| 109 | + from test.retrieval_service.collection import get_collection |
| 110 | + res = await get_collection(*args, **kwargs) |
| 111 | + if res.json()["data"]["status"] == "ready" and res.json()["data"]["num_records"] >= 0 and res.json()["data"]["num_chunks"] >= 0: |
| 112 | + return |
| 113 | + else: |
| 114 | + await asyncio.sleep(1) |
| 115 | + logger.info(f"collection status is {res.json()['data']['status']}") |
| 116 | + logger.info(f"collection num_records is {res.json()['data']['num_records']}") |
| 117 | + logger.info(f"collection num_chunks is {res.json()['data']['num_chunks']}") |
| 118 | + await mq_execute_create_collection(n+1, func, *args, **kwargs) |
| 119 | + |
| 120 | + |
| 121 | +async def mq_execute_delete_collection(n, func, *args, **kwargs): |
| 122 | + if n == 10: |
| 123 | + logger.info(func + "timeout") |
| 124 | + pytest.assume(False) |
| 125 | + else: |
| 126 | + from test.retrieval_service.collection import get_collection |
| 127 | + res = await get_collection(*args, **kwargs) |
| 128 | + if res.status_code == 404: |
| 129 | + logger.info(f"project[{args[0]}]collection[{args[1]}] is deleted") |
| 130 | + return |
| 131 | + else: |
| 132 | + await asyncio.sleep(1) |
| 133 | + await mq_execute_delete_collection(n+1, func, *args, **kwargs) |
| 134 | + |
| 135 | + |
| 136 | +async def mq_execute_create_record(n, func, *args, **kwargs): |
| 137 | + if n == 10: |
| 138 | + logger.info(func + "timeout") |
| 139 | + pytest.assume(False) |
| 140 | + else: |
| 141 | + from test.retrieval_service.record import get_record |
| 142 | + res = await get_record(*args, **kwargs) |
| 143 | + if res.json()["data"]["status"] == "ready" and res.json()["data"]["num_chunks"] >= 0: |
| 144 | + logger.info(f"project[{args[0]}]collection[{args[1]}]record[{args[2]}] status is ready") |
| 145 | + return |
| 146 | + elif res.json()["data"]["status"] == "partial" and res.json()["data"]["num_chunks"] >= 0: |
| 147 | + logger.info(f"project[{args[0]}]collection[{args[1]}]record[{args[2]}] is partial") |
| 148 | + return |
| 149 | + else: |
| 150 | + await asyncio.sleep(1) |
| 151 | + logger.info(f"record status is {res.json()['data']['status']}") |
| 152 | + logger.info(f"record num_chunks is {res.json()['data']['num_chunks']}") |
| 153 | + await mq_execute_create_record(n + 1, func, *args, **kwargs) |
| 154 | + |
| 155 | + |
| 156 | +async def mq_execute_delete_record(n, func, *args, **kwargs): |
| 157 | + if n == 10: |
| 158 | + logger.info(func + "timeout") |
| 159 | + pytest.assume(False) |
| 160 | + else: |
| 161 | + from test.retrieval_service.record import get_record |
| 162 | + res = await get_record(*args, **kwargs) |
| 163 | + if res.status_code == 404: |
| 164 | + logger.info(f"project[{args[0]}]collection[{args[1]}]record[{args[2]}] is deleted") |
| 165 | + return |
| 166 | + else: |
| 167 | + await asyncio.sleep(1) |
| 168 | + await mq_execute_delete_record(n + 1, func, *args, **kwargs) |
| 169 | + |
| 170 | + |
| 171 | +async def mq_execute_delete_project(n, func, *args, **kwargs): |
| 172 | + if n == 10: |
| 173 | + logger.info(func + "timeout") |
| 174 | + pytest.assume(False) |
| 175 | + else: |
| 176 | + from test.retrieval_service.collection import list_collections |
| 177 | + collections_res = await list_collections(*args, **kwargs) |
| 178 | + if collections_res.status_code == 400: |
| 179 | + return |
| 180 | + else: |
| 181 | + await asyncio.sleep(1) |
| 182 | + await mq_execute_delete_project(n + 1, func, *args, **kwargs) |
| 183 | + |
| 184 | + |
| 185 | +def get_random(): |
| 186 | + return ''.join(random.choices(string.ascii_letters, k=7))+str(random.randint(0, 9)) |
| 187 | + |
| 188 | + |
| 189 | +def get_project_id(i: int): |
| 190 | + return (get_random() for j in range(i)) |
| 191 | + |
| 192 | + |
0 commit comments