エンドポイントテストを強化し、ON/OFF状態のテストメソッドをリファクタリング。データ設定系および実行系エンドポイントのテストを追加し、ランダムアクセスのテストを実装。

This commit is contained in:
misyaguziya
2025-09-24 01:06:11 +09:00
parent 95cf247e2e
commit c38e474385

View File

@@ -214,66 +214,54 @@ class TestMainloop():
# "/run/feed_watchdog": {"data": None, "status": 200, "result": True},
}
def test_endpoints_on_off_single(self):
print("----ON/OFF系のエンドポイントのテスト----")
for endpoint in self.validity_endpoints:
print(f"Testing endpoint: {endpoint}", end="", flush=True)
def test_endpoints_on_off_single(self, endpoint):
success = False
if endpoint.startswith("/set/enable/"):
result, status = self.main.handleRequest(endpoint, None)
if result is True and status == 200:
self.config_dict[endpoint.split("/")[-1]] = result
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
success = True
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}")
print(f"Current config_dict: {self.config_dict}")
break
elif endpoint.startswith("/set/disable/"):
result, status = self.main.handleRequest(endpoint, None)
if result is False and status == 200:
self.config_dict[endpoint.split("/")[-1]] = result
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
success = True
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}")
print(f"Current config_dict: {self.config_dict}")
return success
def test_endpoints_on_off_all(self):
print("----ON/OFF系のエンドポイントのテスト----")
for endpoint in self.validity_endpoints:
print(f"Testing endpoint: {endpoint}", end="", flush=True)
if self.test_endpoints_on_off_single(endpoint) is False:
break
print("----ON/OFF系のエンドポイントのテスト終了----")
# def test_endpoints_on_off_random(self):
# print("----ON/OFFでのランダムアクセスのテスト----")
# for i in range(1000):
# endpoint = random.choice(self.validity_endpoints)
# print(f"No.{i:04} Testing endpoint: {endpoint}", end="", flush=True)
# if endpoint.startswith("/set/enable/"):
# result, status = self.main.handleRequest(endpoint, None)
# expected_result = True
# if result == expected_result and status == 200:
# self.config_dict[endpoint.split("/")[-1]] = result
# print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
# else:
# print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}, Expected: {expected_result}")
# pprint.pprint(self.config_dict)
# break
# elif endpoint.startswith("/set/disable/"):
# result, status = self.main.handleRequest(endpoint, None)
# expected_result = False
# if result == expected_result and status == 200:
# self.config_dict[endpoint.split("/")[-1]] = result
# print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
# else:
# print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}, Expected: {expected_result}")
# pprint.pprint(self.config_dict)
# break
def test_endpoints_on_off_random(self):
print("----ON/OFFでのランダムアクセスのテスト----")
for i in range(1000):
endpoint = random.choice(self.validity_endpoints)
print(f"No.{i:04} Testing endpoint: {endpoint}", end="", flush=True)
if self.test_endpoints_on_off_single(endpoint) is False:
break
# # 最後にすべてOFFにして終了
# for endpoint in self.validity_endpoints:
# if endpoint.startswith("/set/disable/"):
# result, status = self.main.handleRequest(endpoint, None)
# time.sleep(0.2)
# print("----ON/OFFでのランダムアクセスのテスト終了----")
# 最後にすべてOFFにして終了
for endpoint in self.validity_endpoints:
if endpoint.startswith("/set/disable/"):
result, status = self.main.handleRequest(endpoint, None)
time.sleep(0.2)
print("----ON/OFFでのランダムアクセスのテスト終了----")
# def test_endpoints_on_off_continuous(self):
# print("----ON/OFF連続テスト----")
# # endpoints = ["/set/enable/websocket_server", "/set/disable/websocket_server"]
def test_endpoints_on_off_continuous(self):
print("----ON/OFF連続テスト----")
endpoints = ["/set/enable/websocket_server", "/set/disable/websocket_server"]
# endpoints = [
# "/set/enable/translation",
# "/set/disable/translation",
@@ -282,40 +270,20 @@ class TestMainloop():
# "/set/enable/transcription_receive",
# "/set/disable/transcription_receive",
# ]
# for i in range(1000):
# endpoint = random.choice(endpoints)
# print(f"No.{i:04} Testing endpoint: {endpoint}", end="", flush=True)
# if endpoint.startswith("/set/enable/"):
# result, status = self.main.handleRequest(endpoint, None)
# expected_result = True
# if result == expected_result and status == 200:
# self.config_dict[endpoint.split("/")[-1]] = result
# print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
# else:
# print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}, Expected: {expected_result}")
# pprint.pprint(self.config_dict)
# break
# elif endpoint.startswith("/set/disable/"):
# result, status = self.main.handleRequest(endpoint, None)
# expected_result = False
# if result == expected_result and status == 200:
# self.config_dict[endpoint.split("/")[-1]] = result
# print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
# else:
# print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}, Expected: {expected_result}")
# pprint.pprint(self.config_dict)
# break
for i in range(1000):
endpoint = random.choice(endpoints)
print(f"No.{i:04} Testing endpoint: {endpoint}", end="", flush=True)
if self.test_endpoints_on_off_single(endpoint) is False:
break
# # 最後にすべてOFFにして終了
# for endpoint in self.validity_endpoints:
# if endpoint.startswith("/set/disable/"):
# result, status = self.main.handleRequest(endpoint, None)
# print("----ON/OFF連続テスト終了----")
# 最後にすべてOFFにして終了
for endpoint in self.validity_endpoints:
if endpoint.startswith("/set/disable/"):
result, status = self.main.handleRequest(endpoint, None)
print("----ON/OFF連続テスト終了----")
def test_set_data_endpoints_single(self):
print("----データ設定系のエンドポイントのテスト----")
for endpoint in self.set_data_endpoints:
print(f"Testing endpoint: {endpoint}", end=" ", flush=True)
def test_set_data_endpoints_single(self, endpoint):
success = False
match endpoint:
case "/set/data/selected_tab_no":
data = random.choice(["1", "2", "3"])
@@ -468,73 +436,129 @@ class TestMainloop():
if status == 200:
self.config_dict[endpoint.split("/")[-1]] = result
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
success = True
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}")
print(f" Current config_dict: {self.config_dict}")
break
else:
print(f"\t -> {Color.YELLOW}[SKIP]{Color.RESET} No data to set for this endpoint.")
success = True
return success
def test_set_data_endpoints_all(self):
print("----データ設定系のエンドポイントのテスト----")
for endpoint in self.set_data_endpoints:
print(f"Testing endpoint: {endpoint}", end=" ", flush=True)
if self.test_set_data_endpoints_single(endpoint) is False:
break
print("----データ設定系のエンドポイントのテスト終了----")
def test_run_endpoints_single(self):
def test_run_endpoints_single(self, endpoint, test):
success = False
match endpoint:
case "/run/send_message_box":
data = test["data"]
expected_status = test["status"]
result, status = self.main.handleRequest(endpoint, data)
if status == expected_status:
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
success = True
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}")
print(f" Current config_dict: {self.config_dict}")
case "/run/typing_message_box" | "/run/stop_typing_message_box":
data = test["data"]
expected_status = test["status"]
expected_result = test["result"]
result, status = self.main.handleRequest(endpoint, data)
if status == expected_status and result == expected_result:
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
success = True
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}, Expected: {expected_result}")
print(f" Current config_dict: {self.config_dict}")
case "/run/send_text_overlay":
data = test["data"]
expected_status = test["status"]
expected_result = test["result"]
result, status = self.main.handleRequest(endpoint, data)
if status == expected_status and result == expected_result:
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
success = True
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}, Expected: {expected_result}")
print(f" Current config_dict: {self.config_dict}")
case "/run/swap_your_language_and_target_language":
data = test["data"]
expected_status = test["status"]
result, status = self.main.handleRequest(endpoint, data)
if status == expected_status:
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
success = True
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}")
print(f" Current config_dict: {self.config_dict}")
case _:
print(f"\t -> {Color.YELLOW}[SKIP]{Color.RESET} No tests defined for this endpoint.")
success = True
return success
def test_run_endpoints_all(self):
print("----実行系のエンドポイントのテスト----")
for endpoint, tests in self.run_endpoints.items():
print(f"Testing endpoint: {endpoint}", end=" ", flush=True)
match endpoint:
case "/run/send_message_box":
success = True
for test in tests:
data = test["data"]
expected_status = test["status"]
result, status = self.main.handleRequest(endpoint, data)
if status == expected_status:
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}")
print(f" Current config_dict: {self.config_dict}")
if self.test_run_endpoints_single(endpoint, test) is False:
success = False
break
case "/run/typing_message_box" | "/run/stop_typing_message_box":
for test in tests:
data = test["data"]
expected_status = test["status"]
expected_result = test["result"]
result, status = self.main.handleRequest(endpoint, data)
if status == expected_status and result == expected_result:
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}, Expected: {expected_result}")
print(f" Current config_dict: {self.config_dict}")
break
case "/run/send_text_overlay":
for test in tests:
data = test["data"]
expected_status = test["status"]
expected_result = test["result"]
result, status = self.main.handleRequest(endpoint, data)
if status == expected_status and result == expected_result:
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}, Expected: {expected_result}")
print(f" Current config_dict: {self.config_dict}")
break
case "/run/swap_your_language_and_target_language":
for test in tests:
data = test["data"]
expected_status = test["status"]
result, status = self.main.handleRequest(endpoint, data)
if status == expected_status:
print(f"\t -> {Color.GREEN}[PASS]{Color.RESET} Status: {status}, Result: {result}")
else:
print(f"\t -> {Color.RED}[ERROR]{Color.RESET} Status: {status}, Result: {result}")
print(f" Current config_dict: {self.config_dict}")
if success is False:
break
print("----実行系のエンドポイントのテスト終了----")
def test_endpoints_all_random(self):
print("----すべてのエンドポイントのランダムアクセスのテスト----")
endpoint_types = [
"validity",
"set_data",
"run",
]
for i in range(1000):
endpoints_type = random.choice(endpoint_types)
match endpoints_type:
case "validity":
endpoint = random.choice(self.validity_endpoints)
print(f"No.{i:04} Testing endpoint: {endpoint}", end="", flush=True)
if self.test_endpoints_on_off_single(endpoint) is False:
break
case "set_data":
endpoint = random.choice(self.set_data_endpoints)
print(f"No.{i:04} Testing endpoint: {endpoint}", end=" ", flush=True)
if self.test_set_data_endpoints_single(endpoint) is False:
break
case "run":
endpoint = random.choice(list(self.run_endpoints.keys()))
test = random.choice(self.run_endpoints[endpoint])
print(f"No.{i:04} Testing endpoint: {endpoint}", end=" ", flush=True)
if self.test_run_endpoints_single(endpoint, test) is False:
break
# 最後にすべてOFFにして終了
for endpoint in self.validity_endpoints:
if endpoint.startswith("/set/disable/"):
_, _ = self.main.handleRequest(endpoint, None)
print("----すべてのエンドポイントのランダムアクセスのテスト終了----")
if __name__ == "__main__":
try:
test = TestMainloop()
test.test_endpoints_on_off_single()
test.test_set_data_endpoints_single()
test.test_run_endpoints_single()
# test.test_endpoints_on_off_all()
# test.test_set_data_endpoints_all()
# test.test_run_endpoints_all()
test.test_endpoints_all_random()
# test.test_endpoints_on_off_continuous()
# test.test_endpoints_on_off_random()
except KeyboardInterrupt:
print("Interrupted by user, shutting down...")
except Exception as e: