Move registry tests to tests/gnustest-registry.el.
[gnus] / lisp / tests / gnustest-registry.el
1 ;;; gnustest-registry.el --- Registry and Gnus registry testing for Gnus
2 ;; Copyright (C) 2011-2012 Free Software Foundation, Inc.
3
4 ;; Author: Ted Zlatanov <tzz@lifelogs.com>
5
6 ;; This file is not part of GNU Emacs.
7
8 ;; GNU Emacs is free software; you can redistribute it and/or modify
9 ;; it under the terms of the GNU General Public License as published by
10 ;; the Free Software Foundation; either version 3, or (at your option)
11 ;; any later version.
12
13 ;; GNU Emacs is distributed in the hope that it will be useful,
14 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
15 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 ;; GNU General Public License for more details.
17
18 ;; You should have received a copy of the GNU General Public License
19 ;; along with GNU Emacs; see the file COPYING.  If not, write to the
20 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 ;; Boston, MA 02110-1301, USA.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (eval-when-compile
28   (when (null (ignore-errors (require 'ert)))
29     (defmacro* ert-deftest (name () &body docstring-keys-and-body))))
30
31 (ignore-errors
32   (require 'ert))
33
34 (require 'registry)
35 (require 'gnus-registry)
36
37 (ert-deftest gnustest-registry-usage-test ()
38   (let* ((n 100)
39          (db (registry-make-testable-db n)))
40     (message "size %d" n)
41     (should (= n (registry-size db)))
42     (message "max-hard test")
43     (should-error (registry-insert db "new" '()))
44     (message "Individual lookup")
45     (should (= 58 (caadr (registry-lookup db '(1 58 99)))))
46     (message "Grouped individual lookup")
47     (should (= 3 (length (registry-lookup db '(1 58 99)))))
48     (when (boundp 'lexical-binding)
49       (message "Individual lookup (breaks before lexbind)")
50       (should (= 58
51                  (caadr (registry-lookup-breaks-before-lexbind db '(1 58 99)))))
52       (message "Grouped individual lookup (breaks before lexbind)")
53       (should (= 3
54                  (length (registry-lookup-breaks-before-lexbind db
55                                                                 '(1 58 99))))))
56     (message "Search")
57     (should (= n (length (registry-search db :all t))))
58     (should (= n (length (registry-search db :member '((sender "me"))))))
59     (message "Secondary index search")
60     (should (= n (length (registry-lookup-secondary-value db 'sender "me"))))
61     (should (equal '(74) (registry-lookup-secondary-value db 'groups "74")))
62     (message "Delete")
63     (should (registry-delete db '(1) t))
64     (decf n)
65     (message "Search after delete")
66     (should (= n (length (registry-search db :all t))))
67     (message "Secondary search after delete")
68     (should (= n (length (registry-lookup-secondary-value db 'sender "me"))))
69     ;; (message "Pruning")
70     ;; (let* ((tokeep (registry-search db :member '((extra "more data"))))
71     ;;        (count (- n (length tokeep)))
72     ;;        (pruned (registry-prune db))
73     ;;        (prune-count (length pruned)))
74     ;;   (message "Expecting to prune %d entries and pruned %d"
75     ;;            count prune-count)
76     ;;   (should (and (= count 5)
77     ;;                (= count prune-count))))
78     (message "Done with usage testing.")))
79
80 (ert-deftest gnustest-registry-persistence-test ()
81   (let* ((n 100)
82          (tempfile (make-temp-file "registry-persistence-"))
83          (name "persistence tester")
84          (db (registry-make-testable-db n name tempfile))
85          size back)
86     (message "Saving to %s" tempfile)
87     (eieio-persistent-save db)
88     (setq size (nth 7 (file-attributes tempfile)))
89     (message "Saved to %s: size %d" tempfile size)
90     (should (< 0 size))
91     (with-temp-buffer
92       (insert-file-contents-literally tempfile)
93       (should (looking-at (concat ";; Object "
94                                   name
95                                   "\n;; EIEIO PERSISTENT OBJECT"))))
96     (message "Reading object back")
97     (setq back (eieio-persistent-read tempfile))
98     (should back)
99     (message "Read object back: %d keys, expected %d==%d"
100              (registry-size back) n (registry-size db))
101     (should (= (registry-size back) n))
102     (should (= (registry-size back) (registry-size db)))
103     (delete-file tempfile))
104   (message "Done with persistence testing."))
105
106 (ert-deftest gnustest-gnus-registry-misc-test ()
107   (should-error (gnus-registry-extract-addresses '("" "")))
108
109   (should (equal '("Ted Zlatanov <tzz@lifelogs.com>"
110                    "noname <ed@you.me>"
111                    "noname <cyd@stupidchicken.com>"
112                    "noname <tzz@lifelogs.com>")
113                  (gnus-registry-extract-addresses
114                   (concat "Ted Zlatanov <tzz@lifelogs.com>, "
115                           "ed <ed@you.me>, " ; "ed" is not a valid name here
116                           "cyd@stupidchicken.com, "
117                           "tzz@lifelogs.com")))))
118
119 (ert-deftest gnustest-gnus-registry-usage-test ()
120   (let* ((n 100)
121          (tempfile (make-temp-file "gnus-registry-persist"))
122          (db (gnus-registry-make-db tempfile))
123          (gnus-registry-db db)
124          back size)
125     (message "Adding %d keys to the test Gnus registry" n)
126     (dotimes (i n)
127       (let ((id (number-to-string i)))
128         (gnus-registry-handle-action id
129                                      (if (>= 50 i) "fromgroup" nil)
130                                      "togroup"
131                                      (when (>= 70 i)
132                                        (format "subject %d" (mod i 10)))
133                                      (when (>= 80 i)
134                                        (format "sender %d" (mod i 10))))))
135     (message "Testing Gnus registry size is %d" n)
136     (should (= n (registry-size db)))
137     (message "Looking up individual keys (registry-lookup)")
138     (should (equal (loop for e
139                          in (mapcar 'cadr
140                                     (registry-lookup db '("20" "83" "72")))
141                          collect (assq 'subject e)
142                          collect (assq 'sender e)
143                          collect (assq 'group e))
144                    '((subject "subject 0") (sender "sender 0") (group "togroup")
145                      (subject) (sender) (group "togroup")
146                      (subject) (sender "sender 2") (group "togroup"))))
147
148     (message "Looking up individual keys (gnus-registry-id-key)")
149     (should (equal (gnus-registry-get-id-key "34" 'group) '("togroup")))
150     (should (equal (gnus-registry-get-id-key "34" 'subject) '("subject 4")))
151     (message "Trying to insert a duplicate key")
152     (should-error (gnus-registry-insert db "55" '()))
153     (message "Looking up individual keys (gnus-registry-get-or-make-entry)")
154     (should (gnus-registry-get-or-make-entry "22"))
155     (message "Saving the Gnus registry to %s" tempfile)
156     (should (gnus-registry-save tempfile db))
157     (setq size (nth 7 (file-attributes tempfile)))
158     (message "Saving the Gnus registry to %s: size %d" tempfile size)
159     (should (< 0 size))
160     (with-temp-buffer
161       (insert-file-contents-literally tempfile)
162       (should (looking-at (concat ";; Object "
163                                   "Gnus Registry"
164                                   "\n;; EIEIO PERSISTENT OBJECT"))))
165     (message "Reading Gnus registry back")
166     (setq back (eieio-persistent-read tempfile))
167     (should back)
168     (message "Read Gnus registry back: %d keys, expected %d==%d"
169              (registry-size back) n (registry-size db))
170     (should (= (registry-size back) n))
171     (should (= (registry-size back) (registry-size db)))
172     (delete-file tempfile)
173     (message "Pruning Gnus registry to 0 by setting :max-soft")
174     (oset db :max-soft 0)
175     (registry-prune db)
176     (should (= (registry-size db) 0)))
177   (message "Done with Gnus registry usage testing."))
178
179 (provide 'gnustest-registry)