Skip to content

Commit a95c084

Browse files
committed
jsonAssert test helper
1 parent 6cb0b67 commit a95c084

7 files changed

Lines changed: 76 additions & 31 deletions

File tree

api/server_test.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"github.com/jackc/pgx/v5"
1515
"github.com/stretchr/testify/assert"
1616
"github.com/stretchr/testify/require"
17+
"github.com/tidwall/gjson"
1718
)
1819

1920
var (
@@ -184,6 +185,12 @@ func testGet(t *testing.T, path string, dest ...any) (int, []byte) {
184185
return res.StatusCode, body
185186
}
186187

188+
func jsonAssert(t *testing.T, body []byte, expectations map[string]string) {
189+
for path, expectation := range expectations {
190+
assert.Equal(t, expectation, gjson.GetBytes(body, path).String())
191+
}
192+
}
193+
187194
// testGetWithWallet makes a GET request with authentication headers for the given wallet address
188195
func testGetWithWallet(t *testing.T, path string, walletAddress string, dest ...any) (int, []byte) {
189196
req := httptest.NewRequest("GET", path, nil)

api/v1_playlist_test.go

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"strings"
54
"testing"
65

76
"bridgerton.audius.co/api/dbv1"
@@ -16,8 +15,10 @@ func TestGetPlaylist(t *testing.T) {
1615
status, body := testGet(t, "/v1/full/playlists/7eP5n", &playlistResponse)
1716
assert.Equal(t, 200, status)
1817

19-
assert.True(t, strings.Contains(string(body), `"playlist_name":"First"`))
20-
assert.True(t, strings.Contains(string(body), `"id":"7eP5n"`))
18+
jsonAssert(t, body, map[string]string{
19+
"data.id": "7eP5n",
20+
"data.playlist_name": "First",
21+
})
2122
}
2223

2324
func TestGetPlaylistFollowDownloadAccess(t *testing.T) {
@@ -26,11 +27,15 @@ func TestGetPlaylistFollowDownloadAccess(t *testing.T) {
2627
}
2728
// No access
2829
_, body1 := testGet(t, "/v1/full/playlists/ML51L", &playlistResponse)
29-
assert.True(t, strings.Contains(string(body1), `"playlist_name":"Follow Gated Stream"`))
30-
assert.True(t, strings.Contains(string(body1), `"access":{"stream":false,"download":false}`))
30+
jsonAssert(t, body1, map[string]string{
31+
"data.playlist_name": "Follow Gated Stream",
32+
"data.access": `{"stream":false,"download":false}`,
33+
})
3134

3235
// With access
3336
_, body2 := testGet(t, "/v1/full/playlists/ML51L?user_id=ELKzn", &playlistResponse)
34-
assert.True(t, strings.Contains(string(body2), `"playlist_name":"Follow Gated Stream"`))
35-
assert.True(t, strings.Contains(string(body2), `"access":{"stream":true,"download":true}`))
37+
jsonAssert(t, body2, map[string]string{
38+
"data.playlist_name": "Follow Gated Stream",
39+
"data.access": `{"stream":true,"download":true}`,
40+
})
3641
}

api/v1_track_test.go

Lines changed: 24 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"strings"
54
"testing"
65

76
"bridgerton.audius.co/api/dbv1"
@@ -16,8 +15,10 @@ func TestGetTrack(t *testing.T) {
1615
status, body := testGet(t, "/v1/full/tracks/eYJyn", &trackResponse)
1716
assert.Equal(t, 200, status)
1817

19-
assert.True(t, strings.Contains(string(body), `"title":"Culca Canyon"`))
20-
assert.True(t, strings.Contains(string(body), `"id":"eYJyn"`))
18+
jsonAssert(t, body, map[string]string{
19+
"data.id": "eYJyn",
20+
"data.title": "Culca Canyon",
21+
})
2122
}
2223

2324
func TestGetTrackFollowDownloadAcess(t *testing.T) {
@@ -26,13 +27,19 @@ func TestGetTrackFollowDownloadAcess(t *testing.T) {
2627
}
2728
// No access
2829
_, body1 := testGet(t, "/v1/full/tracks/eYRWn", &trackResponse)
29-
assert.True(t, strings.Contains(string(body1), `"title":"Follow Gated Download"`))
30-
assert.True(t, strings.Contains(string(body1), `"access":{"stream":true,"download":false}`))
30+
jsonAssert(t, body1, map[string]string{
31+
"data.title": "Follow Gated Download",
32+
"data.access.stream": "true",
33+
"data.access.download": "false",
34+
})
3135

3236
// With access
3337
_, body2 := testGet(t, "/v1/full/tracks/eYRWn?user_id=ELKzn", &trackResponse)
34-
assert.True(t, strings.Contains(string(body2), `"title":"Follow Gated Download"`))
35-
assert.True(t, strings.Contains(string(body2), `"access":{"stream":true,"download":true}`))
38+
jsonAssert(t, body2, map[string]string{
39+
"data.title": "Follow Gated Download",
40+
"data.access.stream": "true",
41+
"data.access.download": "true",
42+
})
3643
}
3744

3845
func TestGetTrackTipStreamAccess(t *testing.T) {
@@ -41,11 +48,17 @@ func TestGetTrackTipStreamAccess(t *testing.T) {
4148
}
4249
// No access
4350
_, body1 := testGet(t, "/v1/full/tracks/L5x7n", &trackResponse)
44-
assert.True(t, strings.Contains(string(body1), `"title":"Tip Gated Stream"`))
45-
assert.True(t, strings.Contains(string(body1), `"access":{"stream":false,"download":false}`))
51+
jsonAssert(t, body1, map[string]string{
52+
"data.title": "Tip Gated Stream",
53+
"data.access.stream": "false",
54+
"data.access.download": "false",
55+
})
4656

4757
// With access
4858
_, body2 := testGet(t, "/v1/full/tracks/L5x7n?user_id=ELKzn", &trackResponse)
49-
assert.True(t, strings.Contains(string(body2), `"title":"Tip Gated Stream"`))
50-
assert.True(t, strings.Contains(string(body2), `"access":{"stream":true,"download":true}`))
59+
jsonAssert(t, body2, map[string]string{
60+
"data.title": "Tip Gated Stream",
61+
"data.access.stream": "true",
62+
"data.access.download": "true",
63+
})
5164
}

api/v1_user_test.go

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"strings"
54
"testing"
65

76
"bridgerton.audius.co/api/dbv1"
@@ -18,9 +17,11 @@ func TestGetUser(t *testing.T) {
1817
assert.Equal(t, 200, status)
1918

2019
// body is response json
21-
assert.True(t, strings.Contains(string(body), `"handle":"rayjacobson"`))
22-
assert.True(t, strings.Contains(string(body), `"user_id":1`))
23-
assert.True(t, strings.Contains(string(body), `"id":"7eP5n"`))
20+
jsonAssert(t, body, map[string]string{
21+
"data.0.handle": "rayjacobson",
22+
"data.0.user_id": "1",
23+
"data.0.id": "7eP5n",
24+
})
2425

2526
// but we also unmarshaled into userResponse
2627
// for structured testing

api/v1_users_test.go

Lines changed: 21 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package api
22

33
import (
4-
"strings"
54
"testing"
65

76
"bridgerton.audius.co/api/dbv1"
@@ -72,24 +71,37 @@ func TestGetUsers(t *testing.T) {
7271
status, body := testGet(t, "/v1/full/users?id=1", &userResponse)
7372
assert.Equal(t, 200, status)
7473

75-
// body is response json
76-
assert.True(t, strings.Contains(string(body), `"handle":"rayjacobson"`))
77-
assert.True(t, strings.Contains(string(body), `"user_id":1`))
78-
assert.True(t, strings.Contains(string(body), `"id":"7eP5n"`))
74+
// jsonAssert helps testing the response body
75+
jsonAssert(t, body, map[string]string{
76+
"data.0.id": "7eP5n",
77+
"data.0.user_id": "1",
78+
"data.0.handle": "rayjacobson",
79+
})
7980

8081
// but we also unmarshaled into userResponse
8182
// for structured testing
82-
// update: this actually gets decoded back to an int in go memory
83-
// even tho it's hashid in the json...
83+
assert.Equal(t, "rayjacobson", userResponse.Data[0].Handle.String)
84+
85+
// this assert won't work:
86+
// because we have custom json marshal functions
8487
// assert.Equal(t, userResponse.Data[0].ID, "7eP5n")
88+
89+
// because it got parsed back to int:
90+
assert.Equal(t, 1, int(userResponse.Data[0].ID))
8591
}
8692

8793
func TestFollowerEndpoint(t *testing.T) {
8894
var userResponse struct {
8995
Data []dbv1.FullUser
9096
}
9197

92-
status, _ := testGet(t, "/v1/full/users/7eP5n/followers", &userResponse)
98+
status, body := testGet(t, "/v1/full/users/7eP5n/followers", &userResponse)
9399
assert.Equal(t, 200, status)
94-
// assert.Equal(t, userResponse.Data[0].ID, "ML51L")
100+
101+
jsonAssert(t, body, map[string]string{
102+
"data.0.id": "ML51L",
103+
"data.0.handle": "stereosteve",
104+
})
105+
106+
assert.Equal(t, "stereosteve", userResponse.Data[0].Handle.String)
95107
}

go.mod

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,9 @@ require (
5454
github.com/sasha-s/go-deadlock v0.3.5 // indirect
5555
github.com/segmentio/asm v1.1.3 // indirect
5656
github.com/supranational/blst v0.3.14 // indirect
57+
github.com/tidwall/gjson v1.18.0 // indirect
58+
github.com/tidwall/match v1.1.1 // indirect
59+
github.com/tidwall/pretty v1.2.1 // indirect
5760
github.com/valyala/bytebufferpool v1.0.0 // indirect
5861
go.opentelemetry.io/auto/sdk v1.1.0 // indirect
5962
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.59.0 // indirect

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,10 +105,14 @@ github.com/supranational/blst v0.3.14 h1:xNMoHRJOTwMn63ip6qoWJ2Ymgvj7E2b9jY2FAwY
105105
github.com/supranational/blst v0.3.14/go.mod h1:jZJtfjgudtNl4en1tzwPIV3KjUnQUvG3/j+w+fVonLw=
106106
github.com/tidwall/gjson v1.14.4 h1:uo0p8EbA09J7RQaflQ1aBRffTR7xedD2bcIVSYxLnkM=
107107
github.com/tidwall/gjson v1.14.4/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
108+
github.com/tidwall/gjson v1.18.0 h1:FIDeeyB800efLX89e5a8Y0BNH+LOngJyGrIWxG2FKQY=
109+
github.com/tidwall/gjson v1.18.0/go.mod h1:/wbyibRr2FHMks5tjHJ5F8dMZh3AcwJEMf5vlfC0lxk=
108110
github.com/tidwall/match v1.1.1 h1:+Ho715JplO36QYgwN9PGYNhgZvoUSc9X2c80KVTi+GA=
109111
github.com/tidwall/match v1.1.1/go.mod h1:eRSPERbgtNPcGhD8UCthc6PmLEQXEWd3PRB5JTxsfmM=
110112
github.com/tidwall/pretty v1.2.0 h1:RWIZEg2iJ8/g6fDDYzMpobmaoGh5OLl4AXtGUGPcqCs=
111113
github.com/tidwall/pretty v1.2.0/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
114+
github.com/tidwall/pretty v1.2.1 h1:qjsOFOWWQl+N3RsoF5/ssm1pHmJJwhjlSbZ51I6wMl4=
115+
github.com/tidwall/pretty v1.2.1/go.mod h1:ITEVvHYasfjBbM0u2Pg8T2nJnzm8xPwvNhhsoaGGjNU=
112116
github.com/tidwall/sjson v1.2.5 h1:kLy8mja+1c9jlljvWTlSazM7cKDRfJuR/bOJhcY5NcY=
113117
github.com/tidwall/sjson v1.2.5/go.mod h1:Fvgq9kS/6ociJEDnK0Fk1cpYF4FIW6ZF7LAe+6jwd28=
114118
github.com/valyala/bytebufferpool v1.0.0 h1:GqA5TC/0021Y/b9FG4Oi9Mr3q7XYx6KllzawFIhcdPw=

0 commit comments

Comments
 (0)