Added better route test cases and some minor cleanup

This commit is contained in:
froge 2025-02-12 13:00:12 +10:00
parent a4acb8a873
commit eb57f3fb83
Signed by: froge
GPG key ID: A825E09930271BFA
4 changed files with 25 additions and 4 deletions

19
auth_test.go Normal file
View file

@ -0,0 +1,19 @@
package main
import (
"testing"
"github.com/stretchr/testify/assert"
)
func TestgetSpotifyAuthURL(t *testing.T) {
// The result of this call is a serialized SpotifyToken struct
// The data is dynamic so we can't easily check it but we validate types/fields
res, err := getSpotifyToken(spotifyClientID, spotifyClientSecret)
if err != nil {
assert.Fail(t, err.Error())
}
testRes := &SpotifyToken{AccessToken: "example", TokenType: "Bearer", ExpiresIn: 100}
assert.IsType(t, testRes, res)
// This should always be "Bearer" according to spotify docs
assert.Equal(t, "Bearer", res.TokenType)
}

View file

@ -4,6 +4,7 @@ import (
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"log/slog"
"fmt"
)
type ArtistProfile struct {
@ -22,7 +23,8 @@ type Genre struct {
func setupTestDatabase(name string) *gorm.DB {
slog.Info("[GOMUSIC] Setting up new test database in memory")
// Open a named DB instance so each test has a clean environment
db, err := gorm.Open(sqlite.Open("file:test1?mode=memory&cache=shared"), &gorm.Config{})
dbName := fmt.Sprintf("file:%s?mode=memory&cache=shared", name)
db, err := gorm.Open(sqlite.Open(dbName), &gorm.Config{})
if err != nil {
panic("Failed to open database")
}

View file

@ -34,7 +34,7 @@ func main() {
slog.Warn("[GOMUSIC] No Spotify secret configured in 'SPOTIFY_SECRET' environment variable")
}
var db = setupDatabase()
db := setupDatabase()
env := &Env{db: db}
// Router/middleware and server setup

View file

@ -45,9 +45,9 @@ func TestGetArtistByIDRoute(t *testing.T) {
assert.Equal(t, "Pitbull", spotifyResp.Name)
var artist ArtistProfile
dbResult := env.db.First(&artist)
dbResult := env.db.Take(&artist)
if dbResult.Error != nil {
assert.Fail(t, fmt.Sprintf("Failed to retrieve new info from test database: %s", err.Error()))
assert.Fail(t, "Failed to retrieve new info from test database")
}
assert.Equal(t, "Pitbull", artist.Name)