Skip to content
Snippets Groups Projects
Unverified Commit 95e4bd92 authored by mergify[bot]'s avatar mergify[bot] Committed by GitHub
Browse files

Merge pull request #7278 from ZeaLoVe/fix-logictest-complete-script

chore: fix complete script
parents 2a5205d6 3fbf1b05
No related branches found
Tags v0.8.7-nightly
No related merge requests found
......@@ -161,7 +161,7 @@ select 1;
1
```
3. if some test has flaky failure, and you want ignore it, simply add skipped before statement query.(Remove it after problem solved)
3. if some test has a flaky failure, and you want to ignore it, simply add skipped before statement query. (Remove it after the problem is solved)
```
statement query skipped I
select 1;
......@@ -177,22 +177,24 @@ select 1;
## Tools
complete.py can auto complete test file for you, It do as follow steps:
complete.py can auto-complete test file for you, It does as follow steps:
1. Get SQLs from source-file, whether an SQL file or logictest file.
2. Execute SQL one by one, if SQL fetches results, add statement query; if SQL fetches nothing, add statement ok; if SQL gets an error, add statement error.
### Usage
- Pre-run, you need to start databend server or mysql server
- Pre-run, you need to start databend server or MySQL server
- Use `./complete.py --source-file="xxx.sql" --dest-file="my-gen"` for SQL files(suffix name must be like *.sql)
- Use `./complete.py --source-file="xxx.test" --fest-file="my-gen"` for logictest files(suffix name not like *.sql, maybe like *.test)
- If you want to see what SQLs get from source-file, add --show-sql
- Use `--enable-auto-cleanup` to add `drop table if exists xxx` or `drop database if exists xxx` at the beginning of the test file
- If you want to see what SQLs get from source-file, add `--show-sql`
- Use the command line to specify host, user, port, password and database. Details in `./complete.py -h`
### Acknowledgement
- *tips* You can use MYSQL syntax to auto-complete test suite, but make sure you know all grammar differences.
- *tips* MYSQL return bool as 1 and 0, this tool make it as `int(I)` in query type option.
- *tips* You can use MYSQL syntax to auto-complete the test suite, but make sure you know all grammar differences.
- *tips* MYSQL return bool as 1 and 0, this tool makes it as `int(I)` in query type option.
- *warning* No multi handlers use in the auto-complete tool(MYSQL only), if handlers return a difference, manual fix it, please.
# Learn More
......
# tuple of (type, name)
# example ('database', 'db1'), ('table', 't1')
need_cleanup_set = set()
enable_auto_cleanup = False
def set_auto_cleanup(flag):
global enable_auto_cleanup
enable_auto_cleanup = flag
def get_cleanup_statements():
if not enable_auto_cleanup:
return []
global need_cleanup_set
res = []
for type, name in need_cleanup_set:
res.append(f"drop {type} if exists {name};")
need_cleanup_set = set()
return res
def pick_create_statement(statement):
if not enable_auto_cleanup:
return
global need_cleanup_set
statement_lower = statement.lower()
if 'create' not in statement_lower:
return
statement_words = statement_lower.strip(';').split()
create_type_index = 1
create_name_index = 2
if 'if' in statement_lower and 'not' in statement_lower and 'exists' in statement_lower:
create_type_index = 5
elif 'if' in statement_lower and 'exists' in statement_lower:
create_type_index = 4
else:
create_type_index = 2
if 'transient' in statement_lower:
create_type_index += 1
create_name_index += 1
create_type = statement_words[1]
create_name = statement_words[create_name_index]
need_cleanup_set.add((create_type, create_name.split('(')[0]))
#!/usr/bin/env python3
# -*- coding: UTF-8 -*-
import os
import re
from argparse import ArgumentParser
import mysql.connector
......@@ -8,10 +9,19 @@ import mysql.connector
from logictest import is_empty_line
from config import mysql_config
from http_connector import format_result
from cleanup import pick_create_statement, get_cleanup_statements, set_auto_cleanup
error_code_regex = r".*Code: (?P<error_code>.*),.*"
target_dir = "./"
def get_error_code(msg):
matched = re.match(error_code_regex, msg, re.MULTILINE | re.IGNORECASE)
if matched is None:
return "{error_code}"
return matched.group("error_code")
def parse_sql_file(source_file):
sqls = list()
f = open(source_file, encoding='UTF-8')
......@@ -113,8 +123,9 @@ def gen_suite_from_sql(sql_and_skips, dest_file):
# use mysql connector
try:
cursor.execute(sql)
pick_create_statement(sql)
except mysql.connector.Error as err:
statements.append(f"statement error {err.errno}\n{sql}\n\n")
statements.append(f"statement error {get_error_code(err.msg)}\n{sql}\n\n")
continue
try:
......@@ -148,6 +159,19 @@ def gen_suite_from_sql(sql_and_skips, dest_file):
except mysql.connector.Error as err:
statements.append(f"statement ok\n{sql}\n\n")
# cleanup database, table
drop_statements = list()
for cleanup_sql in get_cleanup_statements():
try:
drop_statements.append(
f"statement ok\n{cleanup_sql}\n\n"
)
cursor.execute(cleanup_sql)
print(f"Cleanup execute sql: {cleanup_sql}")
except Exception:
pass
out.writelines(drop_statements)
out.writelines(statements)
out.flush()
out.close()
......@@ -167,6 +191,7 @@ def run(args):
mysql_config['database'] = args.mysql_database
print(f"Mysql config: {mysql_config}")
set_auto_cleanup(args.enable_auto_cleanup)
sql_and_skips = get_sql_from_file(args.source_file)
if args.show_sql:
for sql in sql_and_skips:
......@@ -191,6 +216,11 @@ if __name__ == '__main__':
default=False,
help='Show sql from source file')
parser.add_argument('--enable-auto-cleanup',
action='store_true',
default=False,
help="Enable auto cleanup after test per session")
parser.add_argument('--mysql-user', default="root", help='Mysql user')
parser.add_argument('--mysql-host', default="127.0.0.1", help='Mysql host')
......
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment