This repository has been archived by the owner on Jun 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhandlers_test.go
144 lines (118 loc) · 3.21 KB
/
handlers_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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"encoding/json"
"github.com/stretchr/testify/assert"
"github.com/thomaspaulin/snc-server-go/mock"
"github.com/thomaspaulin/snc-server-go/snc"
"net/http"
"net/http/httptest"
"os"
"testing"
"time"
)
// https://github.com/gin-gonic/gin/issues/55
// and
// https://github.com/gin-gonic/gin/pull/37/files
// for writing tests in Gin
func TestMain(m *testing.M) {
// do the setup
var retCode int
// run the tests
retCode = m.Run()
// do the tear down
os.Exit(retCode)
}
func TestGetMatches(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/v1/matches", nil)
matches := make([]*snc.Match, 2)
matches[0] = &snc.Match{
ID: 1,
Start: time.Date(2017, 4, 15, 4, 30, 0, 0, time.UTC),
Division: "C",
Season: 2017,
Status: snc.Over,
Away: "Bears",
Home: "Tigers",
AwayScore: 3,
HomeScore: 1,
Rink: "Botany"}
s := Services{}
ms := mock.MatchService{}
ms.MatchesFn = func() ([]*snc.Match, error) {
return matches, nil
}
s.MatchService = &ms
// this means the entire API has to be created again for each test :(
r := APIEngine(s)
r.ServeHTTP(w, req)
assert.True(t, ms.MatchesInvoked)
assert.Equal(t, 200, w.Code)
actual := make([]*snc.Match, 1)
json.NewDecoder(w.Body).Decode(&actual)
// todo is this looking at memory address or contents?
assert.Equal(t, matches, actual)
}
func TestGetTeams(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/v1/teams", nil)
teams := make([]*snc.Team, 2)
teams[0] = &snc.Team{ID: 1, Division: "C", Name: "Bears"}
teams[1] = &snc.Team{ID: 2, Division: "C", Name: "Hawks"}
s := Services{}
ts := mock.TeamService{}
ts.TeamsFn = func() ([]*snc.Team, error) {
return teams, nil
}
s.TeamService = &ts
// this means the entire API has to be created again for each test :(
r := APIEngine(s)
r.ServeHTTP(w, req)
assert.True(t, ts.TeamsInvoked)
assert.Equal(t, 200, w.Code)
actual := make([]*snc.Team, 2)
json.NewDecoder(w.Body).Decode(&actual)
// todo is this looking at memory address or contents?
assert.Equal(t, teams, actual)
}
func TestGetSpecificRink(t *testing.T) {
w := httptest.NewRecorder()
req, _ := http.NewRequest("GET", "/v1/teams/1", nil)
team := snc.Team{ID: 1, Division: "C", Name: "Bears"}
s := Services{}
ts := mock.TeamService{}
ts.TeamFn = func(id int) (*snc.Team, error) {
assert.Equal(t, id, 1)
return &team, nil
}
s.TeamService = &ts
// this means the entire API has to be created again for each test :(
r := APIEngine(s)
r.ServeHTTP(w, req)
assert.True(t, ts.TeamInvoked)
assert.Equal(t, 200, w.Code)
actual := snc.Team{}
json.NewDecoder(w.Body).Decode(&actual)
assert.Equal(t, team, actual)
}
// todo uncomment when team creation is supported
//func TestCreateTeam(t *testing.T) {
// team := snc.Team{Division: "C", Name: "Lions"}
//
// w := httptest.NewRecorder()
// j, _ := json.Marshal(team)
// req := httptest.NewRequest("POST", "/v1/teams", bytes.NewBuffer(j))
//
// s := Services{}
// ts := mock.TeamService{}
// ts.CreateFn = func(t *snc.Team) error {
// return nil
// }
//
// s.TeamService = &ts
// r := APIEngine(s)
// r.ServeHTTP(w, req)
//
// assert.True(t, ts.CreateInvoked)
// assert.Equal(t, 200, w.Code)
//}