-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcollections-query_test.go
149 lines (107 loc) · 3.3 KB
/
collections-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
package minidb
import (
"reflect"
"strings"
"testing"
)
func TestPush_Collections(t *testing.T) {
filename := "pushcols.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push([]int{1, 2, 3, 4, 5}, "sample")
checkFileContent(filename, `[[1,2,3,4,5],"sample"]`, t)
}
func TestFirstLast_Collections(t *testing.T) {
filename := "firstcols.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push("hello", 1000, false)
if db.First().(string) != "hello" {
t.Fatal("the first element is not equal to `hello`")
}
if db.Last().(bool) != false {
t.Fatal("the last element is not equal to `false`")
}
}
func TestFind_Collections(t *testing.T) {
filename := "findcols.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push("hello", 1000, false, "THIN", "sample")
if db.Find("THIN") != 3 || db.Find("hello") != 0 || db.Find(1000) != 1 {
t.Fatal("wrong index value from find")
}
}
func TestFindAll_Collections(t *testing.T) {
filename := "findall.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push("hello", 1, true, "hello")
if len(db.FindAll("hello")) != 2 {
t.Fatal("wrong return length from db.FindAll")
}
}
func TestMatchString_Collections(t *testing.T) {
filename := "matchstring.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push("hello", 1, true, "hello")
if value, _ := db.MatchString("he"); value != "hello" {
t.Fatal("wrong match")
}
}
func TestMatchStringAll_Collections(t *testing.T) {
filename := "matchstring.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push("hellox", 1, true, "hello_world")
sampleReturn := []string{"hellox", "hello_world"}
if values, _ := db.MatchStringAll("hello"); values[0] != sampleReturn[0] || values[1] != sampleReturn[1] {
t.Fatal("wrong return value from match string all")
}
}
func TestFilter(t *testing.T) {
filename := "filter.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push("hellox", "sample", 1, 100, false)
frString := []string{"hellox", "sample"}
if !reflect.DeepEqual(db.FilterString(), frString) {
t.Fatal("wrong filter string output")
}
frInt := []int{1, 100}
if !reflect.DeepEqual(db.FilterInt(), frInt) {
t.Fatal("wrong filter int output")
}
frBool := []bool{false}
if !reflect.DeepEqual(db.FilterBool(), frBool) {
t.Fatal("wrong filter bool output")
}
}
func TestFilterFunc(t *testing.T) {
filename := "filterfunc.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push("hellox", "sample", 1, 100, false)
db.Push("sample", "another", 100, 9023423489, "he she", false, "hello world")
strValues := db.FilterStringFunc(func(x string) bool {
return strings.Contains(x, "he")
})
if !reflect.DeepEqual(strValues, []string{"hellox", "another", "he she", "hello world"}) {
t.Fatal("wrong returned values from filter func")
}
}
func TestRemove(t *testing.T) {
filename := "removecols.json"
defer cleanFileAfter(filename, t)
db := NewCollections(filename)
db.Push("sample", "another", 100, 9023423489, "sample", false, "sample")
db.RemoveAll("sample")
if len(db.FindAll("sample")) > 0 {
t.Fatal("some items are not removed")
}
db.Remove(100)
if db.Find(100) != -1 {
t.Fatal("item is not removed")
}
}