@@ -4,20 +4,18 @@ import (
44 "encoding/json"
55 "fmt"
66 "io"
7- "log"
87 "net/http"
98 "opskrifter-backend/internal/testutils"
109 "opskrifter-backend/internal/types"
1110 "testing"
1211
12+ "github.com/stretchr/testify/assert"
1313 "github.com/stretchr/testify/require"
1414)
1515
1616func TestCreateHandlerByType (t * testing.T ) {
1717 data , err := json .Marshal (testRecipe )
18- if err != nil {
19- t .Fatalf ("failed to read input file: %v" , err )
20- }
18+ require .NoError (t , err , "failed to marshal testRecipe" )
2119
2220 req , rec := testutils .NewJSONPostRequest (data )
2321 CreateRecipe .ServeHTTP (rec , req )
@@ -28,57 +26,27 @@ func TestCreateHandlerByType(t *testing.T) {
2826 require .Equal (t , http .StatusOK , resp .StatusCode , "expected status 200 OK" )
2927
3028 body , err := io .ReadAll (resp .Body )
31- if err != nil {
32- t .Fatalf ("failed to read response body: %v" , err )
33- }
29+ require .NoError (t , err , "failed to read response body" )
3430
3531 var response Response
36- if err := json .Unmarshal (body , & response ); err != nil {
37- t .Fatalf ("failed to unmarshal response JSON: %v" , err )
38- }
39-
40- if response .ID == "" {
41- t .Errorf ("expected non-empty id field in response" )
42- }
43-
44- err = testutils .AssertCountByType [types.Recipe ](1 , GetCountByType )
32+ require .NoError (t , json .Unmarshal (body , & response ), "failed to unmarshal response JSON" )
33+ require .NotEmpty (t , response .ID , "expected non-empty id field in response" )
34+ require .NoError (t , testutils .AssertCountByType [types.Recipe ](1 , GetCountByType ))
4535
46- if err != nil {
47- t .Fatalf ("failed to get the count %v" , err )
48- }
49-
50- var recipe types.Recipe
51- recipe .ID = response .ID
36+ recipe := types.Recipe {ID : response .ID }
5237 _ , err = DeleteByType (recipe )
53-
54- if err != nil {
55- t .Errorf ("failed to delete recipe" )
56- }
57-
58- err = testutils .AssertCountByType [types.Recipe ](0 , GetCountByType )
59-
60- if err != nil {
61- t .Fatalf ("failed to get the count %v" , err )
62- }
38+ require .NoError (t , err , "failed to delete recipe" )
39+ require .NoError (t , testutils .AssertCountByType [types.Recipe ](0 , GetCountByType ))
6340}
6441
6542func TestDeleteHandlerByType (t * testing.T ) {
6643 id , err := CreateByType (handlerRecipe )
67-
68- if err != nil {
69- t .Fatalf ("failed to create recipe" )
70- }
71-
72- if id == "" {
73- t .Fatalf ("failed to generate id" )
74- }
75-
44+ require .NoError (t , err , "failed to create recipe" )
45+ require .NotEmpty (t , id , "failed to generate id" )
7646 handlerRecipe .ID = id
7747
7848 data , err := json .Marshal (handlerRecipe )
79- if err != nil {
80- t .Fatalf ("failed to marshal handlerRecipe: %v" , err )
81- }
49+ require .NoError (t , err , "failed to marshal handlerRecipe" )
8250
8351 req , rec := testutils .NewJSONPostRequest (data )
8452 DeleteRecipe .ServeHTTP (rec , req )
@@ -89,44 +57,25 @@ func TestDeleteHandlerByType(t *testing.T) {
8957 require .Equal (t , http .StatusOK , resp .StatusCode , "expected status 200 OK" )
9058
9159 body , err := io .ReadAll (resp .Body )
92- if err != nil {
93- t .Fatalf ("failed to read response body: %v" , err )
94- }
60+ require .NoError (t , err , "failed to read response body" )
9561
9662 var response Response
97- if err := json .Unmarshal (body , & response ); err != nil {
98- t .Fatalf ("failed to unmarshal response JSON: %v" , err )
99- }
100-
101- if response .ID == "" {
102- t .Errorf ("expected non-empty id field in response" )
103- }
104-
105- err = testutils .AssertCountByType [types.Recipe ](0 , GetCountByType )
63+ require .NoError (t , json .Unmarshal (body , & response ), "failed to unmarshal response JSON" )
64+ require .NotEmpty (t , response .ID , "expected non-empty id field in response" )
65+ require .NoError (t , testutils .AssertCountByType [types.Recipe ](0 , GetCountByType ))
10666
107- if err != nil {
108- t .Fatalf ("failed get the count %v" , err )
109- }
11067}
11168
11269func TestUpdateHandlerByType (t * testing.T ) {
11370 id , err := CreateByType (handlerRecipe )
114-
115- if err != nil {
116- t .Fatalf ("failed to create recipe" )
117- }
118-
119- if id == "" {
120- t .Fatalf ("failed to generate id" )
121- }
71+ require .NoError (t , err , "failed to create recipe" )
72+ require .NotEmpty (t , id , "failed to generate id" )
12273
12374 updatedRecipe := recipeGenerator .Generate ()
12475 updatedRecipe .ID = id
12576
12677 data , err := json .Marshal (updatedRecipe )
127- if err != nil {
128- t .Fatalf ("failed to marshal handlerRecipe: %v" , err )
129- }
78+ require .NoError (t , err , "failed to marshal updatedRecipe" )
13079
13180 req , rec := testutils .NewJSONPostRequest (data )
13281 UpdateRecipe .ServeHTTP (rec , req )
@@ -137,34 +86,23 @@ func TestUpdateHandlerByType(t *testing.T) {
13786 require .Equal (t , http .StatusOK , resp .StatusCode , "expected status 200 OK" )
13887
13988 body , err := io .ReadAll (resp .Body )
140- if err != nil {
141- t .Fatalf ("failed to read response body: %v" , err )
142- }
89+ require .NoError (t , err , "failed to read response body" )
14390
14491 var response Response
145- if err := json .Unmarshal (body , & response ); err != nil {
146- t .Fatalf ("failed to unmarshal response JSON: %v" , err )
147- }
92+ require .NoError (t , json .Unmarshal (body , & response ), "failed to unmarshal response JSON" )
14893
149- if response .ID == "" {
150- t .Errorf ("expected non-empty id field in response" )
151- }
152-
153- err = testutils .AssertCountByType [types.Recipe ](1 , GetCountByType )
94+ require .NotEmpty (t , response .ID , "expected non-empty id field in response" )
15495
155- if err != nil {
156- t .Fatalf ("failed get the count %v" , err )
157- }
96+ require .NoError (t , testutils .AssertCountByType [types.Recipe ](1 , GetCountByType ), "failed to get the count" )
15897
15998 updated , err := GetByType (updatedRecipe )
160-
161- if err != nil {
162- log .Fatalf ("error updating recipe" )
163- }
99+ require .NoError (t , err , "error fetching updated recipe" )
164100
165101 testutils .EqualByValue (updatedRecipe , updated )
102+
166103 _ , err = DeleteByType (updatedRecipe )
167104 require .NoError (t , err , "error deleting recipe" )
105+
168106}
169107
170108func TestGetManyHandlerByType (t * testing.T ) {
@@ -234,9 +172,7 @@ func TestGetManyHandlerByType(t *testing.T) {
234172 for _ , tc := range testCases {
235173 t .Run (tc .name , func (t * testing.T ) {
236174 data , err := json .Marshal (tc .query )
237- if err != nil {
238- t .Fatalf ("failed to marshal query: %v" , err )
239- }
175+ require .NoError (t , err , "failed to marshal query" )
240176
241177 req , rec := testutils .NewJSONPostRequest (data )
242178 GetManyRecipe .ServeHTTP (rec , req )
@@ -245,41 +181,27 @@ func TestGetManyHandlerByType(t *testing.T) {
245181 defer resp .Body .Close ()
246182
247183 if tc .expectError {
248- if resp .StatusCode == http .StatusOK {
249- t .Error ("expected error status code, got 200" )
250- }
184+ assert .NotEqual (t , http .StatusOK , resp .StatusCode , "expected error status code, got 200" )
251185 return
252186 }
253187
254- if resp .StatusCode != http .StatusOK {
255- t .Fatalf ("expected status 200, got %d" , resp .StatusCode )
256- }
188+ require .Equal (t , http .StatusOK , resp .StatusCode , "expected status 200" )
257189
258190 body , err := io .ReadAll (resp .Body )
259- if err != nil {
260- t .Fatalf ("failed to read response body: %v" , err )
261- }
191+ require .NoError (t , err , "failed to read response body" )
262192
263193 var objs []types.Recipe
264- if err := json .Unmarshal (body , & objs ); err != nil {
265- t .Fatalf ("failed to unmarshal response JSON: %v" , err )
266- }
194+ require .NoError (t , json .Unmarshal (body , & objs ), "failed to unmarshal response JSON" )
267195
268- if tc .expectedLen != len (objs ) {
269- t .Fatalf ("expected %d items, got %d" , tc .expectedLen , len (objs ))
270- }
196+ require .Equal (t , tc .expectedLen , len (objs ), "unexpected number of items" )
271197
272198 if tc .validate != nil {
273- if err := tc .validate (objs ); err != nil {
274- t .Errorf ("validation failed: %v" , err )
275- }
199+ err = tc .validate (objs )
200+ assert .NoError (t , err , "validation failed" )
276201 }
277202 })
278203 }
279204
280205 err = DeleteManyByType (testRecipes )
281-
282- if err != nil {
283- t .Fatalf ("error deleting recipes %v" , err )
284- }
206+ require .NoError (t , err , "error deleting recipes" )
285207}
0 commit comments