Merge branch 'master' into staging
[jackhill/guix/guix.git] / tests / store-database.scm
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
19 (define-module (test-store-database)
20 #:use-module (guix tests)
21 #:use-module (guix store)
22 #:use-module (guix store database)
23 #:use-module ((guix utils) #:select (call-with-temporary-output-file))
24 #:use-module (srfi srfi-26)
25 #:use-module (srfi srfi-64))
26
27 ;; Test the (guix store database) module.
28
29 (define %store
30 (open-connection-for-tests))
31
32 \f
33 (test-begin "store-database")
34
35 (test-assert "register-path"
36 (let ((file (string-append (%store-prefix) "/" (make-string 32 #\f)
37 "-fake")))
38 (when (valid-path? %store file)
39 (delete-paths %store (list file)))
40 (false-if-exception (delete-file file))
41
42 (let ((ref (add-text-to-store %store "ref-of-fake" (random-text)))
43 (drv (string-append file ".drv")))
44 (call-with-output-file file
45 (cut display "This is a fake store item.\n" <>))
46 (register-path file
47 #:references (list ref)
48 #:deriver drv)
49
50 (and (valid-path? %store file)
51 (equal? (references %store file) (list ref))
52 (null? (valid-derivers %store file))
53 (null? (referrers %store file))))))
54
55 (test-equal "new database"
56 (list 1 2)
57 (call-with-temporary-output-file
58 (lambda (db-file port)
59 (delete-file db-file)
60 (with-database db-file db
61 (sqlite-register db
62 #:path "/gnu/foo"
63 #:references '()
64 #:deriver "/gnu/foo.drv"
65 #:hash (string-append "sha256:" (make-string 64 #\e))
66 #:nar-size 1234)
67 (sqlite-register db
68 #:path "/gnu/bar"
69 #:references '("/gnu/foo")
70 #:deriver "/gnu/bar.drv"
71 #:hash (string-append "sha256:" (make-string 64 #\a))
72 #:nar-size 4321)
73 (let ((path-id (@@ (guix store database) path-id)))
74 (list (path-id db "/gnu/foo")
75 (path-id db "/gnu/bar")))))))
76
77 (test-assert "register-path with unregistered references"
78 ;; Make sure we get a "NOT NULL constraint failed: Refs.reference" error
79 ;; when we try to add references that are not registered yet. Better safe
80 ;; than sorry.
81 (call-with-temporary-output-file
82 (lambda (db-file port)
83 (delete-file db-file)
84 (catch 'sqlite-error
85 (lambda ()
86 (with-database db-file db
87 (sqlite-register db #:path "/gnu/foo"
88 #:references '("/gnu/bar")
89 #:deriver "/gnu/foo.drv"
90 #:hash (string-append "sha256:" (make-string 64 #\e))
91 #:nar-size 1234))
92 #f)
93 (lambda args
94 (pk 'welcome-exception! args)
95 #t)))))
96
97 (test-end "store-database")