dream
46 строк · 1.5 Кб
1import requests
2import os
3
4import common.test_utils as test_utils
5
6
7SERVICE_PORT = int(os.getenv("SERVICE_PORT"))
8RANDOM_SEED = int(os.getenv("RANDOM_SEED", 2718))
9URL = f"http://0.0.0.0:{SERVICE_PORT}/respond"
10LANGUAGE = os.getenv("LANGUAGE", "EN")
11
12FAKE_SERVER = os.getenv("FAKE", True)
13
14
15def handler(requested_data, random_seed):
16hypothesis = requests.post(URL, json={**requested_data, "random_seed": random_seed}).json()
17return hypothesis
18
19
20def run_test(handler):
21in_data, out_data = test_utils.get_dataset()
22for test_name in in_data:
23if LANGUAGE == "RU" and "RU" not in test_name:
24# if russian language, skip english tests
25continue
26elif LANGUAGE == "EN" and "EN" not in test_name:
27# if russian language, skip english tests
28continue
29if not FAKE_SERVER and "FAKE" in test_name:
30# skip fake server tests if the server is real
31continue
32
33hypothesis = handler(in_data[test_name], RANDOM_SEED)
34print(f"test name: {test_name}")
35is_equal_flag, msg = test_utils.compare_structs(out_data[test_name], hypothesis, ignored_keys=["id"])
36if msg and len(msg.split("`")) == 5:
37_, ground_truth_text, _, hypothesis_text, _ = msg.split("`")
38is_equal_flag, ratio = test_utils.compare_text(ground_truth_text, hypothesis_text, 0.80)
39if not is_equal_flag:
40msg = f"{msg} ratio = {ratio}"
41assert is_equal_flag, msg
42print("Success")
43
44
45if __name__ == "__main__":
46run_test(handler)
47