forked from joefitzgerald/forecast
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathassignment_test.go
100 lines (86 loc) · 2.46 KB
/
assignment_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
package forecast_test
import (
"fmt"
"net/http"
"net/http/httptest"
. "github.com/joefitzgerald/forecast"
. "github.com/onsi/ginkgo"
. "github.com/onsi/gomega"
)
var _ = Describe("Assignment", func() {
Context("Assignments()", func() {
var (
server *httptest.Server
api *API
)
AfterEach(func() {
api = nil
if server == nil {
return
}
server.Close()
server = nil
})
Context("when a response is returned from the server", func() {
BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := ReadFile("assignments.json")
fmt.Fprintf(w, "%s", response)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusOK)
}))
api = New(server.URL, "test-token", "987654")
})
It("should return assignments and a nil error", func() {
assignments, err := api.Assignments()
Ω(assignments).ShouldNot(BeNil())
Ω(err).ShouldNot(HaveOccurred())
})
})
Context("when an error is returned from the server", func() {
BeforeEach(func() {
server = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
response := "error"
fmt.Fprintf(w, "%s", response)
w.Header().Set("Content-Type", "application/json; charset=utf-8")
w.WriteHeader(http.StatusBadRequest)
}))
api = New(server.URL, "test-token", "987654")
})
It("should return an error", func() {
assignments, err := api.Assignments()
Ω(assignments).Should(BeNil())
Ω(err).Should(HaveOccurred())
})
})
})
Context("Weekdays()", func() {
doTestAssignmentDays := func(start string, end string, expected int) {
assignment := Assignment{
StartDate: start,
EndDate: end,
}
actual := assignment.Weekdays()
Ω(actual).Should(Equal(expected))
}
It("calculates assignment weekdays appropriately", func() {
doTestAssignmentDays("2017-06-20", "2017-07-01", 9)
doTestAssignmentDays("2017-06-20", "2017-06-20", 1)
doTestAssignmentDays("2017-06-20", "2017-06-21", 2)
})
It("returns an error when an invalid date is supplied", func() {
assignment := Assignment{
StartDate: "-------%^#$@",
EndDate: "",
}
actual := assignment.Weekdays()
Ω(actual).Should(Equal(0))
assignment = Assignment{
StartDate: "2017-06-20",
EndDate: "-------%^#$@",
}
actual = assignment.Weekdays()
Ω(actual).Should(Equal(0))
})
})
})