Skip to content

Commit

Permalink
Mockery tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trezorg committed May 8, 2024
1 parent 5bc4cf9 commit 90362a3
Show file tree
Hide file tree
Showing 17 changed files with 575 additions and 128 deletions.
18 changes: 18 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
testonly: false
with-expecter: true
# inpackage: true
# dir: mocks/{{ replaceAll .InterfaceDirRelative "internal" "internal_" }}
# mockname: "{{.InterfaceName}}"
# outpkg: "{{.PackageName}}"
filename: "{{.InterfaceName}}.go"
all: true
packages:
github.com/trezorg/lingualeo/pkg/translator:
config:
recursive: true
with-expecter: true
mockname: "Mock_{{.InterfaceName}}"
# outpkg: mocks
dir: ./{{ replaceAll .InterfaceDirRelative "internal" "internal_" }}
filename: "mock_{{.InterfaceName}}.go"
inpackage: true
23 changes: 18 additions & 5 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ DEST := $(GOPATH)/src/$(GIT_HOST)/$(BASE_DIR)
SOURCES := $(shell find $(DEST) -name '*.go' 2>/dev/null)
HAS_GOLANGCI := $(shell command -v golangci-lint;)
HAS_GOIMPORTS := $(shell command -v goimports;)
HAS_MOCKERY := $(shell command -v mockery;)

TARGETS ?= darwin/amd64 linux/amd64 linux/386 linux/arm linux/arm64 linux/ppc64le linux/s390x
DIST_DIRS = find * -type d -exec
Expand Down Expand Up @@ -66,7 +67,7 @@ fmt:
goimports:
ifndef HAS_GOIMPORTS
echo "installing goimports"
GO111MODULE=off go get golang.org/x/tools/cmd/goimports
go install golang.org/x/tools/cmd/goimports@latest
endif
goimports -d $(shell find . -path ./.go -prune -o -type f -iname "*.go")
find . -iname "*.go"
Expand All @@ -76,24 +77,36 @@ vet:

golangci:
ifndef HAS_GOLANGCI
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.49.0
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.58.0
endif
golangci-lint run

generate:
ifndef HAS_MOCKERY
echo "installing mockery"
go install github.com/vektra/mockery/v2@latest
endif
go generate ./...

cover: work
go test $(TESTARGS) -tags=unit -cover -coverpkg=./ ./...


prepare:
ifndef HAS_GOLANGCI
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.49.0
curl -sfL https://raw.githubusercontent.com/golangci/golangci-lint/master/install.sh | sh -s -- -b $(shell go env GOPATH)/bin v1.58.0
endif
echo "golangci-lint already installed"
ifndef HAS_GOIMPORTS
echo "installing goimports"
GO111MODULE=off go get golang.org/x/tools/cmd/goimports
go install golang.org/x/tools/cmd/goimports@latest
endif
echo "goimports already installed"
ifndef HAS_MOCKERY
echo "installing mockery"
go install github.com/vektra/mockery/v2@latest
endif
echo "mockery already installed"

shell:
$(SHELL) -i
Expand All @@ -104,4 +117,4 @@ clean: work
version:
@echo ${VERSION}

.PHONY: install build cover work fmt test version clean prepare
.PHONY: install build cover work fmt test version clean prepare generate
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ require (
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/russross/blackfriday/v2 v2.0.1 // indirect
github.com/shurcooL/sanitized_anchor_name v1.0.0 // indirect
github.com/stretchr/objx v0.1.0 // indirect
golang.org/x/sys v0.18.0 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
)
1 change: 1 addition & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ github.com/shurcooL/sanitized_anchor_name v1.0.0 h1:PdmoCO6wvbs+7yrJyMORt4/BmY5I
github.com/shurcooL/sanitized_anchor_name v1.0.0/go.mod h1:1NzhyTcUVG4SuEtjjoZeVRXNmyL/1OwPU0+IJeTBvfc=
github.com/sirupsen/logrus v1.5.0 h1:1N5EYkVAPEywqZRJd7cwnRtCb6xJx7NH3T3WUTF980Q=
github.com/sirupsen/logrus v1.5.0/go.mod h1:+F7Ogzej0PZc/94MaYx/nvG9jOFMD2osvC3s+Squfpo=
github.com/stretchr/objx v0.1.0 h1:4G4v2dO3VZwixGIRoQ5Lfboy6nUhCyYzaqnIAPPhYs4=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/testify v1.2.2/go.mod h1:a8OnRcib4nhh0OaRAV+Yts87kKdq0PP7pXfy6kDkUVs=
github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk=
Expand Down
11 changes: 5 additions & 6 deletions internal/fakeapi/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ import (
)

