generated from UCLALibrary/service-template
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvalidator_test.go
65 lines (54 loc) · 2.07 KB
/
validator_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
//go:build unit
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
)
var location = CsvLocation{RowIndex: 2, ColIndex: 5}
var csvData = [][]string{
{"header0", "header1", "header2", "header3", "header4", "header5"},
{"value10", "value11", "value12", "value13", "value14", "value15"},
{"value20", "value21", "value22", "value23", "value24", "value25"},
}
// MockValidator implements the Validator interface so it can be tested.
type MockValidator struct {
ValidateFunc func(profile string, location CsvLocation, csvData [][]string) error
}
// Validate forwards the method call to the function stored in ValidateFunc, passing along the input arguments and
// returning the possible error.
func (m *MockValidator) Validate(profile string, location CsvLocation, csvData [][]string) error {
return m.ValidateFunc(profile, location, csvData)
}
// TestValidatorSuccess tests the Validate interface with a mock validator and expects a successful result.
func TestValidatorSuccess(t *testing.T) {
mock := &MockValidator{
ValidateFunc: func(profile string, location CsvLocation, csvData [][]string) error {
assert.Equal(t, "profile1", profile)
assert.Equal(t, "value24", csvData[2][4])
return nil
},
}
err := mock.Validate("profile1", location, csvData)
if err != nil {
t.Errorf("Expected no error, but got %v", err)
}
}
// TestValidatorError tests the Validate interface with a mock validator and expects an error.
func TestValidatorError(t *testing.T) {
mock := &MockValidator{
ValidateFunc: func(profile string, location CsvLocation, csvData [][]string) error {
if profile != "profile1" {
return fmt.Errorf("Expected 'profile1' as profile, but found '%s'", profile)
}
return nil
},
}
// We pass 'profile2' instead of the expected 'profile1'
err := mock.Validate("profile2", location, csvData)
if err != nil {
// We're expecting an error, but check its message just to confirm it's the one we're expecting
assert.EqualError(t, err, "Expected 'profile1' as profile, but found 'profile2'",
"Expected error message does not match")
}
}