-
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathquery_test.go
180 lines (154 loc) · 3.63 KB
/
query_test.go
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
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
package notmuch
// Copyright © 2015 The go.notmuch Authors. Authors can be found in the AUTHORS file.
// Licensed under the GPLv3 or later.
// See COPYING at the root of the repository for details.
import (
"runtime"
"testing"
)
func TestSearchThreads(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
threads, err := db.NewQuery("").Threads()
if err != nil {
t.Fatalf("error getting the threads: %s", err)
}
var count int
thread := &Thread{}
for threads.Next(&thread) {
count++
// invoke the GC to make sure it's running smoothly.
if count%2 == 0 {
runtime.GC()
}
}
if want, got := 24, count; want != got {
t.Errorf("db.NewQuery(%q).Threads(): want %d got %d", "", want, got)
}
}
func TestSearchMessages(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
msgs, err := db.NewQuery("").Messages()
if err != nil {
t.Fatalf("error getting the threads: %s", err)
}
var count int
msg := &Message{}
for msgs.Next(&msg) {
count++
// invoke the GC to make sure it's running smoothly.
if count%2 == 0 {
runtime.GC()
}
}
if want, got := 52, count; want != got {
t.Errorf("db.NewQuery(%q).Messages(): want %d got %d", "", want, got)
}
}
func TestGetNoResult(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
threads, err := db.NewQuery("subject:notfoundnotfound").Threads()
if err != nil {
t.Fatalf("error getting the threads: %s", err)
}
var count int
thread := &Thread{}
for threads.Next(&thread) {
count++
// invoke the GC to make sure it's running smoothly.
if count%2 == 0 {
runtime.GC()
}
}
if want, got := 0, count; want != got {
t.Errorf("db.NewQuery(%q).Threads(): want %d got %d", "", want, got)
}
}
func TestQueryCountMessages(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
q := db.NewQuery("subject:\"Introducing myself\"")
if want, got := 3, q.CountMessages(); want != got {
t.Errorf("q.Count(): want %d got %d", want, got)
}
}
func TestQueryCountThreads(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
q := db.NewQuery("subject:\"Introducing myself\"")
if want, got := 1, q.CountThreads(); want != got {
t.Errorf("q.Count(): want %d got %d", want, got)
}
}
func TestSetSortScheme(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
q := db.NewQuery("subject:\"Introducing myself\"")
for _, scheme := range []SortMode{
SORT_OLDEST_FIRST,
SORT_NEWEST_FIRST,
SORT_MESSAGE_ID,
SORT_UNSORTED,
} {
q.SetSortScheme(scheme)
}
}
func TestString(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
q := db.NewQuery("subject:\"Introducing myself\"")
if want, got := "subject:\"Introducing myself\"", q.String(); want != got {
t.Errorf("q.String(): want %s got %s", want, got)
}
}
func TestSetExcludeScheme(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
q := db.NewQuery("subject:\"Introducing myself\"")
for _, mode := range []ExcludeMode{
EXCLUDE_FLAG,
EXCLUDE_ALL,
EXCLUDE_TRUE,
EXCLUDE_FALSE,
} {
q.SetExcludeScheme(mode)
}
}
func TestAddTagExclude(t *testing.T) {
db, err := Open(dbPath, DBReadOnly)
if err != nil {
t.Fatal(err)
}
defer db.Close()
q := db.NewQuery("subject:\"Introducing myself\"")
err = q.AddTagExclude("spam")
if err != nil {
t.Errorf("q.AddTagExclude(\"spam\"): unexpected error: %v", err)
}
}