aboutsummaryrefslogtreecommitdiff
path: root/tests/model-package.scm
blob: bf2cf71be82176755274dfe07340cc4950aaaff4 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
(define-module (test-model-package)
  #:use-module (ice-9 match)
  #:use-module (srfi srfi-1)
  #:use-module (srfi srfi-64)
  #:use-module (guix utils)
  #:use-module (guix tests)
  #:use-module (tests mock-inferior)
  #:use-module (guix-data-service model utils)
  #:use-module (guix-data-service model license)
  #:use-module (guix-data-service model license-set)
  #:use-module (guix-data-service model package)
  #:use-module (guix-data-service model package-metadata)
  #:use-module (guix-data-service database))

(test-begin "test-model-package")

(define mock-inferior-package-foo
  (mock-inferior-package
   (name "foo")
   (version "2")
   (synopsis "Foo")
   (description "Foo description")
   (home-page "https://example.com")
   (location (location "file.scm" 5 0))))

(define mock-inferior-package-foo-2
  (mock-inferior-package
   (name "foo")
   (version "2")
   (synopsis "Foo")
   (description "Foo description")
   (home-page #f)
   (location #f)))

(define (test-license-set-ids conn)
  (let ((license-id-lists
         (inferior-packages->license-id-lists
          conn
          '((("License 1"
              "https://gnu.org/licenses/test-1.html"
              "https://example.com/why-license-1"))))))

    (inferior-packages->license-set-ids conn license-id-lists)))

(define mock-inferior-packages
  (list mock-inferior-package-foo
        mock-inferior-package-foo-2))

(define mock-package-metadata
  (map (lambda (mock-inf-pkg)
         (list
          (mock-inferior-package-home-page mock-inf-pkg)
          (mock-inferior-package-location mock-inf-pkg)
          `(("en_US.UTF-8" . "Fake synopsis"))
          `(("en_US.UTF-8" . "Fake description"))))
       mock-inferior-packages))

(with-mock-inferior-packages
 (lambda ()
   (use-modules (guix-data-service model package)
                (guix-data-service model git-repository)
                (guix-data-service model guix-revision)
                (guix-data-service model package-metadata))

   (with-postgresql-connection
    "test-model-package"
    (lambda (conn)
      (check-test-database! conn)

      (with-postgresql-transaction
       conn
       (lambda (conn)
         (test-assert "inferior-packages->package-ids works once"
           (let ((package-metadata-ids
                  (inferior-packages->package-metadata-ids
                   conn
                   mock-package-metadata
                   (test-license-set-ids conn)))
                 (package-replacement-package-ids
                  (make-list (length mock-inferior-packages)
                             (cons "integer" NULL))))
             (match (inferior-packages->package-ids
                     conn
                     (zip (map mock-inferior-package-name mock-inferior-packages)
                          (map mock-inferior-package-version mock-inferior-packages)
                          package-metadata-ids
                          package-replacement-package-ids))
               ((x) (number? x))))))
       #:always-rollback? #t)

      (with-postgresql-transaction
       conn
       (lambda (conn)
         (let ((package-metadata-ids
                (inferior-packages->package-metadata-ids
                 conn
                 mock-package-metadata
                 (test-license-set-ids conn)))
               (package-replacement-package-ids
                (make-list (length mock-inferior-packages)
                           (cons "integer" NULL))))
           (test-equal "inferior-packages->package-ids is idempotent"
             (inferior-packages->package-ids
              conn
              (zip (map mock-inferior-package-name mock-inferior-packages)
                   (map mock-inferior-package-version mock-inferior-packages)
                   package-metadata-ids
                   package-replacement-package-ids))
             (inferior-packages->package-ids
              conn
              (zip (map mock-inferior-package-name mock-inferior-packages)
                   (map mock-inferior-package-version mock-inferior-packages)
                   package-metadata-ids
                   package-replacement-package-ids)))))
       #:always-rollback? #t)))))

(test-end)