Skip to content

Commit

Permalink
minor: improve tests and validations
Browse files Browse the repository at this point in the history
  • Loading branch information
renatoaraujo committed Oct 7, 2023
1 parent a50f169 commit 02226e8
Show file tree
Hide file tree
Showing 2 changed files with 163 additions and 0 deletions.
5 changes: 5 additions & 0 deletions internal/sponsors/handler.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package sponsors

import (
"errors"
"fmt"
)

Expand Down Expand Up @@ -57,6 +58,10 @@ func (h *Handler) Load(dataSource string) error {

// TODO: configure the dynamic header mapper
for _, entry := range processedData {
if len(entry["Organisation Name"]) <= 0 || len(entry["Route"]) <= 0 {
return errors.New("incompatible data, expecting headers `Organisation Name` and `Route`")
}

h.Organisations.AddOrUpdateVisaType(entry["Organisation Name"], entry["Route"])
}
}
Expand Down
158 changes: 158 additions & 0 deletions internal/sponsors/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,3 +121,161 @@ func TestNewHandler(t *testing.T) {
})
}
}

func TestHandler_Load(t *testing.T) {
tests := []struct {
name string
datasource string
fetcherSetup func(client *mocks.Fetcher)
processorSetup func(client *mocks.Processor)
wantEmptyOrgs bool
wantErr bool
}{
{
name: "Successfully load data using default datasource",
datasource: "",
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchDataSource").Once().Return(
"datasource",
nil,
)
client.On("FetchData", mock.Anything).Once().Return(
[]byte(`"Organisation Name", "Town/City", "County", "Type & Rating", "Route"
"Awesome company", "London", "United Kingdom", "Worker (A rating)", "Skilled Worker"
`),
nil,
)
},
processorSetup: func(client *mocks.Processor) {
client.On("ProcessRawData", mock.Anything).Once().Return(
[]map[string]string{
{
"Organisation Name": "Awesome company",
"Route": "Skilled Worker",
},
},
nil,
)
},
wantErr: false,
},
{
name: "Successfully load data using custom datasource",
datasource: "custom_datasource",
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchData", mock.Anything).Once().Return(
[]byte(`"Organisation Name", "Town/City", "County", "Type & Rating", "Route"
"Awesome company", "London", "United Kingdom", "Worker (A rating)", "Skilled Worker"
`),
nil,
)
},
processorSetup: func(client *mocks.Processor) {
client.On("ProcessRawData", mock.Anything).Once().Return(
[]map[string]string{
{
"Organisation Name": "Awesome company",
"Route": "Skilled Worker",
},
},
nil,
)
},
wantErr: false,
},
{
name: "Successfully load data using custom datasource, but it's empty",
datasource: "custom_datasource",
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchData", mock.Anything).Once().Return(
[]byte(``),
nil,
)
},
processorSetup: func(client *mocks.Processor) {
client.On("ProcessRawData", mock.Anything).Once().Return(
[]map[string]string{},
nil,
)
},
wantEmptyOrgs: true,
wantErr: false,
},
{
name: "Failed to load due to failure to fetch datasource",
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchDataSource").Once().Return(
"",
errors.New("failed to fetch datasource"),
)
},
wantErr: true,
},
{
name: "Failed to load due to failure to fetch data from datasource",
datasource: "custom_datasource",
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchData", mock.Anything).Once().Return(
[]byte(""),
errors.New("failed to fetch datasource"),
)
},
wantErr: true,
},
{
name: "Failed to process data due to incompatibility",
datasource: "custom_datasource",
fetcherSetup: func(client *mocks.Fetcher) {
client.On("FetchData", mock.Anything).Once().Return(
[]byte(""),
nil,
)
},
processorSetup: func(client *mocks.Processor) {
client.On("ProcessRawData", mock.Anything).Once().Return(
[]map[string]string{
{
"OrganisationName": "Awesome company",
"Route": "Skilled Worker",
},
},
nil,
)
},
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, false)
require.NoError(t, err)

err = handler.Load(tt.datasource)
if tt.wantErr {
require.Error(t, err)
require.Empty(t, handler.Organisations.List())
} else {
require.NoError(t, err)

if tt.wantEmptyOrgs {
require.Empty(t, handler.Organisations.List())
}
}

mock.AssertExpectationsForObjects(t, fetcherMock)
mock.AssertExpectationsForObjects(t, processorMock)
})
}

}

0 comments on commit 02226e8

Please sign in to comment.