Skip to content

Commit

Permalink
minor: rafactoring and adding some tests
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoaraujo committed Oct 7, 2023
1 parent b409e8f commit 9caeb85
Show file tree
Hide file tree
Showing 11 changed files with 390 additions and 14 deletions.
9 changes: 9 additions & 0 deletions .mockery.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
with-expecter: true
packages:
renatoaraujo/uk-visa-sponsors/internal/sponsors:
config:
all: True
dir: "{{.InterfaceDirRelative}}/mocks"
outpkg: "mocks_test"
filename: "{{.InterfaceName}}.go"
mockname: "{{.InterfaceName}}"
5 changes: 4 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
.PHONY: build

build:
go build -o sponsors
go build -o sponsors ./cmd/cli/main.go

test:
go test -race -v ./...

help: build
@./sponsors help
Expand Down
4 changes: 2 additions & 2 deletions cmd/find.go → cmd/cli/commands/find.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package commands

import (
"fmt"
Expand Down Expand Up @@ -70,6 +70,6 @@ func init() {
findCmd.Flags().StringVarP(&role, "role", "r", "software engineering", "Role that you're interested.")
err := findCmd.MarkFlagRequired("company")
if err != nil {
log.Fatalf("failed to initialise; %w", err)
log.Fatalf("failed to initialise; %s", err)
}
}
2 changes: 1 addition & 1 deletion cmd/root.go → cmd/cli/commands/root.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package cmd
package commands

import (
"os"
Expand Down
9 changes: 9 additions & 0 deletions cmd/cli/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package main

import (
"renatoaraujo/uk-visa-sponsors/cmd/cli/commands"
)

func main() {
commands.Execute()
}
7 changes: 6 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,19 @@ go 1.21.1

require (
github.com/fatih/color v1.15.0
github.com/sashabaranov/go-openai v1.15.4
github.com/spf13/cobra v1.7.0
github.com/stretchr/testify v1.8.4
)

require (
github.com/davecgh/go-spew v1.1.1 // indirect
github.com/inconshreveable/mousetrap v1.1.0 // indirect
github.com/mattn/go-colorable v0.1.13 // indirect
github.com/mattn/go-isatty v0.0.17 // indirect
github.com/sashabaranov/go-openai v1.15.4 // indirect
github.com/pmezard/go-difflib v1.0.0 // indirect
github.com/spf13/pflag v1.0.5 // indirect
github.com/stretchr/objx v0.5.0 // indirect
golang.org/x/sys v0.6.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
16 changes: 16 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
@@ -1,4 +1,7 @@
github.com/cpuguy83/go-md2man/v2 v2.0.2/go.mod h1:tgQtvFlXSQOSOSIRvRPT7W67SCa46tRHOmNcaadrF8o=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/fatih/color v1.15.0 h1:kOqh6YHBtK8aywxGerMG2Eq3H6Qgoqeo13Bk2Mv/nBs=
github.com/fatih/color v1.15.0/go.mod h1:0h5ZqXfHYED7Bhv2ZJamyIOUej9KtShiJESRwBDUSsw=
github.com/inconshreveable/mousetrap v1.1.0 h1:wN+x4NVGpMsO7ErUn/mUI3vEoE6Jt13X2s0bqwp9tc8=
Expand All @@ -8,15 +11,28 @@ github.com/mattn/go-colorable v0.1.13/go.mod h1:7S9/ev0klgBDR4GtXTXX8a3vIGJpMovk
github.com/mattn/go-isatty v0.0.16/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/mattn/go-isatty v0.0.17 h1:BTarxUcIeDqL27Mc+vyvdWYSL28zpIhv3RoTdsLMPng=
github.com/mattn/go-isatty v0.0.17/go.mod h1:kYGgaQfpe5nmfYZH+SKPsOc2e4SrIfOl2e/yFXSvRLM=
github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM=
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/russross/blackfriday/v2 v2.1.0/go.mod h1:+Rmxgy9KzJVeS9/2gXHxylqXiyQDYRxCVz55jmeOWTM=
github.com/sashabaranov/go-openai v1.15.4 h1:BXCR0Uxk5RipeY4yBC7g6pBVfcjh8jwrMNOYdie6yuk=
github.com/sashabaranov/go-openai v1.15.4/go.mod h1:lj5b/K+zjTSFxVLijLSTDZuP7adOgerWeFyZLUhAKRg=
github.com/spf13/cobra v1.7.0 h1:hyqWnYt1ZQShIddO5kBpj3vu05/++x6tJ6dg8EC572I=
github.com/spf13/cobra v1.7.0/go.mod h1:uLxZILRyS/50WlhOIKD7W6V5bgeIt+4sICxh6uRMrb0=
github.com/spf13/pflag v1.0.5 h1:iy+VFUOCP1a+8yFto/drg2CJ5u0yRoB7fZw3DKv/JXA=
github.com/spf13/pflag v1.0.5/go.mod h1:McXfInJRrz4CZXVZOBLb0bTZqETkiAhM9Iw0y3An2Bg=
github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME=
github.com/stretchr/objx v0.4.0/go.mod h1:YvHI0jy2hoMjB+UWwv71VJQ9isScKT/TqJzVSSt89Yw=
github.com/stretchr/objx v0.5.0 h1:1zr/of2m5FGMsad5YfcqgdqdWrIhu+EBEJRhR1U7z/c=
github.com/stretchr/objx v0.5.0/go.mod h1:Yh+to48EsGEfYuaHDzXPcE3xhTkx73EhmCGUpEOglKo=
github.com/stretchr/testify v1.7.1/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
github.com/stretchr/testify v1.8.0/go.mod h1:yNjHg4UonilssWZ8iaSj1OCr/vHnekPRkoO+kdMU+MU=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
golang.org/x/sys v0.0.0-20220811171246-fbc7d0a398ab/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
golang.org/x/sys v0.6.0 h1:MVltZSvRTcU2ljQOhs94SXPftV6DCNnZViHeQps87pQ=
golang.org/x/sys v0.6.0/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.0-20200313102051-9f266ea9e77c/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=
120 changes: 120 additions & 0 deletions internal/sponsors/handler_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,120 @@
package sponsors_test

import (
"errors"
"testing"

"renatoaraujo/uk-visa-sponsors/internal/sponsors"
mocks "renatoaraujo/uk-visa-sponsors/internal/sponsors/mocks"

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

func TestNewHandler(t *testing.T) {
tests := []struct {
name string
load bool
fetcherSetup func(client *mocks.Fetcher)
processorSetup func(client *mocks.Processor)
wantErr bool
}{
{
name: "Successfully initialise Handler without preloading data",
load: false,
wantErr: false,
},
{
name: "Successfully to initialise Handler preloading data",
load: true,
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchDataSource").Return(
"datasource",
nil,
)
client.On("FetchData", mock.Anything).Return(
[]byte("some primitive data"),
nil,
)
},
processorSetup: func(client *mocks.Processor) {
client.On("ProcessRawData", mock.Anything).Return(
[]map[string]string{
{
"Organisation Name": "Awesome company",
"Route": "Skilled Worker Visa",
},
},
nil,
)
},
wantErr: false,
},
{
name: "Failed to initialise Handler preloading due to failure on fetching data",
load: true,
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchDataSource").Return(
"datasource",
nil,
)
client.On("FetchData", mock.Anything).Return(
nil,
errors.New("failed to fetch data"),
)
},
wantErr: true,
},
{
name: "Failed to initialise Handler preloading due to failure on processing raw data",
load: true,
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchDataSource").Return(
"datasource",
nil,
)
client.On("FetchData", mock.Anything).Return(
[]byte("some primitive data"),
nil,
)
},
processorSetup: func(client *mocks.Processor) {
client.On("ProcessRawData", mock.Anything).Return(
nil,
errors.New("failed to process data"),
)
},
wantErr: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {

fetcherMock := &mocks.Fetcher{}
if tt.fetcherSetup != nil {
tt.fetcherSetup(fetcherMock)
}

processorMock := &mocks.Processor{}
if tt.processorSetup != nil {
tt.processorSetup(processorMock)
}

handler, err := sponsors.NewHandler(fetcherMock, processorMock, tt.load)
if tt.wantErr {
require.Error(t, err)
} else {
require.NoError(t, err)
}

if handler != nil {
assert.IsType(t, &sponsors.Handler{}, handler)
}

mock.AssertExpectationsForObjects(t, fetcherMock)
mock.AssertExpectationsForObjects(t, processorMock)
})
}
}
137 changes: 137 additions & 0 deletions internal/sponsors/mocks/Fetcher.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 9caeb85

Please sign in to comment.