-
Notifications
You must be signed in to change notification settings - Fork 0
/
jsondb_test.go
105 lines (87 loc) · 1.88 KB
/
jsondb_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 jsondb
import (
"os"
"testing"
)
const jsonPath = "test.json"
func TestSet(t *testing.T) {
defer os.Remove(jsonPath)
db, err := New(jsonPath)
if err != nil {
t.Errorf("New() failed: %v", err)
}
// Test Set() method
if err := db.Set("key1", "value1"); err != nil {
t.Errorf("Set() failed: %v", err)
}
if err := db.Set("key2.subkey", 123); err != nil {
t.Errorf("Set() failed: %v", err)
}
// Test Get() method
val, err := db.Get("key1")
if err != nil {
t.Errorf("Get() failed: %v", err)
}
if val != "value1" {
t.Errorf("Get() returned %v, want %v", val, "value1")
}
val, err = db.Get("key2.subkey")
if err != nil {
t.Errorf("Get() failed: %v", err)
}
if val != 123 {
t.Errorf("Get() returned %v, want %v", val, 123)
}
}
func TestAdd(t *testing.T) {
defer os.Remove(jsonPath)
db, err := New(jsonPath)
if err != nil {
t.Errorf("New() failed: %v", err)
}
// Test Add() method
db.Add("key4", 1)
val, _ := db.Get("key4")
if val != 1 {
t.Errorf("Add() failed: %v", val)
}
db.Add("key4", 2)
val, _ = db.Get("key4")
if val != 3 {
t.Errorf("Add() failed: %v", val)
}
// Test Sub() method
db.Sub("key4", 2)
val, _ = db.Get("key4")
if val != 1 {
t.Errorf("Sub() failed: %v", val)
}
db.Sub("key4", 1)
val, _ = db.Get("key4")
if val != 0 {
t.Errorf("Sub() failed: %v", val)
}
}
func TestPush(t *testing.T) {
defer os.Remove(jsonPath)
db, err := New(jsonPath)
if err != nil {
t.Errorf("New() failed: %v", err)
}
// Test Push() method
if err := db.Push("key3", "value1"); err != nil {
t.Errorf("Push() failed: %v", err)
}
if err := db.Push("key3", "value2"); err != nil {
t.Errorf("Push() failed: %v", err)
}
if !db.Has("key3") {
t.Errorf("Has() failed: key3 not found")
}
if err := db.Delete("key3"); err != nil {
t.Errorf("Delete() failed: %v", err)
}
if db.Has("key3") {
t.Errorf("Delete() failed: key3 still exists")
}
}