This repository has been archived by the owner on Apr 5, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgocd_test.go
108 lines (91 loc) · 2.56 KB
/
gocd_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
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
package main
import (
"os"
"reflect"
"strings"
"testing"
)
func ErrorContains(out error, want string) bool {
if out == nil {
return want == ""
}
if want == "" {
return false
}
return strings.Contains(out.Error(), want)
}
func TestShouldReturnErrorIfInvalidJsonPassedIn(t *testing.T) {
invalidJSON := []byte(`abc123`)
_, err := parseGocdPipelineComparison(invalidJSON)
if err == nil {
t.Errorf("expected error from parseGocdPipelineComparison: %v", err)
}
if !ErrorContains(err, "cannot create object - invalid json") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestParseInvalidPipelineHistory(t *testing.T) {
invalidJSON := []byte(`abc123`)
_, err := parseGocdPipelineHistory(invalidJSON)
if err == nil {
t.Errorf("expected error from parseGocdPipelineHistory: %v", err)
}
if !ErrorContains(err, "cannot create object - invalid json") {
t.Errorf("unexpected error message: %v", err)
}
}
func TestShouldGetPipelineComparisonFromValidJson(t *testing.T) {
filename := "./sample-data/gocd-pipeline-compare-long.json"
validJSON, err := os.ReadFile(filename)
if err != nil {
t.Errorf("could not read file: %s", err)
}
_, err = parseGocdPipelineComparison(validJSON)
if err != nil {
t.Errorf("unexpected error from parseGocdPipelineComparison: %v", err)
}
}
func TestShouldGetPipelineFromValidJson(t *testing.T) {
filename := "./sample-data/gocd-pipeline-history.json"
validJSON, err := os.ReadFile(filename)
if err != nil {
t.Errorf("could not read file: %s", err)
}
_, err = parseGocdPipelineHistory(validJSON)
if err != nil {
t.Errorf("unexpected error from parseGocdPipelineHistory: %v", err)
}
}
func TestConvertScheduledDateToGo(t *testing.T) {
type test struct {
input int64
want string
}
tests := []test{
{input: 1615391237492, want: "2021-03-10"},
}
for _, tc := range tests {
timestamp := convertGocdTimestampToGo(tc.input)
got := timestamp.Format("2006-01-02")
if !reflect.DeepEqual(tc.want, got) {
t.Fatalf("expected: %v, got: %v", tc.want, got)
}
}
}
func TestConvertScheduledDateFromJSONToGo(t *testing.T) {
// Arrange
filename := "./sample-data/gocd-pipeline-history.json"
validJSON, err := os.ReadFile(filename)
if err != nil {
t.Errorf("could not read file: %s", err)
}
h, _ := parseGocdPipelineHistory(validJSON)
expectedDate := "2021-03-10"
// Act
timestamp := convertGocdTimestampToGo(h.ScheduledDate)
// Assert
actualDate := timestamp.Format("2006-01-02")
if actualDate != expectedDate {
t.Errorf("actual date %s doesn't match the expected date: %s", actualDate, expectedDate)
}
}