-
Notifications
You must be signed in to change notification settings - Fork 0
/
helpers_test.go
244 lines (210 loc) · 5.21 KB
/
helpers_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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
package main
import (
"database/sql"
"flag"
"os"
"path/filepath"
"reflect"
"testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
func init() {
dbPtr = &_testDB
// set default output for flags to &buf
flag.CommandLine.SetOutput(&buf)
// set Logrus output to &buf
log.SetOutput(&buf)
}
func TestGetDefaultDBPath(t *testing.T) {
path, _ := os.Getwd()
msg := getDefaultDBPath()
want := filepath.Join(path, "orunmila.db")
if msg != want {
t.Fatalf(`Error() = %q, want match for %#q, nil`, msg, want)
}
}
func TestIsFileExists(t *testing.T) {
if isFileExists("nonexistant.dbfile") {
t.Fatalf(`Error: isFileExists("nonexistant.dbfile")`)
}
_, err := os.Create("empty.db")
if err != nil {
t.Fatal(err)
}
defer os.Remove("empty.db")
if !isFileExists("empty.db") {
t.Fatal(`Error: isFileExists("empty.db")`)
}
}
func TestTagsToIdsInString(t *testing.T) {
Tags = make(map[string]int64)
wants := ""
if TagsToIdsInString() != wants {
t.Fatal(`Error: TagsToIdsInString not empty`)
}
Tags["test"] = 1
wants = "1"
got := TagsToIdsInString()
if got != wants {
t.Fatalf(`Error: TagsToIdsInString got %s not %s`, got, wants)
}
Tags["test2"] = -1
wants = "1,-1"
got = TagsToIdsInString()
// hash maps have random order
if got != wants && got != "-1,1" {
t.Fatalf(`Error: TagsToIdsInString not %v got %v`, wants, got)
}
}
func TestGetTagId(t *testing.T) {
// requires db access
Tags = make(map[string]int64)
t.Log(`SOFTFAIL: not implemented`)
}
func TestGetWordId(t *testing.T) {
// requires db access
t.Log(`SOFTFAIL: not implemented`)
}
func TestCreateDB(t *testing.T) {
// cleanup any existing files
os.Remove("random.db")
// create database
err := createDB("random.db")
if err != nil {
t.Fatalf(`Error: createDB failed %v`, err)
}
err = createDB("random.db")
if err != nil {
t.Fatalf(`Error: 2nd createDB failed, %v`, err)
}
defer os.Remove("random.db")
}
func TestStringToArray(t *testing.T) {
var wants map[string]int64
wants = map[string]int64{
"a": -1,
"b": -1,
"c": -1,
}
got := stringToArray("a,b,c")
if !reflect.DeepEqual(got, wants) {
t.Fatalf(`Error: stringToArray wants %v got %v`, wants, got)
}
got = stringToArray("a,b")
if reflect.DeepEqual(got, wants) {
t.Fatalf(`Error: stringToArray wants %v got %v`, wants, got)
}
got = stringToArray("")
wants = map[string]int64{}
if !reflect.DeepEqual(got, wants) {
t.Fatalf(`Error: stringToArray wants %v got %v`, wants, got)
}
}
func TestPopulateTagIds(t *testing.T) {
t.Log(`SOFTFAIL: not implemented`)
}
func TestRemoveEmptyTags(t *testing.T) {
var wants map[string]int64
wants = map[string]int64{}
Tags = map[string]int64{
"a": -1,
"b": -1,
"c": -1,
}
removeEmptyTags()
if !reflect.DeepEqual(Tags, wants) {
t.Fatalf(`Error: removeEmptyTags want %v got %v`, wants, Tags)
}
wants = map[string]int64{"b": 1}
Tags = map[string]int64{
"a": -1,
"b": 1,
"c": -1,
}
removeEmptyTags()
if !reflect.DeepEqual(Tags, wants) {
t.Fatalf(`Error: removeEmptyTags want %v got %v`, wants, Tags)
}
}
func TestImportTags(t *testing.T) {
Tags = make(map[string]int64)
Tags = map[string]int64{
"a": -1,
"b": -1,
"c": -1,
}
createDbFileifNotExists("random.db")
defer os.Remove("random.db")
db, err := sql.Open("sqlite3", "random.db")
if err != nil {
t.Fatal(err)
}
defer db.Close()
importTags(db)
for k, v := range Tags {
assert.Greaterf(t, int64(v), int64(-1), `importTags: failed initial insert for key %v has id %v`, k, v)
}
Tags = map[string]int64{
"a": -1,
"b": -1,
"c": -1,
}
importTags(db)
for k, v := range Tags {
assert.Greaterf(t, int64(v), int64(-1), `importTags: failed to populate key %v has id %v`, k, v)
}
Tags = map[string]int64{
"a": -1,
"b": -1,
"c": -1,
"d": -1,
}
importTags(db)
for k, v := range Tags {
assert.Greaterf(t, int64(v), int64(-1), `importTags: populate existing and insert last\nkey %v has id %v`, k, v)
}
}
func TestImportFileWords(t *testing.T) {
t.Log(`SOFTFAIL: not implemented`)
}
func TestSearchWordsByTagIds(t *testing.T) {
// populate words
// populate tags
// populate word_tags
var dbname = "random.db"
createDbFileifNotExists(dbname)
defer os.Remove(dbname)
db, err := sql.Open("sqlite3", dbname)
assert.NoError(t, err)
searchWordsByTagIds(db, "a", true)
t.Log(`SOFTFAIL: not implemented`)
}
func TestCreateDbFileifNotExists(t *testing.T) {
var dbname = "random.db"
_, err := os.Stat(dbname)
if err != nil && !os.IsNotExist(err) {
t.Fatal(err)
}
createDbFileifNotExists(dbname)
defer os.Remove(dbname)
_, err = os.Stat(dbname)
if os.IsNotExist(err) {
t.Fatal(err)
}
}
func TestContainsValue(t *testing.T) {
var haystack map[string]int64
assert := assert.New(t)
wants := false
needle := "notexist"
got := containsValue(haystack, needle)
assert.Falsef(got, `containsValue haystack %v, needle %v, wants %v got %v`, haystack, needle, wants, got)
haystack = map[string]int64{"exist": 1}
got = containsValue(haystack, needle)
assert.Falsef(got, `containsValue haystack %v, needle %v, wants %v got %v`, haystack, needle, wants, got)
needle = "exist"
got = containsValue(haystack, needle)
wants = true
assert.Truef(got, `containsValue haystack %v, needle %v, wants %v got %v`, haystack, needle, wants, got)
}