store-database.scm 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. ;;; GNU Guix --- Functional package management for GNU
  2. ;;; Copyright © 2017, 2018 Ludovic Courtès <ludo@gnu.org>
  3. ;;;
  4. ;;; This file is part of GNU Guix.
  5. ;;;
  6. ;;; GNU Guix is free software; you can redistribute it and/or modify it
  7. ;;; under the terms of the GNU General Public License as published by
  8. ;;; the Free Software Foundation; either version 3 of the License, or (at
  9. ;;; your option) any later version.
  10. ;;;
  11. ;;; GNU Guix is distributed in the hope that it will be useful, but
  12. ;;; WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;;; GNU General Public License for more details.
  15. ;;;
  16. ;;; You should have received a copy of the GNU General Public License
  17. ;;; along with GNU Guix. If not, see <http://www.gnu.org/licenses/>.
  18. (define-module (test-store-database)
  19. #:use-module (guix tests)
  20. #:use-module (guix store)
  21. #:use-module (guix store database)
  22. #:use-module ((guix utils) #:select (call-with-temporary-output-file))
  23. #:use-module (srfi srfi-26)
  24. #:use-module (srfi srfi-64))
  25. ;; Test the (guix store database) module.
  26. (define %store
  27. (open-connection-for-tests))
  28. (test-begin "store-database")
  29. (test-equal "register-path"
  30. '(1 1)
  31. (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
  32. "-fake")))
  33. (when (valid-path? %store file)
  34. (delete-paths %store (list file)))
  35. (false-if-exception (delete-file file))
  36. (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
  37. (drv (string-append file ".drv")))
  38. (call-with-output-file file
  39. (cut display "This is a fake store item.\n" <>))
  40. (register-path file
  41. #:references (list ref)
  42. #:deriver drv)
  43. (and (valid-path? %store file)
  44. (equal? (references %store file) (list ref))
  45. (null? (valid-derivers %store file))
  46. (null? (referrers %store file))
  47. (list (stat:mtime (lstat file))
  48. (stat:mtime (lstat ref)))))))
  49. (test-equal "new database"
  50. (list 1 2)
  51. (call-with-temporary-output-file
  52. (lambda (db-file port)
  53. (delete-file db-file)
  54. (with-database db-file db
  55. (sqlite-register db
  56. #:path "/gnu/foo"
  57. #:references '()
  58. #:deriver "/gnu/foo.drv"
  59. #:hash (string-append "sha256:" (make-string 64 #\e))
  60. #:nar-size 1234)
  61. (sqlite-register db
  62. #:path "/gnu/bar"
  63. #:references '("/gnu/foo")
  64. #:deriver "/gnu/bar.drv"
  65. #:hash (string-append "sha256:" (make-string 64 #\a))
  66. #:nar-size 4321)
  67. (let ((path-id (@@ (guix store database) path-id)))
  68. (list (path-id db "/gnu/foo")
  69. (path-id db "/gnu/bar")))))))
  70. (test-assert "register-path with unregistered references"
  71. ;; Make sure we get a "NOT NULL constraint failed: Refs.reference" error
  72. ;; when we try to add references that are not registered yet. Better safe
  73. ;; than sorry.
  74. (call-with-temporary-output-file
  75. (lambda (db-file port)
  76. (delete-file db-file)
  77. (catch 'sqlite-error
  78. (lambda ()
  79. (with-database db-file db
  80. (sqlite-register db #:path "/gnu/foo"
  81. #:references '("/gnu/bar")
  82. #:deriver "/gnu/foo.drv"
  83. #:hash (string-append "sha256:" (make-string 64 #\e))
  84. #:nar-size 1234))
  85. #f)
  86. (lambda args
  87. (pk 'welcome-exception! args)
  88. #t)))))
  89. (test-end "store-database")