Skip to content

Commit

Permalink
fix tests
Browse files Browse the repository at this point in the history
  • Loading branch information
beesaferoot committed Aug 28, 2024
1 parent 8b64394 commit c2a7de8
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 12 deletions.
36 changes: 35 additions & 1 deletion api/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type Server interface {
Run() error
SetupRouter(mock bool) *gin.Engine
}

type ServerConfig struct {
Expand All @@ -36,8 +37,15 @@ func NewServer(serverConfig ServerConfig) Server {
}
}

func NewMockServer(db db.DbConn, cache cache.Cache) Server {
return &server{
db: db,
cache: cache,
}
}

func (s *server) Run() error {
router := s.setupRouter()
router := s.SetupRouter(false)

err := router.SetTrustedProxies([]string{"127.0.0.1"})
if err != nil {
Expand All @@ -47,6 +55,13 @@ func (s *server) Run() error {
return router.Run(s.conf.Addr + ":" + s.conf.Port)
}

func (s *server) SetupRouter(mock bool) *gin.Engine {
if mock {
return s.setupMockRouter()
}
return s.setupRouter()
}

func (s *server) setupRouter() *gin.Engine {
r := gin.Default()

Expand Down Expand Up @@ -95,6 +110,25 @@ func (s *server) setupRouter() *gin.Engine {
return r
}

func (s *server) setupMockRouter() *gin.Engine {
r := gin.Default()
r.Use(injectCache(s.cache))
r.POST("/upload_track", func(c *gin.Context) {
AddTrackHandler(c, db.NewTrackRepo(s.db), db.NewArtistRepo(s.db))
})
r.POST("/upload_batch_track", func(c *gin.Context) {
BulkTrackUploadHandler(c, db.NewTrackRepo(s.db))
})
r.GET("/tracks", func(c *gin.Context) {
FetchTracksHandler(c, db.NewTrackRepo(s.db))
})
r.GET("/search", func(c *gin.Context) {
FetchTracksByArtistHandler(c, db.NewTrackRepo(s.db))
})

return r
}

func createHTTPHandler(funcHandler func(*gin.Context, ...interface{}), repos ...interface{}) gin.HandlerFunc {
return func(c *gin.Context) {
funcHandler(c, repos...)
Expand Down
2 changes: 1 addition & 1 deletion db/artist_repo.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ func (r *artistRepo) GetArtistById(ctx context.Context, id int) (*Artist, error)
WHERE id = $1`
row := r.Db.QueryRow(ctx, stmt, id)

err := row.Scan(&artist.Name, artist.CreatedAt)
err := row.Scan(&artist.Name, &artist.CreatedAt)

return artist, err
}
36 changes: 26 additions & 10 deletions tests/api_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,10 @@ package tests

import (
"auxstream/api"
"auxstream/db"
"auxstream/cache"
fs "auxstream/file_system"
"context"
"fmt"
"github.com/imroc/req"
"github.com/jackc/pgx/v5"
"github.com/pashagolub/pgxmock/v2"
"github.com/stretchr/testify/require"
"log"
"net/http/httptest"
"net/url"
Expand All @@ -18,20 +14,35 @@ import (
"strconv"
"testing"
"time"

"github.com/alicebob/miniredis/v2"
"github.com/gin-gonic/gin"
"github.com/imroc/req"
"github.com/jackc/pgx/v5"
"github.com/pashagolub/pgxmock/v2"
"github.com/redis/go-redis/v9"
"github.com/stretchr/testify/require"
)

var cwd, _ = os.Getwd()
var testDataPath = filepath.Join(cwd, "testdata")
var mockConn pgxmock.PgxConnIface
var router = api.SetupTestRouter()
var router *gin.Engine

func setupTest(_ *testing.T) func(t *testing.T) {
var err error
mockConn, err = pgxmock.NewConn()
if err != nil {
log.Fatalf("Failed to set up mock database connection: %v", err)
}
db.DAO = db.NewWithMockConn(mockConn)

mr, _ := miniredis.Run()
opts := &redis.Options{
Addr: mr.Addr(),
}
r := cache.NewRedis(opts)
server := api.NewMockServer(mockConn, r)
router = server.SetupRouter(true)

return tearDownTest
}
Expand All @@ -45,6 +56,11 @@ func TestHTTPAddTrack(t *testing.T) {
defer teardown(t)

columns := []string{"id", "created_at"}
mockConn.ExpectQuery("SELECT name, created_at FROM auxstream.artists").
WithArgs(1).
WillReturnRows(pgxmock.NewRows([]string{"name", "created_at"}).
AddRow("Hike", time.Now()))

mockConn.ExpectQuery("INSERT INTO auxstream.tracks").
WithArgs("Sample Title", 1, pgxmock.AnyArg()).
WillReturnRows(pgxmock.NewRows(columns).AddRow(1, time.Now()))
Expand Down Expand Up @@ -72,12 +88,12 @@ func TestHTTPAddTrack(t *testing.T) {
File: file,
FileName: "audio",
})
require.Equal(t, post.Response().StatusCode, 200)
require.Equal(t, 200, post.Response().StatusCode)
require.Equal(t, fs.LStore.Writes(), 1)
data := &map[string]interface{}{}
err = post.ToJSON(data)
require.NoError(t, err)
//fmt.Println("response body: ", data)
// fmt.Println("response body: ", data)
}

func TestHTTPSearchByArtist(t *testing.T) {
Expand Down Expand Up @@ -166,7 +182,7 @@ func TestHTTPFetchTracks(t *testing.T) {
resp, err := req.Get(tserver.URL + "/tracks?pagesize=2&pagenumber=1")

require.NoError(t, err)
require.Equal(t, resp.Response().StatusCode, 200)
require.Equal(t, 200, resp.Response().StatusCode)
data := &map[string]interface{}{}
err = resp.ToJSON(data)
require.NoError(t, err)
Expand Down

0 comments on commit c2a7de8

Please sign in to comment.