forked from avinassh/go-caskdb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
disk_store_test.go
104 lines (96 loc) · 2.46 KB
/
disk_store_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
package caskdb
import (
"os"
"testing"
)
func TestDiskStore_Get(t *testing.T) {
store, err := NewDiskStore("test.db")
if err != nil {
t.Fatalf("failed to create disk store: %v", err)
}
defer os.Remove("test.db")
store.Set("name", "jojo")
if val := store.Get("name"); val != "jojo" {
t.Errorf("Get() = %v, want %v", val, "jojo")
}
}
func TestDiskStore_GetInvalid(t *testing.T) {
store, err := NewDiskStore("test.db")
if err != nil {
t.Fatalf("failed to create disk store: %v", err)
}
defer os.Remove("test.db")
if val := store.Get("some key"); val != "" {
t.Errorf("Get() = %v, want %v", val, "")
}
}
func TestDiskStore_SetWithPersistence(t *testing.T) {
store, err := NewDiskStore("test.db")
if err != nil {
t.Fatalf("failed to create disk store: %v", err)
}
defer os.Remove("test.db")
tests := map[string]string{
"crime and punishment": "dostoevsky",
"anna karenina": "tolstoy",
"war and peace": "tolstoy",
"hamlet": "shakespeare",
"othello": "shakespeare",
"brave new world": "huxley",
"dune": "frank herbert",
}
for key, val := range tests {
store.Set(key, val)
if store.Get(key) != val {
t.Errorf("Get() = %v, want %v", store.Get(key), val)
}
}
store.Close()
store, err = NewDiskStore("test.db")
if err != nil {
t.Fatalf("failed to create disk store: %v", err)
}
for key, val := range tests {
if store.Get(key) != val {
t.Errorf("Get() = %v, want %v", store.Get(key), val)
}
}
store.Close()
}
func TestDiskStore_Delete(t *testing.T) {
store, err := NewDiskStore("test.db")
if err != nil {
t.Fatalf("failed to create disk store: %v", err)
}
defer os.Remove("test.db")
tests := map[string]string{
"crime and punishment": "dostoevsky",
"anna karenina": "tolstoy",
"war and peace": "tolstoy",
"hamlet": "shakespeare",
"othello": "shakespeare",
"brave new world": "huxley",
"dune": "frank herbert",
}
for key, val := range tests {
store.Set(key, val)
}
for key, _ := range tests {
store.Set(key, "")
}
store.Set("end", "yes")
store.Close()
store, err = NewDiskStore("test.db")
if err != nil {
t.Fatalf("failed to create disk store: %v", err)
}
for key := range tests {
if store.Get(key) != "" {
t.Errorf("Get() = %v, want '' (empty)", store.Get(key))
}
}
if store.Get("end") != "yes" {
t.Errorf("Get() = %v, want %v", store.Get("end"), "yes")
}
store.Close()
}