var (
responseData = []byte(`{"error_msg":"","translate_source":"base","is_user":0,
SoundURL = "http://audiocdn.lingualeo.com/v2/3/102085-631152000.mp3"
ResponseData = []byte(`{"error_msg":"","translate_source":"base","is_user":0,
"word_forms":[{"word":"accommodation","type":"прил."}],
"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/3589594.png",
"translate":[
Expand All @@ -25,19 +26,17 @@ var (
SearchWord = "accommodation"
)

type FakeAPI struct {
api.Translator
}
type FakeAPI struct{}

func (f *FakeAPI) TranslateWord(word string) api.OperationResult {
res := api.Result{Word: word}
err := res.FromResponse(responseData)
err := res.FromResponse(ResponseData)
return api.OperationResult{Result: res, Error: err}
}

func (f *FakeAPI) AddWord(word string, _ []string) api.OperationResult {
res := api.Result{Word: word}
err := res.FromResponse(responseData)
err := res.FromResponse(ResponseData)
return api.OperationResult{Result: res, Error: err}
}

Expand Down
46 changes: 5 additions & 41 deletions internal/files/download.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,18 @@
package files

import (
"context"
"fmt"
"io"
"net/http"
"os"
"sync"

"github.com/trezorg/lingualeo/internal/logger"
"github.com/trezorg/lingualeo/pkg/channel"
)

const fileTemplate = "lingualeo"
const filePath = "/tmp"

// Downloader interface
type Downloader interface {
Download(url string) (string, error)
Writer() (io.WriteCloser, string, error)
}

// NewDownloader function type
type NewDownloader func(url string) Downloader
const (
fileTemplate = "lingualeo"
filePath = "/tmp"
)

// File represents file for downloading
type File struct {
Expand All @@ -37,8 +27,7 @@ func (f File) GetIndex() int {
}

// FileDownloader structure
type FileDownloader struct {
}
type FileDownloader struct{}

// NewFileDownloader initialize new file downloader
func NewFileDownloader() *FileDownloader {
Expand Down Expand Up @@ -89,28 +78,3 @@ func (f *FileDownloader) Download(url string) (string, error) {
}
return filename, nil
}

// DownloadFiles download files from URLs channel
func DownloadFiles(ctx context.Context, urls <-chan string, downloader Downloader) <-chan File {
out := make(chan File)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
idx := 0
for url := range channel.OrDone(ctx, urls) {
wg.Add(1)
go func(idx int, url string) {
defer wg.Done()
filename, err := downloader.Download(url)
out <- File{Error: err, Filename: filename, Index: idx}
}(idx, url)
idx++
}
}()
go func() {
defer close(out)
wg.Wait()
}()
return out
}
26 changes: 0 additions & 26 deletions internal/files/testing/download_file_test.go

This file was deleted.

16 changes: 4 additions & 12 deletions pkg/api/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,6 @@ import (
"golang.org/x/net/publicsuffix"
)

// Translator interface
type Translator interface {
translateWord(word string) OperationResult
TranslateWords(ctx context.Context, results <-chan string) <-chan OperationResult
addWord(word string, translate []string) OperationResult
AddWords(ctx context.Context, results <-chan Result) <-chan OperationResult
}

// API structure represents API request
type API struct {
client *http.Client
Expand Down Expand Up @@ -226,15 +218,15 @@ func (api *API) addRequest(word string, translate []string) ([]byte, error) {
return request("POST", addWordURL, api.client, jsonValue, "", api.Debug)
}

func (api *API) translateWord(word string) OperationResult {
func (api *API) TranslateWord(word string) OperationResult {
body, err := api.translateRequest(word)
if err != nil {
return OperationResult{Error: err}
}
return opResultFromBody(word, body)
}

func (api *API) addWord(word string, translate []string) OperationResult {
func (api *API) AddWord(word string, translate []string) OperationResult {
body, err := api.addRequest(word, translate)
if err != nil {
return OperationResult{Error: err}
Expand All @@ -253,7 +245,7 @@ func (api *API) TranslateWords(ctx context.Context, results <-chan string) <-cha
wg.Add(1)
go func(word string) {
defer wg.Done()
out <- api.translateWord(word)
out <- api.TranslateWord(word)
}(word)
}
}()
Expand All @@ -276,7 +268,7 @@ func (api *API) AddWords(ctx context.Context, results <-chan Result) <-chan Oper
result := res
go func(result Result) {
defer wg.Done()
out <- api.addWord(result.Word, result.Words)
out <- api.AddWord(result.Word, result.Words)
}(result)
}
}()
Expand Down
27 changes: 0 additions & 27 deletions pkg/api/testing/translate_words_test.go

This file was deleted.

41 changes: 41 additions & 0 deletions pkg/translator/download.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
package translator

import (
"context"
"sync"

"github.com/trezorg/lingualeo/internal/files"
"github.com/trezorg/lingualeo/pkg/channel"
)

// Downloader interface
//
//go:generate mockery
type Downloader interface {
Download(url string) (string, error)
}

// DownloadFiles download files from URLs channel
func DownloadFiles(ctx context.Context, urls <-chan string, downloader Downloader) <-chan files.File {
out := make(chan files.File)
var wg sync.WaitGroup
wg.Add(1)
go func() {
defer wg.Done()
idx := 0
for url := range channel.OrDone(ctx, urls) {
wg.Add(1)
go func(idx int, url string) {
defer wg.Done()
filename, err := downloader.Download(url)
out <- files.File{Error: err, Filename: filename, Index: idx}
}(idx, url)
idx++
}
}()
go func() {
defer close(out)
wg.Wait()
}()
return out
}
27 changes: 27 additions & 0 deletions pkg/translator/download_file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package translator

import (
"context"
"testing"

"github.com/stretchr/testify/assert"
)

func TestDownloadWordFile(t *testing.T) {
downloader := NewMock_Downloader(t)
url := "http://test.com/file"
testFile := "/tmp/test.file"

downloader.EXPECT().Download(url).Return(testFile, nil).Once()

inChan := make(chan string, 1)
inChan <- url

ctx := context.Background()

close(inChan)

out := DownloadFiles(ctx, inChan, downloader)
fileName := (<-out).Filename
assert.Equal(t, fileName, testFile)
}
Loading

0 comments on commit 90362a3

Please sign in to comment.