@@ -10,6 +10,9 @@ import (
1010 "encoding/json"
1111 "fmt"
1212 "testing"
13+
14+ "github.com/stretchr/testify/assert"
15+ "github.com/stretchr/testify/require"
1316)
1417
1518func TestBundleList (t * testing.T ) {
@@ -20,9 +23,7 @@ func TestBundleList(t *testing.T) {
2023 VALUES (?, ?, 'my bundle', true)` , userID , projID )
2124
2225 items := getList (t , s , "/api/1.4/bundles" )
23- if len (items ) != 1 {
24- t .Fatalf ("got %d, want 1" , len (items ))
25- }
26+ require .Len (t , items , 1 )
2627 b := items [0 ]
2728 assertField (t , b , "id" )
2829 assertField (t , b , "name" )
@@ -34,9 +35,7 @@ func TestBundleList(t *testing.T) {
3435func TestBundleListEmpty (t * testing.T ) {
3536 s := newTestServer (t )
3637 items := getList (t , s , "/api/1.4/bundles" )
37- if len (items ) != 0 {
38- t .Errorf ("got %d, want 0" , len (items ))
39- }
38+ assert .Len (t , items , 0 )
4039}
4140
4241func TestBundleListPublicOnly (t * testing.T ) {
@@ -49,12 +48,8 @@ func TestBundleListPublicOnly(t *testing.T) {
4948 VALUES (?, ?, 'private', false)` , userID , projID )
5049
5150 items := getList (t , s , "/api/1.4/bundles" )
52- if len (items ) != 1 {
53- t .Errorf ("got %d, want 1" , len (items ))
54- }
55- if items [0 ]["name" ] != "public" {
56- t .Errorf ("name = %v, want public" , items [0 ]["name" ])
57- }
51+ assert .Len (t , items , 1 )
52+ assert .Equal (t , "public" , items [0 ]["name" ])
5853}
5954
6055func TestBundleListAuthSeesOwnPrivate (t * testing.T ) {
@@ -72,16 +67,12 @@ func TestBundleListAuthSeesOwnPrivate(t *testing.T) {
7267 VALUES (?, ?, 'public-one', true)` , otherID , projID )
7368
7469 resp := s .authRequest (t , "GET" , "/api/1.4/bundles" , token , nil )
75- if resp .StatusCode != 200 {
76- t .Fatalf ("status = %d" , resp .StatusCode )
77- }
70+ require .Equal (t , 200 , resp .StatusCode )
7871 var items []map [string ]any
7972 json .NewDecoder (resp .Body ).Decode (& items )
8073 resp .Body .Close ()
8174
82- if len (items ) != 2 {
83- t .Fatalf ("got %d, want 2 (own private + public)" , len (items ))
84- }
75+ require .Len (t , items , 2 )
8576}
8677
8778func TestBundleDetail (t * testing.T ) {
@@ -94,17 +85,13 @@ func TestBundleDetail(t *testing.T) {
9485 userID , projID ).Scan (context .Background (), & bundleID )
9586
9687 b := getOne (t , s , fmt .Sprintf ("/api/1.4/bundles/%d" , bundleID ))
97- if b ["name" ] != "detail bundle" {
98- t .Errorf ("name = %v" , b ["name" ])
99- }
88+ assert .Equal (t , "detail bundle" , b ["name" ])
10089}
10190
10291func TestBundleDetailInvalid (t * testing.T ) {
10392 s := newTestServer (t )
10493 resp := s .get (t , "/api/1.4/bundles/invalid" )
105- if resp .StatusCode != 422 {
106- t .Errorf ("status = %d, want 422" , resp .StatusCode )
107- }
94+ assert .Equal (t , 422 , resp .StatusCode )
10895}
10996
11097func TestBundleDetailPatches (t * testing.T ) {
@@ -128,24 +115,16 @@ func TestBundleDetailPatches(t *testing.T) {
128115 assertNested (t , b , "project" , "id" )
129116
130117 patches := b ["patches" ].([]any )
131- if len (patches ) != 1 {
132- t .Fatalf ("patches: got %d, want 1" , len (patches ))
133- }
118+ require .Len (t , patches , 1 )
134119 patchObj := patches [0 ].(map [string ]any )
135- if int (patchObj ["id" ].(float64 )) != int (patchID ) {
136- t .Errorf ("patches[0].id = %v, want %d" , patchObj ["id" ], patchID )
137- }
138- if patchObj ["name" ] != "bundle patch" {
139- t .Errorf ("patches[0].name = %v, want 'bundle patch'" , patchObj ["name" ])
140- }
120+ assert .Equal (t , float64 (patchID ), patchObj ["id" ])
121+ assert .Equal (t , "bundle patch" , patchObj ["name" ])
141122}
142123
143124func TestBundleNotFound (t * testing.T ) {
144125 s := newTestServer (t )
145126 resp := s .get (t , "/api/1.4/bundles/99999" )
146- if resp .StatusCode != 404 {
147- t .Errorf ("status = %d, want 404" , resp .StatusCode )
148- }
127+ assert .Equal (t , 404 , resp .StatusCode )
149128}
150129
151130func TestBundleFilterOwner (t * testing.T ) {
@@ -156,13 +135,9 @@ func TestBundleFilterOwner(t *testing.T) {
156135 VALUES (?, ?, 'b', true)` , userID , projID )
157136
158137 items := getList (t , s , fmt .Sprintf ("/api/1.4/bundles/?owner=%d" , userID ))
159- if len (items ) != 1 {
160- t .Errorf ("got %d, want 1" , len (items ))
161- }
138+ assert .Len (t , items , 1 )
162139 items = getList (t , s , "/api/1.4/bundles/?owner=99999" )
163- if len (items ) != 0 {
164- t .Errorf ("got %d, want 0" , len (items ))
165- }
140+ assert .Len (t , items , 0 )
166141}
167142
168143func TestBundleFilterProject (t * testing.T ) {
@@ -173,13 +148,9 @@ func TestBundleFilterProject(t *testing.T) {
173148 VALUES (?, ?, 'b', true)` , userID , projID )
174149
175150 items := getList (t , s , fmt .Sprintf ("/api/1.4/bundles/?project=%d" , projID ))
176- if len (items ) != 1 {
177- t .Errorf ("got %d, want 1" , len (items ))
178- }
151+ assert .Len (t , items , 1 )
179152 items = getList (t , s , "/api/1.4/bundles/?project=99999" )
180- if len (items ) != 0 {
181- t .Errorf ("got %d, want 0" , len (items ))
182- }
153+ assert .Len (t , items , 0 )
183154}
184155
185156// --- Create ---
@@ -191,9 +162,7 @@ func TestBundleCreateAnonymous(t *testing.T) {
191162
192163 resp := s .authRequest (t , "POST" , "/api/1.4/bundles" , "" ,
193164 map [string ]any {"name" : "b" , "patches" : []int {patchID }})
194- if resp .StatusCode != 401 {
195- t .Errorf ("status = %d, want 401" , resp .StatusCode )
196- }
165+ assert .Equal (t , 401 , resp .StatusCode )
197166}
198167
199168func TestBundleCreateValid (t * testing.T ) {
@@ -210,9 +179,7 @@ func TestBundleCreateValid(t *testing.T) {
210179 "public" : true ,
211180 "patches" : []int {p1 , p2 },
212181 })
213- if resp .StatusCode != 201 {
214- t .Fatalf ("status = %d, want 201" , resp .StatusCode )
215- }
182+ require .Equal (t , 201 , resp .StatusCode )
216183
217184 var b map [string ]any
218185 json .NewDecoder (resp .Body ).Decode (& b )
@@ -224,9 +191,7 @@ func TestBundleCreateValid(t *testing.T) {
224191 assertNested (t , b , "project" , "id" )
225192
226193 patches := b ["patches" ].([]any )
227- if len (patches ) != 2 {
228- t .Fatalf ("patches: got %d, want 2" , len (patches ))
229- }
194+ require .Len (t , patches , 2 )
230195}
231196
232197func TestBundleCreateEmptyPatches (t * testing.T ) {
@@ -240,9 +205,7 @@ func TestBundleCreateEmptyPatches(t *testing.T) {
240205 "name" : "empty-bundle" ,
241206 "patches" : []int {},
242207 })
243- if resp .StatusCode != 400 {
244- t .Errorf ("status = %d, want 400" , resp .StatusCode )
245- }
208+ assert .Equal (t , 400 , resp .StatusCode )
246209}
247210
248211func TestBundleCreateCrossProject (t * testing.T ) {
@@ -268,9 +231,7 @@ func TestBundleCreateCrossProject(t *testing.T) {
268231 "name" : "cross-bundle" ,
269232 "patches" : []int {p1 , p2 },
270233 })
271- if resp .StatusCode != 400 {
272- t .Errorf ("status = %d, want 400" , resp .StatusCode )
273- }
234+ assert .Equal (t , 400 , resp .StatusCode )
274235}
275236
276237// --- Update ---
@@ -289,9 +250,7 @@ func TestBundleUpdateOwner(t *testing.T) {
289250 "public" : false ,
290251 "patches" : []int {patchID },
291252 })
292- if resp .StatusCode != 201 {
293- t .Fatalf ("create status = %d" , resp .StatusCode )
294- }
253+ require .Equal (t , 201 , resp .StatusCode )
295254 var created map [string ]any
296255 json .NewDecoder (resp .Body ).Decode (& created )
297256 resp .Body .Close ()
@@ -303,9 +262,7 @@ func TestBundleUpdateOwner(t *testing.T) {
303262 "name" : "new-name" ,
304263 "public" : true ,
305264 })
306- if resp .StatusCode != 200 {
307- t .Fatalf ("update status = %d, want 200" , resp .StatusCode )
308- }
265+ require .Equal (t , 200 , resp .StatusCode )
309266 var updated map [string ]any
310267 json .NewDecoder (resp .Body ).Decode (& updated )
311268 resp .Body .Close ()
@@ -338,17 +295,13 @@ func TestBundleUpdatePatches(t *testing.T) {
338295 map [string ]any {
339296 "patches" : []int {p2 , p3 },
340297 })
341- if resp .StatusCode != 200 {
342- t .Fatalf ("status = %d, want 200" , resp .StatusCode )
343- }
298+ require .Equal (t , 200 , resp .StatusCode )
344299 var updated map [string ]any
345300 json .NewDecoder (resp .Body ).Decode (& updated )
346301 resp .Body .Close ()
347302
348303 patches := updated ["patches" ].([]any )
349- if len (patches ) != 2 {
350- t .Fatalf ("patches: got %d, want 2" , len (patches ))
351- }
304+ require .Len (t , patches , 2 )
352305}
353306
354307func TestBundleUpdateNonOwner (t * testing.T ) {
@@ -368,9 +321,7 @@ func TestBundleUpdateNonOwner(t *testing.T) {
368321
369322 resp := s .authRequest (t , "PATCH" , fmt .Sprintf ("/api/1.4/bundles/%d" , bundleID ), otherToken ,
370323 map [string ]any {"name" : "hijacked" })
371- if resp .StatusCode != 403 {
372- t .Errorf ("status = %d, want 403" , resp .StatusCode )
373- }
324+ assert .Equal (t , 403 , resp .StatusCode )
374325}
375326
376327// --- Delete ---
@@ -393,15 +344,11 @@ func TestBundleDeleteOwner(t *testing.T) {
393344 bundleID := int (created ["id" ].(float64 ))
394345
395346 resp = s .authRequest (t , "DELETE" , fmt .Sprintf ("/api/1.4/bundles/%d" , bundleID ), token , nil )
396- if resp .StatusCode != 204 {
397- t .Errorf ("status = %d, want 204" , resp .StatusCode )
398- }
347+ assert .Equal (t , 204 , resp .StatusCode )
399348
400349 // verify it's gone
401350 resp = s .get (t , fmt .Sprintf ("/api/1.4/bundles/%d" , bundleID ))
402- if resp .StatusCode != 404 {
403- t .Errorf ("after delete: status = %d, want 404" , resp .StatusCode )
404- }
351+ assert .Equal (t , 404 , resp .StatusCode )
405352}
406353
407354func TestBundleDeleteNonOwner (t * testing.T ) {
@@ -417,15 +364,11 @@ func TestBundleDeleteNonOwner(t *testing.T) {
417364 ownerID , projID ).Scan (context .Background (), & bundleID )
418365
419366 resp := s .authRequest (t , "DELETE" , fmt .Sprintf ("/api/1.4/bundles/%d" , bundleID ), otherToken , nil )
420- if resp .StatusCode != 403 {
421- t .Errorf ("status = %d, want 403" , resp .StatusCode )
422- }
367+ assert .Equal (t , 403 , resp .StatusCode )
423368}
424369
425370func TestBundleDeleteAnonymous (t * testing.T ) {
426371 s := newTestServer (t )
427372 resp := s .authRequest (t , "DELETE" , "/api/1.4/bundles/1" , "" , nil )
428- if resp .StatusCode != 401 {
429- t .Errorf ("status = %d, want 401" , resp .StatusCode )
430- }
373+ assert .Equal (t , 401 , resp .StatusCode )
431374}
0 commit comments