-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdevops_test.go
More file actions
101 lines (95 loc) · 2.41 KB
/
devops_test.go
File metadata and controls
101 lines (95 loc) · 2.41 KB
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
package main
import (
"testing"
"time"
"reflect"
"encoding/json"
)
var isValidUserTest = []struct{
name string
expected bool
}{
{"joel", true},
{"alice", true},
{"charles2", false},
{"89monica", false},
}
func Test_validateUser(t *testing.T) {
for _, u := range isValidUserTest {
t.Run(u.name, func(t *testing.T) {
got := validateUser(u.name)
if got != u.expected {
t.Errorf("Expected: %v, got %v", u.expected, got)
}
})
}
}
var isValidDateTest = []struct{
date string
expected string
}{
{"2023-05-25", ""},
{"2023-15-25", "parsing time \"2023-15-25\": month out of range"},
{"2023-05-40", "parsing time \"2023-05-40\": day out of range"},
}
func Test_validateDate(t *testing.T) {
for _, d := range isValidDateTest {
t.Run(d.date, func(t *testing.T) {
err := validateDate(d.date)
var errMsg string
if err != nil {
errMsg = err.Error()
}
if errMsg != d.expected {
t.Errorf("Expected error message %s, got %s", d.expected, errMsg)
}
})
}
}
var convertStringToDateTest = []struct{
date string
expected string
}{
{"1982-05-25", ""},
{"Monday, 02-Jan-06 15:04:05 MST", "parsing time \"Monday, 02-Jan-06 15:04:05 MST\" as \"2006-01-02\": cannot parse \"Monday, 02-Jan-06 15:04:05 MST\" as \"2006\""},
}
func Test_convertStringToDate(t *testing.T) {
for _, d := range convertStringToDateTest {
t.Run(d.date, func(t *testing.T) {
tt, err := convertStringToDate(d.date)
if reflect.TypeOf(tt) != reflect.TypeOf(time.Time{}) {
t.Errorf("Return value type expected: time.Time, got %T", reflect.TypeOf(tt))
}
var errMsg string
if err != nil {
errMsg = err.Error()
}
if errMsg != d.expected {
t.Errorf("Expected error message %s, got %s", d.expected, errMsg)
}
})
}
}
var isValidJSONTest = []struct{
message string
jsonMessage string
}{
{"Hello, John! Your birthday is in 7 day(s)","{\"message\":\"Hello, John! Your birthday is in 7 day(s)\"}"},
{"Hello, John! Happy birthday!","{\"message\":\"Hello, John! Happy birthday!\"}"},
}
func Test_sendJSONResponse(t *testing.T) {
for _, r := range isValidJSONTest {
t.Run(r.message, func(t *testing.T) {
resp := make(map[string]string)
resp["message"] = r.message
jsonResp, err := json.Marshal(resp)
if err == nil {
if (string(jsonResp) != string(r.jsonMessage)) {
t.Errorf("Expected %s, got %s", string(r.jsonMessage), jsonResp)
}
} else {
t.Errorf("Got %v", err)
}
})
}
}