Extracted scenario

This commit is contained in:
Artyom Belousov 2021-06-06 11:00:50 +03:00
parent c02435ccaa
commit cc2058090c
13 changed files with 235 additions and 173 deletions

View file

@ -0,0 +1,89 @@
package scenario
import (
"errors"
"log"
"strings"
)
const (
incorrectMessage = "Некорректная команда"
cardNotFoundMessage = "Карта не найдена"
pricesUnavailableMessage = "Цены временно недоступны, попробуйте позже"
)
type Scenario struct {
Sender Sender
Logger *log.Logger
InfoFetcher CardInfoFetcher
Cache CardCache
}
type UserMessage struct {
Body string
UserId int64
}
type CardCache interface {
Get(cardName string) (string, error)
Set(cardName string, message string)
}
type CardInfoFetcher interface {
GetFormattedCardPrices(name string) (string, error)
GetNameByCardId(set string, number string) string
GetOriginalName(name string) string
}
type Sender interface {
Send(userId int64, message string)
}
func (s *Scenario) HandleSearch(msg *UserMessage) {
cardName, err := s.getCardNameByCommand(msg.Body)
if err != nil {
s.Sender.Send(msg.UserId, incorrectMessage)
s.Logger.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), msg.Body)
} else if cardName == "" {
s.Sender.Send(msg.UserId, cardNotFoundMessage)
s.Logger.Printf("[info] Could not find card. User input: %s", msg.Body)
} else {
message, err := s.getMessage(cardName)
if err != nil {
s.Sender.Send(msg.UserId, pricesUnavailableMessage)
s.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
return
}
s.Sender.Send(msg.UserId, message)
}
}
func (s *Scenario) getMessage(cardName string) (string, error) {
val, err := s.Cache.Get(cardName)
if err != nil {
message, err := s.InfoFetcher.GetFormattedCardPrices(cardName)
if err != nil {
return "", err
}
s.Cache.Set(cardName, message)
return message, nil
}
return val, nil
}
func (s *Scenario) getCardNameByCommand(command string) (string, error) {
var name string
switch {
case strings.HasPrefix(command, "!s"):
split := strings.Split(command, " ")
if len(split) < 3 {
return "", errors.New("wrong command")
}
set := split[1]
number := split[2]
name = s.InfoFetcher.GetNameByCardId(set, number)
default:
name = s.InfoFetcher.GetOriginalName(command)
}
return name, nil
}

View file

@ -0,0 +1,82 @@
package scenario
import (
"strings"
"testing"
"github.com/stretchr/testify/assert"
)
func TestScenario_HandleSearch_BadCommand(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "!s",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
userId: 1,
message: incorrectMessage,
},
}, testCtx.Sender.sent)
assert.True(t, strings.Contains(testCtx.LogBuf.String(), "[info]"))
}
func TestScenario_HandleSearch_GoodCommand(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "!s grn 228",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
userId: 1,
message: "good",
},
}, testCtx.Sender.sent)
}
func TestScenario_HandleSearch_NotFoundCard(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "absolutely_random_card",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
userId: 1,
message: cardNotFoundMessage,
},
}, testCtx.Sender.sent)
assert.True(t, strings.Contains(testCtx.LogBuf.String(), "[info]"))
}
func TestScenario_HandleSearch_BadCard(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "bad",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
userId: 1,
message: pricesUnavailableMessage,
},
}, testCtx.Sender.sent)
assert.True(t, strings.Contains(testCtx.LogBuf.String(), "[error]"))
}
func TestScenario_HandleSearch_Uncached(t *testing.T) {
testCtx := GetTestScenarioCtx()
testCtx.Scenario.HandleSearch(&UserMessage{
Body: "uncached",
UserId: 1,
})
assert.Equal(t, []testMessage{
{
userId: 1,
message: "uncached",
},
}, testCtx.Sender.sent)
msg, _ := testCtx.Scenario.Cache.Get("uncached")
assert.Equal(t, "uncached", msg)
}

View file

@ -0,0 +1,19 @@
package scenario
import "errors"
type testCache struct {
table map[string]string
}
func (t *testCache) Get(cardName string) (string, error) {
msg, ok := t.table[cardName]
if !ok {
return "", errors.New("test")
}
return msg, nil
}
func (t *testCache) Set(cardName string, message string) {
t.table[cardName] = message
}

View file

@ -0,0 +1,31 @@
package scenario
import (
"bytes"
"log"
)
type TestScenarioCtx struct {
Scenario *Scenario
Sender *testSender
LogBuf *bytes.Buffer
}
func GetTestScenarioCtx() TestScenarioCtx {
sender := &testSender{}
buf := &bytes.Buffer{}
return TestScenarioCtx{
LogBuf: buf,
Scenario: &Scenario{
Sender: sender,
Logger: log.New(buf, "", 0),
InfoFetcher: &testInfoFetcher{},
Cache: &testCache{
table: map[string]string{
"good": "good",
},
},
},
Sender: sender,
}
}

View file

@ -0,0 +1,25 @@
package scenario
import (
"errors"
)
type testInfoFetcher struct{}
func (t *testInfoFetcher) GetFormattedCardPrices(name string) (string, error) {
if name == "good" || name == "uncached" {
return name, nil
}
return "", errors.New("test")
}
func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {
return "good"
}
func (t *testInfoFetcher) GetOriginalName(name string) string {
if name == "good" || name == "bad" || name == "uncached" {
return name
}
return ""
}

View file

@ -0,0 +1,17 @@
package scenario
type testSender struct {
sent []testMessage
}
type testMessage struct {
userId int64
message string
}
func (s *testSender) Send(userId int64, message string) {
s.sent = append(s.sent, testMessage{
userId: userId,
message: message,
})
}