-
Notifications
You must be signed in to change notification settings - Fork 1
/
chatdb_test.go
58 lines (46 loc) · 1.12 KB
/
chatdb_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
package main
import (
"io/ioutil"
"os"
"path/filepath"
"testing"
"github.com/magiconair/properties/assert"
)
func TestInitChatDB(t *testing.T) {
tmp := createTempDir(t)
defer tmp.cleanup(t)
file := filepath.Join(tmp.RootDir, "chat.yaml")
_, err := InitChatDB(file)
if err != nil {
t.Errorf("InitChatDB(): %s", err)
}
_, err = os.Stat(file)
if err != nil {
t.Errorf("InitChatDB(): chatdb not created (error = %s)", err)
}
}
func TestUpdateWith(t *testing.T) {
tmp := createTempDir(t)
defer tmp.cleanup(t)
file := filepath.Join(tmp.RootDir, "chat.yaml")
chatdb, err := InitChatDB(file)
if err != nil {
t.Errorf("InitChatDB(): %s", err)
}
err = chatdb.UpdateWith("john", 123456)
if err != nil {
t.Errorf("UpdateWith(): %s", err)
}
if _, ok := chatdb.Db["john"]; !ok {
t.Errorf("UpdateWith(): john is missing")
}
_, err = os.Stat(file + ".bak")
if err != nil {
t.Errorf("InitChatDB(): chatdb backup not created (error = %s)", err)
}
content, err := ioutil.ReadFile(file)
if err != nil {
t.Errorf("ioutil.ReadFile: %s", err)
}
assert.Equal(t, string(content), "john: 123456\n", "chatdb content")
}