diff --git a/api/server.go b/api/server.go index 766ae74..80010db 100644 --- a/api/server.go +++ b/api/server.go @@ -14,6 +14,7 @@ import ( type Server interface { Run() error + SetupRouter(mock bool) *gin.Engine } type ServerConfig struct { @@ -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 { @@ -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() @@ -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...) diff --git a/db/artist_repo.go b/db/artist_repo.go index 0e565b7..53a4a8f 100644 --- a/db/artist_repo.go +++ b/db/artist_repo.go @@ -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 } diff --git a/tests/api_test.go b/tests/api_test.go index f9789e2..fb17d9a 100644 --- a/tests/api_test.go +++ b/tests/api_test.go @@ -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" @@ -18,12 +14,20 @@ 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 @@ -31,7 +35,14 @@ func setupTest(_ *testing.T) func(t *testing.T) { 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 } @@ -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())) @@ -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) { @@ -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)