run_test.py 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. from pathlib import Path
  2. import sys
  3. import time
  4. from watchdog.observers import Observer
  5. from watchdog.events import FileSystemEventHandler
  6. from watchdog.events import LoggingEventHandler
  7. import asyncio.subprocess as subprocess
  8. import asyncio
  9. from watchfiles import awatch, watch
  10. from termcolor import colored
  11. from datetime import datetime
  12. import orjson
  13. import os
  14. async def assert_sql(sql, target):
  15. p = await subprocess.create_subprocess_exec(
  16. "xmake",
  17. "run",
  18. "sql-parser",
  19. sql,
  20. stdout=subprocess.PIPE,
  21. stderr=subprocess.PIPE,
  22. )
  23. stdout, stderr = await p.communicate()
  24. if stderr:
  25. print(colored(stderr, "yellow"))
  26. if b"error" in stdout:
  27. print(stdout.decode("utf-8"))
  28. print(datetime.now(), "-" * 40)
  29. assert False, "sql-parser error"
  30. try:
  31. output = orjson.loads(stdout)
  32. except Exception as e:
  33. output = {"error": e, "output": stdout.decode("utf-8")}
  34. assert (
  35. output == target
  36. ), f"""{colored("sql-parser error", "red")}
  37. input: {colored(sql, "yellow")}
  38. expect: {colored(target, "green")}
  39. actual: {colored(output, "red")}
  40. """
  41. async def assert_sqls():
  42. await assert_sql(
  43. "create table asd;", [{"type": "create_table", "table_name": "asd", "cols": []}]
  44. )
  45. await assert_sql(
  46. "create table tb (col1 INT, col2 string, col3 FLOAT);",
  47. [
  48. {
  49. "type": "create_table",
  50. "table_name": "tb",
  51. "cols": [
  52. {
  53. "type": "create_column",
  54. "column_name": "col1",
  55. "data_type": "INT",
  56. "primary_key": False,
  57. },
  58. {
  59. "type": "create_column",
  60. "column_name": "col2",
  61. "data_type": "string",
  62. "primary_key": False,
  63. },
  64. {
  65. "type": "create_column",
  66. "column_name": "col3",
  67. "data_type": "FLOAT",
  68. "primary_key": False,
  69. },
  70. ],
  71. }
  72. ],
  73. )
  74. await assert_sql(
  75. """
  76. create table tb1 (
  77. col1 int primary key,
  78. col2 FLOAT
  79. );
  80. """,
  81. [
  82. {
  83. "type": "create_table",
  84. "table_name": "tb1",
  85. "cols": [
  86. {
  87. "type": "create_column",
  88. "column_name": "col1",
  89. "data_type": "int",
  90. "primary_key": True,
  91. },
  92. {
  93. "type": "create_column",
  94. "column_name": "col2",
  95. "data_type": "FLOAT",
  96. "primary_key": False,
  97. },
  98. ],
  99. }
  100. ],
  101. )
  102. await assert_sql(
  103. """
  104. create table tb2 (
  105. x float,
  106. y int,
  107. z int
  108. );
  109. """,
  110. [
  111. {
  112. "type": "create_table",
  113. "table_name": "tb2",
  114. "cols": [
  115. {
  116. "type": "create_column",
  117. "column_name": "x",
  118. "data_type": "float",
  119. "primary_key": False,
  120. },
  121. {
  122. "type": "create_column",
  123. "column_name": "y",
  124. "data_type": "int",
  125. "primary_key": False,
  126. },
  127. {
  128. "type": "create_column",
  129. "column_name": "z",
  130. "data_type": "int",
  131. "primary_key": False,
  132. },
  133. ],
  134. }
  135. ],
  136. )
  137. await assert_sql(
  138. """insert into tb1 values (1, 'foo');""",
  139. [
  140. {
  141. "type": "insert",
  142. "table_name": "tb1",
  143. "values": [
  144. {"type": "int", "value": 1},
  145. {"type": "string", "value": "'foo'"},
  146. ],
  147. }
  148. ],
  149. )
  150. await assert_sql(
  151. """insert into tb1 values (2, 'foo', 'zxc', 1234.234);""",
  152. [
  153. {
  154. "type": "insert",
  155. "table_name": "tb1",
  156. "values": [
  157. {"type": "int", "value": 2},
  158. {"type": "string", "value": "'foo'"},
  159. {"type": "string", "value": "'zxc'"},
  160. {"type": "float", "value": 1234.234},
  161. ],
  162. }
  163. ],
  164. )
  165. await assert_sql(
  166. "update tb1 set col1=3 where col1=2 and col2=col3;",
  167. [
  168. {
  169. "type": "update",
  170. "table_name": "tb1",
  171. "set": [
  172. {
  173. "type": "assign_const",
  174. "left": {"type": "identifier", "value": "col1"},
  175. "right": {"type": "int", "value": 3},
  176. }
  177. ],
  178. "where": [
  179. {
  180. "type": "where_condition",
  181. "left": {"type": "identifier", "value": "col1"},
  182. "right": {"type": "int", "value": 2},
  183. },
  184. {
  185. "type": "where_condition",
  186. "left": {"type": "identifier", "value": "col2"},
  187. "right": {"type": "identifier", "value": "col3"},
  188. },
  189. ],
  190. }
  191. ],
  192. )
  193. await assert_sql(
  194. "delete from tb1 where c1 = 1 and c2= 3;",
  195. [
  196. {
  197. "type": "delete",
  198. "table_name": "tb1",
  199. "where": [
  200. {
  201. "type": "where_condition",
  202. "left": {"type": "identifier", "value": "c1"},
  203. "right": {"type": "int", "value": 1},
  204. },
  205. {
  206. "type": "where_condition",
  207. "left": {"type": "identifier", "value": "c2"},
  208. "right": {"type": "int", "value": 3},
  209. },
  210. ],
  211. }
  212. ],
  213. )
  214. async def on_modified(event):
  215. p = await subprocess.create_subprocess_shell(
  216. "xmake", stdout=subprocess.PIPE, stderr=subprocess.PIPE
  217. )
  218. stdout, _ = await p.communicate()
  219. if b"error" in stdout:
  220. print(stdout.decode("utf-8"))
  221. print(datetime.now(), "-" * 40)
  222. return
  223. try:
  224. await assert_sqls()
  225. except Exception as e:
  226. print(e)
  227. else:
  228. print(datetime.now(), colored("all tests right!", "green"))
  229. async def restart():
  230. async for _ in awatch(__file__):
  231. print("restart")
  232. os.execl("/bin/python", Path(__file__).as_posix(), Path(__file__).as_posix())
  233. async def watch_src():
  234. async for changes in awatch("src"):
  235. print(datetime.now(), "re run...")
  236. await asyncio.wait_for(on_modified(changes), 20)
  237. async def main():
  238. try:
  239. await assert_sqls()
  240. except Exception as e:
  241. print(e)
  242. await asyncio.gather(restart(), watch_src())
  243. if __name__ == "__main__":
  244. asyncio.run(main())