-
Notifications
You must be signed in to change notification settings - Fork 8
/
stacks_test.go
74 lines (64 loc) · 2.34 KB
/
stacks_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
package scalingo
import (
"context"
"encoding/json"
"testing"
"time"
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/Scalingo/go-scalingo/v7/http/httpmock"
)
func TestStackIsDeprecated(t *testing.T) {
isDeprecatedCases := map[string]struct {
date time.Time
deprecated bool
}{
"test isDeprecated is false when deprecation date is null": {time.Time{}, false},
"test isDeprecated is true when deprecation date is today's date": {time.Now(), true},
"test isDeprecated is true when deprecation date is in the past": {time.Now().AddDate(0, 0, -1), true},
"test isDeprecated is false when deprecation date is in the future": {time.Now().AddDate(0, 0, 1), false},
}
for message, test := range isDeprecatedCases {
t.Run(message, func(t *testing.T) {
stack := Stack{DeprecatedAt: DeprecationDate{test.date}}
assert.Equal(t, test.deprecated, stack.IsDeprecated())
})
}
}
func TestStacksList(t *testing.T) {
ctx := context.Background()
stacksListCases := map[string]struct {
json string
expectedStack Stack
}{
"test StacksList with stacks with deprecated_at being null": {
json: `{"stacks": [{"deprecated_at": null}]}`,
expectedStack: Stack{DeprecatedAt: DeprecationDate{time.Time{}}},
},
"test StacksList with stacks with deprecated_at is empty": {
json: `{"stacks": [{}]}`,
expectedStack: Stack{DeprecatedAt: DeprecationDate{time.Time{}}},
},
"test StacksList with stacks with deprecated_at being set": {
json: `{"stacks": [{"deprecated_at": "2022-08-31"}]}`,
expectedStack: Stack{DeprecatedAt: DeprecationDate{time.Date(2022, 8, 31, 0, 0, 0, 0, time.UTC)}},
},
}
for message, test := range stacksListCases {
t.Run(message, func(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()
client, err := New(ctx, ClientConfig{})
require.NoError(t, err)
apiMock := httpmock.NewMockClient(ctrl)
client.apiClient = apiMock
apiMock.EXPECT().DoRequest(gomock.Any(), gomock.Any(), gomock.Any()).Do(func(_ context.Context, _, res interface{}) {
err := json.Unmarshal([]byte(test.json), &res)
require.NoError(t, err)
}).Return(nil)
stacks, _ := client.StacksList(ctx)
assert.Equal(t, test.expectedStack.DeprecatedAt, stacks[0].DeprecatedAt)
})
}
}