-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy patht_Hstore_test.go
182 lines (159 loc) · 4.67 KB
/
t_Hstore_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
package hstore
import (
"fmt"
"strings"
"testing"
"time"
)
func TestHstore(t *testing.T) {
var hs Hstore
// log.Printf("%+v %+v %+v, %+v", hs, hs.Hstore, hs.Len(), hs.Hstore == nil)
if !hs.IsEmpty() || hs.Hstore != nil {
t.Fatalf("Hstore must be empty")
}
hs.InitIfEmpty()
if !hs.IsEmpty() || hs.Hstore == nil {
t.Fatalf("Hstore still should be empty but initialized")
}
hs.Set("aaa", 111)
if hs.IsEmpty() {
t.Fatalf("Hstore should not be empty now")
}
}
//
// func TestHstoreDB(t *testing.T) {
// db, mock := NewMockDatabase()
// DB = db
// DB.LogMode(true)
//
// hs := NewHstore()
// hs.Set("hkXXey", "hvaXXl")
//
// // mock.ExpectBegin()
// rows := sqlmock.NewRows([]string{"hs"}).AddRow(hs) // new ID=1
// mock.ExpectQuery(regexp.QuoteMeta(`SELECT ? as hs`)).
// WithArgs(`"hkey"=>"hval"`).
// WillReturnRows(rows)
// // mock.ExpectCommit()
//
// DB.Exec(`SELECT 1 as 'a', ? as hs`, hs)
//
// }
func TestHstoreValues(t *testing.T) {
timetest := time.Now()
hs := NewHstore()
hs.Set("aaa", "111")
hs.SetInt("bbb", 222)
hs.SetFloat("ccc1", 0.345, 1)
hs.SetFloat("ccc2", 0.345, 2)
hs.SetFloat("ccc3", 0.345, 3)
hs.SetFloat("ccc4", 12.0, 4)
hs.SetFloat("ccc5", 0.0, 5)
hs.SetFloat("ccc6", 100, 6)
hs.SetFloat("ccc7", 100.00, 7)
hs.SetInt("k", 123456)
hs.SetInt("m", 12345678)
hs.Set("mydate", timetest)
hs.Set("nilval", nil)
// Field counts must appear in hstore
cnt := 13
if hs.Len() != cnt {
t.Fatalf("Must be stored %d items. Found: %d", cnt, hs.Len())
}
if d := hs.GetInt("aaa"); d != 111 {
t.Fatalf("Integer `111` must be found. Found: %d", d)
}
if s := hs.Get("ccc1"); s != "0.3" {
t.Fatalf("This must saved with 1 decimal number. Found: %s", s)
}
if s := hs.Get("ccc2"); s != "0.34" {
t.Fatalf("This must saved with 2 decimal number. Found: %s", s)
}
if s := hs.Get("ccc3"); s != "0.345" {
t.Fatalf("This must saved with 2 decimal number. Found: %s", s)
}
if s := hs.Get("ccc4"); s != "12" {
t.Fatalf("Zeroes must be removed. Found: %s", s)
}
if s := hs.Get("ccc5"); s != "0" {
t.Fatalf("Zeroes must be removed. Found: %s", s)
}
if s := hs.Get("ccc6"); s != "100" {
t.Fatalf("Not all zeroes must be removed. Found: %s", s)
}
if s := hs.Get("ccc7"); s != "100" {
t.Fatalf("Not all zeroes must be removed. Found: %s", s)
}
if mytime := hs.GetTime("mydate"); mytime.UnixNano() != timetest.UnixNano() {
t.Fatalf("Time should be [%v]. Found:[%v]", timetest, mytime)
}
if f := hs.GetFloat("xxx"); f != 0.0 {
t.Fatalf("Non-existent key must return 0.0. Found: %v", f)
}
if f := hs.GetFloat("mydate"); f != 0.0 {
t.Fatalf("Non-existent key must return 0.0. Found: %v", f)
}
if mytime := hs.GetTime("xxx"); !mytime.IsZero() {
t.Fatalf("Non-existent key must return zero time. Found: %v", mytime)
}
// Deleting simple
hs.Delete("aaa")
if s := hs.Get("aaa"); s != "" {
t.Fatalf("Item `aaa` should be deleted. Found[%s]", s)
}
if hs.Len() != cnt-1 {
t.Fatalf("After delete there should be %d items. Found: %d", cnt-1, hs.Len())
}
// Deleting regex
hs.DeleteByRegex("^ccc.+")
if s := hs.Get("ccc1"); s != "" {
t.Fatalf("Item `ccc1` should be deleted. Found[%s]", s)
}
if hs.Len() != cnt-8 {
t.Fatalf("After delete there should be %d items. Found: %d", cnt-8, hs.Len())
}
// Cache
hs.saveToCache("test.cache", 102)
vcache := hs.loadFromCache("test.cache")
if vcache == nil {
t.Fatalf("There should be cached value 102. Found: %v", vcache)
}
if fmt.Sprintf("%T", vcache) != "int" {
t.Fatalf("Cached value type should be int. Found: %T", vcache)
}
if vcache.(int) != 102 {
t.Fatalf("Cached value should be 102. Found: %v", vcache)
}
vcache = hs.loadFromCache("no.cache.key")
if vcache != nil {
t.Fatalf("Not cached value should be empty. Found: %v", vcache)
}
// Append
hs.Append("words", "apple", ",")
if s := hs.Get("words"); s != "apple" {
t.Fatalf("Appended value should be `apple`. Found: %s", s)
}
hs.Append("words", "banana", ",")
if s := hs.Get("words"); s != "apple,banana" {
t.Fatalf("Appended value should be `apple,banana`. Found: %s", s)
}
hs.Append("words", "lemon", ",")
if s := hs.Get("words"); s != "apple,banana,lemon" {
t.Fatalf("Appended value should be `apple,banana,lemon`. Found: %s", s)
}
// GetSlice
if arr := hs.GetAsSlice("words", ","); strings.Join(arr, "-") != "apple-banana-lemon" {
t.Fatalf("Slice value should be `apple banana lemon`. Found: %v", arr)
}
if arr := hs.GetAsSlice("no-key", ";"); len(arr) != 0 {
t.Fatalf("No key slice should be empty. Found: %v", arr)
}
// nil
v, exists := hs.Hstore["nilval"]
if !exists {
t.Fatalf("`nilval` should exist. Not found")
}
if *v != "" {
t.Fatalf("`nilval` should be empty string. Found: %v, %T", *v, *v)
}
}