basic.scm 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. ;;;; basic.scm --- -*- mode: scheme; coding: utf-8; -*-
  2. ;;;;
  3. ;;;; Copyright (C) 2011 Detlev Zundel <dzu@denx.de>
  4. ;;;; Copyright (C) 2018 Ludovic Courtès <ludo@gnu.org>
  5. ;;;;
  6. ;;;; This library is free software; you can redistribute it and/or
  7. ;;;; modify it under the terms of the GNU Lesser General Public
  8. ;;;; License as published by the Free Software Foundation; either
  9. ;;;; version 3 of the License, or (at your option) any later version.
  10. ;;;;
  11. ;;;; This library is distributed in the hope that it will be useful,
  12. ;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. ;;;; Lesser General Public License for more details.
  15. ;;;;
  16. ;;;; You should have received a copy of the GNU Lesser General Public
  17. ;;;; License along with this library; if not, write to the Free Software
  18. ;;;; Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  19. (define-module (tests basic-test)
  20. #:use-module (srfi srfi-64)
  21. #:use-module (ice-9 format)
  22. #:use-module (sqlite3))
  23. (define (sqlite-exec* db sql key value)
  24. (let ((stmt (sqlite-prepare db sql)))
  25. (sqlite-bind stmt key value)
  26. (sqlite-map display stmt)
  27. (sqlite-finalize stmt)
  28. #t))
  29. ;; Cleanup database so we can check creation
  30. (define db-name "tests/simple.db")
  31. (if (file-exists? db-name)
  32. (begin
  33. (format #t "Removing leftover database ~a~%" db-name)
  34. (delete-file db-name)))
  35. (define db
  36. ;; Global database used for tests.
  37. #f)
  38. (test-begin "basic")
  39. (test-assert "sqlite-open"
  40. (begin
  41. (set! db (sqlite-open db-name (logior SQLITE_OPEN_CREATE
  42. SQLITE_OPEN_READWRITE)))
  43. (sqlite-db? db)))
  44. (test-assert "sqlite-busy-timeout"
  45. (sqlite-busy-timeout db 20))
  46. (test-assert "create table"
  47. (sqlite-exec db
  48. "create table project (
  49. reference integer primary key,
  50. name text,
  51. website text
  52. )"))
  53. (test-assert "insert"
  54. (sqlite-exec db
  55. "insert into project values (1, 'Guile', '');
  56. insert into project values (2, 'Guix', 'gnu.org');"))
  57. (test-assert "sqlite-prepare with caching"
  58. (let* ((s "SELECT * FROM project")
  59. (stmt (sqlite-prepare db s #:cache? #t)))
  60. (eq? stmt (sqlite-prepare db s #:cache? #t))))
  61. (test-equal "select"
  62. '(#(1 "Guile" "") #(2 "Guix" "gnu.org"))
  63. (let* ((stmt (sqlite-prepare db "select * from project"))
  64. (result (sqlite-map identity stmt)))
  65. (sqlite-finalize stmt)
  66. result))
  67. (test-assert "select with named parameters"
  68. (sqlite-exec* db "select * from project where 'bla' = :foo" ":foo" "bla"))
  69. (test-assert "select with named parameters, alternate form"
  70. (sqlite-exec* db "select * from project where 'bla' = :foo" 'foo "bla"))
  71. (test-assert "insert with sqlite-bind"
  72. (begin
  73. (sqlite-exec db "CREATE TABLE foos (dbid INTEGER PRIMARY KEY, name TEXT)")
  74. (let ((stmt (sqlite-prepare db "INSERT INTO foos(name) VALUES(?)")))
  75. (sqlite-bind stmt 1 "myfoo")
  76. (sqlite-step stmt)
  77. (sqlite-finalize stmt)
  78. #t)))
  79. (test-assert "drop"
  80. (sqlite-exec db "DROP TABLE IF EXISTS foos"))
  81. (sqlite-close db)
  82. (delete-file db-name)
  83. (test-end "basic")