-
Notifications
You must be signed in to change notification settings - Fork 0
/
ob-sql-session-tests.el
203 lines (169 loc) · 5.72 KB
/
ob-sql-session-tests.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
;;; tests.el --- Tests for ob-sql-session.el -*- lexical-binding: t -*-
;;; Code:
(load-file "./ob-sql.el")
;; redefine (or patch...)
(defun sql-comint-sqlite (product &optional options buf-name)
"Create comint buffer and connect to SQLite."
;; Put all parameters to the program (if defined) in a list and call
;; make-comint.
(let ((params
(append options
;; allows connection to in-memory database.
(if (and sql-database
(not (string-empty-p sql-database)))
`(,(expand-file-name sql-database))))))
(sql-comint product params buf-name)))
(defun results-block-contents (&optional position)
"Return the contents of the *only* results block in the buffer.
Assume the source block is at POSITION if non-nil."
(interactive)
(save-excursion
(progn
(if position
(goto-char position)
(goto-char 0)
(org-babel-next-src-block))
(goto-char (org-babel-where-is-src-block-result))
(let ((result (org-babel-read-result)))
result))))
(defmacro with-buffer-contents (s &rest forms)
"Create a temporary buffer with contents S and execute FORMS."
`(save-excursion
(with-temp-buffer
(progn
(goto-char 0)
(insert ,s)
(goto-char 0)
,@forms))))
(defun setup (body)
"Initialise the test environment and run BODY."
(let ((org-babel-sql-session-start-interpreter-prompt
(lambda (&rest _) t))
(org-confirm-babel-evaluate
(lambda (lang body)
(not (or (string= lang "sql")
(string= lang "sql-session"))))))
(funcall body)))
(defun babel-block-test (setup header code expect)
"Given SETUP function, execute SQL CODE block with HEADER.
Compare the result against EXPECT."
(setup
(lambda ()
(let ((buffer-contents (format "
#+begin_src %s
%s
#+end_src" header code)))
(message buffer-contents)
(with-buffer-contents
buffer-contents
(org-mode)
(org-babel-next-src-block)
(org-babel-execute-src-block)
(should (string= expect (results-block-contents)))
)))))
(defun sqlite-test (code expect)
"SQL test CODE block, EXPECT 'ing this result."
(babel-block-test
#'setup "sql :engine sqlite :session sqlite::tests :results raw"
code expect))
(ert-deftest sqlite-000:test-header ()
"Create table."
(sqlite-test ".headers off" nil))
(ert-deftest sqlite-001:test-create ()
"Create table."
(sqlite-test ".headers off
create table test(one varchar(10), two int);" nil))
(ert-deftest sqlite-002:test-insert ()
"Insert into table."
(sqlite-test "insert into test values(\'hello\',\'world\');" nil))
(ert-deftest sqlite-003:test-select ()
"Select from table."
(sqlite-test "select * from test;"
"hello|world\n"))
(ert-deftest sqlite-004:test-filter-tabs ()
"Insert with tabs."
(sqlite-test "
--create table test(x,y);
select * from test;
"
"hello|world\n"))
;; gh is on SQLite version 3.37.2 2022-01-06,
;; and its error message is slightly different
;; (ert-deftest sqlite-005:test-stop-on-error ()
;; "stop on error.
;; joining line isn't ideal on that. May consider solution (2)"
;; (sqlite-test "create table test(x,y);
;; select 1;
;; "
;; "Parse error: table test already exists\n create table test(x,y); select 1; \n ^--- error here" ))
;; (ert-deftest sqlite-005a:test-multiple-commands ()
;; "Copy pasting this in sqlite3 will give the same result."
;; :expected-result :failed
;; (sqlite-test
;; "
;; .headers on
;; -- ?
;; .bail on
;; select 1;
;; "
;; "Parse error: near \".\": syntax error\n .headers on .bail on " ; select 1; \n ^--- error here"
;; )) ;; variations expected between sqlite versions
(ert-deftest sqlite-005a:test-commands ()
(sqlite-test
".headers on
" nil))
(ert-deftest sqlite-005b:test-header-on ()
(sqlite-test
".headers on
--create table test(x,y);
delete from test;
insert into test values ('sqlite','3.40');
insert into test values (1,2);
select * from test;"
"one|two
sqlite|3.4
1|2
"))
;; additionally, an error after a command can clutter the next shell
;;
(ert-deftest sqlite-006:drop ()
(sqlite-test "Drop table test;" nil))
(ert-deftest sqlite-007:test-close-session()
(with-current-buffer "*SQL: [sqlite::tests]*" ; sqlite:///nil*"
(quit-process nil t)
(let ((kill-buffer-query-functions nil))
(kill-this-buffer))))
(defun pg-test (code expect)
"Test Postgres SQL CODE, with EXPECT 'ed result."
(babel-block-test
#'setup
"sql :engine postgres :dbhost localhost :database pg :dbuser pg :dbpassword pg :results raw"
code expect))
(defun pg-test-session (code expect)
"Test Postgres SQL CODE, with EXPECT 'ed result."
(babel-block-test
#'setup
"sql :engine postgres :dbhost localhost :database pg :dbuser pg :dbpassword pg :results raw \
:session pg::tests"
code expect))
(ert-deftest pg-001:test-session-var-set ()
"Select in a table."
(pg-test-session "\\set id10 10 \n \\set id13 13" nil))
(ert-deftest pg-002:test-session-var-read ()
"Select in a table."
(pg-test-session "select :id10 as A,:id13 as B;" "a|b\n10|13\n"))
(ert-deftest pg-001:test-create-insert-select ()
"Select in a table."
(pg-test "DROP TABLE if exists publications;
CREATE TABLE publications (id int2, database text);
INSERT INTO publications VALUES (10, 'HGNC'), (13, 'FlyBase');
SELECT database FROM publications where id=10 or id=13;" "DROP TABLE
CREATE TABLE
INSERT 0 2
database\nHGNC\nFlyBase\n"))
;; (eval-buffer)
;; (ert :new)
;; (ert t)
;; (ert-delete-all-tests)
;; (with-current-buffer "ob-sql.el" (save-buffer))
;; (progn (ert-delete-all-tests)(eval-buffer)(ert :new))