fix tests
This commit is contained in:
parent
77933ceacd
commit
785c07dcf5
3 changed files with 59 additions and 48 deletions
83
command.go
83
command.go
|
@ -13,15 +13,10 @@ import (
|
|||
"strings"
|
||||
)
|
||||
|
||||
var (
|
||||
lastPath string
|
||||
lastKey string
|
||||
)
|
||||
|
||||
func ExecuteCommands(db DB, commands string, lastPathIn string, lastKeyIn string) (error, string, string) {
|
||||
lastPath = lastPathIn
|
||||
lastKey = lastKeyIn
|
||||
func ExecuteCommands(db DB, commands string, lastPathPtr *string, lastKeyPtr *string) error {
|
||||
for _, command := range strings.Split(commands, ";") {
|
||||
lastPath := *lastPathPtr
|
||||
lastKey := *lastKeyPtr
|
||||
command = strings.Trim(command, " ")
|
||||
parts := strings.Split(command, " ")
|
||||
if len(parts) > 1 {
|
||||
|
@ -40,19 +35,23 @@ func ExecuteCommands(db DB, commands string, lastPathIn string, lastKeyIn string
|
|||
lastPath = parts[1]
|
||||
}
|
||||
|
||||
err := ExecuteCommand(db, parts)
|
||||
err, gotKey := ExecuteCommand(db, parts)
|
||||
if err != nil {
|
||||
return err, lastPath, lastKey
|
||||
return err
|
||||
}
|
||||
if gotKey != "" {
|
||||
*lastKeyPtr = gotKey
|
||||
}
|
||||
}
|
||||
|
||||
return nil, lastPath, lastKey
|
||||
return nil
|
||||
}
|
||||
|
||||
func ExecuteCommand(db DB, parts []string) error {
|
||||
func ExecuteCommand(db DB, parts []string) (error, string) {
|
||||
var newId string
|
||||
|
||||
switch strings.ToUpper(parts[0]) {
|
||||
case "KEYS":
|
||||
case "\\K":
|
||||
case "KEYS", "\\K":
|
||||
kpath := "."
|
||||
if !(len(parts) == 1 || len(parts[1]) == 0) {
|
||||
kpath = parts[1]
|
||||
|
@ -62,10 +61,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
fmt.Println(db.Keys(kpath))
|
||||
fmt.Println("")
|
||||
|
||||
case "INFO":
|
||||
case "\\?":
|
||||
case "INFO", "\\?":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: INFO [path]\n")
|
||||
return fmt.Errorf("err format: INFO [path]\n"), newId
|
||||
}
|
||||
|
||||
node, ok := db.GetNode(parts[1])
|
||||
|
@ -90,8 +88,7 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "TREE":
|
||||
case "\\T":
|
||||
case "TREE", "\\T":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 || parts[1] == "." {
|
||||
fmt.Printf("TREE\n-----------------\n\\\n")
|
||||
printNodeTree(&db, 1)
|
||||
|
@ -101,7 +98,7 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
|
||||
node, ok := db.GetNode(parts[1])
|
||||
if !ok {
|
||||
return fmt.Errorf("err format: TREE [path]\n")
|
||||
return fmt.Errorf("err format: TREE [path]\n"), newId
|
||||
} else {
|
||||
fmt.Printf("{%s} TREE\n-----------------\n", parts[1])
|
||||
if !node.List && !node.Object {
|
||||
|
@ -115,10 +112,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
|
||||
fmt.Println("")
|
||||
|
||||
case "GET":
|
||||
case "\\G":
|
||||
case "GET", "\\G":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: GET [path]\n")
|
||||
return fmt.Errorf("err format: GET [path]\n"), newId
|
||||
}
|
||||
|
||||
value, ok := db.Get(parts[1])
|
||||
|
@ -131,10 +127,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "INC":
|
||||
case "\\I":
|
||||
case "INC", "\\I":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: INC [path]\n")
|
||||
return fmt.Errorf("err format: INC [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.Inc(parts[1])
|
||||
|
@ -146,10 +141,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "DEC":
|
||||
case "\\D":
|
||||
case "DEC", "\\D":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: DEC [path]\n")
|
||||
return fmt.Errorf("err format: DEC [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.Dec(parts[1])
|
||||
|
@ -161,10 +155,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "NOW":
|
||||
case "\\N":
|
||||
case "NOW", "\\N":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: NOW [path]\n")
|
||||
return fmt.Errorf("err format: NOW [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.Now(parts[1])
|
||||
|
@ -176,10 +169,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "DEL":
|
||||
case "\\R":
|
||||
case "DEL", "\\R":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: DEL [path]\n")
|
||||
return fmt.Errorf("err format: DEL [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.Del(parts[1])
|
||||
|
@ -191,10 +183,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "SAVE":
|
||||
case "\\S":
|
||||
case "SAVE", "\\S":
|
||||
if len(parts) < 3 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: SAVE [path] [value|long value]\n")
|
||||
return fmt.Errorf("err format: SAVE [path] [value|long value]\n"), newId
|
||||
}
|
||||
|
||||
value := strings.Trim(strings.Join(parts[2:], " "), "\"")
|
||||
|
@ -209,10 +200,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
}
|
||||
db.Refresh()
|
||||
|
||||
case "CREATE":
|
||||
case "\\C":
|
||||
case "CREATE", "\\C":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: CREATE [path]\n")
|
||||
return fmt.Errorf("err format: CREATE [path]\n"), newId
|
||||
}
|
||||
|
||||
ok := db.CreateNode(parts[1])
|
||||
|
@ -224,10 +214,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
fmt.Println("")
|
||||
}
|
||||
|
||||
case "ADD":
|
||||
case "\\A":
|
||||
case "ADD", "\\A":
|
||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
||||
return fmt.Errorf("err format: ADD [path]\n")
|
||||
return fmt.Errorf("err format: ADD [path]\n"), newId
|
||||
}
|
||||
|
||||
id, err := db.AddObject(parts[1])
|
||||
|
@ -237,14 +226,14 @@ func ExecuteCommand(db DB, parts []string) error {
|
|||
} else {
|
||||
fmt.Printf("{%s} ADDED with ID: %s\n", parts[1], id)
|
||||
fmt.Println("")
|
||||
lastKey = id
|
||||
newId = id
|
||||
}
|
||||
|
||||
default:
|
||||
return fmt.Errorf("err[00] unknown command, try HELP")
|
||||
return fmt.Errorf("err[00] unknown command, try HELP"), newId
|
||||
}
|
||||
|
||||
return nil
|
||||
return nil, newId
|
||||
}
|
||||
|
||||
func printNodeTree(node GetNodes, i int) {
|
||||
|
|
|
@ -366,7 +366,7 @@ func (db *DB) GetRandomNode(vpath string) (Node, error) {
|
|||
return node.Nodes[randomKey], nil
|
||||
}
|
||||
|
||||
func (db *DB) ExecuteCommands(commands string, lastPath string, lastKey string) (error, string, string) {
|
||||
func (db *DB) ExecuteCommands(commands string, lastPath *string, lastKey *string) error {
|
||||
return ExecuteCommands(*db, commands, lastPath, lastKey)
|
||||
}
|
||||
|
||||
|
|
|
@ -280,3 +280,25 @@ func TestListRandomNode(t *testing.T) {
|
|||
t.Fatal("Random node value wrong")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExecuteCommands(t *testing.T) {
|
||||
copy(t)
|
||||
|
||||
db, err := InitDB("./testdata2")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
var lastPath string
|
||||
var lastKey string
|
||||
err = db.ExecuteCommands("SAVE newKey2 newValue", &lastPath, &lastKey)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
db.Refresh()
|
||||
val, found := db.Get("newKey2")
|
||||
if !found || val != "newValue" {
|
||||
t.Fatal("newKey2 value get fail")
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue