|
1 | 1 | package server
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "bytes" |
5 |
| - "encoding/json" |
6 |
| - "net/http" |
7 |
| - "net/http/httptest" |
8 |
| - "testing" |
| 4 | + "bytes" |
| 5 | + "encoding/json" |
| 6 | + "net/http" |
| 7 | + "net/http/httptest" |
| 8 | + "testing" |
9 | 9 |
|
10 |
| - "zendo/models" |
| 10 | + "zendo/models" |
11 | 11 | )
|
12 | 12 |
|
13 | 13 | // MockDB is a mock database implementing the necessary methods for testing
|
14 | 14 | type MockDB struct{}
|
15 | 15 |
|
16 | 16 | func (db *MockDB) Health() map[string]string {
|
17 |
| - return map[string]string{"status": "healthy"} |
| 17 | + return map[string]string{"status": "healthy"} |
18 | 18 | }
|
19 | 19 |
|
20 | 20 | func (db *MockDB) Close() error {
|
21 |
| - // Mock close method |
22 |
| - return nil |
| 21 | + // Mock close method |
| 22 | + return nil |
23 | 23 | }
|
24 | 24 |
|
25 | 25 | func (db *MockDB) GetTodos() ([]models.Todo, error) {
|
26 |
| - return []models.Todo{ |
27 |
| - {Id: 1, Description: "Test Todo 1", Status: 0}, |
28 |
| - {Id: 2, Description: "Test Todo 2", Status: 1}, |
29 |
| - }, nil |
| 26 | + return []models.Todo{ |
| 27 | + {Id: 1, Description: "Test Todo 1", Status: 0}, |
| 28 | + {Id: 2, Description: "Test Todo 2", Status: 1}, |
| 29 | + }, nil |
30 | 30 | }
|
31 | 31 |
|
32 | 32 | func (db *MockDB) GetTodo(id int64) (models.Todo, error) {
|
33 |
| - return models.Todo{Id: id, Description: "Test Todo", Status: 0}, nil |
| 33 | + return models.Todo{Id: id, Description: "Test Todo", Status: 0}, nil |
34 | 34 | }
|
35 | 35 |
|
36 | 36 | func (db *MockDB) DeleteTodo(id int64) (int64, error) {
|
37 |
| - return id, nil |
| 37 | + return id, nil |
38 | 38 | }
|
39 | 39 |
|
40 | 40 | func (db *MockDB) InsertTodo(todo *models.Todo) error {
|
41 |
| - return nil |
| 41 | + return nil |
42 | 42 | }
|
43 | 43 |
|
44 | 44 | func (db *MockDB) UpdateTodo(todo *models.Todo) error {
|
45 |
| - return nil |
| 45 | + return nil |
46 | 46 | }
|
47 | 47 |
|
48 | 48 | // TestHandleGetTodos tests the handleGetTodos function
|
49 | 49 | func TestHandleGetTodos(t *testing.T) {
|
50 |
| - mockDB := &MockDB{} |
51 |
| - server := &Server{Port: 8080, db: mockDB} |
| 50 | + mockDB := &MockDB{} |
| 51 | + server := &Server{Port: 8080, db: mockDB} |
52 | 52 |
|
53 |
| - req, err := http.NewRequest("GET", "/todos", nil) |
54 |
| - if err != nil { |
55 |
| - t.Fatal(err) |
56 |
| - } |
| 53 | + req, err := http.NewRequest("GET", "/todos", nil) |
| 54 | + if err != nil { |
| 55 | + t.Fatal(err) |
| 56 | + } |
57 | 57 |
|
58 |
| - rr := httptest.NewRecorder() |
59 |
| - handler := http.HandlerFunc(server.handleGetTodos) |
| 58 | + rr := httptest.NewRecorder() |
| 59 | + handler := http.HandlerFunc(server.handleGetTodos) |
60 | 60 |
|
61 |
| - handler.ServeHTTP(rr, req) |
| 61 | + handler.ServeHTTP(rr, req) |
62 | 62 |
|
63 |
| - if status := rr.Code; status != http.StatusOK { |
64 |
| - t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) |
65 |
| - } |
| 63 | + if status := rr.Code; status != http.StatusOK { |
| 64 | + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) |
| 65 | + } |
66 | 66 |
|
67 |
| - expectedTodos := []models.Todo{ |
68 |
| - {Id: 1, Description: "Test Todo 1", Status: 0}, |
69 |
| - {Id: 2, Description: "Test Todo 2", Status: 1}, |
70 |
| - } |
71 |
| - expectedResponse, _ := json.Marshal(expectedTodos) |
| 67 | + expectedTodos := []models.Todo{ |
| 68 | + {Id: 1, Description: "Test Todo 1", Status: 0}, |
| 69 | + {Id: 2, Description: "Test Todo 2", Status: 1}, |
| 70 | + } |
| 71 | + expectedResponse, _ := json.Marshal(expectedTodos) |
72 | 72 |
|
73 |
| - if rr.Body.String() != string(expectedResponse) { |
74 |
| - t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), string(expectedResponse)) |
75 |
| - } |
| 73 | + if rr.Body.String() != string(expectedResponse) { |
| 74 | + t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), string(expectedResponse)) |
| 75 | + } |
76 | 76 | }
|
77 | 77 |
|
78 | 78 | // TestHandleGetTodo tests the handleGetTodo function
|
| 79 | +// Todo: refactor stuff; do not want to use a real server in test |
| 80 | +// but httptest. Maybe: https://ieftimov.com/posts/testing-in-go-testing-http-servers/ |
| 81 | +// is useful |
79 | 82 | func TestHandleGetTodo(t *testing.T) {
|
80 |
| - mockDB := &MockDB{} |
81 |
| - server := &Server{Port: 8080, db: mockDB} |
| 83 | + mockDB := &MockDB{} |
| 84 | + server := &Server{Port: 8080, db: mockDB} |
82 | 85 |
|
83 |
| - req, err := http.NewRequest("GET", "/todos/1", nil) |
84 |
| - if err != nil { |
85 |
| - t.Fatal(err) |
86 |
| - } |
| 86 | + httpServer := &http.Server{ |
| 87 | + Handler: server.RegisterRoutes(), |
| 88 | + } |
87 | 89 |
|
88 |
| - rr := httptest.NewRecorder() |
89 |
| - handler := http.HandlerFunc(server.handleGetTodo) |
| 90 | + req, err := http.NewRequest("GET", "/todos/1", nil) |
| 91 | + if err != nil { |
| 92 | + t.Fatal(err) |
| 93 | + } |
90 | 94 |
|
91 |
| - handler.ServeHTTP(rr, req) |
| 95 | + rr := httptest.NewRecorder() |
92 | 96 |
|
93 |
| - if status := rr.Code; status != http.StatusOK { |
94 |
| - t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) |
95 |
| - } |
| 97 | + httpServer.Handler.ServeHTTP(rr, req) |
96 | 98 |
|
97 |
| - expectedTodo := models.Todo{Id: 1, Description: "Test Todo", Status: 0} |
98 |
| - expectedResponse, _ := json.Marshal(expectedTodo) |
| 99 | + if status := rr.Code; status != http.StatusOK { |
| 100 | + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) |
| 101 | + } |
99 | 102 |
|
100 |
| - if rr.Body.String() != string(expectedResponse) { |
101 |
| - t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), string(expectedResponse)) |
102 |
| - } |
| 103 | + expectedTodo := models.Todo{Id: 1, Description: "Test Todo", Status: 0} |
| 104 | + expectedResponse, _ := json.Marshal(expectedTodo) |
| 105 | + |
| 106 | + if rr.Body.String() != string(expectedResponse) { |
| 107 | + t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), string(expectedResponse)) |
| 108 | + } |
103 | 109 | }
|
104 | 110 |
|
105 | 111 | func TestHandleAddTodo(t *testing.T) {
|
106 |
| - mockDB := &MockDB{} |
107 |
| - server := &Server{Port: 8080, db: mockDB} |
108 |
| - |
109 |
| - newTodo := models.Todo{Description: "New Test Todo", Status: 0} |
110 |
| - todoJSON, _ := json.Marshal(newTodo) |
111 |
| - req, err := http.NewRequest("POST", "/todos", bytes.NewBuffer(todoJSON)) |
112 |
| - if err != nil { |
113 |
| - t.Fatal(err) |
114 |
| - } |
115 |
| - req.Header.Set("Content-Type", "application/json") |
116 |
| - |
117 |
| - rr := httptest.NewRecorder() |
118 |
| - handler := http.HandlerFunc(server.handleAddTodo) |
119 |
| - |
120 |
| - handler.ServeHTTP(rr, req) |
121 |
| - |
122 |
| - if status := rr.Code; status != http.StatusCreated { |
123 |
| - t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusCreated) |
124 |
| - } |
125 |
| - |
126 |
| - expectedResponse := `{"id":0,"description":"New Test Todo","status":0}` |
127 |
| - if rr.Body.String() != expectedResponse { |
128 |
| - t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expectedResponse) |
129 |
| - } |
| 112 | + mockDB := &MockDB{} |
| 113 | + server := &Server{Port: 8080, db: mockDB} |
| 114 | + |
| 115 | + newTodo := models.Todo{Description: "New Test Todo", Status: 0} |
| 116 | + todoJSON, _ := json.Marshal(newTodo) |
| 117 | + req, err := http.NewRequest("POST", "/todos", bytes.NewBuffer(todoJSON)) |
| 118 | + if err != nil { |
| 119 | + t.Fatal(err) |
| 120 | + } |
| 121 | + req.Header.Set("Content-Type", "application/json") |
| 122 | + |
| 123 | + rr := httptest.NewRecorder() |
| 124 | + handler := http.HandlerFunc(server.handleAddTodo) |
| 125 | + |
| 126 | + handler.ServeHTTP(rr, req) |
| 127 | + |
| 128 | + if status := rr.Code; status != http.StatusCreated { |
| 129 | + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusCreated) |
| 130 | + } |
| 131 | + |
| 132 | + expectedResponse := `{"id":0,"description":"New Test Todo","status":0}` |
| 133 | + if rr.Body.String() != expectedResponse { |
| 134 | + t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expectedResponse) |
| 135 | + } |
130 | 136 | }
|
131 | 137 |
|
132 | 138 | // TestHandleDeleteTodo tests the handleDeleteTodo function
|
133 | 139 | func TestHandleDeleteTodo(t *testing.T) {
|
134 |
| - mockDB := &MockDB{} |
135 |
| - server := &Server{Port: 8080, db: mockDB} |
| 140 | + mockDB := &MockDB{} |
| 141 | + server := &Server{Port: 8080, db: mockDB} |
136 | 142 |
|
137 |
| - req, err := http.NewRequest("DELETE", "/todos/1", nil) |
138 |
| - if err != nil { |
139 |
| - t.Fatal(err) |
140 |
| - } |
| 143 | + req, err := http.NewRequest("DELETE", "/todos/1", nil) |
| 144 | + if err != nil { |
| 145 | + t.Fatal(err) |
| 146 | + } |
141 | 147 |
|
142 |
| - rr := httptest.NewRecorder() |
143 |
| - handler := http.HandlerFunc(server.handleDeleteTodo) |
| 148 | + rr := httptest.NewRecorder() |
| 149 | + handler := http.HandlerFunc(server.handleDeleteTodo) |
144 | 150 |
|
145 |
| - handler.ServeHTTP(rr, req) |
| 151 | + handler.ServeHTTP(rr, req) |
146 | 152 |
|
147 |
| - if status := rr.Code; status != http.StatusOK { |
148 |
| - t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) |
149 |
| - } |
| 153 | + if status := rr.Code; status != http.StatusOK { |
| 154 | + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) |
| 155 | + } |
150 | 156 |
|
151 |
| - expectedResponse := `{"message":"Todo successfully deleted"}` |
152 |
| - if rr.Body.String() != expectedResponse { |
153 |
| - t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expectedResponse) |
154 |
| - } |
| 157 | + expectedResponse := `{"message":"Todo successfully deleted"}` |
| 158 | + if rr.Body.String() != expectedResponse { |
| 159 | + t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expectedResponse) |
| 160 | + } |
155 | 161 | }
|
156 | 162 |
|
157 | 163 | // TestHandleUpdateTodo tests the handleUpdateTodo function
|
158 | 164 | func TestHandleUpdateTodo(t *testing.T) {
|
159 |
| - mockDB := &MockDB{} |
160 |
| - server := &Server{Port: 8080, db: mockDB} |
161 |
| - |
162 |
| - updatedTodo := models.Todo{Id: 1, Description: "Updated Test Todo", Status: 1} |
163 |
| - todoJSON, _ := json.Marshal(updatedTodo) |
164 |
| - req, err := http.NewRequest("PUT", "/todos/1", bytes.NewBuffer(todoJSON)) |
165 |
| - if err != nil { |
166 |
| - t.Fatal(err) |
167 |
| - } |
168 |
| - req.Header.Set("Content-Type", "application/json") |
169 |
| - |
170 |
| - rr := httptest.NewRecorder() |
171 |
| - handler := http.HandlerFunc(server.handleUpdateTodo) |
172 |
| - |
173 |
| - handler.ServeHTTP(rr, req) |
174 |
| - |
175 |
| - if status := rr.Code; status != http.StatusOK { |
176 |
| - t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) |
177 |
| - } |
178 |
| - |
179 |
| - expectedResponse := `{"message":"Todo successfully updated"}` |
180 |
| - if rr.Body.String() != expectedResponse { |
181 |
| - t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expectedResponse) |
182 |
| - } |
| 165 | + mockDB := &MockDB{} |
| 166 | + server := &Server{Port: 8080, db: mockDB} |
| 167 | + |
| 168 | + updatedTodo := models.Todo{Id: 1, Description: "Updated Test Todo", Status: 1} |
| 169 | + todoJSON, _ := json.Marshal(updatedTodo) |
| 170 | + req, err := http.NewRequest("PUT", "/todos/1", bytes.NewBuffer(todoJSON)) |
| 171 | + if err != nil { |
| 172 | + t.Fatal(err) |
| 173 | + } |
| 174 | + req.Header.Set("Content-Type", "application/json") |
| 175 | + |
| 176 | + rr := httptest.NewRecorder() |
| 177 | + handler := http.HandlerFunc(server.handleUpdateTodo) |
| 178 | + |
| 179 | + handler.ServeHTTP(rr, req) |
| 180 | + |
| 181 | + if status := rr.Code; status != http.StatusOK { |
| 182 | + t.Errorf("handler returned wrong status code: got %v want %v", status, http.StatusOK) |
| 183 | + } |
| 184 | + |
| 185 | + expectedResponse := `{"message":"Todo successfully updated"}` |
| 186 | + if rr.Body.String() != expectedResponse { |
| 187 | + t.Errorf("handler returned unexpected body: got %v want %v", rr.Body.String(), expectedResponse) |
| 188 | + } |
183 | 189 | }
|
0 commit comments