aboutsummaryrefslogtreecommitdiffstats
path: root/test/src/sqlite-tests.el
diff options
context:
space:
mode:
authorLars Ingebrigtsen2021-12-11 04:55:57 +0100
committerLars Ingebrigtsen2021-12-11 04:55:57 +0100
commit3d38d1d1345aa65c4018b42e6c648606e32216f8 (patch)
tree5e6b51f9aa79c6a2e7604934f3ffe9314db21c71 /test/src/sqlite-tests.el
parentaf1c5ec0fcd3f25234cfe2986c873ff2e5ed63a0 (diff)
downloademacs-3d38d1d1345aa65c4018b42e6c648606e32216f8.tar.gz
emacs-3d38d1d1345aa65c4018b42e6c648606e32216f8.zip
Add sqlite3 support to Emacs
* configure.ac: Add check for the sqlite library. * doc/lispref/text.texi (Database): Document it. * lisp/sqlite.el: New file. * lisp/term/w32-win.el (dynamic-library-alist): Add a mapping. * src/Makefile.in (SQLITE3_LIBS): Add the libraries. * src/alloc.c (union emacs_align_type): Add a Lisp_Sqlite struct. * src/data.c (Ftype_of): Add sqlite. * src/emacs.c (main): Load the syms. * src/lisp.h (DEFINE_GDB_SYMBOL_BEGIN): Add PVEC_SQLITE. (GCALIGNED_STRUCT): New struct to keep data for sqlite database objects and statement objects. (SQLITEP, SQLITE, CHECK_SQLITE, XSQLITE): New macros for accessing the objects. * src/pdumper.c (dump_vectorlike): Update hash. (dump_vectorlike): Don't dump it. * src/print.c (print_vectorlike): Add a printer for the sqlite object. * src/sqlite.c: New file. * test/src/sqlite-tests.el: Add tests.
Diffstat (limited to 'test/src/sqlite-tests.el')
-rw-r--r--test/src/sqlite-tests.el175
1 files changed, 175 insertions, 0 deletions
diff --git a/test/src/sqlite-tests.el b/test/src/sqlite-tests.el
new file mode 100644
index 00000000000..3fffa0100ba
--- /dev/null
+++ b/test/src/sqlite-tests.el
@@ -0,0 +1,175 @@
1;;; sqlite-tests.el --- Tests for sqlite.el -*- lexical-binding: t; -*-
2
3;; Copyright (C) 2021 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(ert-deftest sqlite-select ()
30 (skip-unless (sqlite-available-p))
31 (let ((db (sqlite-open)))
32 (should (eq (type-of db) 'sqlite))
33 (should (sqlitep db))
34 (should-not (sqlitep 'foo))
35
36 (should
37 (zerop
38 (sqlite-execute
39 db "create table if not exists test1 (col1 text, col2 integer, col3 float, col4 blob)")))
40
41 (should-error
42 (sqlite-execute
43 db "insert into test1 (col1, col2, col3, col4) values ('foo', 2, 9.45, 'bar', 'zot')"))
44
45 (should
46 (=
47 (sqlite-execute
48 db "insert into test1 (col1, col2, col3, col4) values ('foo', 2, 9.45, 'bar')")
49 1))
50
51 (should
52 (equal
53 (sqlite-select db "select * from test1" nil 'full)
54 '(("col1" "col2" "col3" "col4") ("foo" 2 9.45 "bar"))))))
55
56;; (setq db (sqlite-open))
57
58(ert-deftest sqlite-set ()
59 (skip-unless (sqlite-available-p))
60 (let ((db (sqlite-open))
61 set)
62 (should
63 (zerop
64 (sqlite-execute
65 db "create table if not exists test1 (col1 text, col2 integer)")))
66
67 (should
68 (=
69 (sqlite-execute db "insert into test1 (col1, col2) values ('foo', 1)")
70 1))
71 (should
72 (=
73 (sqlite-execute db "insert into test1 (col1, col2) values ('bar', 2)")
74 1))
75
76 (setq set (sqlite-select db "select * from test1" nil 'set))
77 (should (sqlitep set))
78 (should (sqlite-more-p set))
79 (should (equal (sqlite-next set)
80 '("foo" 1)))
81 (should (equal (sqlite-next set)
82 '("bar" 2)))
83 (should-not (sqlite-next set))
84 (should-not (sqlite-more-p set))))
85
86(ert-deftest sqlite-chars ()
87 (skip-unless (sqlite-available-p))
88 (let (db)
89 (setq db (sqlite-open))
90 (sqlite-execute
91 db "create table if not exists test2 (col1 text, col2 integer)")
92 (sqlite-execute
93 db "insert into test2 (col1, col2) values ('fóo', 3)")
94 (sqlite-execute
95 db "insert into test2 (col1, col2) values ('fó‚o', 3)")
96 (sqlite-execute
97 db "insert into test2 (col1, col2) values ('f‚o', 4)")
98 (should
99 (equal (sqlite-select db "select * from test2" nil 'full)
100 '(("col1" "col2") ("fóo" 3) ("fó‚o" 3) ("f‚o" 4))))))
101
102(ert-deftest sqlite-numbers ()
103 (skip-unless (sqlite-available-p))
104 (let (db)
105 (setq db (sqlite-open))
106 (sqlite-execute
107 db "create table if not exists test3 (col1 integer)")
108 (let ((big (expt 2 50))
109 (small (expt 2 10)))
110 (sqlite-execute db (format "insert into test3 values (%d)" small))
111 (sqlite-execute db (format "insert into test3 values (%d)" big))
112 (should
113 (equal
114 (sqlite-select db "select * from test3")
115 (list (list small) (list big)))))))
116
117(ert-deftest sqlite-param ()
118 (skip-unless (sqlite-available-p))
119 (let (db)
120 (setq db (sqlite-open))
121 (sqlite-execute
122 db "create table if not exists test4 (col1 text, col2 number)")
123 (sqlite-execute db "insert into test4 values (?, ?)" (list "foo" 1))
124 (should
125 (equal
126 (sqlite-select db "select * from test4 where col2 = ?" '(1))
127 '(("foo" 1))))
128 (should
129 (equal
130 (sqlite-select db "select * from test4 where col2 = ?" [1])
131 '(("foo" 1))))))
132
133(ert-deftest sqlite-binary ()
134 (skip-unless (sqlite-available-p))
135 (let (db)
136 (setq db (sqlite-open))
137 (sqlite-execute
138 db "create table if not exists test5 (col1 text, col2 number)")
139 (let ((string (with-temp-buffer
140 (set-buffer-multibyte nil)
141 (insert 0 1 2)
142 (buffer-string))))
143 (should-not (multibyte-string-p string))
144 (sqlite-execute
145 db "insert into test5 values (?, ?)" (list string 2))
146 (let ((out (caar
147 (sqlite-select db "select col1 from test5 where col2 = 2"))))
148 (should (equal out string))))))
149
150(ert-deftest sqlite-different-dbs ()
151 (skip-unless (sqlite-available-p))
152 (let (db1 db2)
153 (setq db1 (sqlite-open))
154 (setq db2 (sqlite-open))
155 (sqlite-execute
156 db1 "create table if not exists test6 (col1 text, col2 number)")
157 (sqlite-execute
158 db2 "create table if not exists test6 (col1 text, col2 number)")
159 (sqlite-execute
160 db1 "insert into test6 values (?, ?)" '("foo" 2))
161 (should (sqlite-select db1 "select * from test6"))
162 (should-not (sqlite-select db2 "select * from test6"))))
163
164(ert-deftest sqlite-close-dbs ()
165 (skip-unless (sqlite-available-p))
166 (let (db)
167 (setq db (sqlite-open))
168 (sqlite-execute
169 db "create table if not exists test6 (col1 text, col2 number)")
170 (sqlite-execute db "insert into test6 values (?, ?)" '("foo" 2))
171 (should (sqlite-select db "select * from test6"))
172 (sqlite-close db)
173 (should-error (sqlite-select db "select * from test6"))))
174
175;;; sqlite-tests.el ends here