-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_test.go
165 lines (145 loc) · 3.4 KB
/
db_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
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package postdb
import (
"fmt"
"net/http"
"strings"
"testing"
"time"
"github.com/ije/postdb/q"
)
func TestDB(t *testing.T) {
db, err := Open("test.db", 0666, false)
if err != nil {
t.Fatal(err)
}
defer db.Close()
// flush
_, err = db.Delete(q.Owner(42))
if err != nil {
t.Fatal(err)
}
posts, err := db.List(q.Owner(42))
if err != nil {
t.Fatal(err)
}
toBe(t, "posts length", len(posts), 0)
for i := 0; i < 10; i++ {
_, err := db.Put(
q.Alias(fmt.Sprintf("hello-world-%d", i+1)),
q.Status(1),
q.Owner(42),
q.Tags("hello", "world"),
q.KV{
"title": []byte(fmt.Sprintf("Hello World #%d", i+1)),
"date": []byte(time.Now().Format(http.TimeFormat)),
},
)
if err != nil {
t.Fatal(err)
}
}
tmp, err := db.Put(
q.Alias("tmp"),
q.Owner(42),
q.KV{
"title": []byte("Hello World!"),
"date": []byte(time.Now().Format(http.TimeFormat)),
},
)
if err != nil {
t.Fatal(err)
}
posts, err = db.List()
if err != nil {
t.Fatal(err)
}
toBe(t, "posts length", len(posts), 11)
_, err = db.Delete(q.ID(tmp.ID()))
if err != nil {
t.Fatal(err)
}
posts, err = db.List()
if err != nil {
t.Fatal(err)
}
toBe(t, "posts length", len(posts), 10)
postZh, err := db.Put(
q.Alias("hello-world-cn"),
q.Status(1),
q.Owner(42),
q.Tags("hello", "world"),
q.KV{
"title": []byte("Hello World!"),
"date": []byte(time.Now().Format(http.TimeFormat)),
},
)
if err != nil {
t.Fatal(err)
}
posts, err = db.List(q.Tags("world"))
if err != nil {
t.Fatal(err)
}
toBe(t, "posts length", len(posts), 11)
_, err = db.Update(
q.ID(postZh.ID()),
q.Alias("hello-world-zh"),
q.Status(2),
q.Owner(42),
q.Tags("你好", "世界"),
q.KV{
"title": []byte("你好世界!"),
"date": []byte(time.Now().Format(http.TimeFormat + " :)")),
"newkey": []byte("v"),
},
)
if err != nil {
t.Fatal(postZh.ID(), err)
}
_, err = db.Get(q.Alias("hello-world-zh"))
if err != nil {
t.Fatal(err)
}
postZh, err = db.Get(q.ID(postZh.ID()), q.Select("*"))
if err != nil {
t.Fatal(err)
}
toBe(t, "postZh.Alias", postZh.Alias, "hello-world-zh")
toBe(t, "postZh.Status", postZh.Status, uint8(2))
toBe(t, "postZh.Owner", postZh.Owner, uint32(42))
toBe(t, "postZh.Tags", strings.Join(postZh.Tags, " "), "你好 世界")
toBe(t, "postZh.KV.title", string(postZh.KV["title"]), "你好世界!")
toBe(t, "postZh.KV.date", strings.HasSuffix(string(postZh.KV["date"]), ":)"), true)
toBe(t, "postZh.KV.newkey", string(postZh.KV["newkey"]), "v")
posts, err = db.List()
if err != nil {
t.Fatal(err)
}
toBe(t, "posts length", len(posts), 11)
posts, err = db.List(q.Tags("world"), q.Limit(5), q.Order(q.ASC), q.Select("*"))
if err != nil {
t.Fatal(err)
}
toBe(t, "posts length", len(posts), 5)
for _, post := range posts {
t.Logf(`%s(%s) "%s" %s`, post.ID(), post.Alias, string(post.KV["title"]), string(post.KV["date"]))
}
posts, err = db.List(q.Offset(10), q.Limit(5))
if err != nil {
t.Fatal(err)
}
toBe(t, "posts length", len(posts), 1)
posts, err = db.List(q.Tags("世界"), q.Select("*"))
if err != nil {
t.Fatal(err)
}
toBe(t, "posts length", len(posts), 1)
for _, post := range posts {
t.Logf(`%s(%s) "%s" %s`, post.ID(), post.Alias, string(post.KV["title"]), string(post.KV["date"]))
}
}
func toBe(t *testing.T, name string, a interface{}, b interface{}) {
if a != b {
t.Fatalf("the %s should equal to %v, but %v", name, b, a)
}
}