Skip to content

Commit

Permalink
refactor fix unit test add new module
Browse files Browse the repository at this point in the history
  • Loading branch information
ssalvatori committed Apr 1, 2019
1 parent ee79b78 commit 86e5ba4
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 60 deletions.
6 changes: 2 additions & 4 deletions commands/commands.go
Expand Up @@ -28,6 +28,8 @@ type Levels struct {
Version int
Ping int
Last int
Rand int
Find int
}

var (
Expand All @@ -37,7 +39,6 @@ var (
// CommandsList list of commandElement
type CommandsList struct {
List *list.List
Db db.ZbotDatabase
}

type commandElement struct {
Expand All @@ -49,8 +50,6 @@ type commandElement struct {
// Chain add a command and the required level to use it to the list of command
func (cmdList *CommandsList) Chain(cmdDefinition string, cmd interface{}, level int) *CommandsList {

cmd.(zbotCommand).SetDB(cmdList.Db)

newCommand := &commandElement{
command: cmd.(zbotCommand),
requiredLevel: level,
Expand Down Expand Up @@ -80,7 +79,6 @@ func (cmdList *CommandsList) Run(cmd string, msg string, user user.User) string

// zbotCommand interface to be implemented by each command
type zbotCommand interface {
SetDB(db db.ZbotDatabase)
ProcessText(text string, username user.User) (string, error)
}

Expand Down
6 changes: 1 addition & 5 deletions commands/commands_test.go
Expand Up @@ -102,17 +102,13 @@ var fake3Command = &FakeCommand2{cmd: "fakecommand3"}

var cmdList = &CommandsList{
List: list.New(),
Db: &db.MockZbotDatabase{
Term: "foo",
Meaning: "bar",
},
}

func TestChainAndRun(t *testing.T) {

cmdList.Chain("fakecommand1", fake1Command, 0)
cmdList.Chain("fakecommand2", fake2Command, 0)
cmdList.Chain("fakecommand3", fake2Command, 0)
cmdList.Chain("fakecommand3", fake3Command, 0)
assert.Equal(t, 3, cmdList.List.Len(), "Chain add elements in the list")

assert.Equal(t, "Fake 2", cmdList.Run("fakecommand2", "!fakecommand2", userTest))
Expand Down
22 changes: 9 additions & 13 deletions commands/find.go
@@ -1,6 +1,7 @@
package command

import (
"errors"
"fmt"
"regexp"
"strings"
Expand All @@ -10,29 +11,24 @@ import (
"github.com/ssalvatori/zbot-telegram-go/user"
)

//FindCommand defintion
type FindCommand struct {
Next HandlerCommand
Db db.ZbotDatabase
Levels Levels
Db db.ZbotDatabase
}

func (handler *FindCommand) ProcessText(text string, user user.User) string {
//ProcessText run command
func (handler *FindCommand) ProcessText(text string, user user.User) (string, error) {

commandPattern := regexp.MustCompile(`^!find\s(\S*)`)
result := ""

if commandPattern.MatchString(text) {
term := commandPattern.FindStringSubmatch(text)
results, err := handler.Db.Find(term[1])
if err != nil {
log.Error(fmt.Errorf("Error learn %v", err))
return ""
}
result = fmt.Sprintf("%s", strings.Join(getTerms(results), " "))
} else {
if handler.Next != nil {
result = handler.Next.ProcessText(text, user)
log.Error(err)
return "", err
}
return fmt.Sprintf("%s", strings.Join(getTerms(results), " ")), nil
}
return result
return "", errors.New("text doesn't match")
}
30 changes: 22 additions & 8 deletions commands/find_test.go
Expand Up @@ -16,18 +16,32 @@ func TestFindCommandOK(t *testing.T) {
Meaning: "bar",
}

assert.Equal(t, "bar", findCommand.ProcessText("!find foo", userTest), "Last Command")
var result string

result, _ = findCommand.ProcessText("!find foo", userTest)
assert.Equal(t, "bar", result, "Last Command")
findCommand.Db = &db.MockZbotDatabase{
Not_found: true,
}
assert.Equal(t, "", findCommand.ProcessText("!find lalal", userTest), "Last Command")
result, _ = findCommand.ProcessText("!find lalal", userTest)
assert.Equal(t, "", result, "Last Command")

}
func TestFindCommandNotMatch(t *testing.T) {

result, _ := findCommand.ProcessText("!find6", userTest)
assert.Equal(t, "", result, "Empty output doesn't match")

_, err := findCommand.ProcessText("!find6", userTest)
assert.Equal(t, "text doesn't match", err.Error(), "Error output doesn't match")
}

func TestFindCommandError(t *testing.T) {

findCommand.Db = &db.MockZbotDatabase{
Error: true,
Rand_def: db.DefinitionItem{Term: "foo", Meaning: "bar"},
Error: true,
}
assert.Equal(t, "", findCommand.ProcessText("!find lala", userTest), "Error DB")

assert.Equal(t, "", findCommand.ProcessText("!find", userTest), "Last no next command")
findCommand.Next = &FakeCommand{}
assert.Equal(t, "Fake OK", findCommand.ProcessText("?? ", userTest), "Last next command")
_, err := findCommand.ProcessText("!find lala", userTest)
assert.Equal(t, "mock", err.Error(), "Db error")
}
5 changes: 4 additions & 1 deletion commands/ping.go
Expand Up @@ -10,10 +10,13 @@ import (

// PingCommand command definition
type PingCommand struct {
Db db.ZbotDatabase
}

//SetDb set db connection if the module need it
func (handler *PingCommand) SetDb(db db.ZbotDatabase) {}
func (handler *PingCommand) SetDb(db db.ZbotDatabase) {
handler.Db = db
}

// ProcessText run command
func (handler *PingCommand) ProcessText(text string, user user.User) (string, error) {
Expand Down
6 changes: 0 additions & 6 deletions commands/version.go
Expand Up @@ -5,22 +5,16 @@ import (
"fmt"
"regexp"

"github.com/ssalvatori/zbot-telegram-go/db"

"github.com/ssalvatori/zbot-telegram-go/user"
)

//VersionCommand configuration for version
type VersionCommand struct {
//Next HandlerCommand
Version string
GitHash string
BuildTime string
}

//SetDb set db connection if the module need it
func (handler *VersionCommand) SetDb(db db.ZbotDatabase) {}

//ProcessText run command
func (handler *VersionCommand) ProcessText(text string, user user.User) (string, error) {

Expand Down
37 changes: 14 additions & 23 deletions zbot/zbot.go
Expand Up @@ -53,6 +53,8 @@ var levelsConfig = command.Levels{
Version: 0,
Ping: 0,
Last: 0,
Rand: 0,
Find: 0,
}

//Execute run Zbot
Expand Down Expand Up @@ -146,34 +148,31 @@ func processing(db db.ZbotDatabase, msg tb.Message) string {
}

/*
// TODO: how to clean this code
//commands := &command.PingCommand{}
//versionCommand := &command.VersionCommand{Version: version, BuildTime: buildTime, GitHash: gitHash}
//statsCommand := &command.StatsCommand{Db: db, Levels: levelsConfig}
randCommand := &command.RandCommand{Db: db, Levels: levelsConfig}
//topCommand := &command.TopCommand{Db: db, Levels: levelsConfig}
lastCommand := &command.LastCommand{Db: db, Levels: levelsConfig}
getCommand := &command.GetCommand{Db: db, Levels: levelsConfig}
findCommand := &command.FindCommand{Db: db, Levels: levelsConfig}
searchCommand := &command.SearchCommand{Db: db, Levels: levelsConfig}
learnCommand := &command.LearnCommand{Db: db, Levels: levelsConfig}
levelCommand := &command.LevelCommand{Db: db, Levels: levelsConfig}
ignoreCommand := &command.IgnoreCommand{Db: db, Levels: levelsConfig}
lockCommand := &command.LockCommand{Db: db, Levels: levelsConfig}
appendCommand := &command.AppendCommand{Db: db, Levels: levelsConfig}
whoCommand := &command.WhoCommand{Db: db, Levels: levelsConfig}
forgetCommand := &command.ForgetCommand{Db: db, Levels: levelsConfig}
*/

commandsList := &command.CommandsList{
List: list.New(),
Db: db,
}
commandsList.Chain("ping", command.PingCommand{}, levelsConfig.Ping)
commandsList.Chain("version", command.VersionCommand{Version: version, BuildTime: buildTime}, levelsConfig.Version)
commandsList.Chain("top", command.TopCommand{}, levelsConfig.Top)
commandsList.Chain("stats", command.StatsCommand{}, levelsConfig.Stats)
commandsList.Chain("last", command.LastCommand{}, levelsConfig.Last)
commandsList.Chain("ping", &command.PingCommand{Db: db}, levelsConfig.Ping)
commandsList.Chain("version", &command.VersionCommand{
GitHash: gitHash,
Version: version,
BuildTime: buildTime,
}, levelsConfig.Version)
commandsList.Chain("top", &command.TopCommand{Db: db}, levelsConfig.Top)
commandsList.Chain("stats", &command.StatsCommand{Db: db}, levelsConfig.Stats)
commandsList.Chain("last", &command.LastCommand{Db: db}, levelsConfig.Last)
commandsList.Chain("rand", &command.RandCommand{Db: db}, levelsConfig.Rand)
commandsList.Chain("who", &command.WhoCommand{Db: db}, levelsConfig.Who)
commandsList.Chain("find", &command.FindCommand{Db: db}, levelsConfig.Find)

/*
TODO: check error handler
Expand All @@ -185,20 +184,12 @@ func processing(db db.ZbotDatabase, msg tb.Message) string {
PathModules: ModulesPath,
}
commands.Next = versionCommand
versionCommand.Next = statsCommand
statsCommand.Next = randCommand
randCommand.Next = topCommand
topCommand.Next = lastCommand
lastCommand.Next = getCommand
getCommand.Next = findCommand
findCommand.Next = searchCommand
searchCommand.Next = learnCommand
learnCommand.Next = levelCommand
levelCommand.Next = lockCommand
lockCommand.Next = appendCommand
appendCommand.Next = whoCommand
whoCommand.Next = forgetCommand
forgetCommand.Next = ignoreCommand
ignoreCommand.Next = externalCommand
*/
Expand Down

0 comments on commit 86e5ba4

Please sign in to comment.