Skip to content

feat: add unitest workflows #10

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 11 commits into
base: main
Choose a base branch
from
43 changes: 43 additions & 0 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,49 @@ jobs:
uses: golangci/golangci-lint-action@v2
with:
version: latest
test:
runs-on: ubuntu-latest
env:
CLOUDANT_URL: ${{ secrets.CLOUDANT_URL }}
GOOGLE_APPLICATION_API_KEY: ${{ secrets.GOOGLE_APPLICATION_API_KEY }}
DEBUG: true
CI_TEST: true
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: 1.18
- name: Checkout code
uses: actions/checkout@v2
- name: Run tests
run: make test

coverage:
runs-on: ubuntu-latest
env:
CLOUDANT_URL: ${{ secrets.CLOUDANT_URL }}
GOOGLE_APPLICATION_API_KEY: ${{ secrets.GOOGLE_APPLICATION_API_KEY }}
DEBUG: true
CI_TEST: true
steps:
- name: Install Go
if: success()
uses: actions/setup-go@v2
with:
go-version: 1.18
- name: Checkout code
uses: actions/checkout@v2
- name: Make coverage
run: |
make coverage.out
- name: Convert coverage.out to coverage.lcov
uses: jandelgado/[email protected]
- name: Coveralls
uses: coverallsapp/[email protected]
with:
github-token: ${{ secrets.github_token }}
path-to-lcov: coverage.lcov

vulns:
runs-on: ubuntu-latest
Expand Down
32 changes: 15 additions & 17 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,24 +1,22 @@
all: test

clean:
rm -f get-started

install: prepare
godep go install

prepare:
go get github.com/tools/godep

build: prepare
godep go build

test: prepare build
echo "no tests"
NAME := get-started
MOCKGEN_BUILD_FLAGS ?= -mod=mod
GO ?= go
GO_ENV ?= GOPRIVATE="github.com/hblab-ngocnd/get-started"

deploy:
ibmcloud cf push

go.list:
go list -json -m all > go.list

.PHONY: install prepare build test
mockgen:
mockgen -build_flags=$(MOCKGEN_BUILD_FLAGS) -destination=./services/mock_services/mock_dictionary.go github.com/hblab-ngocnd/get-started/services DictionaryService
mockgen -build_flags=$(MOCKGEN_BUILD_FLAGS) -destination=./services/mock_services/mock_translate.go github.com/hblab-ngocnd/get-started/services TranslateService

.PHONY: test
test: FLAGS ?= -parallel 3
test:
$(GO_ENV) CI_TEST=test $(GO) test $(FLAGS) ./... -cover

coverage.out:
go test -v -covermode=count -coverprofile=coverage.out ./...
84 changes: 23 additions & 61 deletions controller/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,20 @@ package controller

import (
"context"
"fmt"
"log"
"net/http"
"os"
"strconv"
"strings"
"sync"

"github.com/hblab-ngocnd/get-started/models"
"github.com/hblab-ngocnd/get-started/services"
"github.com/hblab-ngocnd/get-started/usecase"
"github.com/labstack/echo/v4"
)

type dictHandler struct {
translateService services.TranslateService
dictionaryService services.DictionaryService
cacheData map[string][]models.Word
mu sync.Mutex
dictUseCase usecase.DictUseCase
}

func NewDictHandler() *dictHandler {
return &dictHandler{
translateService: services.NewTranslate(),
dictionaryService: services.NewDictionary(),
dictUseCase: usecase.NewDictUseCase(),
}
}

