-
Notifications
You must be signed in to change notification settings - Fork 0
/
nopog_test.go
162 lines (149 loc) · 4.5 KB
/
nopog_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
package nopog
import (
"testing"
"github.com/stretchr/testify/require"
)
var testObject string = `{"ob":"test ✔⚓🛸🛴"}`
var testKey string = "test/"
var testServerIP string = "localhost"
var testServerDatabase string = "postgres"
var testServerUser = "postgres"
var testServerPassword = "postgres"
func TestKeys(t *testing.T) {
storage := &Storage{
Name: testServerDatabase,
User: testServerUser,
Host: testServerIP,
Password: testServerPassword,
}
storage.Start()
defer storage.Close()
storage.Clear()
keys, err := storage.Keys()
require.NoError(t, err)
require.Equal(t, []string{}, keys)
_, err = storage.Set(testKey+"1", testObject)
require.NoError(t, err)
_, err = storage.Set("notValidKey//", testObject)
require.Error(t, err)
_, err = storage.Set("notValidKey/**", testObject)
require.Error(t, err)
_, err = storage.Set("notValidKey/*/", testObject)
require.Error(t, err)
_, err = storage.Set(testKey+"1", `not json`)
require.Error(t, err)
keys, err = storage.Keys()
require.NoError(t, err)
require.Equal(t, []string{testKey + "1"}, keys)
}
func TestSetAndGet(t *testing.T) {
storage := &Storage{
Name: testServerDatabase,
User: testServerUser,
Host: testServerIP,
Password: testServerPassword,
}
storage.Start()
defer storage.Close()
storage.Clear()
key := testKey + "1"
_, err := storage.Set(key, testObject)
require.NoError(t, err)
_, err = storage.Set(key, `not json`)
require.Error(t, err)
dataList, err := storage.Get(key)
require.NoError(t, err)
require.Equal(t, 1, len(dataList))
data := dataList[0]
require.NoError(t, err)
require.Equal(t, key, data.Key)
require.Equal(t, testObject, string(data.Value))
}
func TestGetPath(t *testing.T) {
storage := &Storage{
Name: testServerDatabase,
User: testServerUser,
Host: testServerIP,
Password: testServerPassword,
}
storage.Start()
defer storage.Close()
storage.Clear()
_, err := storage.Set(testKey+"1", testObject)
require.NoError(t, err)
_, err = storage.Set(testKey+"2", testObject)
require.NoError(t, err)
dataList, err := storage.Get(testKey + "*")
require.NoError(t, err)
require.Equal(t, 2, len(dataList))
data := dataList[0]
require.NoError(t, err)
require.Equal(t, testKey+"2", data.Key)
require.Equal(t, testObject, string(data.Value))
}
func TestGetN(t *testing.T) {
storage := &Storage{
Name: testServerDatabase,
User: testServerUser,
Host: testServerIP,
Password: testServerPassword,
}
storage.Start()
defer storage.Close()
storage.Clear()
_, err := storage.Set(testKey+"1", testObject)
require.NoError(t, err)
_, err = storage.Set(testKey+"2", testObject)
require.NoError(t, err)
dataList, err := storage.GetN(testKey+"*", 1)
require.NoError(t, err)
require.Equal(t, 1, len(dataList))
data := dataList[0]
require.NoError(t, err)
require.Equal(t, testKey+"2", data.Key)
require.Equal(t, testObject, string(data.Value))
}
func TestRange(t *testing.T) {
storage := &Storage{
Name: testServerDatabase,
User: testServerUser,
Host: testServerIP,
Password: testServerPassword,
}
storage.Start()
defer storage.Close()
storage.Clear()
_, err := storage.Set(testKey+"1", testObject)
require.NoError(t, err)
secondOpTime, err := storage.Set(testKey+"2", testObject)
require.NoError(t, err)
dataList, err := storage.GetNRange(testKey+"*", secondOpTime, secondOpTime, 2)
require.NoError(t, err)
require.Equal(t, 1, len(dataList))
data := dataList[0]
require.NoError(t, err)
require.Equal(t, testKey+"2", data.Key)
require.Equal(t, testObject, string(data.Value))
keys, err := storage.KeysRange(testKey+"*", secondOpTime, secondOpTime, 2)
require.NoError(t, err)
require.Equal(t, []string{testKey + "2"}, keys)
dataList, err = storage.GetRange(testKey+"*", secondOpTime, secondOpTime)
require.NoError(t, err)
require.Equal(t, 1, len(dataList))
data = dataList[0]
require.Equal(t, testKey+"2", data.Key)
require.Equal(t, testObject, string(data.Value))
keys, err = storage.KeysRange(testKey+"*", secondOpTime, secondOpTime, 2)
require.NoError(t, err)
require.Equal(t, []string{testKey + "2"}, keys)
dataList, err = storage.GetUpdatedRange(testKey+"*", secondOpTime, secondOpTime)
require.NoError(t, err)
require.Equal(t, 0, len(dataList))
thirdOpTime, err := storage.Set(testKey+"1", testObject)
require.NoError(t, err)
dataList, err = storage.GetUpdatedRange(testKey+"*", secondOpTime, thirdOpTime)
require.NoError(t, err)
require.Equal(t, 1, len(dataList))
data = dataList[0]
require.Equal(t, testKey+"1", data.Key)
}