Covered handler.go with tests
This commit is contained in:
parent
f0f64bd511
commit
d5f9a495b7
7 changed files with 259 additions and 68 deletions
|
|
@ -1,26 +0,0 @@
|
|||
package cardsinfo
|
||||
|
||||
import (
|
||||
"github.com/stretchr/testify/assert"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestFormat(t *testing.T) {
|
||||
data := []CardPrice{
|
||||
&TcgCardPrice{
|
||||
Name: "Green lotus",
|
||||
PriceFoil: "22.8",
|
||||
Link: "scg.com/1",
|
||||
Edition: "alpha",
|
||||
},
|
||||
&TcgCardPrice{
|
||||
Name: "White lotus",
|
||||
Price: "3.22",
|
||||
Link: "scg.com/2",
|
||||
Edition: "gamma",
|
||||
},
|
||||
}
|
||||
res := FormatCardPrices("Black Lotus", data)
|
||||
ans := "Оригинальное название: Black Lotus\nРезультатов: 2\n1. alpha\nRegular: -\nFoil: $22.8\nscg.com/1\n2. gamma\nRegular: $3.22\nFoil: -\nscg.com/2\n"
|
||||
assert.Equal(t, ans, res)
|
||||
}
|
||||
|
|
@ -9,26 +9,6 @@ type CardPrice interface {
|
|||
Format() string
|
||||
}
|
||||
|
||||
type TcgCardPrice struct {
|
||||
FullArt bool
|
||||
Name string
|
||||
Price string
|
||||
PriceFoil string
|
||||
Link string
|
||||
Edition string
|
||||
}
|
||||
|
||||
func (t *TcgCardPrice) Format() string {
|
||||
return fmt.Sprintf("%v\nRegular: %v\nFoil: %v\n%v\n", t.Edition, formatTcgPrice(t.Price), formatTcgPrice(t.PriceFoil), t.Link)
|
||||
}
|
||||
|
||||
func formatTcgPrice(price string) string {
|
||||
if price == "" {
|
||||
return "-"
|
||||
}
|
||||
return fmt.Sprintf("$%v", price)
|
||||
}
|
||||
|
||||
type ScgCardPrice struct {
|
||||
Price string
|
||||
Edition string
|
||||
|
|
|
|||
|
|
@ -2,16 +2,28 @@ package vk
|
|||
|
||||
import (
|
||||
"errors"
|
||||
"io"
|
||||
"log"
|
||||
"net/http"
|
||||
"os"
|
||||
"strings"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/caching"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
)
|
||||
|
||||
type CardInfoFetcher interface {
|
||||
GetPrices(name string) ([]cardsinfo.CardPrice, error)
|
||||
FormatCardPrices(name string, prices []cardsinfo.CardPrice) string
|
||||
GetNameByCardId(set string, number string) string
|
||||
GetOriginalName(name string, dict io.Reader) string
|
||||
}
|
||||
|
||||
type CardCache interface {
|
||||
Get(cardName string) (string, error)
|
||||
Set(cardName string, message string)
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
Sender Sender
|
||||
Logger *log.Logger
|
||||
|
|
@ -19,7 +31,8 @@ type Handler struct {
|
|||
GroupId int64
|
||||
ConfirmationString string
|
||||
DictPath string
|
||||
CachingClient *caching.CacheClient
|
||||
Cache CardCache
|
||||
InfoFetcher CardInfoFetcher
|
||||
}
|
||||
|
||||
type messageRequest struct {
|
||||
|
|
@ -34,6 +47,12 @@ type userMessage struct {
|
|||
UserId int64 `json:"peer_id"`
|
||||
}
|
||||
|
||||
const (
|
||||
incorrectMessage = "Некорректная команда"
|
||||
cardNotFoundMessage = "Карта не найдена"
|
||||
pricesUnavailableMessage = "Цены временно недоступны, попробуйте позже"
|
||||
)
|
||||
|
||||
func (h *Handler) HandleMessage(c *gin.Context) {
|
||||
var req messageRequest
|
||||
_ = c.BindJSON(&req)
|
||||
|
|
@ -50,23 +69,17 @@ func (h *Handler) HandleMessage(c *gin.Context) {
|
|||
}
|
||||
|
||||
func (h *Handler) handleSearch(req *messageRequest) {
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
h.Logger.Printf("[error] Search panicked. Exception info: %s", r)
|
||||
}
|
||||
}()
|
||||
|
||||
cardName, err := h.getCardNameByCommand(req.Object.Body)
|
||||
if err != nil {
|
||||
h.Sender.Send(req.Object.UserId, "Некорректная команда")
|
||||
h.Sender.Send(req.Object.UserId, incorrectMessage)
|
||||
h.Logger.Printf("[info] Not correct command. Message: %s user input: %s", err.Error(), req.Object.Body)
|
||||
} else if cardName == "" {
|
||||
h.Sender.Send(req.Object.UserId, "Карта не найдена")
|
||||
h.Sender.Send(req.Object.UserId, cardNotFoundMessage)
|
||||
h.Logger.Printf("[info] Could not find card. User input: %s", req.Object.Body)
|
||||
} else {
|
||||
message, err := h.getMessage(cardName)
|
||||
if err != nil {
|
||||
h.Sender.Send(req.Object.UserId, "Цены временно недоступны, попробуйте позже")
|
||||
h.Sender.Send(req.Object.UserId, pricesUnavailableMessage)
|
||||
h.Logger.Printf("[error] Could not find SCG prices. Message: %s card name: %s", err.Error(), cardName)
|
||||
return
|
||||
}
|
||||
|
|
@ -81,14 +94,14 @@ func (h *Handler) handleConfirmation(c *gin.Context, req *messageRequest) {
|
|||
}
|
||||
|
||||
func (h *Handler) getMessage(cardName string) (string, error) {
|
||||
val, err := h.CachingClient.Get(cardName)
|
||||
val, err := h.Cache.Get(cardName)
|
||||
if err != nil {
|
||||
prices, err := cardsinfo.GetPrices(cardName)
|
||||
prices, err := h.InfoFetcher.GetPrices(cardName)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
message := cardsinfo.FormatCardPrices(cardName, prices)
|
||||
h.CachingClient.Set(cardName, message)
|
||||
message := h.InfoFetcher.FormatCardPrices(cardName, prices)
|
||||
h.Cache.Set(cardName, message)
|
||||
return message, nil
|
||||
}
|
||||
return val, nil
|
||||
|
|
@ -104,10 +117,10 @@ func (h *Handler) getCardNameByCommand(command string) (string, error) {
|
|||
}
|
||||
set := split[1]
|
||||
number := split[2]
|
||||
name = cardsinfo.GetNameByCardId(set, number)
|
||||
name = h.InfoFetcher.GetNameByCardId(set, number)
|
||||
default:
|
||||
dict, _ := os.Open(h.DictPath)
|
||||
name = cardsinfo.GetOriginalName(command, dict)
|
||||
name = h.InfoFetcher.GetOriginalName(command, dict)
|
||||
}
|
||||
return name, nil
|
||||
}
|
||||
|
|
|
|||
222
internal/vk/handler_test.go
Normal file
222
internal/vk/handler_test.go
Normal file
|
|
@ -0,0 +1,222 @@
|
|||
package vk
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"github.com/gin-gonic/gin"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
"io"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func getTestRequestCtx(msgReq *messageRequest, recorder *httptest.ResponseRecorder) *gin.Context {
|
||||
ctx, _ := gin.CreateTestContext(recorder)
|
||||
body, _ := json.Marshal(msgReq)
|
||||
ctx.Request = httptest.NewRequest("POST", "/", bytes.NewReader(body))
|
||||
return ctx
|
||||
}
|
||||
|
||||
type testCtx struct {
|
||||
handler *Handler
|
||||
recorder *httptest.ResponseRecorder
|
||||
sender *testSender
|
||||
logBuf *bytes.Buffer
|
||||
}
|
||||
|
||||
type testMessage struct {
|
||||
userId int64
|
||||
message string
|
||||
}
|
||||
|
||||
type testSender struct {
|
||||
sent []testMessage
|
||||
}
|
||||
|
||||
func (s *testSender) Send(userId int64, message string) {
|
||||
s.sent = append(s.sent, testMessage{
|
||||
userId: userId,
|
||||
message: message,
|
||||
})
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
|
||||
func getTestHandlerCtx() testCtx {
|
||||
sender := &testSender{}
|
||||
buf := &bytes.Buffer{}
|
||||
return testCtx{
|
||||
logBuf: buf,
|
||||
handler: &Handler{
|
||||
SecretKey: "sec",
|
||||
GroupId: 10,
|
||||
ConfirmationString: "con",
|
||||
Sender: sender,
|
||||
Logger: log.New(buf, "", 0),
|
||||
InfoFetcher: &testInfoFetcher{},
|
||||
Cache: &testCache{
|
||||
table: map[string]string{
|
||||
"good": "good",
|
||||
},
|
||||
},
|
||||
},
|
||||
sender: sender,
|
||||
recorder: httptest.NewRecorder(),
|
||||
}
|
||||
}
|
||||
|
||||
type testInfoFetcher struct{}
|
||||
|
||||
func (t *testInfoFetcher) GetPrices(name string) ([]cardsinfo.CardPrice, error) {
|
||||
if name == "good" || name == "uncached" {
|
||||
return nil, nil
|
||||
}
|
||||
return nil, errors.New("test")
|
||||
}
|
||||
|
||||
func (t *testInfoFetcher) FormatCardPrices(name string, _ []cardsinfo.CardPrice) string {
|
||||
return name
|
||||
}
|
||||
|
||||
func (t *testInfoFetcher) GetNameByCardId(_ string, _ string) string {
|
||||
return "good"
|
||||
}
|
||||
|
||||
func (t *testInfoFetcher) GetOriginalName(name string, _ io.Reader) string {
|
||||
if name == "good" || name == "bad" || name == "uncached" {
|
||||
return name
|
||||
}
|
||||
return ""
|
||||
}
|
||||
|
||||
func TestHandler_HandleMessage_Confirm(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
ctx := getTestRequestCtx(&messageRequest{
|
||||
Type: "confirmation",
|
||||
GroupId: testCtx.handler.GroupId,
|
||||
Secret: testCtx.handler.SecretKey,
|
||||
}, testCtx.recorder)
|
||||
testCtx.handler.HandleMessage(ctx)
|
||||
assert.Equal(t, testCtx.handler.ConfirmationString, testCtx.recorder.Body.String())
|
||||
}
|
||||
|
||||
func TestHandler_HandleMessage_Message(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
ctx := getTestRequestCtx(&messageRequest{
|
||||
Type: "message_new",
|
||||
Secret: testCtx.handler.SecretKey,
|
||||
}, testCtx.recorder)
|
||||
testCtx.handler.HandleMessage(ctx)
|
||||
assert.Equal(t, "ok", testCtx.recorder.Body.String())
|
||||
}
|
||||
|
||||
func TestHandler_HandleMessage_NoSecretKey(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
ctx := getTestRequestCtx(&messageRequest{
|
||||
Type: "message_new",
|
||||
}, testCtx.recorder)
|
||||
testCtx.handler.HandleMessage(ctx)
|
||||
assert.Equal(t, "", testCtx.recorder.Body.String())
|
||||
}
|
||||
|
||||
func TestHandler_handleSearch_BadCommand(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: 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 TestHandler_handleSearch_GoodCommand(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: userMessage{
|
||||
Body: "!s grn 228",
|
||||
UserId: 1,
|
||||
},
|
||||
})
|
||||
assert.Equal(t, []testMessage{
|
||||
{
|
||||
userId: 1,
|
||||
message: "good",
|
||||
},
|
||||
}, testCtx.sender.sent)
|
||||
}
|
||||
|
||||
func TestHandler_handleSearch_NotFoundCard(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: 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 TestHandler_handleSearch_BadCard(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: 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 TestHandler_handleSearch_Uncached(t *testing.T) {
|
||||
testCtx := getTestHandlerCtx()
|
||||
testCtx.handler.handleSearch(&messageRequest{
|
||||
Object: userMessage{
|
||||
Body: "uncached",
|
||||
UserId: 1,
|
||||
},
|
||||
})
|
||||
assert.Equal(t, []testMessage{
|
||||
{
|
||||
userId: 1,
|
||||
message: "uncached",
|
||||
},
|
||||
}, testCtx.sender.sent)
|
||||
msg, _ := testCtx.handler.Cache.Get("uncached")
|
||||
assert.Equal(t, "uncached", msg)
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue