-
Notifications
You must be signed in to change notification settings - Fork 0
/
check_test.go
83 lines (77 loc) · 2.23 KB
/
check_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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
package resource_test
import (
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
resource "github.com/taylorsilva/static-list-resource"
)
type CheckTestSuite struct {
suite.Suite
*require.Assertions
}
func (c *CheckTestSuite) TestCheck() {
tests := map[string]struct {
Request resource.CheckRequest
ExpectedResponse resource.CheckResponse
ExpctedErr string
}{
"given no version, it should return the first item": {
Request: resource.CheckRequest{
Source: resource.Source{
List: []string{"item1", "item2", "item3", "item4", "item5"},
},
},
ExpectedResponse: resource.CheckResponse{resource.Version{Item: "item1"}},
},
"when given a previouos version it should return nothing": {
Request: resource.CheckRequest{
Source: resource.Source{
List: []string{"item1", "item2", "item3", "item4", "item5"},
},
Version: resource.Version{Item: "item3"},
},
ExpectedResponse: resource.CheckResponse{},
},
"first item in list should be returned if given version is not found": {
Request: resource.CheckRequest{
Source: resource.Source{
List: []string{"item1", "item2", "item3", "item4", "item5"},
},
Version: resource.Version{Item: "item6"},
},
ExpectedResponse: resource.CheckResponse{resource.Version{Item: "item1"}},
},
"return error when source list is empty": {
Request: resource.CheckRequest{
Source: resource.Source{
List: []string{},
},
Version: resource.Version{Item: "item2"},
},
ExpctedErr: "empty list provided in resouce's Source. At least one item required",
},
}
for name, tc := range tests {
c.Run(name, func() {
check := resource.NewCheck()
response, err := check.Run(tc.Request)
if response != nil {
if len(tc.ExpectedResponse) == 0 {
c.Equal(tc.ExpectedResponse, response, name)
} else {
c.Equal(tc.ExpectedResponse[0].Item, response[0].Item, name)
c.NotEqual(time.Time{}, response[0].Date, "time is not nil/default time.Time")
}
}
if tc.ExpctedErr != "" {
c.EqualError(err, tc.ExpctedErr)
} else {
c.NoError(err)
}
})
}
}
func TestCheckSuite(t *testing.T) {
suite.Run(t, &CheckTestSuite{Assertions: require.New(t)})
}