-
Notifications
You must be signed in to change notification settings - Fork 0
/
searchSubcmd_test.go
97 lines (77 loc) · 2.66 KB
/
searchSubcmd_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
package main
import (
"bytes"
"database/sql"
"flag"
"fmt"
"os"
"testing"
log "github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
)
var (
_testDB = "TestSearchSubcmd.db"
buf bytes.Buffer
)
func init() {
dbPtr = &_testDB
// set default output for flags to &buf
flag.CommandLine.SetOutput(&buf)
// set Logrus output to &buf
log.SetOutput(&buf)
}
// Test undefined argument
func TestSearchSubcmdUndefinedArgument(t *testing.T) {
args := []string{"-undefined"}
err := searchSubcmd(args)
assert.EqualError(t, err, `flag provided but not defined: -undefined`)
}
// test help argument
func TestSearchSubcmdHelp(t *testing.T) {
args := []string{"-help"}
err := searchSubcmd(args)
assert.EqualError(t, err, `flag: help requested`)
}
// test tags
func TestSearchSubcmdTags(t *testing.T) {
createDbFileifNotExists(*dbPtr)
defer os.Remove(*dbPtr)
args := []string{"-tags", "a,b,c"}
err := searchSubcmd(args)
assert.NoError(t, err, `Failed to perform search with tags %v`, args)
}
func TestSearchSubcmdEmptyTags(t *testing.T) {
args := []string{"-tags", ""}
createDbFileifNotExists(*dbPtr)
defer os.Remove(*dbPtr)
err := searchSubcmd(args)
assert.NoError(t, err, `Failed to perform search with tags %v`, args)
}
func TestSearchSubcmdExistingTags(t *testing.T) {
createDbFileifNotExists(*dbPtr)
defer os.Remove(*dbPtr)
Nrecords := int64(0)
args := []string{"-tags", "a,b,c", "word1", "word2"}
// add the tags and words first
addSubcmd(args)
//search for the same arguments except the last two (word1,word2)
err := searchSubcmd(args[:2])
assert.NoError(t, err, `Failed to perform search with tags %v`, args)
//Open the database and ensure no error
dsn := fmt.Sprintf("file:%s?mode=ro", *dbPtr)
db, err := sql.Open("sqlite3", dsn)
assert.NoError(t, err, `Failed to open database %v`, dsn)
defer db.Close()
//Query and check for no error and correct number of tags where added
err = db.QueryRow("select count(*) from tags").Scan(&Nrecords)
assert.NoError(t, err, `Failed to select count(*) from tags`)
assert.Equal(t, int64(3), Nrecords, `Number of tag records returned did not match`)
//Query and check for no error and correct number of words where added
err = db.QueryRow("select count(*) from words").Scan(&Nrecords)
assert.NoError(t, err, `Failed to select count(*) from words`)
assert.Equal(t, int64(2), Nrecords, `Number of words records returned did not match`)
//Query and check for no error and correct number of wt where added
err = db.QueryRow("select count(*) from wt").Scan(&Nrecords)
assert.NoError(t, err, `Failed to select count(*) from wt`)
assert.Equal(t, int64(2*3), Nrecords, `Number of words records returned did not match`)
}