Skip to content

Commit 5e8b8be

Browse files
committed
improve json assert
1 parent be6ab35 commit 5e8b8be

13 files changed

Lines changed: 65 additions & 50 deletions

api/dbv1/get_events.sql.go

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

api/server_test.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package api
33
import (
44
"context"
55
"encoding/json"
6+
"fmt"
67
"io"
78
"net/http/httptest"
89
"os"
@@ -202,9 +203,23 @@ func testGet(t *testing.T, path string, dest ...any) (int, []byte) {
202203
return res.StatusCode, body
203204
}
204205

205-
func jsonAssert(t *testing.T, body []byte, expectations map[string]string) {
206+
func jsonAssert(t *testing.T, body []byte, expectations map[string]any) {
206207
for path, expectation := range expectations {
207-
assert.Equal(t, expectation, gjson.GetBytes(body, path).String())
208+
var actual any
209+
switch v := expectation.(type) {
210+
case string:
211+
actual = gjson.GetBytes(body, path).String()
212+
case bool:
213+
actual = gjson.GetBytes(body, path).Bool()
214+
case float64:
215+
actual = gjson.GetBytes(body, path).Float()
216+
case int:
217+
actual = int(gjson.GetBytes(body, path).Int())
218+
default:
219+
t.Errorf("unsupported type for expectation: %T", v)
220+
}
221+
msg := fmt.Sprintf("Expected %s to be %v got %v", path, expectation, actual)
222+
assert.Equal(t, expectation, actual, msg)
208223
}
209224
}
210225

api/v1_events_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestGetEvents(t *testing.T) {
1616
status, body := testGet(t, "/v1/events", &eventsResponse)
1717
assert.Equal(t, 200, status)
1818

19-
jsonAssert(t, body, map[string]string{
19+
jsonAssert(t, body, map[string]any{
2020
"data.0.event_id": trashid.MustEncodeHashID(1),
2121
"data.0.entity_id": trashid.MustEncodeHashID(100),
2222

@@ -46,7 +46,7 @@ func TestGetEventsEntity(t *testing.T) {
4646
)
4747
assert.Equal(t, 200, status)
4848

49-
jsonAssert(t, body, map[string]string{
49+
jsonAssert(t, body, map[string]any{
5050
"data.0.event_id": trashid.MustEncodeHashID(6),
5151
"data.0.entity_id": trashid.MustEncodeHashID(102),
5252
})

api/v1_playlist_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestGetPlaylist(t *testing.T) {
1616
status, body := testGet(t, "/v1/full/playlists/7eP5n", &playlistResponse)
1717
assert.Equal(t, 200, status)
1818

19-
jsonAssert(t, body, map[string]string{
19+
jsonAssert(t, body, map[string]any{
2020
"data.0.id": "7eP5n",
2121
"data.0.playlist_name": "First",
2222
})
@@ -28,7 +28,7 @@ func TestGetPlaylistFollowDownloadAccess(t *testing.T) {
2828
}
2929
// No access
3030
_, body1 := testGet(t, "/v1/full/playlists/ML51L", &playlistResponse)
31-
jsonAssert(t, body1, map[string]string{
31+
jsonAssert(t, body1, map[string]any{
3232
"data.0.playlist_name": "Follow Gated Stream",
3333
"data.0.access": `{"stream":false,"download":false}`,
3434
})
@@ -40,7 +40,7 @@ func TestGetPlaylistFollowDownloadAccess(t *testing.T) {
4040
"0x4954d18926ba0ed9378938444731be4e622537b2",
4141
&playlistResponse,
4242
)
43-
jsonAssert(t, body2, map[string]string{
43+
jsonAssert(t, body2, map[string]any{
4444
"data.0.playlist_name": "Follow Gated Stream",
4545
"data.0.access": `{"stream":true,"download":true}`,
4646
})
@@ -52,7 +52,7 @@ func TestGetPlaylistUsdcPurchaseStreamAccess(t *testing.T) {
5252
}
5353
// No access
5454
_, body1 := testGet(t, "/v1/full/playlists/ELKzn", &playlistResponse)
55-
jsonAssert(t, body1, map[string]string{
55+
jsonAssert(t, body1, map[string]any{
5656
"data.0.playlist_name": "Purchase Gated Stream",
5757
"data.0.access": `{"stream":false,"download":false}`,
5858
})
@@ -64,7 +64,7 @@ func TestGetPlaylistUsdcPurchaseStreamAccess(t *testing.T) {
6464
"0x855d28d495ec1b06364bb7a521212753e2190b95",
6565
&playlistResponse,
6666
)
67-
jsonAssert(t, body2, map[string]string{
67+
jsonAssert(t, body2, map[string]any{
6868
"data.0.playlist_name": "Purchase Gated Stream",
6969
"data.0.access": `{"stream":true,"download":true}`,
7070
})
@@ -89,7 +89,7 @@ func TestGetPlaylistUsdcPurchaseSelfAccess(t *testing.T) {
8989
"0xc3d1d41e6872ffbd15c473d14fc3a9250be5b5e0",
9090
&playlistResponse,
9191
)
92-
jsonAssert(t, body2, map[string]string{
92+
jsonAssert(t, body2, map[string]any{
9393
"data.0.playlist_name": "Purchase Gated Stream",
9494
"data.0.access": `{"stream":true,"download":true}`,
9595
})

api/v1_playlists_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestPlaylistsEndpoint(t *testing.T) {
1515
status, body := testGet(t, "/v1/full/playlists?id=7eP5n", &resp)
1616
assert.Equal(t, 200, status)
1717

18-
jsonAssert(t, body, map[string]string{
18+
jsonAssert(t, body, map[string]any{
1919
"data.0.id": "7eP5n",
2020
"data.0.playlist_name": "First",
2121
})
@@ -29,7 +29,7 @@ func TestPlaylistsEndpointWithPlaylistPermalink(t *testing.T) {
2929
status, body := testGet(t, "/v1/full/playlists?permalink=/PlaylistsByPermalink/playlist/playlist-by-permalink", &resp)
3030
assert.Equal(t, 200, status)
3131

32-
jsonAssert(t, body, map[string]string{
32+
jsonAssert(t, body, map[string]any{
3333
"data.0.id": "eYake",
3434
"data.0.playlist_name": "playlist by permalink",
3535
})
@@ -43,7 +43,7 @@ func TestPlaylistsEndpointWithAlbumPermalink(t *testing.T) {
4343
status, body := testGet(t, "/v1/full/playlists?permalink=/AlbumsByPermalink/album/album-by-permalink", &resp)
4444
assert.Equal(t, 200, status)
4545

46-
jsonAssert(t, body, map[string]string{
46+
jsonAssert(t, body, map[string]any{
4747
"data.0.id": "ePVXL",
4848
"data.0.playlist_name": "album by permalink",
4949
})

api/v1_track_comments_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,16 +9,16 @@ import (
99
func TestTrackComments(t *testing.T) {
1010
status, body := testGet(t, "/v1/tracks/ePgRD/comments")
1111
assert.Equal(t, 200, status)
12-
jsonAssert(t, body, map[string]string{
12+
jsonAssert(t, body, map[string]any{
1313
"data.0.message": "flame emoji",
1414
"data.0.id": "7eP5n",
1515
"data.0.user_id": "7eP5n",
1616
"data.0.entity_id": "ePgRD",
17-
"data.0.reply_count": "1",
17+
"data.0.reply_count": 1,
1818
"data.0.replies.0.user_id": "ML51L",
1919

2020
// there is no second comment
21-
"data.#": "1",
21+
"data.#": 1,
2222
"data.1.id": "",
2323
})
2424
}

api/v1_track_test.go

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ func TestGetTrack(t *testing.T) {
1616
status, body := testGet(t, "/v1/full/tracks/eYJyn", &trackResponse)
1717
assert.Equal(t, 200, status)
1818

19-
jsonAssert(t, body, map[string]string{
19+
jsonAssert(t, body, map[string]any{
2020
"data.id": "eYJyn",
2121
"data.title": "Culca Canyon",
2222
})
@@ -28,10 +28,10 @@ func TestGetTrackFollowDownloadAcess(t *testing.T) {
2828
}
2929
// No access
3030
_, body1 := testGet(t, "/v1/full/tracks/eYRWn", &trackResponse)
31-
jsonAssert(t, body1, map[string]string{
31+
jsonAssert(t, body1, map[string]any{
3232
"data.title": "Follow Gated Download",
33-
"data.access.stream": "true",
34-
"data.access.download": "false",
33+
"data.access.stream": true,
34+
"data.access.download": false,
3535
})
3636

3737
// With access
@@ -41,10 +41,10 @@ func TestGetTrackFollowDownloadAcess(t *testing.T) {
4141
"0x4954d18926ba0ed9378938444731be4e622537b2",
4242
&trackResponse,
4343
)
44-
jsonAssert(t, body2, map[string]string{
44+
jsonAssert(t, body2, map[string]any{
4545
"data.title": "Follow Gated Download",
46-
"data.access.stream": "true",
47-
"data.access.download": "true",
46+
"data.access.stream": true,
47+
"data.access.download": true,
4848
})
4949
}
5050

@@ -54,10 +54,10 @@ func TestGetTrackTipStreamAccess(t *testing.T) {
5454
}
5555
// No access
5656
_, body1 := testGet(t, "/v1/full/tracks/L5x7n", &trackResponse)
57-
jsonAssert(t, body1, map[string]string{
57+
jsonAssert(t, body1, map[string]any{
5858
"data.title": "Tip Gated Stream",
59-
"data.access.stream": "false",
60-
"data.access.download": "false",
59+
"data.access.stream": false,
60+
"data.access.download": false,
6161
})
6262

6363
// With access
@@ -67,10 +67,10 @@ func TestGetTrackTipStreamAccess(t *testing.T) {
6767
"0x4954d18926ba0ed9378938444731be4e622537b2",
6868
&trackResponse,
6969
)
70-
jsonAssert(t, body2, map[string]string{
70+
jsonAssert(t, body2, map[string]any{
7171
"data.title": "Tip Gated Stream",
72-
"data.access.stream": "true",
73-
"data.access.download": "true",
72+
"data.access.stream": true,
73+
"data.access.download": true,
7474
})
7575
}
7676

@@ -80,10 +80,10 @@ func TestGetTrackUsdcPurchaseStreamAccess(t *testing.T) {
8080
}
8181
// No access
8282
_, body1 := testGet(t, "/v1/full/tracks/ebdJL", &trackResponse)
83-
jsonAssert(t, body1, map[string]string{
83+
jsonAssert(t, body1, map[string]any{
8484
"data.title": "Pay Gated Stream",
85-
"data.access.stream": "false",
86-
"data.access.download": "false",
85+
"data.access.stream": false,
86+
"data.access.download": false,
8787
})
8888

8989
// With access
@@ -93,10 +93,10 @@ func TestGetTrackUsdcPurchaseStreamAccess(t *testing.T) {
9393
"0x855d28d495ec1b06364bb7a521212753e2190b95",
9494
&trackResponse,
9595
)
96-
jsonAssert(t, body2, map[string]string{
96+
jsonAssert(t, body2, map[string]any{
9797
"data.title": "Pay Gated Stream",
98-
"data.access.stream": "true",
99-
"data.access.download": "true",
98+
"data.access.stream": true,
99+
"data.access.download": true,
100100
})
101101
}
102102

@@ -119,9 +119,9 @@ func TestGetTrackUsdcPurchaseSelfAccess(t *testing.T) {
119119
"0xc3d1d41e6872ffbd15c473d14fc3a9250be5b5e0",
120120
&trackResponse,
121121
)
122-
jsonAssert(t, body2, map[string]string{
122+
jsonAssert(t, body2, map[string]any{
123123
"data.title": "Pay Gated Stream",
124-
"data.access.stream": "true",
125-
"data.access.download": "true",
124+
"data.access.stream": true,
125+
"data.access.download": true,
126126
})
127127
}

api/v1_tracks_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ func TestTracksEndpoint(t *testing.T) {
1515
status, body := testGet(t, "/v1/full/tracks?id=eYZmn", &resp)
1616
assert.Equal(t, 200, status)
1717

18-
jsonAssert(t, body, map[string]string{
18+
jsonAssert(t, body, map[string]any{
1919
"data.0.id": "eYZmn",
2020
"data.0.title": "T1",
2121
})
@@ -29,7 +29,7 @@ func TestGetTracksByPermalink(t *testing.T) {
2929
status, body := testGet(t, "/v1/full/tracks?permalink=/TracksByPermalink/track-by-permalink", &tracksResponse)
3030
assert.Equal(t, 200, status)
3131

32-
jsonAssert(t, body, map[string]string{
32+
jsonAssert(t, body, map[string]any{
3333
"data.0.id": "eYake",
3434
"data.0.title": "track by permalink",
3535
})

api/v1_user_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,9 +17,9 @@ func TestGetUser(t *testing.T) {
1717
assert.Equal(t, 200, status)
1818

1919
// body is response json
20-
jsonAssert(t, body, map[string]string{
20+
jsonAssert(t, body, map[string]any{
2121
"data.0.handle": "rayjacobson",
22-
"data.0.user_id": "1",
22+
"data.0.user_id": 1,
2323
"data.0.id": "7eP5n",
2424
})
2525

api/v1_users_comments_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import (
99
func TestUserComments(t *testing.T) {
1010
status, body := testGet(t, "/v1/users/7eP5n/comments")
1111
assert.Equal(t, 200, status)
12-
jsonAssert(t, body, map[string]string{
12+
jsonAssert(t, body, map[string]any{
1313
"data.0.message": "flame emoji",
1414
"data.0.user_id": "7eP5n",
1515
"data.0.entity_id": "ePgRD",

0 commit comments

Comments
 (0)