-
Notifications
You must be signed in to change notification settings - Fork 0
/
tools_test.go
79 lines (76 loc) · 1.32 KB
/
tools_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
package cdb
import (
"os"
"testing"
)
func TestMapStringToBytes(t *testing.T) {
data := MapStringToBytes(map[string]string{
"1": "2",
"3": "4",
})
v, err := BytesGetValueString(data, "1")
if err != nil {
panic(err)
}
if v != "2" {
panic(v)
}
v, err = BytesGetValueString(data, "3")
if err != nil {
panic(err)
}
if v != "4" {
panic(v)
}
_, err = BytesGetValueString(data, "5")
if err != ErrNoData {
panic(err)
}
}
func TestFileGetValueString(t *testing.T) {
_, err := FileGetValueString("testdata/no_file.cdb", "key")
if err != ErrNoData {
panic(err)
}
data := MapStringToBytes(map[string]string{
"key": "value",
})
const fName = "testdata/tmp1.cdb"
err = os.WriteFile(fName, data, 0777)
if err != nil {
panic(err)
}
value, err := FileGetValueString(fName, "key")
if err != nil {
panic(err)
}
if value != "value" {
panic(err)
}
_, err = FileGetValueString(fName, "no_key")
if err != ErrNoData {
panic(err)
}
err = FileRewriteKeyValue(fName, "no_key", "v1")
if err != nil {
panic(err)
}
value, err = FileGetValueString(fName, "key")
if err != nil {
panic(err)
}
if value != "value" {
panic(err)
}
value, err = FileGetValueString(fName, "no_key")
if err != nil {
panic(err)
}
if value != "v1" {
panic(err)
}
err = os.Remove(fName)
if err != nil {
panic(err)
}
}