Expand All @@ -37,7 +27,6 @@ func (f *dictHandler) ApiDict(c echo.Context) error {
notCache := c.QueryParam("not_cache")
level := c.QueryParam("level")
start, err := strconv.Atoi(c.QueryParam("start"))
var end int
if err != nil {
start = 0
}
Expand All @@ -53,45 +42,19 @@ func (f *dictHandler) ApiDict(c echo.Context) error {
default:
return c.NoContent(http.StatusBadRequest)
}
if notCache != "true" && f.cacheData != nil && f.cacheData[level] != nil {
f.mu.Lock()
defer f.mu.Unlock()
if start > len(f.cacheData[level]) {
start = len(f.cacheData[level])
}
end = start + pageSize
if end > len(f.cacheData[level]) {
end = len(f.cacheData[level])
}
return c.JSON(http.StatusOK, f.cacheData[level][start:end])
}
if notCache == "true" {
pwd := c.QueryParam("password")
if len(strings.TrimSpace(pwd)) == 0 || pwd != os.Getenv("SYNC_PASS") {
return c.NoContent(http.StatusBadRequest)
}
}
f.mu.Lock()
defer f.mu.Unlock()
pwd := c.QueryParam("password")
ctx := context.Background()
url := fmt.Sprintf("https://japanesetest4you.com/jlpt-%s-vocabulary-list/", level)
data, err := f.dictionaryService.GetDictionary(ctx, url)
if err != nil {
log.Fatal(err)
}
data = f.translateService.TranslateData(ctx, data)
if f.cacheData == nil {
f.cacheData = make(map[string][]models.Word)
}
f.cacheData[level] = data
if start > len(data) {
start = len(data)
}
end = start + pageSize
if end > len(data) {
end = len(data)
data, err := f.dictUseCase.GetDict(ctx, start, pageSize, notCache, level, pwd)
switch err {
case nil:
case usecase.InvalidErr:
return c.NoContent(http.StatusBadRequest)
case usecase.PermissionDeniedErr:
return c.NoContent(http.StatusForbidden)
default:
return c.String(http.StatusInternalServerError, err.Error())
}
return c.JSON(http.StatusOK, data[start:end])
return c.JSON(http.StatusOK, data)
}

func (f *dictHandler) ApiGetDetail(c echo.Context) error {
Expand All @@ -105,18 +68,17 @@ func (f *dictHandler) ApiGetDetail(c echo.Context) error {
default:
return c.NoContent(http.StatusBadRequest)
}
if f.cacheData[level] == nil && index >= len(f.cacheData[level]) {
ctx := context.Background()
data, err := f.dictUseCase.GetDetail(ctx, level, index)
switch err {
case nil:
case usecase.InvalidErr:
return c.NoContent(http.StatusBadRequest)
default:
return c.String(http.StatusInternalServerError, err.Error())
}
detailURL := f.cacheData[level][index].Link
if strings.TrimSpace(detailURL) == "" {
if data == nil {
return c.String(http.StatusOK, "")
}
ctx := context.Background()
data, err := f.dictionaryService.GetDetail(ctx, detailURL, index)
if err != nil {
return c.String(http.StatusInternalServerError, err.Error())
}
f.cacheData[level][index].Detail = data
return c.String(http.StatusOK, data)
return c.String(http.StatusOK, *data)
}
9 changes: 0 additions & 9 deletions controller/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,18 +6,9 @@ import (
"net/http"
"sync"
"testing"

"github.com/joho/godotenv"
)

func InitTest() {
err := godotenv.Load("../.env_test")
if err != nil {
panic(err)
}
}
func TestNewDictHandler(t *testing.T) {
InitTest()
var wg sync.WaitGroup
for i := 0; i < 2; i++ {
j := i + 1
Expand Down
10 changes: 3 additions & 7 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ require (
github.com/stretchr/testify v1.7.0
github.com/timjacobi/go-couchdb v0.0.0-20160817113035-5f9d2a1a29e5
golang.org/x/net v0.0.0-20220624214902-1bab6f366d9e
golang.org/x/text v0.3.7
google.golang.org/api v0.90.0
)

require (
Expand All @@ -19,29 +21,23 @@ require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/golang-jwt/jwt v3.2.2+incompatible // indirect
github.com/golang/groupcache v0.0.0-20200121045136-8c9f03a8e57e // indirect
github.com/golang/mock v1.6.0 // indirect
github.com/golang/protobuf v1.5.2 // indirect
github.com/google/uuid v1.3.0 // indirect
github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/kr/fs v0.1.0 // indirect
github.com/kr/pretty v0.1.0 // indirect
github.com/kr/text v0.1.0 // indirect
github.com/labstack/gommon v0.3.1 // indirect
github.com/mattn/go-colorable v0.1.11 // indirect
github.com/mattn/go-isatty v0.0.14 // indirect
github.com/mitchellh/mapstructure v1.1.2 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/tools/godep v0.0.0-20180126220526-ce0bfadeb516 // indirect
github.com/valyala/bytebufferpool v1.0.0 // indirect
github.com/valyala/fasttemplate v1.2.1 // indirect
go.opencensus.io v0.23.0 // indirect
golang.org/x/crypto v0.0.0-20210817164053-32db794688a5 // indirect
golang.org/x/oauth2 v0.0.0-20220622183110-fd043fe589d2 // indirect
golang.org/x/sys v0.0.0-20220624220833-87e55d714810 // indirect
golang.org/x/text v0.3.7 // indirect
golang.org/x/time v0.0.0-20201208040808-7e3f01d25324 // indirect
golang.org/x/tools v0.1.5 // indirect
google.golang.org/api v0.90.0 // indirect
google.golang.org/appengine v1.6.7 // indirect
google.golang.org/genproto v0.0.0-20220624142145-8cd45d7dbd1f // indirect
google.golang.org/grpc v1.47.0 // indirect
Expand Down
6 changes: 1 addition & 5 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -113,6 +113,7 @@ github.com/golang/mock v1.4.1/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt
github.com/golang/mock v1.4.3/go.mod h1:UOMv5ysSaYNkG+OFQykRIcU/QvvxJf3p21QfJ2Bt3cw=
github.com/golang/mock v1.4.4/go.mod h1:l3mdAwkq5BuhzHwde/uurv3sEJeZMXNpwsxVWU71h+4=
github.com/golang/mock v1.5.0/go.mod h1:CWnOUgYIOo4TcNZ0wHX3YZCqsaM1I1Jvs6v3mP3KVu8=
github.com/golang/mock v1.6.0 h1:ErTB+efbowRARo13NNdxyJji2egdxLGQhRaY+DUumQc=
github.com/golang/mock v1.6.0/go.mod h1:p6yTPP+5HYm5mzsMV8JkE6ZKdX+/wYM6Hr+LicevLPs=
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
Expand Down Expand Up @@ -199,8 +200,6 @@ github.com/joho/godotenv v1.4.0/go.mod h1:f4LDr5Voq0i2e/R5DDNOoa2zzDfwtkZa6DnEwA
github.com/jstemmer/go-junit-report v0.0.0-20190106144839-af01ea7f8024/go.mod h1:6v2b51hI/fHJwM22ozAgKL4VKDeJcHhJFhtBdhmNjmU=
github.com/jstemmer/go-junit-report v0.9.1/go.mod h1:Brl9GWCQeLvo8nXZwPNNblvFj/XSXhF0NWZEnDohbsk=
github.com/kisielk/gotool v1.0.0/go.mod h1:XhKaO+MFFWcvkIS/tQcRk01m1F5IRFswLeQ+oQHNcck=
github.com/kr/fs v0.1.0 h1:Jskdu9ieNAYnjxsi0LbQp1ulIKZV1LAFgK1tWhpZgl8=
github.com/kr/fs v0.1.0/go.mod h1:FFnZGqtBN9Gxj7eW1uZ42v5BccTP0vu6NEaFoC2HwRg=
github.com/kr/pretty v0.1.0 h1:L/CwN0zerZDmRFUapSPitk6f+Q3+0za1rQkzVuMiMFI=
github.com/kr/pretty v0.1.0/go.mod h1:dAy3ld7l9f0ibDNOQOHHMYYIIbhfbHSm3C4ZsoJORNo=
github.com/kr/pty v1.1.1/go.mod h1:pFQYn66WHrOpPYNljwOMqo10TkYh1fy3cYio2l3bCsQ=
Expand Down Expand Up @@ -236,8 +235,6 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/timjacobi/go-couchdb v0.0.0-20160817113035-5f9d2a1a29e5 h1:1MtUq8K7y3+AVXBDaJqgJIbAfQrllywEV8RcgJDAaTs=
github.com/timjacobi/go-couchdb v0.0.0-20160817113035-5f9d2a1a29e5/go.mod h1:0FpLWWweDFBAoZdeODdifM8kBL1D1gsSHTpamQv4m5Y=
github.com/tools/godep v0.0.0-20180126220526-ce0bfadeb516 h1:h4a8ZFxjlRVGsFGP4l/AdnoUYcF3pfxzyepS3oKZ8mE=
github.com/tools/godep v0.0.0-20180126220526-ce0bfadeb516/go.mod h1:OGh2HQGYVW+2+ZdB+DgJhI75kivkKWtVcIxI/pesDsY=
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=
github.com/valyala/bytebufferpool v1.0.0/go.mod h1:6bBcMArwyJ5K/AmCkWv1jt77kVWyCJ6HpOuEn7z0Csc=
github.com/valyala/fasttemplate v1.2.1 h1:TVEnxayobAdVkhQfrfes2IzOB6o+z4roRkPF52WA1u4=
Expand Down Expand Up @@ -506,7 +503,6 @@ golang.org/x/tools v0.1.1/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.2/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.3/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.4/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/tools v0.1.5 h1:ouewzE6p+/VEB31YYnTbEJdi8pFqKp4P4n85vwo3DHA=
golang.org/x/tools v0.1.5/go.mod h1:o0xws9oXOQQZyjljx8fwUC0k7L1pTE6eaCbjGeHmOkk=
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
Expand Down
2 changes: 2 additions & 0 deletions services/dictionary.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,9 +98,11 @@ func (d *dictionaryService) GetDictionary(ctx context.Context, url string) ([]mo
}
return data, nil
}

func (d *dictionaryService) GetDetail(ctx context.Context, url string, i int) (string, error) {
return d.getDetail(ctx, url, i)
}

func (d *dictionaryService) getDetail(ctx context.Context, url string, i int) (string, error) {
log.Println("start with goroutine ", i)
defer log.Println("end with goroutine ", i)
Expand Down
15 changes: 7 additions & 8 deletions services/dictionary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,13 @@ import (
"github.com/stretchr/testify/assert"
)

func TestDictionaryService_GetDictionary(t *testing.T) {
InitTest()
BucketSize = 2
ctx := context.Background()
data, err := NewDictionary().GetDictionary(ctx, "https://japanesetest4you.com/jlpt-n2-vocabulary-list/")
fmt.Printf("%+v", data)
assert.Equal(t, nil, err)
}
//func TestDictionaryService_GetDictionary(t *testing.T) {
// BucketSize = 2
// ctx := context.Background()
// data, err := NewDictionary().GetDictionary(ctx, "https://japanesetest4you.com/jlpt-n2-vocabulary-list/")
// fmt.Printf("%+v", data)
// assert.Equal(t, nil, err)
//}

func TestDictHandler_getDetail(t *testing.T) {
ctx := context.Background()
Expand Down
Loading