diff --git a/internal/vk/handler.go b/internal/vk/handler.go index 60fa484..5005e87 100644 --- a/internal/vk/handler.go +++ b/internal/vk/handler.go @@ -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"` diff --git a/internal/vk/handler_test.go b/internal/vk/handler_test.go index 6400883..c59560b 100644 --- a/internal/vk/handler_test.go +++ b/internal/vk/handler_test.go @@ -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{ diff --git a/internal/vk/test_cache.go b/internal/vk/test_cache.go new file mode 100644 index 0000000..6cf799a --- /dev/null +++ b/internal/vk/test_cache.go @@ -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 +} diff --git a/internal/vk/test_ctx.go b/internal/vk/test_ctx.go new file mode 100644 index 0000000..c704707 --- /dev/null +++ b/internal/vk/test_ctx.go @@ -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 +} diff --git a/internal/vk/test_info_fetcher.go b/internal/vk/test_info_fetcher.go new file mode 100644 index 0000000..2d61ab9 --- /dev/null +++ b/internal/vk/test_info_fetcher.go @@ -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 "" +} diff --git a/internal/vk/test_sender.go b/internal/vk/test_sender.go new file mode 100644 index 0000000..fb68251 --- /dev/null +++ b/internal/vk/test_sender.go @@ -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, + }) +}