|
| 1 | +;;; test-sqlite3.el --- Unit tests for sqlite3.el |
| 2 | + |
| 3 | +;; Copyright (C) 2016 by Syohei YOSHIDA |
| 4 | + |
| 5 | +;; Author: Syohei YOSHIDA <syohex@gmail.com> |
| 6 | + |
| 7 | +;; This program 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 | +;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>. |
| 19 | + |
| 20 | +;;; Commentary: |
| 21 | + |
| 22 | +;;; Code: |
| 23 | + |
| 24 | +(require 'ert) |
| 25 | +(require 'sqlite3) |
| 26 | + |
| 27 | +(ert-deftest sqlite3-new () |
| 28 | + "Create sqlite3 instance" |
| 29 | + (let ((db (sqlite3-new))) |
| 30 | + (should db))) |
| 31 | + |
| 32 | +(ert-deftest sqlite3-execute-batch () |
| 33 | + "Execute SQL query" |
| 34 | + (let ((db (sqlite3-new))) |
| 35 | + (sqlite3-execute-batch db "CREATE TABLE foo(id integer primary key, name text);") |
| 36 | + (let ((got (sqlite3-execute-batch db "INSERT INTO foo(id, name) values(1, \"Tom\");"))) |
| 37 | + (should (= got 1))))) |
| 38 | + |
| 39 | +(ert-deftest sqlite3-execute-batch-placeholder () |
| 40 | + "Execute SQL query with placeholder" |
| 41 | + (let ((db (sqlite3-new))) |
| 42 | + (sqlite3-execute-batch db "CREATE TABLE foo(id integer primary key, name text);") |
| 43 | + (let ((got (sqlite3-execute-batch db "INSERT INTO foo(name) values(?);" ["Bob"]))) |
| 44 | + (should (= got 1))))) |
| 45 | + |
| 46 | +(ert-deftest sqlite3-execute-select-with-callback () |
| 47 | + "Execute SELECT query with callback" |
| 48 | + (let ((db (sqlite3-new))) |
| 49 | + (sqlite3-execute-batch db "CREATE TABLE foo(id integer primary key, name text);") |
| 50 | + (sqlite3-execute-batch db "INSERT INTO foo(name) values(\"Tom\");") |
| 51 | + (sqlite3-execute-batch db "INSERT INTO foo(name) values(\"Bob\");") |
| 52 | + (sqlite3-execute-batch db "INSERT INTO foo(name) values(\"Chris\");") |
| 53 | + (let ((rows 0)) |
| 54 | + (sqlite3-execute |
| 55 | + db |
| 56 | + "SELECT name FROM foo" |
| 57 | + (lambda (row fields) |
| 58 | + (cl-incf rows) |
| 59 | + (should (member (car row) '("Tom" "Bob" "Chris"))))) |
| 60 | + (should (= rows 3))) |
| 61 | + |
| 62 | + (let ((rows 0)) |
| 63 | + (sqlite3-execute |
| 64 | + db |
| 65 | + "SELECT name id, name FROM foo where id == 2" |
| 66 | + (lambda (row fields) |
| 67 | + (cl-incf rows) |
| 68 | + (should (string= (cl-second row) "Bob")))) |
| 69 | + (should (= rows 1))))) |
| 70 | + |
| 71 | +(ert-deftest sqlite3-execute-select-with-resultset () |
| 72 | + "Execute SELECT query with resultset" |
| 73 | + (let ((db (sqlite3-new))) |
| 74 | + (sqlite3-execute-batch db "CREATE TABLE foo(id integer primary key, name text);") |
| 75 | + (sqlite3-execute-batch db "INSERT INTO foo(name) values(\"Tom\");") |
| 76 | + (sqlite3-execute-batch db "INSERT INTO foo(name) values(\"Bob\");") |
| 77 | + (sqlite3-execute-batch db "INSERT INTO foo(name) values(\"Chris\");") |
| 78 | + (let ((resultset (sqlite3-execute db "SELECT name FROM foo"))) |
| 79 | + (should resultset) |
| 80 | + (should (equal (sqlite3-resultset-fields resultset) '("name"))) |
| 81 | + (dotimes (_ 3) |
| 82 | + (let ((row (sqlite3-resultset-next resultset))) |
| 83 | + (should (member (car row) '("Tom" "Bob" "Chris"))))) |
| 84 | + (sqlite3-resultset-next resultset) ;; last call |
| 85 | + (should (sqlite3-resultset-eof resultset))))) |
| 86 | + |
| 87 | +;;; test-sqlite3.el ends here |
0 commit comments