This repository has been archived by the owner on Oct 15, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main_test.go
150 lines (119 loc) · 3.9 KB
/
main_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
package gortex_test
import (
"fmt"
"github.com/jnfeinstein/gorm"
"github.com/jnfeinstein/gortex"
_ "github.com/lib/pq"
"os"
"testing"
)
type Note struct {
Id int64
Contents string
Author string
}
var DB gorm.DB
var foxNote Note = Note{Contents: "The quick brown fox jumped over the green dog.", Author: "Luke"}
var grassNote Note = Note{Contents: "The grass is always green on the other side.", Author: "Lando"}
func init() {
var err error
pgArgs := os.Getenv("POSTGRES_URL")
if len(pgArgs) == 0 {
pgArgs = "dbname=gortex sslmode=disable"
}
DB, err = gorm.Open("postgres", pgArgs)
DB.LogMode(false)
if err != nil {
panic(fmt.Sprintf("No error should happen when connect database, but got %+v", err))
}
DB.DB().SetMaxIdleConns(10)
setupDB()
}
func setupDB() {
DB.DropTableIfExists(Note{})
DB.AutoMigrate(Note{})
gortex.AutoIndex(&DB, "english", Note{})
DB.Delete(Note{})
DB.Save(&foxNote).Save(&grassNote)
}
func TestSingle(t *testing.T) {
searchScope := gortex.NewSearchScope(Note{Contents: "brown"})
var notes []Note
if err := DB.Scopes(searchScope).Select("*").Find(¬es).Error; err != nil {
t.Errorf("Encountered DB error - %s", err.Error())
} else if len(notes) != 1 {
t.Errorf("Should have found one note")
}
}
func BenchmarkSingle(b *testing.B) {
searchScope := gortex.NewSearchScope(Note{Contents: "brown"})
var notes []Note
for i := 0; i < b.N; i++ {
DB.Scopes(searchScope).Select("*").Find(¬es)
}
}
func TestMultiple(t *testing.T) {
searchScope := gortex.NewSearchScope(Note{Contents: "green"})
var notes []Note
if err := DB.Scopes(searchScope).Select("*").Find(¬es).Error; err != nil {
t.Errorf("Encountered DB error - %s", err.Error())
} else if len(notes) != 2 {
t.Errorf("Should have found two notes")
}
}
func TestExclusive(t *testing.T) {
searchScope := gortex.NewSearchScope(Note{Contents: "green", Author: "Luke"},
map[string]interface{}{"exclusive": true})
var notes []Note
if err := DB.Scopes(searchScope).Select("*").Find(¬es).Error; err != nil {
t.Errorf("Encountered DB error - %s", err.Error())
} else if len(notes) != 1 {
t.Errorf("Should have found one note")
}
}
func TestNotExclusive(t *testing.T) {
searchScope := gortex.NewSearchScope(Note{Contents: "green", Author: "Luke"},
map[string]interface{}{"exclusive": false})
var notes []Note
if err := DB.Scopes(searchScope).Select("*").Find(¬es).Error; err != nil {
t.Errorf("Encountered DB error - %s", err.Error())
} else if len(notes) != 2 {
t.Errorf("Should have found two notes")
}
}
func TestAdvancedSearch(t *testing.T) {
searchScope := gortex.NewSearchScope(Note{Contents: "!brown"})
var notes []Note
if err := DB.Scopes(searchScope).Select("*").Find(¬es).Error; err != nil {
t.Errorf("Encountered DB error - %s", err.Error())
} else if len(notes) != 1 {
t.Errorf("Should have found one note")
}
}
func TestFuzzy(t *testing.T) {
gortex.InitFuzzySearch(&DB)
gortex.SetFuzzySearchLimit(&DB, 0.05)
searchScope := gortex.NewFuzzySearchScope(Note{Contents: "gre"}, map[string]interface{}{"limit": 0.05})
var notes []Note
if err := DB.Scopes(searchScope).Select("*").Find(¬es).Error; err != nil {
t.Errorf("Encountered DB error - %s", err.Error())
} else if len(notes) != 2 {
t.Errorf("Should have found two notes")
}
}
type customSearchFormat struct{}
func (c customSearchFormat) Rank(field string, opts map[string]interface{}) string {
return fmt.Sprintf("LENGTH(%s)", field)
}
func (c customSearchFormat) Condition(field string, opts map[string]interface{}) string {
return fmt.Sprintf("%s LIKE ?", field)
}
func TestCustomSearch(t *testing.T) {
searchScope := gortex.NewCustomSearchScope(customSearchFormat{}, Note{Author: "L%"})
var notes []Note
if err := DB.Scopes(searchScope).Select("*").Find(¬es).Error; err != nil {
t.Errorf("Encountered DB error - %s", err.Error())
} else if len(notes) != 2 {
t.Errorf("Should have found two notes")
}
}