aboutsummaryrefslogtreecommitdiffstats
path: root/test/src/sqlite-tests.el
diff options
context:
space:
mode:
Diffstat (limited to 'test/src/sqlite-tests.el')
-rw-r--r--test/src/sqlite-tests.el244
1 files changed, 244 insertions, 0 deletions
diff --git a/test/src/sqlite-tests.el b/test/src/sqlite-tests.el
new file mode 100644
index 00000000000..5af43923012
--- /dev/null
+++ b/test/src/sqlite-tests.el
@@ -0,0 +1,244 @@
1;;; sqlite-tests.el --- Tests for sqlite.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2021-2022 Free Software Foundation, Inc.
4
5;; This file is part of GNU Emacs.
6
7;; GNU Emacs is free software: you can redistribute it and/or modify
8;; it under the terms of the GNU General Public License as published by
9;; the Free Software Foundation, either version 3 of the License, or
10;; (at your option) any later version.
11
12;; GNU Emacs is distributed in the hope that it will be useful,
13;; but WITHOUT ANY WARRANTY; without even the implied warranty of
14;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15;; GNU General Public License for more details.
16
17;; You should have received a copy of the GNU General Public License
18;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
19
20;;; Commentary:
21
22;;
23
24;;; Code:
25
26(require 'ert)
27(require 'ert-x)
28
29(declare-function sqlite-execute "sqlite.c")
30(declare-function sqlite-close "sqlite.c")
31(declare-function sqlitep "sqlite.c")
32(declare-function sqlite-available-p "sqlite.c")
33(declare-function sqlite-finalize "sqlite.c")
34(declare-function sqlite-next "sqlite.c")
35(declare-function sqlite-more-p "sqlite.c")
36(declare-function sqlite-select "sqlite.c")
37(declare-function sqlite-open "sqlite.c")
38(declare-function sqlite-load-extension "sqlite.c")
39
40(ert-deftest sqlite-select ()
41 (skip-unless (sqlite-available-p))
42 (let ((db (sqlite-open)))
43 (should (eq (type-of db) 'sqlite))
44 (should (sqlitep db))
45 (should-not (sqlitep 'foo))
46
47 (should
48 (zerop
49 (sqlite-execute
50 db "create table if not exists test1 (col1 text, col2 integer, col3 float, col4 blob)")))
51
52 (should-error
53 (sqlite-execute
54 db "insert into test1 (col1, col2, col3, col4) values ('foo', 2, 9.45, 'bar', 'zot')"))
55
56 (should
57 (=
58 (sqlite-execute
59 db "insert into test1 (col1, col2, col3, col4) values ('foo', 2, 9.45, 'bar')")
60 1))
61
62 (should
63 (equal
64 (sqlite-select db "select * from test1" nil 'full)
65 '(("col1" "col2" "col3" "col4") ("foo" 2 9.45 "bar"))))))
66
67(ert-deftest sqlite-set ()
68 (skip-unless (sqlite-available-p))
69 (let ((db (sqlite-open))
70 set)
71 (should
72 (zerop
73 (sqlite-execute
74 db "create table if not exists test1 (col1 text, col2 integer)")))
75
76 (should
77 (=
78 (sqlite-execute db "insert into test1 (col1, col2) values ('foo', 1)")
79 1))
80 (should
81 (=
82 (sqlite-execute db "insert into test1 (col1, col2) values ('bar', 2)")
83 1))
84
85 (setq set (sqlite-select db "select * from test1" nil 'set))
86 (should (sqlitep set))
87 (should (sqlite-more-p set))
88 (should (equal (sqlite-next set)
89 '("foo" 1)))
90 (should (equal (sqlite-next set)
91 '("bar" 2)))
92 (should-not (sqlite-next set))
93 (should-not (sqlite-more-p set))
94 (sqlite-finalize set)
95 (should-error (sqlite-next set))))
96
97(ert-deftest sqlite-chars ()
98 (skip-unless (sqlite-available-p))
99 (let (db)
100 (setq db (sqlite-open))
101 (sqlite-execute
102 db "create table if not exists test2 (col1 text, col2 integer)")
103 (sqlite-execute
104 db "insert into test2 (col1, col2) values ('fóo', 3)")
105 (sqlite-execute
106 db "insert into test2 (col1, col2) values ('fó‚o', 3)")
107 (sqlite-execute
108 db "insert into test2 (col1, col2) values ('f‚o', 4)")
109 (should
110 (equal (sqlite-select db "select * from test2" nil 'full)
111 '(("col1" "col2") ("fóo" 3) ("fó‚o" 3) ("f‚o" 4))))))
112
113(ert-deftest sqlite-numbers ()
114 (skip-unless (sqlite-available-p))
115 (let (db)
116 (setq db (sqlite-open))
117 (sqlite-execute
118 db "create table if not exists test3 (col1 integer)")
119 (let ((big (expt 2 50))
120 (small (expt 2 10)))
121 (sqlite-execute db (format "insert into test3 values (%d)" small))
122 (sqlite-execute db (format "insert into test3 values (%d)" big))
123 (should
124 (equal
125 (sqlite-select db "select * from test3")
126 (list (list small) (list big)))))))
127
128(ert-deftest sqlite-param ()
129 (skip-unless (sqlite-available-p))
130 (let (db)
131 (setq db (sqlite-open))
132 (sqlite-execute
133 db "create table if not exists test4 (col1 text, col2 number)")
134 (sqlite-execute db "insert into test4 values (?, ?)" (list "foo" 1))
135 (should
136 (equal
137 (sqlite-select db "select * from test4 where col2 = ?" '(1))
138 '(("foo" 1))))
139 (should
140 (equal
141 (sqlite-select db "select * from test4 where col2 = ?" [1])
142 '(("foo" 1))))))
143
144(ert-deftest sqlite-binary ()
145 (skip-unless (sqlite-available-p))
146 (let (db)
147 (setq db (sqlite-open))
148 (sqlite-execute
149 db "create table if not exists test5 (col1 text, col2 number)")
150 (let ((string (with-temp-buffer
151 (set-buffer-multibyte nil)
152 (insert 0 1 2)
153 (buffer-string))))
154 (should-not (multibyte-string-p string))
155 (sqlite-execute
156 db "insert into test5 values (?, ?)" (list string 2))
157 (let ((out (caar
158 (sqlite-select db "select col1 from test5 where col2 = 2"))))
159 (should (equal out string))))))
160
161(ert-deftest sqlite-different-dbs ()
162 (skip-unless (sqlite-available-p))
163 (let (db1 db2)
164 (setq db1 (sqlite-open))
165 (setq db2 (sqlite-open))
166 (sqlite-execute
167 db1 "create table if not exists test6 (col1 text, col2 number)")
168 (sqlite-execute
169 db2 "create table if not exists test6 (col1 text, col2 number)")
170 (sqlite-execute
171 db1 "insert into test6 values (?, ?)" '("foo" 2))
172 (should (sqlite-select db1 "select * from test6"))
173 (should-not (sqlite-select db2 "select * from test6"))))
174
175(ert-deftest sqlite-close-dbs ()
176 (skip-unless (sqlite-available-p))
177 (let (db)
178 (setq db (sqlite-open))
179 (sqlite-execute
180 db "create table if not exists test6 (col1 text, col2 number)")
181 (sqlite-execute db "insert into test6 values (?, ?)" '("foo" 2))
182 (should (sqlite-select db "select * from test6"))
183 (sqlite-close db)
184 (should-error (sqlite-select db "select * from test6"))))
185
186(ert-deftest sqlite-load-extension ()
187 (skip-unless (sqlite-available-p))
188 (skip-unless (fboundp 'sqlite-load-extension))
189 (let (db)
190 (setq db (sqlite-open))
191 (should-error
192 (sqlite-load-extension db "/usr/lib/sqlite3/notpcre.so"))
193 (should-error
194 (sqlite-load-extension db "/usr/lib/sqlite3/n"))
195 (should-error
196 (sqlite-load-extension db "/usr/lib/sqlite3/"))
197 (should-error
198 (sqlite-load-extension db "/usr/lib/sqlite3"))
199 (should
200 (memq
201 (sqlite-load-extension db "/usr/lib/sqlite3/pcre.so")
202 '(nil t)))
203
204 (should-error
205 (sqlite-load-extension
206 db "/usr/lib/x86_64-linux-gnu/libsqlite3_mod_notcsvtable.so"))
207 (should-error
208 (sqlite-load-extension
209 db "/usr/lib/x86_64-linux-gnu/libsqlite3_mod_csvtablen.so"))
210 (should-error
211 (sqlite-load-extension
212 db "/usr/lib/x86_64-linux-gnu/libsqlite3_mod_csvtable"))
213 (should
214 (memq
215 (sqlite-load-extension
216 db "/usr/lib/x86_64-linux-gnu/libsqlite3_mod_csvtable.so")
217 '(nil t)))))
218
219(ert-deftest sqlite-blob ()
220 (skip-unless (sqlite-available-p))
221 (let (db)
222 (progn
223 (setq db (sqlite-open))
224 (sqlite-execute
225 db "create table if not exists test10 (col1 text, col2 blob, col3 numbre)")
226 (let ((string (with-temp-buffer
227 (set-buffer-multibyte nil)
228 (insert 0 1 2)
229 (buffer-string))))
230 (should-not (multibyte-string-p string))
231 (sqlite-execute
232 db "insert into test10 values (?, ?, 1)"
233 (list string
234 (propertize string
235 'coding-system 'binary)))
236 (cl-destructuring-bind
237 (c1 c2 _)
238 (car (sqlite-select db "select * from test10 where col3 = 1"))
239 (should (equal c1 string))
240 (should (equal c2 string))
241 (should (multibyte-string-p c1))
242 (should-not (multibyte-string-p c2)))))))
243
244;;; sqlite-tests.el ends here