forked from alash3al/goukv
-
Notifications
You must be signed in to change notification settings - Fork 0
/
providers_test.go
100 lines (86 loc) · 1.82 KB
/
providers_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
package postgres
import (
"testing"
"time"
"github.com/alash3al/goukv"
)
func openDBAndDo(fn func(db goukv.Provider)) error {
dsn, err := goukv.NewDSN("postgres://postgres:@localhost/tst?table=default")
if err != nil {
return err
}
p := Provider{}
db, err := p.Open(dsn)
if err != nil {
return err
}
defer func() {
db.Close()
}()
fn(db)
return nil
}
func TestPutGet(t *testing.T) {
err := openDBAndDo(func(db goukv.Provider) {
entry := goukv.Entry{
Key: []byte("k"),
Value: []byte("v"),
}
err := db.Put(&entry)
if err != nil {
t.Error(err)
}
val, err := db.Get(entry.Key)
if err != nil {
t.Error(err)
}
if string(val) != string(entry.Value) {
t.Errorf("expected (%s), found(%s)", string(entry.Value), string(entry.Value))
}
})
if err != nil {
t.Error(err.Error())
}
}
func TestPutGetInvalidKey(t *testing.T) {
err := openDBAndDo(func(db goukv.Provider) {
entry := goukv.Entry{
Key: []byte("kNotFound"),
Value: []byte("v"),
}
_, err := db.Get(entry.Key)
if err == nil {
t.Error("expected an error!")
return
}
if err != goukv.ErrKeyNotFound {
t.Errorf("expected (%s), found(%s)", goukv.ErrKeyNotFound.Error(), err.Error())
}
})
if err != nil {
t.Error(err.Error())
}
}
func TestTTL(t *testing.T) {
err := openDBAndDo(func(db goukv.Provider) {
entry := goukv.Entry{
Key: []byte("k"),
Value: []byte("v"),
TTL: time.Second * 10,
}
err := db.Put(&entry)
if err != nil {
t.Error(err)
}
expiresAt, err := db.TTL(entry.Key)
if err != nil {
t.Error(err)
}
if !(expiresAt.Before(time.Now().Add(entry.TTL)) || expiresAt.Equal(time.Now().Add(entry.TTL))) {
t.Errorf("expected to be expires <= (%d), found (%d)", time.Now().Add(entry.TTL).Unix(), expiresAt.Unix())
}
})
if err != nil {
t.Error(err.Error())
}
}