This repository has been archived by the owner on Jun 15, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathrename_test.go
89 lines (75 loc) · 1.45 KB
/
rename_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
package kvs
import (
"os"
"testing"
"time"
)
func TestKvs_Rename(t *testing.T) {
db, err := open(t.Name(), "", 2*time.Minute)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("db created.")
k, v := "foo", "bar"
db.Set(k, v)
t.Logf("Key-value pair is set.")
t.Logf("%s: %s", k, v)
ok, err := db.Rename(k, k+k)
if err != nil {
t.Errorf(err.Error())
}
if !ok {
t.Errorf("key didn't change")
} else {
t.Logf("key renamed.")
}
val := db.Get(k)
if val != "" {
t.Errorf("%s-%s pair is still exits.", k, val)
}
t.Logf("%s-%s", k, val)
val = db.Get(k + k)
if val != v {
t.Errorf("expected %s-%s pair, but got %s-%s", k+k, v, k+k, val)
}
t.Logf("%s-%s", k+k, val)
err = db.Close()
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("db closed.")
err = os.RemoveAll(db.dir)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("temp db removed.")
}
func TestKvs_RenameExistsNewKey(t *testing.T) {
db, err := open(t.Name(), "", 2*time.Minute)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("db created.")
k, v := "foo", "bar"
db.Set(k, v)
t.Logf("Key-value pair is set.")
t.Logf("%s: %s", k, v)
ok, err := db.Rename(k, k)
if !ok {
t.Logf("key didn't change")
}
val := db.Get(k)
if val != v {
t.Errorf("%s-%s pair is wrong.", k, val)
}
err = db.Close()
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("db closed.")
err = os.RemoveAll(db.dir)
if err != nil {
t.Fatalf(err.Error())
}
t.Logf("temp db removed.")
}