Files
golib/tmdb/api_test.go

95 lines
1.9 KiB
Go

package tmdb
import (
"os"
"testing"
)
func TestNewAPIConnection_Success(t *testing.T) {
// Skip if no API token is provided
token := os.Getenv("TMDB_TOKEN")
if token == "" {
t.Skip("Skipping integration test: TMDB_TOKEN not set")
}
api, err := NewAPIConnection()
if err != nil {
t.Fatalf("NewAPIConnection() failed: %v", err)
}
if api == nil {
t.Fatal("NewAPIConnection() returned nil API")
}
if api.token == "" {
t.Error("API token should not be empty")
}
if api.Config == nil {
t.Error("API config should be loaded")
}
t.Log("API connection created successfully")
}
func TestNewAPIConnection_NoToken(t *testing.T) {
// Temporarily unset the token
originalToken := os.Getenv("TMDB_TOKEN")
os.Unsetenv("TMDB_TOKEN")
defer func() {
if originalToken != "" {
os.Setenv("TMDB_TOKEN", originalToken)
}
}()
api, err := NewAPIConnection()
if err == nil {
t.Error("NewAPIConnection() should fail without token")
}
if api != nil {
t.Error("NewAPIConnection() should return nil API on error")
}
if err.Error() != "No TMDB API Token provided" {
t.Errorf("expected 'No TMDB API Token provided' error, got: %v", err)
}
}
func TestAPI_Struct(t *testing.T) {
config := &Config{
Image: Image{
SecureBaseURL: "https://image.tmdb.org/t/p/",
},
}
api := &API{
Config: config,
token: "test-token",
}
// Verify struct fields are accessible
if api.token != "test-token" {
t.Error("API token field not accessible")
}
if api.Config == nil {
t.Error("API config field should not be nil")
}
if api.Config.Image.SecureBaseURL != "https://image.tmdb.org/t/p/" {
t.Error("API config not properly set")
}
}
func TestAPI_TokenHandling(t *testing.T) {
// Test that token is properly stored and accessible
api := &API{
token: "test-token-123",
}
if api.token != "test-token-123" {
t.Error("Token not properly stored in API struct")
}
}