kvrepl/parse_test.go

148 lines
2.2 KiB
Go

package kvrepl
import "testing"
func TestParse(t *testing.T) {
var tests = []struct {
cmds []string
db *DB
expected string
}{
{
cmds: []string{
"write a b",
"start",
"write a c",
"abort",
"read a",
},
db: &DB{
KV1: map[string]string{},
KV2: map[string]string{},
PKV: true,
},
expected: "b",
},
{
cmds: []string{
"write a b",
"start",
"write a hello-again",
"start",
"delete a",
"commit",
"write a once-more",
"abort",
"read a",
},
db: &DB{
KV1: map[string]string{},
KV2: map[string]string{},
PKV: true,
},
expected: "b",
},
{
cmds: []string{
"write a b",
"start",
"write a hello-again",
"start",
"delete a",
"commit",
"write a once-more",
"start",
"write a foo",
"abort",
"read a",
},
db: &DB{
KV1: map[string]string{},
KV2: map[string]string{},
PKV: true,
},
expected: "b",
},
{
cmds: []string{
"write a b",
"start",
"write a c",
"commit",
"read a",
},
db: &DB{
KV1: map[string]string{},
KV2: map[string]string{},
PKV: true,
},
expected: "c",
},
{
cmds: []string{
"write a b",
"start",
"write a c",
"start",
"delete a",
"write a d",
"commit",
"read a",
},
db: &DB{
KV1: map[string]string{},
KV2: map[string]string{},
PKV: true,
},
expected: "d",
},
{
cmds: []string{
"write a b",
"start",
"write a c",
"start",
"delete a",
"write a d",
"abort",
"read a",
},
db: &DB{
KV1: map[string]string{},
KV2: map[string]string{},
PKV: true,
},
expected: "b",
},
{
cmds: []string{
"write a b",
"start",
"delete a",
"commit",
"read a",
},
db: &DB{
KV1: map[string]string{},
KV2: map[string]string{},
PKV: true,
},
expected: "",
},
}
for _, rt := range tests {
actual := ""
for _, c := range rt.cmds {
actual, _ = Parse(c, rt.db)
}
if actual != rt.expected {
t.Errorf(
"failed parse:\n\texpected: %v\n\t actual: %v",
rt.expected,
actual,
)
}
}
}