Skip to content

Commit eb907ba

Browse files
tests: add thumb handler subtests
1 parent 60434e1 commit eb907ba

File tree

2 files changed

+97
-20
lines changed

2 files changed

+97
-20
lines changed

internal/adapters/rest/handler/thumb_handler_test.go

Lines changed: 97 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package handler
33
import (
44
"bytes"
55
"encoding/json"
6+
"errors"
67
"net/http"
78
"net/http/httptest"
89
"testing"
@@ -52,6 +53,42 @@ func TestCreateProcess(t *testing.T) {
5253

5354
assert.Equal(t, http.StatusBadRequest, w.Code)
5455
})
56+
57+
t.Run("service error", func(t *testing.T) {
58+
mockService.On("CreateProcessAsync", mock.AnythingOfType("*ports.CreateProcessRequest")).
59+
Return(errors.New("service error")).Once()
60+
61+
body := CreateProcessRequest{URL: "https://example.com/video.mp4"}
62+
jsonBody, _ := json.Marshal(body)
63+
64+
w := httptest.NewRecorder()
65+
req, _ := http.NewRequest("POST", "/thumbs", bytes.NewBuffer(jsonBody))
66+
req.Header.Set("Content-Type", "application/json")
67+
router.ServeHTTP(w, req)
68+
69+
assert.Equal(t, http.StatusInternalServerError, w.Code)
70+
71+
var response ErrorResponse
72+
err := json.Unmarshal(w.Body.Bytes(), &response)
73+
assert.NoError(t, err)
74+
assert.Equal(t, "service error", response.Error)
75+
})
76+
77+
t.Run("api bind json error", func(t *testing.T) {
78+
invalidJSON := []byte(`{"url": invalid-json}`)
79+
80+
w := httptest.NewRecorder()
81+
req, _ := http.NewRequest("POST", "/thumbs", bytes.NewBuffer(invalidJSON))
82+
req.Header.Set("Content-Type", "application/json")
83+
router.ServeHTTP(w, req)
84+
85+
assert.Equal(t, http.StatusBadRequest, w.Code)
86+
87+
var response ErrorResponse
88+
err := json.Unmarshal(w.Body.Bytes(), &response)
89+
assert.NoError(t, err)
90+
assert.Equal(t, "Invalid request format", response.Error)
91+
})
5592
}
5693

5794
func TestUpdateProcess(t *testing.T) {
@@ -82,6 +119,66 @@ func TestUpdateProcess(t *testing.T) {
82119

83120
assert.Equal(t, http.StatusOK, w.Code)
84121
})
122+
123+
t.Run("api bind ID param error", func(t *testing.T) {
124+
body := UpdateProcessRequest{
125+
Status: "completed",
126+
ThumbnailPath: "path/to/thumbnail.jpg",
127+
}
128+
jsonBody, _ := json.Marshal(body)
129+
130+
w := httptest.NewRecorder()
131+
132+
req, _ := http.NewRequest("PUT", "/thumbs/invalid-uuid", bytes.NewBuffer(jsonBody))
133+
req.Header.Set("Content-Type", "application/json")
134+
router.ServeHTTP(w, req)
135+
136+
assert.Equal(t, http.StatusBadRequest, w.Code)
137+
138+
var response ErrorResponse
139+
err := json.Unmarshal(w.Body.Bytes(), &response)
140+
assert.NoError(t, err)
141+
assert.Equal(t, "Process ID is required", response.Error)
142+
})
143+
144+
t.Run("api bind json error", func(t *testing.T) {
145+
invalidJSON := []byte(`{"status": invalid-json}`)
146+
147+
w := httptest.NewRecorder()
148+
req, _ := http.NewRequest("PUT", "/thumbs/"+mockedUUID.String(), bytes.NewBuffer(invalidJSON))
149+
req.Header.Set("Content-Type", "application/json")
150+
router.ServeHTTP(w, req)
151+
152+
assert.Equal(t, http.StatusBadRequest, w.Code)
153+
154+
var response ErrorResponse
155+
err := json.Unmarshal(w.Body.Bytes(), &response)
156+
assert.NoError(t, err)
157+
assert.Equal(t, "Invalid request format", response.Error)
158+
})
159+
160+
t.Run("service error", func(t *testing.T) {
161+
mockService.On("UpdateProcess", mock.AnythingOfType("*ports.UpdateProcessRequest")).
162+
Return(nil, errors.New("service error")).Once()
163+
164+
body := UpdateProcessRequest{
165+
Status: "completed",
166+
ThumbnailPath: "path/to/thumbnail.jpg",
167+
}
168+
jsonBody, _ := json.Marshal(body)
169+
170+
w := httptest.NewRecorder()
171+
req, _ := http.NewRequest("PUT", "/thumbs/"+mockedUUID.String(), bytes.NewBuffer(jsonBody))
172+
req.Header.Set("Content-Type", "application/json")
173+
router.ServeHTTP(w, req)
174+
175+
assert.Equal(t, http.StatusInternalServerError, w.Code)
176+
177+
var response ErrorResponse
178+
err := json.Unmarshal(w.Body.Bytes(), &response)
179+
assert.NoError(t, err)
180+
assert.Equal(t, "service error", response.Error)
181+
})
85182
}
86183

87184
func TestListProcesses(t *testing.T) {

internal/config/config_test.go

Lines changed: 0 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -89,23 +89,3 @@ func TestLoad(t *testing.T) {
8989
})
9090
}
9191
}
92-
93-
// Example of a test for a function that uses the config
94-
func TestSomethingThatUsesConfig(t *testing.T) {
95-
// Set necessary environment variables
96-
os.Setenv("S3_BUCKET", "test-bucket")
97-
os.Setenv("SQS_QUEUE_URL", "test-queue")
98-
defer func() {
99-
os.Unsetenv("S3_BUCKET")
100-
os.Unsetenv("SQS_QUEUE_URL")
101-
}()
102-
103-
cfg, err := Load()
104-
assert.NoError(t, err)
105-
106-
// Now you can use cfg in your assertions or test logic
107-
assert.Equal(t, "test-bucket", cfg.S3.Bucket)
108-
assert.Equal(t, "test-queue", cfg.SQS.QueueURL)
109-
110-
// ... your test logic here ...
111-
}

0 commit comments

Comments
 (0)