ClickHouse/docker/test/sqltest/test.py

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

149 lines
4.8 KiB
Python
Raw Permalink Normal View History

2023-07-19 01:42:32 +00:00
#!/usr/bin/env python3
2023-07-19 01:54:09 +00:00
import html
2024-09-27 10:19:39 +00:00
import os
2023-07-19 01:42:32 +00:00
import random
import string
2024-09-27 10:19:39 +00:00
import yaml
from clickhouse_driver import Client
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
client = Client(host="localhost", port=9000)
settings = {
"default_table_engine": "Memory",
"union_default_mode": "DISTINCT",
"calculate_text_stack_trace": 0,
}
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
database_name = "sqltest_" + "".join(
random.choice(string.ascii_lowercase) for _ in range(10)
)
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
client.execute(f"DROP DATABASE IF EXISTS {database_name}", settings=settings)
client.execute(f"CREATE DATABASE {database_name}", settings=settings)
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
client = Client(host="localhost", port=9000, database=database_name)
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
summary = {"success": 0, "total": 0, "results": {}}
2023-07-19 01:42:32 +00:00
log_file = open("test.log", "w")
report_html_file = open("report.html", "w")
2023-07-19 02:37:02 +00:00
with open("features.yml", "r") as file:
2023-07-19 01:42:32 +00:00
yaml_content = yaml.safe_load(file)
for category in yaml_content:
2023-07-19 01:54:09 +00:00
log_file.write(category.capitalize() + " features:\n")
2023-07-19 02:37:02 +00:00
summary["results"][category] = {"success": 0, "total": 0, "results": {}}
2023-07-19 01:42:32 +00:00
for test in yaml_content[category]:
2023-07-19 01:54:09 +00:00
log_file.write(test + ": " + yaml_content[category][test] + "\n")
2023-07-19 02:37:02 +00:00
summary["results"][category]["results"][test] = {
"success": 0,
"total": 0,
"description": yaml_content[category][test],
}
2023-07-19 01:42:32 +00:00
test_path = test[0] + "/" + test + ".tests.yml"
if os.path.exists(test_path):
2023-07-19 02:37:02 +00:00
with open(test_path, "r") as test_file:
test_yaml_content = yaml.load_all(test_file, Loader=yaml.FullLoader)
2023-07-19 01:42:32 +00:00
for test_case in test_yaml_content:
2023-07-19 02:37:02 +00:00
queries = test_case["sql"]
2023-07-19 01:42:32 +00:00
if not isinstance(queries, list):
queries = [queries]
for query in queries:
# Example: E011-01
2023-07-19 02:37:02 +00:00
test_group = ""
if "-" in test:
2023-07-19 01:42:32 +00:00
test_group = test.split("-", 1)[0]
2023-07-19 02:37:02 +00:00
summary["results"][category]["results"][test_group][
"total"
] += 1
summary["results"][category]["results"][test]["total"] += 1
summary["results"][category]["total"] += 1
summary["total"] += 1
2023-07-19 01:42:32 +00:00
2023-07-19 01:54:09 +00:00
log_file.write(query + "\n")
2023-07-19 01:42:32 +00:00
try:
2023-07-19 02:37:02 +00:00
result = client.execute(query, settings=settings)
2023-07-19 02:00:50 +00:00
log_file.write(str(result) + "\n")
2023-07-19 01:42:32 +00:00
if test_group:
2023-07-19 02:37:02 +00:00
summary["results"][category]["results"][test_group][
"success"
] += 1
summary["results"][category]["results"][test][
"success"
] += 1
summary["results"][category]["success"] += 1
summary["success"] += 1
2023-07-19 01:42:32 +00:00
except Exception as e:
2023-07-19 01:54:09 +00:00
log_file.write(f"Error occurred: {str(e)}\n")
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
client.execute(f"DROP DATABASE {database_name}", settings=settings)
2023-07-19 01:42:32 +00:00
def enable_color(ratio):
if ratio == 0:
return "<b style='color: red;'>"
elif ratio < 0.5:
return "<b style='color: orange;'>"
elif ratio < 1:
return "<b style='color: gray;'>"
else:
return "<b style='color: green;'>"
2023-07-19 02:37:02 +00:00
reset_color = "</b>"
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
def print_ratio(indent, name, success, total, description):
report_html_file.write(
"{}{}: {}{} / {} ({:.1%}){}{}\n".format(
" " * indent,
name.capitalize(),
enable_color(success / total),
success,
total,
success / total,
reset_color,
f" - " + html.escape(description) if description else "",
)
)
report_html_file.write(
"<html><body><pre style='font-size: 16pt; padding: 1em; line-height: 1.25;'>\n"
)
print_ratio(0, "Total", summary["success"], summary["total"], "")
for category in summary["results"]:
cat_summary = summary["results"][category]
if cat_summary["total"] == 0:
2023-07-19 01:42:32 +00:00
continue
2023-07-19 02:37:02 +00:00
print_ratio(2, category, cat_summary["success"], cat_summary["total"], "")
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
for test in summary["results"][category]["results"]:
test_summary = summary["results"][category]["results"][test]
2023-07-19 01:42:32 +00:00
2023-07-19 02:37:02 +00:00
if test_summary["total"] == 0:
2023-07-19 01:42:32 +00:00
continue
2023-07-19 02:37:02 +00:00
print_ratio(
6 if "-" in test else 4,
test,
test_summary["success"],
test_summary["total"],
test_summary["description"],
)
2023-07-19 01:42:32 +00:00
2023-07-19 01:54:09 +00:00
report_html_file.write("</pre></body></html>\n")