run_sql_parser_test.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. from pathlib import Path
  2. import asyncio.subprocess as subprocess
  3. import asyncio
  4. from watchfiles import awatch
  5. from termcolor import colored
  6. from datetime import datetime
  7. import orjson
  8. import os
  9. import tempfile
  10. from tests_config import sql_parser_tests
  11. async def run_and_output(
  12. *args: str, timeout=5
  13. ) -> tuple[bytes, bytes]:
  14. p = await subprocess.create_subprocess_exec(
  15. *args,
  16. stdout=subprocess.PIPE,
  17. stderr=subprocess.PIPE,
  18. )
  19. stdout, stderr = await asyncio.wait_for(p.communicate(), timeout=timeout)
  20. return stdout, stderr
  21. async def rebuild() -> bool:
  22. stdout, _ = await run_and_output('xmake')
  23. if b"error" in stdout:
  24. print(stdout.decode("utf-8"))
  25. print(datetime.now(), "-" * 40)
  26. return False
  27. else:
  28. return True
  29. async def assert_sql(sql: str, expected: dict):
  30. stdout, stderr = await run_and_output('xmake', 'run', "sql-parser", sql)
  31. if b"error" in stdout:
  32. print(stdout.decode("utf-8"))
  33. print(datetime.now(), "-" * 40)
  34. print(f'other: {colored(stderr.decode("utf-8"), "yellow")}')
  35. assert False, "sql-parser error"
  36. try:
  37. output = orjson.loads(stdout)
  38. except Exception as e:
  39. output = {"error": e, "output": stdout.decode("utf-8")}
  40. open("/tmp/temp/test.py", "wb").write(
  41. f'"{sql}"\n\n'.encode("utf-8")
  42. + orjson.dumps(output, option=orjson.OPT_INDENT_2)
  43. + (b"\n\n" + stderr).replace(b"\n", b"\n# ")
  44. )
  45. assert (
  46. output == expected
  47. ), f"""{colored("sql-parser error", "red")}
  48. input: {colored(sql, "yellow")}
  49. expect: {colored(expected, "green")}
  50. actual: {colored(output, "red")}
  51. other: {colored(stderr.decode("utf-8"), "yellow")}
  52. """
  53. async def assert_sqls():
  54. for sql, excepted in sql_parser_tests:
  55. await assert_sql(sql, excepted)
  56. async def on_parser_modified():
  57. print(datetime.now(), colored("run parser tests...", "yellow"))
  58. try:
  59. await assert_sqls()
  60. except Exception as e:
  61. print(e)
  62. else:
  63. print(datetime.now(), colored("all parser tests right!", "green"))
  64. async def on_checker_modified():
  65. print(datetime.now(), colored("run checker tests...", "yellow"))
  66. stdout, stderr = await run_and_output('xmake', 'run', "sql-checker")
  67. print(stdout.decode("utf-8"))
  68. print(stderr.decode("utf-8"))
  69. print(datetime.now(), colored("all checker tests right!", "green"))
  70. async def restart():
  71. async for _ in awatch(__file__):
  72. print("restart")
  73. os.execl("/bin/python", Path(__file__).as_posix(), Path(__file__).as_posix())
  74. async def watch_parser():
  75. async for changes in awatch("./src/parser.y", "./src/parser.l"):
  76. if await rebuild():
  77. await asyncio.wait_for(on_parser_modified(), 10)
  78. async def watch_checker():
  79. async for changes in awatch("./src/checker.cpp", "./src/checker.h", "./src/utils.h", "./src/utils.cpp"):
  80. if await rebuild():
  81. await on_checker_modified()
  82. async def main():
  83. await asyncio.gather(
  84. restart(),
  85. watch_parser(),
  86. watch_checker(),
  87. on_parser_modified(),
  88. on_checker_modified(),
  89. )
  90. if __name__ == "__main__":
  91. asyncio.run(main())