test-ob-sql.el 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  1. ;;; test-ob-sql.el --- tests for ob-sql.el -*- lexical-binding: t; -*-
  2. ;; Copyright (C) 2021 Robin Joy
  3. ;; Author: Robin Joy <rcj@robinjoy.net>
  4. ;; Keywords: lisp
  5. ;; This program is free software; you can redistribute it and/or modify
  6. ;; it under the terms of the GNU General Public License as published by
  7. ;; the Free Software Foundation, either version 3 of the License, or
  8. ;; (at your option) any later version.
  9. ;; This program is distributed in the hope that it will be useful,
  10. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. ;; GNU General Public License for more details.
  13. ;; You should have received a copy of the GNU General Public License
  14. ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
  15. ;;; Code:
  16. (unless (featurep 'ob-sql)
  17. (signal 'missing-test-dependency "Support for sql code blocks"))
  18. (defmacro ob-sql/command (&rest body)
  19. "Execute body and return the command that would have been executed."
  20. `(cl-letf (((symbol-function 'org-babel-eval)
  21. (lambda (command &rest _) (throw 'sql-command command))))
  22. (catch 'sql-command
  23. ,@body)))
  24. (defmacro ob-sql/command-should-contain (regexp sql-block)
  25. "Check that REGEXP is contained in the command executed when evaluating SQL-BLOCK."
  26. `(let ((regexps ,(if (listp regexp) regexp `(list ,regexp)))
  27. (command (ob-sql/command (org-test-with-temp-text
  28. ,sql-block
  29. (org-babel-next-src-block)
  30. (org-babel-execute-src-block)))))
  31. (dolist (regexp regexps)
  32. (should (string-match-p regexp command)))))
  33. (defmacro ob-sql/command-should-not-contain (regexp sql-block)
  34. "Check that REGEXP is not contained in the command executed when evaluating SQL-BLOCK."
  35. `(let ((command (ob-sql/command
  36. (org-test-with-temp-text
  37. ,sql-block
  38. (org-babel-next-src-block)
  39. (org-babel-execute-src-block)))))
  40. (should-not (string-match-p ,regexp command))))
  41. ;;; dbish
  42. (ert-deftest ob-sql/engine-dbi-uses-dbish ()
  43. (ob-sql/command-should-contain "^dbish " "
  44. #+begin_src sql :engine dbi
  45. select * from dummy;
  46. #+end_src"))
  47. (ert-deftest ob-sql/engine-dbish-uses-batch-mode ()
  48. (ob-sql/command-should-contain " --batch " "
  49. #+begin_src sql :engine dbi :dbuser dummy
  50. select * from dummy;
  51. #+end_src"))
  52. (ert-deftest ob-sql/engine-dbish-can-pass-additional-cmdline-params ()
  53. (ob-sql/command-should-contain " cmdlineparams " "
  54. #+begin_src sql :engine dbi :dbpassword dummy :cmdline cmdlineparams
  55. select * from dummy;
  56. #+end_src"))
  57. ;;; monetdb
  58. (ert-deftest ob-sql/engine-monetdb-uses-mclient ()
  59. (ob-sql/command-should-contain "^mclient " "
  60. #+begin_src sql :engine monetdb
  61. select * from dummy;
  62. #+end_src"))
  63. (ert-deftest ob-sql/engine-monetdb-outputs-values-tab-separated ()
  64. (ob-sql/command-should-contain " -f tab " "
  65. #+begin_src sql :engine monetdb
  66. select * from dummy;
  67. #+end_src"))
  68. (ert-deftest ob-sql/engine-monetdb-can-pass-additional-cmdline-params ()
  69. (ob-sql/command-should-contain " cmdlineparams " "
  70. #+begin_src sql :engine monetdb :dbpassword dummy :cmdline cmdlineparams
  71. select * from dummy;
  72. #+end_src"))
  73. ;;; mssql
  74. (ert-deftest ob-sql/engine-mssql-uses-sqlcmd ()
  75. (ob-sql/command-should-contain "^sqlcmd " "
  76. #+begin_src sql :engine mssql
  77. select * from dummy;
  78. #+end_src"))
  79. (ert-deftest ob-sql/engine-mssql-outputs-values-tab-separated ()
  80. (ob-sql/command-should-contain " -s \"\t\" " "
  81. #+begin_src sql :engine mssql
  82. select * from dummy;
  83. #+end_src"))
  84. (ert-deftest ob-sql/engine-mssql-can-pass-additional-cmdline-params ()
  85. (ob-sql/command-should-contain " cmdlineparams " "
  86. #+begin_src sql :engine mssql :dbpassword dummy :cmdline cmdlineparams
  87. select * from dummy;
  88. #+end_src"))
  89. (ert-deftest ob-sql/engine-mssql-passes-user-if-provided ()
  90. (ob-sql/command-should-contain " -U \"dummy\" " "
  91. #+begin_src sql :engine mssql :dbuser dummy
  92. select * from dummy;
  93. #+end_src"))
  94. (ert-deftest ob-sql/engine-mssql-passes-password-if-provided ()
  95. (ob-sql/command-should-contain " -P \"dummy\" " "
  96. #+begin_src sql :engine mssql :dbpassword dummy
  97. select * from dummy;
  98. #+end_src"))
  99. (ert-deftest ob-sql/engine-mssql-passes-dbhost-if-provided ()
  100. (ob-sql/command-should-contain " -S \"localhost\" " "
  101. #+begin_src sql :engine mssql :dbhost localhost
  102. select * from dummy;
  103. #+end_src"))
  104. (ert-deftest ob-sql/engine-mssql-passes-database-if-provided ()
  105. (ob-sql/command-should-contain " -d \"R01\" " "
  106. #+begin_src sql :engine mssql :database R01
  107. select * from dummy;
  108. #+end_src"))
  109. (ert-deftest ob-sql/engine-mssql-passes-all-parameter-provided ()
  110. (ob-sql/command-should-contain '(" -d \"R01\" " " -S \"localhost\" " " -P \"pwd\" " " -U \"usr\" ") "
  111. #+begin_src sql :engine mssql :database R01 :dbhost localhost :dbport 30101 :dbinstance 1 :dbuser usr :dbpassword pwd
  112. select * from dummy;
  113. #+end_src"))
  114. ;;; MySQL
  115. (ert-deftest ob-sql/engine-mysql-uses-mysql ()
  116. (ob-sql/command-should-contain "^mysql " "
  117. #+begin_src sql :engine mysql
  118. select * from dummy;
  119. #+end_src"))
  120. (ert-deftest ob-sql/engine-mysql-passes-user-if-provided ()
  121. (ob-sql/command-should-contain " -udummy " "
  122. #+begin_src sql :engine mysql :dbuser dummy
  123. select * from dummy;
  124. #+end_src"))
  125. (ert-deftest ob-sql/engine-mysql-passes-password-if-provided ()
  126. (ob-sql/command-should-contain " -pdummy " "
  127. #+begin_src sql :engine mysql :dbpassword dummy
  128. select * from dummy;
  129. #+end_src"))
  130. (ert-deftest ob-sql/engine-mysql-passes-dbhost-if-provided ()
  131. (ob-sql/command-should-contain " -hlocalhost " "
  132. #+begin_src sql :engine mysql :dbhost localhost
  133. select * from dummy;
  134. #+end_src"))
  135. (ert-deftest ob-sql/engine-mysql-passes-host-if-provided ()
  136. (ob-sql/command-should-contain " -P30101 " "
  137. #+begin_src sql :engine mysql :dbport 30101
  138. select * from dummy;
  139. #+end_src"))
  140. (ert-deftest ob-sql/engine-mysql-passes-database-if-provided ()
  141. (ob-sql/command-should-contain " -dR01 " "
  142. #+begin_src sql :engine mysql :database R01
  143. select * from dummy;
  144. #+end_src"))
  145. (ert-deftest ob-sql/engine-mysql-passes-all-parameter-provided ()
  146. (ob-sql/command-should-contain '(" -dR01 " " -hlocalhost " " -P30101 " " -ppwd " " -uusr ") "
  147. #+begin_src sql :engine mysql :database R01 :dbhost localhost :dbport 30101 :dbinstance 1 :dbuser usr :dbpassword pwd
  148. select * from dummy;
  149. #+end_src"))
  150. ;;; oracle
  151. (ert-deftest ob-sql/engine-oracle-uses-sqlplus ()
  152. (ob-sql/command-should-contain "^sqlplus " "
  153. #+begin_src sql :engine oracle :dbuser dummy :dbpassword dummy :database dummy
  154. select * from dummy;
  155. #+end_src"))
  156. (ert-deftest ob-sql/engine-oracle-passes-user-pwd-database-host-port-if-provided ()
  157. (ob-sql/command-should-contain " dummy/dummypwd@localhost:12345/R01 " "
  158. #+begin_src sql :engine oracle :dbuser dummy :dbpassword dummypwd :dbhost localhost :database R01 :dbport 12345
  159. select * from dummy;
  160. #+end_src"))
  161. (ert-deftest ob-sql/engine-oracle-passes-user-pwd-database-if-no-host-port-provided ()
  162. (ob-sql/command-should-contain " dummy/dummypwd@R01 " "
  163. #+begin_src sql :engine oracle :dbuser dummy :dbpassword dummypwd :database R01
  164. select * from dummy;
  165. #+end_src"))
  166. ;;; postgresql
  167. (ert-deftest ob-sql/engine-postgresql-uses-psql ()
  168. (ob-sql/command-should-contain "^psql " "
  169. #+begin_src sql :engine postgresql
  170. select * from dummy;
  171. #+end_src"))
  172. (ert-deftest ob-sql/engine-postgresql-passes-password-if-provided ()
  173. (ob-sql/command-should-contain "^PGPASSWORD=dummy " "
  174. #+begin_src sql :engine postgresql :dbpassword dummy
  175. select * from dummy;
  176. #+end_src"))
  177. (ert-deftest ob-sql/engine-postgresql-stop-on-error ()
  178. (ob-sql/command-should-contain " --set=\"ON_ERROR_STOP=1\" " "
  179. #+begin_src sql :engine postgresql
  180. select * from dummy;
  181. #+end_src"))
  182. (ert-deftest ob-sql/engine-postgresql-does-not-output-column-names-if-requested ()
  183. (ob-sql/command-should-contain " -t " "
  184. #+begin_src sql :engine postgresql :colnames no
  185. select * from dummy;
  186. #+end_src"))
  187. (ert-deftest ob-sql/engine-postgresql-outputs-column-names-by-default ()
  188. (ob-sql/command-should-not-contain " -t " "
  189. #+begin_src sql :engine postgresql
  190. select * from dummy;
  191. #+end_src"))
  192. (ert-deftest ob-sql/engine-postgresql-can-pass-additional-cmdline-params ()
  193. (ob-sql/command-should-contain " cmdlineparams$" "
  194. #+begin_src sql :engine postgresql :dbpassword dummy :cmdline cmdlineparams
  195. select * from dummy;
  196. #+end_src"))
  197. ;;; SAP HANA
  198. (ert-deftest ob-sql/engine-saphana-uses-hdbsql ()
  199. (ob-sql/command-should-contain "^hdbsql " "
  200. #+begin_src sql :engine saphana
  201. select * from dummy;
  202. #+end_src"))
  203. (ert-deftest ob-sql/engine-saphana-passes-user-if-provided ()
  204. (ob-sql/command-should-contain " -u dummy " "
  205. #+begin_src sql :engine saphana :dbuser dummy
  206. select * from dummy;
  207. #+end_src"))
  208. (ert-deftest ob-sql/engine-saphana-passes-password-if-provided ()
  209. (ob-sql/command-should-contain " -p dummy " "
  210. #+begin_src sql :engine saphana :dbpassword dummy
  211. select * from dummy;
  212. #+end_src"))
  213. (ert-deftest ob-sql/engine-saphana-passes-dbinstance-if-provided ()
  214. (ob-sql/command-should-contain " -i 1 " "
  215. #+begin_src sql :engine saphana :dbinstance 1
  216. select * from dummy;
  217. #+end_src"))
  218. (ert-deftest ob-sql/engine-saphana-passes-dbhost-if-provided ()
  219. (ob-sql/command-should-contain " -n localhost " "
  220. #+begin_src sql :engine saphana :dbhost localhost
  221. select * from dummy;
  222. #+end_src"))
  223. (ert-deftest ob-sql/engine-saphana-passes-dbhost-and-dbport-if-provided ()
  224. (ob-sql/command-should-contain " -n localhost:30101 " "
  225. #+begin_src sql :engine saphana :dbhost localhost :dbport 30101
  226. select * from dummy;
  227. #+end_src"))
  228. (ert-deftest ob-sql/engine-saphana-does-not-pass-host-port-if-only-port-provided ()
  229. (ob-sql/command-should-not-contain " -n" "
  230. #+begin_src sql :engine saphana :dbport 30101
  231. select * from dummy;
  232. #+end_src"))
  233. (ert-deftest ob-sql/engine-saphana-passes-database-if-provided ()
  234. (ob-sql/command-should-contain " -d R01 " "
  235. #+begin_src sql :engine saphana :database R01
  236. select * from dummy;
  237. #+end_src"))
  238. (ert-deftest ob-sql/engine-saphana-passes-all-parameter-provided ()
  239. (ob-sql/command-should-contain '(" -d R01 " " -n localhost:30101 " " -i 1 " " -p pwd " " -u usr") "
  240. #+begin_src sql :engine saphana :database R01 :dbhost localhost :dbport 30101 :dbinstance 1 :dbuser usr :dbpassword pwd
  241. select * from dummy;
  242. #+end_src"))
  243. (ert-deftest ob-sql/engine-saphana-can-pass-additional-cmdline-params ()
  244. (ob-sql/command-should-contain " cmdlineparams$" "
  245. #+begin_src sql :engine saphana :dbpassword dummy :cmdline cmdlineparams
  246. select * from dummy;
  247. #+end_src"))
  248. ;;; sqsh
  249. (ert-deftest ob-sql/engine-sqsh-uses-sqsh ()
  250. (ob-sql/command-should-contain "^sqsh " "
  251. #+begin_src sql :engine sqsh
  252. select * from dummy;
  253. #+end_src"))
  254. (ert-deftest ob-sql/engine-sqsh-can-pass-additional-cmdline-params ()
  255. (ob-sql/command-should-contain " cmdlineparams " "
  256. #+begin_src sql :engine sqsh :dbpassword dummy :cmdline cmdlineparams
  257. select * from dummy;
  258. #+end_src"))
  259. (ert-deftest ob-sql/engine-sqsh-passes-user-if-provided ()
  260. (ob-sql/command-should-contain " -U \"dummy\" " "
  261. #+begin_src sql :engine sqsh :dbuser dummy
  262. select * from dummy;
  263. #+end_src"))
  264. (ert-deftest ob-sql/engine-sqsh-passes-password-if-provided ()
  265. (ob-sql/command-should-contain " -P \"dummy\" " "
  266. #+begin_src sql :engine sqsh :dbpassword dummy
  267. select * from dummy;
  268. #+end_src"))
  269. (ert-deftest ob-sql/engine-sqsh-passes-host-if-provided ()
  270. (ob-sql/command-should-contain " -S \"localhost\" " "
  271. #+begin_src sql :engine sqsh :dbhost localhost
  272. select * from dummy;
  273. #+end_src"))
  274. (ert-deftest ob-sql/engine-sqsh-passes-database-if-provided ()
  275. (ob-sql/command-should-contain " -D \"R01\" " "
  276. #+begin_src sql :engine sqsh :database R01
  277. select * from dummy;
  278. #+end_src"))
  279. ;;; vertica
  280. (ert-deftest ob-sql/engine-vertica-uses-vsql ()
  281. (ob-sql/command-should-contain "^vsql " "
  282. #+begin_src sql :engine vertica
  283. select * from dummy;
  284. #+end_src"))
  285. (ert-deftest ob-sql/engine-vertica-can-pass-additional-cmdline-params ()
  286. (ob-sql/command-should-contain " cmdlineparams$" "
  287. #+begin_src sql :engine vertica :dbpassword dummy :cmdline cmdlineparams
  288. select * from dummy;
  289. #+end_src"))
  290. (ert-deftest ob-sql/engine-vertica-passes-user-if-provided ()
  291. (ob-sql/command-should-contain " -U dummy " "
  292. #+begin_src sql :engine vertica :dbuser dummy
  293. select * from dummy;
  294. #+end_src"))
  295. (ert-deftest ob-sql/engine-vertica-passes-password-if-provided ()
  296. (ob-sql/command-should-contain " -w dummy " "
  297. #+begin_src sql :engine vertica :dbpassword dummy
  298. select * from dummy;
  299. #+end_src"))
  300. (ert-deftest ob-sql/engine-vertica-passes-host-if-provided ()
  301. (ob-sql/command-should-contain " -h localhost " "
  302. #+begin_src sql :engine vertica :dbhost localhost
  303. select * from dummy;
  304. #+end_src"))
  305. (ert-deftest ob-sql/engine-vertica-passes-database-if-provided ()
  306. (ob-sql/command-should-contain " -d R01 " "
  307. #+begin_src sql :engine vertica :database R01
  308. select * from dummy;
  309. #+end_src"))
  310. (ert-deftest ob-sql/engine-vertica-passes-port-if-provided ()
  311. (ob-sql/command-should-contain " -p 12345 " "
  312. #+begin_src sql :engine vertica :dbport 12345
  313. select * from dummy;
  314. #+end_src"))
  315. (provide 'test-ob-sql)
  316. ;;; test-ob-sql.el ends here