-
Notifications
You must be signed in to change notification settings - Fork 0
/
storage_test.go
105 lines (85 loc) · 1.91 KB
/
storage_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
package walletutils
import (
"os"
"testing"
"github.com/glifio/glif/v2/util"
)
const testFilename = "test_data.toml"
var defaultMap = map[string]string{
"key1": "",
"key2": "",
}
func TestNewStorage(t *testing.T) {
_, err := util.NewStorage(testFilename, defaultMap, true)
if err != nil {
t.Errorf("NewStorage() error: %v", err)
}
// Cleanup
os.Remove(testFilename)
}
func TestSetNonExistentKey(t *testing.T) {
store, err := util.NewStorage(testFilename, defaultMap, true)
if err != nil {
t.Fatalf("NewStorage() error: %v", err)
}
err = store.Set("key3", "value3")
if err != nil {
t.Errorf("Set() error: %v", err)
t.FailNow()
}
v3, err := store.Get("key3")
if err != nil {
t.Errorf("Get() error: %v", err)
t.FailNow()
}
if v3 != "value3" {
t.Errorf("Get returned the wrong value")
t.FailNow()
}
}
func TestGetSetDelete(t *testing.T) {
store, err := util.NewStorage(testFilename, defaultMap, true)
if err != nil {
t.Fatalf("NewStorage() error: %v", err)
}
// Test Set()
err = store.Set("key1", "value1")
if err != nil {
t.Errorf("Set() error: %v", err)
}
// Test Get()
value, err := store.Get("key1")
if err != nil {
t.Errorf("Get() error: %v", err)
}
if value != "value1" {
t.Errorf("Get() expected 'value1', got '%s'", value)
}
// Test Delete()
err = store.Delete("key1")
if err != nil {
t.Errorf("Delete() error: %v", err)
}
_, err = store.Get("key1")
if err == nil {
t.Errorf("Get() expected error, got nil")
}
// Cleanup
os.Remove(testFilename)
}
func TestNonexistentKey(t *testing.T) {
store, err := util.NewStorage(testFilename, defaultMap, true)
if err != nil {
t.Fatalf("NewStorage() error: %v", err)
}
_, err = store.Get("nonexistent_key")
if err == nil {
t.Errorf("Get() expected error, got nil")
}
err = store.Delete("nonexistent_key")
if err == nil {
t.Errorf("Delete() expected error, got nil")
}
// Cleanup
os.Remove(testFilename)
}