Refactored test utils in VK module
This commit is contained in:
parent
d5f9a495b7
commit
121bb9fc9c
6 changed files with 124 additions and 112 deletions
|
|
@ -12,6 +12,17 @@ import (
|
|||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
)
|
||||
|
||||
type Handler struct {
|
||||
Sender Sender
|
||||
Logger *log.Logger
|
||||
SecretKey string
|
||||
GroupId int64
|
||||
ConfirmationString string
|
||||
DictPath string
|
||||
Cache CardCache
|
||||
InfoFetcher CardInfoFetcher
|
||||
}
|
||||
|
||||
type CardInfoFetcher interface {
|
||||
GetPrices(name string) ([]cardsinfo.CardPrice, error)
|
||||
FormatCardPrices(name string, prices []cardsinfo.CardPrice) string
|
||||
|
|
@ -24,17 +35,6 @@ type CardCache interface {
|
|||
Set(cardName string, message string)
|
||||
}
|
||||
|
||||
type Handler struct {
|
||||
Sender Sender
|
||||
Logger *log.Logger
|
||||
SecretKey string
|
||||
GroupId int64
|
||||
ConfirmationString string
|
||||
DictPath string
|
||||
Cache CardCache
|
||||
InfoFetcher CardInfoFetcher
|
||||
}
|
||||
|
||||
type messageRequest struct {
|
||||
Type string `json:"type"`
|
||||
GroupId int64 `json:"group_id"`
|
||||
|
|
|
|||
|
|
@ -1,112 +1,11 @@
|
|||
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{
|
||||
|
|
|
|||
19
internal/vk/test_cache.go
Normal file
19
internal/vk/test_cache.go
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
package vk
|
||||
|
||||
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
|
||||
}
|
||||
46
internal/vk/test_ctx.go
Normal file
46
internal/vk/test_ctx.go
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
package vk
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"github.com/gin-gonic/gin"
|
||||
"log"
|
||||
"net/http/httptest"
|
||||
)
|
||||
|
||||
type testCtx struct {
|
||||
handler *Handler
|
||||
recorder *httptest.ResponseRecorder
|
||||
sender *testSender
|
||||
logBuf *bytes.Buffer
|
||||
}
|
||||
|
||||
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(),
|
||||
}
|
||||
}
|
||||
|
||||
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
|
||||
}
|
||||
31
internal/vk/test_info_fetcher.go
Normal file
31
internal/vk/test_info_fetcher.go
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
package vk
|
||||
|
||||
import (
|
||||
"errors"
|
||||
"gitlab.com/flygrounder/go-mtg-vk/internal/cardsinfo"
|
||||
"io"
|
||||
)
|
||||
|
||||
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 ""
|
||||
}
|
||||
17
internal/vk/test_sender.go
Normal file
17
internal/vk/test_sender.go
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
package vk
|
||||
|
||||
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,
|
||||
})
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue