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"
|
"strings"
|
||||||
)
|
)
|
||||||
|
|
||||||
var (
|
func ExecuteCommands(db DB, commands string, lastPathPtr *string, lastKeyPtr *string) error {
|
||||||
lastPath string
|
|
||||||
lastKey string
|
|
||||||
)
|
|
||||||
|
|
||||||
func ExecuteCommands(db DB, commands string, lastPathIn string, lastKeyIn string) (error, string, string) {
|
|
||||||
lastPath = lastPathIn
|
|
||||||
lastKey = lastKeyIn
|
|
||||||
for _, command := range strings.Split(commands, ";") {
|
for _, command := range strings.Split(commands, ";") {
|
||||||
|
lastPath := *lastPathPtr
|
||||||
|
lastKey := *lastKeyPtr
|
||||||
command = strings.Trim(command, " ")
|
command = strings.Trim(command, " ")
|
||||||
parts := strings.Split(command, " ")
|
parts := strings.Split(command, " ")
|
||||||
if len(parts) > 1 {
|
if len(parts) > 1 {
|
||||||
|
@ -40,19 +35,23 @@ func ExecuteCommands(db DB, commands string, lastPathIn string, lastKeyIn string
|
||||||
lastPath = parts[1]
|
lastPath = parts[1]
|
||||||
}
|
}
|
||||||
|
|
||||||
err := ExecuteCommand(db, parts)
|
err, gotKey := ExecuteCommand(db, parts)
|
||||||
if err != nil {
|
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]) {
|
switch strings.ToUpper(parts[0]) {
|
||||||
case "KEYS":
|
case "KEYS", "\\K":
|
||||||
case "\\K":
|
|
||||||
kpath := "."
|
kpath := "."
|
||||||
if !(len(parts) == 1 || len(parts[1]) == 0) {
|
if !(len(parts) == 1 || len(parts[1]) == 0) {
|
||||||
kpath = parts[1]
|
kpath = parts[1]
|
||||||
|
@ -62,10 +61,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
fmt.Println(db.Keys(kpath))
|
fmt.Println(db.Keys(kpath))
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
|
|
||||||
case "INFO":
|
case "INFO", "\\?":
|
||||||
case "\\?":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
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])
|
node, ok := db.GetNode(parts[1])
|
||||||
|
@ -90,8 +88,7 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "TREE":
|
case "TREE", "\\T":
|
||||||
case "\\T":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 || parts[1] == "." {
|
if len(parts) == 1 || len(parts[1]) == 0 || parts[1] == "." {
|
||||||
fmt.Printf("TREE\n-----------------\n\\\n")
|
fmt.Printf("TREE\n-----------------\n\\\n")
|
||||||
printNodeTree(&db, 1)
|
printNodeTree(&db, 1)
|
||||||
|
@ -101,7 +98,7 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
|
|
||||||
node, ok := db.GetNode(parts[1])
|
node, ok := db.GetNode(parts[1])
|
||||||
if !ok {
|
if !ok {
|
||||||
return fmt.Errorf("err format: TREE [path]\n")
|
return fmt.Errorf("err format: TREE [path]\n"), newId
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("{%s} TREE\n-----------------\n", parts[1])
|
fmt.Printf("{%s} TREE\n-----------------\n", parts[1])
|
||||||
if !node.List && !node.Object {
|
if !node.List && !node.Object {
|
||||||
|
@ -115,10 +112,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
|
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
|
|
||||||
case "GET":
|
case "GET", "\\G":
|
||||||
case "\\G":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
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])
|
value, ok := db.Get(parts[1])
|
||||||
|
@ -131,10 +127,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "INC":
|
case "INC", "\\I":
|
||||||
case "\\I":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
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])
|
ok := db.Inc(parts[1])
|
||||||
|
@ -146,10 +141,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "DEC":
|
case "DEC", "\\D":
|
||||||
case "\\D":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
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])
|
ok := db.Dec(parts[1])
|
||||||
|
@ -161,10 +155,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "NOW":
|
case "NOW", "\\N":
|
||||||
case "\\N":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
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])
|
ok := db.Now(parts[1])
|
||||||
|
@ -176,10 +169,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "DEL":
|
case "DEL", "\\R":
|
||||||
case "\\R":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
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])
|
ok := db.Del(parts[1])
|
||||||
|
@ -191,10 +183,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "SAVE":
|
case "SAVE", "\\S":
|
||||||
case "\\S":
|
|
||||||
if len(parts) < 3 || len(parts[1]) == 0 {
|
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:], " "), "\"")
|
value := strings.Trim(strings.Join(parts[2:], " "), "\"")
|
||||||
|
@ -209,10 +200,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
}
|
}
|
||||||
db.Refresh()
|
db.Refresh()
|
||||||
|
|
||||||
case "CREATE":
|
case "CREATE", "\\C":
|
||||||
case "\\C":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
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])
|
ok := db.CreateNode(parts[1])
|
||||||
|
@ -224,10 +214,9 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
}
|
}
|
||||||
|
|
||||||
case "ADD":
|
case "ADD", "\\A":
|
||||||
case "\\A":
|
|
||||||
if len(parts) == 1 || len(parts[1]) == 0 {
|
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])
|
id, err := db.AddObject(parts[1])
|
||||||
|
@ -237,14 +226,14 @@ func ExecuteCommand(db DB, parts []string) error {
|
||||||
} else {
|
} else {
|
||||||
fmt.Printf("{%s} ADDED with ID: %s\n", parts[1], id)
|
fmt.Printf("{%s} ADDED with ID: %s\n", parts[1], id)
|
||||||
fmt.Println("")
|
fmt.Println("")
|
||||||
lastKey = id
|
newId = id
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
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) {
|
func printNodeTree(node GetNodes, i int) {
|
||||||
|
|
|
@ -366,7 +366,7 @@ func (db *DB) GetRandomNode(vpath string) (Node, error) {
|
||||||
return node.Nodes[randomKey], nil
|
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)
|
return ExecuteCommands(*db, commands, lastPath, lastKey)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -280,3 +280,25 @@ func TestListRandomNode(t *testing.T) {
|
||||||
t.Fatal("Random node value wrong")
|
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