-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstore_test.go
77 lines (59 loc) · 1.43 KB
/
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
package minidb
import (
"io/ioutil"
"testing"
)
/* start test structs */
type TestStoreReadFunc struct {
Value string `json:"value"`
Bool bool `json:"bool"`
}
type TestSampleMapReadKey struct {
Name string `json:"name"`
Age int `json:"age"`
}
func TestStoreWithDefault(t *testing.T) {
dbname := "storedef"
db := New(dbname)
store := db.StoreWithDefault("def", map[string]interface{}{
"hello": "world",
})
if store.GetString("hello") != "world" {
t.Fatal("expected to have default value but wrong returned")
}
cleanFileAfter(dbname, t)
}
/* end test structs */
func TestSet_Store(t *testing.T) {
defer cleanFileAfter("setstore.json", t)
db := NewStore("setstore.json")
db.Set("hello", "world")
if check, err := ioutil.ReadFile("setstore.json"); err == nil {
if string(check) != `{"hello":"world"}` {
t.Fatal("write is not similar to the output file")
}
} else {
t.Fatal(err)
}
}
func TestRead_Store(t *testing.T) {
filename := ("readstore.json")
defer cleanFileAfter(filename, t)
db := NewStore(filename)
db.Set("value", "hello world")
db.Set("bool", true)
db.Set("user", map[string]interface{}{
"name": "John",
"age": 20,
})
reader := TestStoreReadFunc{}
db.Read(&reader)
user := TestSampleMapReadKey{}
db.ReadKey("user", &user)
if reader.Value != "hello world" {
t.Fatal("wrong value from Read function")
}
if user.Name != "John" {
t.Fatal("wrong value from ReadKey function")
}
}