Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion queries/db.go → api/dbv1/db.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion queries/full_playlists.go → api/dbv1/full_playlists.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package queries
package dbv1

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion queries/full_tracks.go → api/dbv1/full_tracks.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package queries
package dbv1

import (
"context"
Expand Down
2 changes: 1 addition & 1 deletion queries/full_users.go → api/dbv1/full_users.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package queries
package dbv1

import (
"context"
Expand Down

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion queries/get_tracks.sql.go → api/dbv1/get_tracks.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion queries/get_users.sql.go → api/dbv1/get_users.sql.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion queries/jsonb_types.go → api/dbv1/jsonb_types.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package queries
package dbv1

type UsageConditions *struct {
UsdcPurchase *struct {
Expand Down
2 changes: 1 addition & 1 deletion queries/models.go → api/dbv1/models.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

File renamed without changes.
File renamed without changes.
File renamed without changes.
15 changes: 15 additions & 0 deletions api/fixture_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,21 @@ var (
"is_owned_by_user": false,
}

playlistBaseRow = map[string]any{
// "playlist_id": 1,
// "playlist_owner_id": 42,
"is_album": false,
"is_private": false,
"playlist_contents": "{}",
"is_current": true,
"is_delete": false,
"created_at": time.Now(),
"updated_at": time.Now(),
"txhash": "0xabcde12345",
"is_image_autogenerated": false,
"is_scheduled_release": false,
}

followBaseRow = map[string]any{
"blockhash": "block1",
"blocknumber": 101,
Expand Down
6 changes: 3 additions & 3 deletions api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"strconv"
"time"

"bridgerton.audius.co/queries"
"bridgerton.audius.co/api/dbv1"
"bridgerton.audius.co/trashid"
adapter "github.com/axiomhq/axiom-go/adapters/zap"
"github.com/axiomhq/axiom-go/axiom"
Expand Down Expand Up @@ -75,7 +75,7 @@ func NewApiServer(config Config) *ApiServer {
ErrorHandler: errorHandler(logger),
}),
pool,
queries.New(pool),
dbv1.New(pool),
logger,
}

Expand Down Expand Up @@ -139,7 +139,7 @@ func NewApiServer(config Config) *ApiServer {
type ApiServer struct {
*fiber.App
pool *pgxpool.Pool
queries *queries.Queries
queries *dbv1.Queries
logger *zap.Logger
}

Expand Down
148 changes: 39 additions & 109 deletions api/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@ package api

import (
"context"
"encoding/json"
"io"
"net/http/httptest"
"os"
"strings"
"testing"

"bridgerton.audius.co/queries"
"github.com/jackc/pgx/v5"
"github.com/stretchr/testify/assert"
)
Expand Down Expand Up @@ -63,6 +63,7 @@ func TestMain(m *testing.M) {

insertFixtures("users", userBaseRow, "testdata/user_fixtures.csv")
insertFixtures("tracks", trackBaseRow, "testdata/track_fixtures.csv")
insertFixtures("playlists", playlistBaseRow, "testdata/playlist_fixtures.csv")
insertFixtures("follows", followBaseRow, "testdata/follow_fixtures.csv")
insertFixtures("reposts", repostBaseRow, "testdata/repost_fixtures.csv")
insertFixtures("developer_apps", developerAppBaseRow, "testdata/developer_app_fixtures.csv")
Expand All @@ -73,126 +74,55 @@ func TestMain(m *testing.M) {
os.Exit(code)
}

func TestFixtures(t *testing.T) {
// as anon
{
users, err := app.queries.FullUsers(t.Context(), queries.GetUsersParams{
Handle: "rayjacobson",
})
assert.NoError(t, err)
user := users[0]
assert.Equal(t, int32(1), user.UserID)
assert.Equal(t, "7eP5n", user.ID)
assert.Equal(t, "rayjacobson", *user.Handle)
assert.False(t, user.DoesCurrentUserFollow)
assert.False(t, user.DoesFollowCurrentUser)
}

// as stereosteve
{
users, err := app.queries.FullUsers(t.Context(), queries.GetUsersParams{
MyID: 2,
Handle: "rayjacobson",
})
assert.NoError(t, err)
user := users[0]
assert.Equal(t, "rayjacobson", *user.Handle)
assert.True(t, user.DoesCurrentUserFollow)
assert.True(t, user.DoesFollowCurrentUser)
}

// stereosteve views stereosteve
{
users, err := app.queries.FullUsers(t.Context(), queries.GetUsersParams{
MyID: 2,
Ids: []int32{2},
})
assert.NoError(t, err)
user := users[0]
assert.Equal(t, "stereosteve", *user.Handle)
assert.False(t, user.DoesCurrentUserFollow)
assert.False(t, user.DoesFollowCurrentUser)
}

// multiple users
{
users, err := app.queries.FullUsers(t.Context(), queries.GetUsersParams{
MyID: 2,
Ids: []int32{1, 2, -1},
})
assert.NoError(t, err)
assert.Len(t, users, 2)
assert.Equal(t, "rayjacobson", *users[0].Handle)
assert.Equal(t, "stereosteve", *users[1].Handle)
}
func TestHome(t *testing.T) {
status, body := testGet(t, "/")
assert.Equal(t, 200, status)
assert.Equal(t, "OK", string(body))
}

func TestGetTracks(t *testing.T) {
// someone else can only see public tracks
{
tracks, err := app.queries.FullTracks(t.Context(), queries.GetTracksParams{
MyID: 1,
OwnerID: 2,
})
assert.NoError(t, err)
assert.Len(t, tracks, 1)
assert.True(t, tracks[0].HasCurrentUserReposted)
func Test200(t *testing.T) {
urls := []string{
"/v1/full/users?id=7eP5n&id=_some_invalid_hash_id",
"/v1/full/users/7eP5n/followers",
"/v1/full/users/7eP5n/following",
"/v1/full/users/7eP5n/mutuals",
"/v1/full/users/7eP5n/supporting",

"/v1/full/tracks?id=eYJyn",
"/v1/full/tracks/eYJyn/reposts",
"/v1/full/tracks/eYJyn/favorites",

"/v1/full/playlists?id=7eP5n",
"/v1/full/playlists/7eP5n/reposts",
"/v1/full/playlists/7eP5n/favorites",
}

// I can see all my tracks
{
tracks, err := app.queries.FullTracks(t.Context(), queries.GetTracksParams{
MyID: 2,
OwnerID: 2,
})
assert.NoError(t, err)
assert.Len(t, tracks, 2)
}
for _, u := range urls {
status, _ := testGet(t, u)
assert.Equal(t, 200, status, u)

{
tracks, err := app.queries.FullTracks(t.Context(), queries.GetTracksParams{
MyID: 2,
Ids: []int32{301},
})
assert.NoError(t, err)
track := tracks[0]
assert.Equal(t, 135.0, track.DownloadConditions.UsdcPurchase.Price)
// also test as a user
if strings.Contains(u, "?") {
u += "&user_id=7eP5n"
} else {
u += "?user_id=7eP5n"
}

status, _ = testGet(t, u)
assert.Equal(t, 200, status, u)
}
}

func TestGetDeveloperAppsQueries(t *testing.T) {
userId := int32(1)
developerApps, err := app.queries.GetDeveloperAppsByUser(t.Context(), &userId)
assert.NoError(t, err)
assert.Len(t, developerApps, 1)
assert.Equal(t, "0x7d7b6b7a97d1deefe3a1ccc5a13c48e8f055e0b6", developerApps[0].Address)
}

func TestGetDeveloperApp(t *testing.T) {
status, body := testGet(t, "/v1/developer_apps/0x7d7b6b7a97d1deefe3a1ccc5a13c48e8f055e0b6")
assert.Equal(t, 200, status)
assert.True(t, strings.Contains(string(body), `"user_id":"7eP5n"`))
}

func TestHome(t *testing.T) {
status, body := testGet(t, "/")
assert.Equal(t, 200, status)
assert.Equal(t, "OK", string(body))
}

func TestGetUser(t *testing.T) {
status, body := testGet(t, "/v1/full/users?id=1")
assert.Equal(t, 200, status)
assert.True(t, strings.Contains(string(body), `"handle":"rayjacobson"`))
assert.True(t, strings.Contains(string(body), `"user_id":1`))
assert.True(t, strings.Contains(string(body), `"id":"7eP5n"`))
}

func testGet(t *testing.T, path string) (int, []byte) {
func testGet(t *testing.T, path string, dest ...any) (int, []byte) {
req := httptest.NewRequest("GET", path, nil)
res, err := app.Test(req, -1)
assert.NoError(t, err)
body, _ := io.ReadAll(res.Body)

if len(dest) > 0 {
err = json.Unmarshal(body, &dest[0])
assert.NoError(t, err)
}

return res.StatusCode, body
}
2 changes: 2 additions & 0 deletions api/testdata/playlist_fixtures.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
playlist_id,playlist_name,playlist_owner_id,is_album
1,First,1,f
7 changes: 1 addition & 6 deletions api/v1_developer_apps.go
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
package api

import (
"context"
"strings"

"bridgerton.audius.co/trashid"
"github.com/gofiber/fiber/v2"
"go.uber.org/zap"
)

func (as *ApiServer) v1DeveloperAppByAddress(c *fiber.Ctx) error {
Expand All @@ -20,11 +18,8 @@ func (as *ApiServer) v1DeveloperAppByAddress(c *fiber.Ctx) error {
address = "0x" + address
}

developerApp, err := as.queries.GetDeveloperAppByAddress(context.Background(), address)
developerApp, err := as.queries.GetDeveloperAppByAddress(c.Context(), address)
if err != nil {
as.logger.Error("Failed to get developer app",
zap.String("address", address),
zap.Error(err))
return err
}

Expand Down
22 changes: 22 additions & 0 deletions api/v1_developer_apps_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package api

import (
"strings"
"testing"

"github.com/stretchr/testify/assert"
)

func TestGetDeveloperAppsQueries(t *testing.T) {
userId := int32(1)
developerApps, err := app.queries.GetDeveloperAppsByUser(t.Context(), &userId)
assert.NoError(t, err)
assert.Len(t, developerApps, 1)
assert.Equal(t, "0x7d7b6b7a97d1deefe3a1ccc5a13c48e8f055e0b6", developerApps[0].Address)
}

func TestGetDeveloperApp(t *testing.T) {
status, body := testGet(t, "/v1/developer_apps/0x7d7b6b7a97d1deefe3a1ccc5a13c48e8f055e0b6")
assert.Equal(t, 200, status)
assert.True(t, strings.Contains(string(body), `"user_id":"7eP5n"`))
}
4 changes: 2 additions & 2 deletions api/v1_playlists.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package api

import (
"bridgerton.audius.co/queries"
"bridgerton.audius.co/api/dbv1"
"bridgerton.audius.co/trashid"
"github.com/gofiber/fiber/v2"
"github.com/jackc/pgx/v5"
Expand All @@ -11,7 +11,7 @@ func (app *ApiServer) v1playlists(c *fiber.Ctx) error {
myId, _ := trashid.DecodeHashId(c.Query("user_id"))
ids := decodeIdList(c)

playlists, err := app.queries.FullPlaylists(c.Context(), queries.GetPlaylistsParams{
playlists, err := app.queries.FullPlaylists(c.Context(), dbv1.GetPlaylistsParams{
MyID: myId,
Ids: ids,
})
Expand Down
Loading