Skip to content

Commit

Permalink
Mockery tests
Browse files Browse the repository at this point in the history
  • Loading branch information
trezorg committed May 9, 2024
1 parent 5bc4cf9 commit 2a93a86
Show file tree
Hide file tree
Showing 20 changed files with 605 additions and 254 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
85 changes: 0 additions & 85 deletions internal/fakeapi/api.go

This file was deleted.

35 changes: 35 additions & 0 deletions internal/fakeapi/data.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package fakeapi

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
"github.com/trezorg/lingualeo/pkg/api"
)

var (
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":[
{"id":2569250,"value":"жильё","votes":5703,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/31064.png"},
{"id":2718711,"value":"проживание","votes":1589,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/335521.png"},
{"id":185932,"value":"размещение","votes":880,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/374830.png"},
{"id":2735899,"value":"помещение","votes":268,"is_user":0,"pic_url":"http:\/\/contentcdn.lingualeo.com\/uploads\/picture\/620779.png"}
],
"transcription":"əkəədˈeɪːʃən","word_id":102085,"word_top":0,
"sound_url":"http:\/\/audiocdn.lingualeo.com\/v2\/3\/102085-631152000.mp3"}`)
Expected = []string{"жильё", "проживание", "размещение", "помещение"}
SearchWord = "accommodation"
)

func CheckResult(t *testing.T, res api.Result, searchWord string, expected []string) {
assert.Equalf(t, res.Word, searchWord, "Incorrect search word: %s", searchWord)
assert.Len(t, res.Words, 4, "Incorrect number of translated words: %d. Expected: %d", len(res.Words), len(expected))
assert.Equalf(t, res.Words, expected, "Incorrect translated words order: %s. Expected: %s",
strings.Join(expected, ", "),
strings.Join(res.Words, ", "),
)
}
29 changes: 0 additions & 29 deletions internal/fakeapi/files.go

This file was deleted.

18 changes: 0 additions & 18 deletions internal/fakeapi/results.go

This file was deleted.

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.

Loading

0 comments on commit 2a93a86

Please sign in to comment.