Skip to content

Commit 3f2feeb

Browse files
committed
fix(test): fix time comparison error in test with go 1.9
1 parent d5e4555 commit 3f2feeb

File tree

5 files changed

+29
-17
lines changed

5 files changed

+29
-17
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
# Changelog
22
## v0.0.2
3+
- 17/11/03 fix(test): fix time comparison error in test with go 1.9 (SFR)
34
- 17/10/05 fix(test): enhance web test errors on full server test (SFR)
45
refact(model): refact model constructor
56
fix(dao): fix error name

dao/task-dao-mongo.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ import (
1313
var _ TaskDAO = (*TaskDAOMongo)(nil)
1414

1515
var (
16+
// ErrInvalidUUID is used on invalid UUID number
1617
ErrInvalidUUID = errors.New("invalid input to UUID")
1718
)
1819

model/task.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,3 +49,14 @@ func NewTask() *Task {
4949
Priority: PriorityMedium,
5050
}
5151
}
52+
53+
// Equal compares a Task to another and returns true if they are equal false otherwise
54+
func (t Task) Equal(task Task) bool {
55+
return t.ID == task.ID &&
56+
t.Title == task.Title &&
57+
t.Description == task.Description &&
58+
t.Status == task.Status &&
59+
t.Priority == task.Priority &&
60+
t.CreationDate.Equal(task.CreationDate) &&
61+
t.DueDate.Equal(task.DueDate)
62+
}

web/controller.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ func (sh *TaskController) Update(w http.ResponseWriter, r *http.Request) {
161161
err := GetJSONContent(task, r)
162162

163163
if err != nil {
164-
logger.WithField("error", err).Warn("unable to decode task to create")
164+
logger.WithField("error", err).Warn("unable to decode task to update")
165165
SendJSONError(w, "Error while decoding task to create", http.StatusBadRequest)
166166
return
167167
}

web/web_test.go

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,6 @@ import (
1212
"time"
1313
)
1414

15-
func init() {
16-
// set timezone as UTC for bson/json time marshalling
17-
time.Local = time.UTC
18-
}
19-
2015
// This test is composed by several subtests and uses httptest.ResponseRecorder type to record http response.
2116
// These are NOT end-to-end tests as we are directly calling the controller methods we want to test.
2217
func TestTaskControllerGet(t *testing.T) {
@@ -48,7 +43,11 @@ func TestTaskControllerGet(t *testing.T) {
4843
t.Errorf("Wrong response code. Got %d instead of %d.", res.Code, http.StatusOK)
4944
}
5045

51-
if dao.MockedTask != taskOut[0] {
46+
if len(taskOut) < 1 {
47+
t.Fatal("Wrong result size < 1")
48+
}
49+
50+
if !dao.MockedTask.Equal(taskOut[0]) {
5251
t.Errorf("Expected different from %v output %v", dao.MockedTask, taskOut[0])
5352
}
5453
})
@@ -78,7 +77,7 @@ func TestTaskControllerGet(t *testing.T) {
7877
t.Errorf("Wrong response code. Got %d instead of %d.", res.Code, http.StatusOK)
7978
}
8079

81-
if dao.MockedTask != taskOut {
80+
if !dao.MockedTask.Equal(taskOut) {
8281
t.Errorf("Expected different from %v output %v", dao.MockedTask, taskOut)
8382
}
8483
})
@@ -116,7 +115,7 @@ func TestTaskControllerGet(t *testing.T) {
116115
}
117116

118117
task.ID = taskOut.ID
119-
if task != taskOut {
118+
if !task.Equal(taskOut) {
120119
t.Errorf("Expected different from %v output %v", task, taskOut)
121120
}
122121
})
@@ -167,7 +166,7 @@ func TestTaskControllerGetServer(t *testing.T) {
167166
}
168167

169168
task.ID = taskTest.ID
170-
if task != taskTest {
169+
if !task.Equal(taskTest) {
171170
t.Errorf("Expected different from %v output %v", task, taskTest)
172171
}
173172
})
@@ -190,11 +189,11 @@ func TestTaskControllerGetServer(t *testing.T) {
190189
}
191190

192191
if len(resTask) < 1 {
193-
t.Errorf("Expected length different from %v output 1", len(resTask))
194-
} else {
195-
if resTask[0] != taskTest {
196-
t.Errorf("Expected different from %v output %v", resTask[0], taskTest)
197-
}
192+
t.Fatal("Wrong result size < 1")
193+
}
194+
195+
if !resTask[0].Equal(taskTest) {
196+
t.Errorf("Expected different from %v output %v", resTask[0], taskTest)
198197
}
199198
})
200199

@@ -215,7 +214,7 @@ func TestTaskControllerGetServer(t *testing.T) {
215214
t.Errorf("Wrong response code. Got %d instead of %d.", res.StatusCode, http.StatusOK)
216215
}
217216

218-
if resTask != taskTest {
217+
if !resTask.Equal(taskTest) {
219218
t.Errorf("Expected different from %v output %v", resTask, taskTest)
220219
}
221220
})
@@ -229,7 +228,7 @@ func BenchmarkTaskControllerGet(b *testing.B) {
229228
handler := NewTaskController(daoMock)
230229

231230
// build a request
232-
req, err := http.NewRequest("GET", "localhost/tasks", nil)
231+
req, err := http.NewRequest(http.MethodGet, "localhost/tasks", nil)
233232
if err != nil {
234233
b.Fatal(err)
235234
}

0 commit comments

Comments
 (0)