a609210a2d945d94feec270daeeb66b6d1c1f151
[software/mumi.git] / tests / xapian.scm
1 ;;; mumi -- Mediocre, uh, mail interface
2 ;;; Copyright © 2020 Ricardo Wurmus <rekado@elephly.net>
3 ;;;
4 ;;; This program is free software: you can redistribute it and/or
5 ;;; modify it under the terms of the GNU Affero General Public License
6 ;;; as published by the Free Software Foundation, either version 3 of
7 ;;; the License, or (at your option) any later version.
8 ;;;
9 ;;; This program is distributed in the hope that it will be useful,
10 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
11 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 ;;; Affero General Public License for more details.
13 ;;;
14 ;;; You should have received a copy of the GNU Affero General Public
15 ;;; License along with this program. If not, see
16 ;;; <http://www.gnu.org/licenses/>.
17
18 (define-module (test-xapian)
19 #:use-module (mumi xapian)
20 #:use-module (mumi test-utils)
21 #:use-module (srfi srfi-19)
22 #:use-module (srfi srfi-64))
23
24 (test-begin "xapian")
25
26 (define (time->datestamp time)
27 (date->string (time-utc->date time) "~Y~m~d"))
28
29 (define sanitize
30 (@@ (mumi xapian) sanitize-date-range))
31
32 (test-equal "sanitize-date-range: turns \"today\" into datestamp"
33 (sanitize "date:today..today")
34 (let ((today (time->datestamp (current-time))))
35 (format #f "date:~a..~a" today today)))
36
37 (test-equal "sanitize-date-range: turns \"now\" into datestamp"
38 (sanitize "date:today..now")
39 (let ((today (time->datestamp (current-time))))
40 (format #f "date:~a..~a" today today)))
41
42 (test-equal "sanitize-date-range: turns \"yesterday\" into datestamp"
43 (sanitize "date:yesterday..today")
44 (let* ((today (current-time))
45 (yesterday (subtract-duration today (make-time time-duration 0 (* 60 60 24)))))
46 (format #f "date:~a..~a"
47 (time->datestamp yesterday)
48 (time->datestamp today))))
49
50 (test-equal "sanitize-date-range: pads short dates"
51 (sanitize "date:20..19")
52 "date:20000000..19000000")
53
54 (test-equal "sanitize-date-range: turns 1m into datestamp one month ago"
55 (sanitize "date:1m..now")
56 (let* ((today (current-time))
57 (1m (subtract-duration today (make-time time-duration 0 (* 60 60 24 30)))))
58 (format #f "date:~a..~a"
59 (time->datestamp 1m)
60 (time->datestamp today))))
61
62 (define tokenize
63 (@@ (mumi xapian) tokenize))
64
65 (test-equal "tokenize: keeps phrases intact 1"
66 (tokenize "subject:\"hello world\" how are you")
67 '("subject:\"hello world\"" "how" "are" "you"))
68
69 (test-equal "tokenize: keeps phrases intact 2"
70 (tokenize "subject:\"hello world\" how \"are\" you")
71 '("subject:\"hello world\"" "how" "\"are\"" "you"))
72
73 (test-end "xapian")