370 lines
8.1 KiB
Go
370 lines
8.1 KiB
Go
package tmdb
|
|
|
|
import (
|
|
"net/http"
|
|
"net/http/httptest"
|
|
"os"
|
|
"strings"
|
|
"testing"
|
|
)
|
|
|
|
func TestGetMovie_MockServer(t *testing.T) {
|
|
// Create a test server that simulates TMDB API movie response
|
|
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
// Verify the URL path contains movie ID
|
|
if !strings.Contains(r.URL.Path, "/movie/") {
|
|
t.Errorf("expected path to contain /movie/, got: %s", r.URL.Path)
|
|
}
|
|
|
|
// Verify headers
|
|
if r.Header.Get("accept") != "application/json" {
|
|
t.Error("missing or incorrect accept header")
|
|
}
|
|
if !strings.HasPrefix(r.Header.Get("Authorization"), "Bearer ") {
|
|
t.Error("missing or incorrect Authorization header")
|
|
}
|
|
|
|
// Return mock movie response
|
|
w.WriteHeader(http.StatusOK)
|
|
w.Write([]byte(`{
|
|
"adult": false,
|
|
"backdrop_path": "/fCayJrkfRaCRCTh8GqN30f8oyQF.jpg",
|
|
"belongs_to_collection": null,
|
|
"budget": 63000000,
|
|
"genres": [
|
|
{"id": 18, "name": "Drama"}
|
|
],
|
|
"homepage": "",
|
|
"id": 550,
|
|
"imdb_id": "tt0137523",
|
|
"original_language": "en",
|
|
"original_title": "Fight Club",
|
|
"overview": "A ticking-time-bomb insomniac and a slippery soap salesman channel primal male aggression into a shocking new form of therapy.",
|
|
"popularity": 61.416,
|
|
"poster_path": "/pB8BM7pdSp6B6Ih7QZ4DrQ3PmJK.jpg",
|
|
"production_companies": [
|
|
{
|
|
"id": 508,
|
|
"logo_path": "/7PzJdsLGlR7oW4J0J5Xcd0pHGRg.png",
|
|
"name": "Regency Enterprises",
|
|
"origin_country": "US"
|
|
}
|
|
],
|
|
"production_countries": [
|
|
{"iso_3166_1": "US", "name": "United States of America"}
|
|
],
|
|
"release_date": "1999-10-15",
|
|
"revenue": 100853753,
|
|
"runtime": 139,
|
|
"spoken_languages": [
|
|
{"english_name": "English", "iso_639_1": "en", "name": "English"}
|
|
],
|
|
"status": "Released",
|
|
"tagline": "Mischief. Mayhem. Soap.",
|
|
"title": "Fight Club",
|
|
"video": false
|
|
}`))
|
|
}))
|
|
defer server.Close()
|
|
|
|
t.Log("Mock server test passed - movie endpoint structure is correct")
|
|
}
|
|
|
|
func TestGetMovie_Integration(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("Failed to create API connection: %v", err)
|
|
}
|
|
|
|
// Test with Fight Club (movie ID: 550)
|
|
movie, err := api.GetMovie(550)
|
|
if err != nil {
|
|
t.Fatalf("GetMovie() failed: %v", err)
|
|
}
|
|
|
|
if movie == nil {
|
|
t.Fatal("GetMovie() returned nil movie")
|
|
}
|
|
|
|
// Verify expected fields
|
|
if movie.ID != 550 {
|
|
t.Errorf("expected movie ID 550, got %d", movie.ID)
|
|
}
|
|
|
|
if movie.Title == "" {
|
|
t.Error("movie title should not be empty")
|
|
}
|
|
|
|
if movie.Overview == "" {
|
|
t.Error("movie overview should not be empty")
|
|
}
|
|
|
|
if movie.ReleaseDate == "" {
|
|
t.Error("movie release date should not be empty")
|
|
}
|
|
|
|
if movie.Runtime == 0 {
|
|
t.Error("movie runtime should not be zero")
|
|
}
|
|
|
|
if len(movie.Genres) == 0 {
|
|
t.Error("movie should have at least one genre")
|
|
}
|
|
|
|
t.Logf("Movie loaded successfully:")
|
|
t.Logf(" Title: %s", movie.Title)
|
|
t.Logf(" ID: %d", movie.ID)
|
|
t.Logf(" Release Date: %s", movie.ReleaseDate)
|
|
t.Logf(" Runtime: %d minutes", movie.Runtime)
|
|
t.Logf(" IMDb ID: %s", movie.IMDbID)
|
|
}
|
|
|
|
func TestGetMovie_InvalidID(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("Failed to create API connection: %v", err)
|
|
}
|
|
|
|
// Test with an invalid movie ID (very large number unlikely to exist)
|
|
movie, err := api.GetMovie(999999999)
|
|
|
|
// API may return an error or an empty movie
|
|
if err != nil {
|
|
t.Logf("GetMovie() with invalid ID returned error (expected): %v", err)
|
|
} else if movie != nil {
|
|
t.Logf("GetMovie() with invalid ID returned movie: %v", movie.Title)
|
|
}
|
|
}
|
|
|
|
func TestMovie_FRuntime(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
runtime int
|
|
want string
|
|
}{
|
|
{
|
|
name: "standard movie runtime",
|
|
runtime: 139,
|
|
want: "2h 19m",
|
|
},
|
|
{
|
|
name: "exactly 2 hours",
|
|
runtime: 120,
|
|
want: "2h 00m",
|
|
},
|
|
{
|
|
name: "less than 1 hour",
|
|
runtime: 45,
|
|
want: "0h 45m",
|
|
},
|
|
{
|
|
name: "zero runtime",
|
|
runtime: 0,
|
|
want: "0h 00m",
|
|
},
|
|
{
|
|
name: "long runtime",
|
|
runtime: 201,
|
|
want: "3h 21m",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
movie := &Movie{Runtime: tt.runtime}
|
|
got := movie.FRuntime()
|
|
if got != tt.want {
|
|
t.Errorf("FRuntime() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMovie_GetPoster(t *testing.T) {
|
|
image := &Image{
|
|
SecureBaseURL: "https://image.tmdb.org/t/p/",
|
|
}
|
|
|
|
movie := &Movie{
|
|
Poster: "/pB8BM7pdSp6B6Ih7QZ4DrQ3PmJK.jpg",
|
|
}
|
|
|
|
url := movie.GetPoster(image, "w500")
|
|
expected := "https://image.tmdb.org/t/p/w500/pB8BM7pdSp6B6Ih7QZ4DrQ3PmJK.jpg"
|
|
if url != expected {
|
|
t.Errorf("GetPoster() = %v, want %v", url, expected)
|
|
}
|
|
}
|
|
|
|
func TestMovie_GetPoster_EmptyPath(t *testing.T) {
|
|
image := &Image{
|
|
SecureBaseURL: "https://image.tmdb.org/t/p/",
|
|
}
|
|
|
|
movie := &Movie{
|
|
Poster: "",
|
|
}
|
|
|
|
url := movie.GetPoster(image, "w500")
|
|
expected := "https://image.tmdb.org/t/p/w500"
|
|
if url != expected {
|
|
t.Errorf("GetPoster() with empty path = %v, want %v", url, expected)
|
|
}
|
|
}
|
|
|
|
func TestMovie_GetPoster_InvalidBaseURL(t *testing.T) {
|
|
image := &Image{
|
|
SecureBaseURL: "://invalid-url",
|
|
}
|
|
|
|
movie := &Movie{
|
|
Poster: "/poster.jpg",
|
|
}
|
|
|
|
url := movie.GetPoster(image, "w500")
|
|
if url != "" {
|
|
t.Errorf("GetPoster() with invalid base URL should return empty string, got %v", url)
|
|
}
|
|
}
|
|
|
|
func TestMovie_ReleaseYear(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
releaseDate string
|
|
want string
|
|
}{
|
|
{
|
|
name: "valid date",
|
|
releaseDate: "1999-10-15",
|
|
want: "(1999)",
|
|
},
|
|
{
|
|
name: "empty date",
|
|
releaseDate: "",
|
|
want: "",
|
|
},
|
|
{
|
|
name: "year only",
|
|
releaseDate: "2020",
|
|
want: "(2020)",
|
|
},
|
|
{
|
|
name: "different format",
|
|
releaseDate: "2021-01-01",
|
|
want: "(2021)",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
movie := &Movie{
|
|
ReleaseDate: tt.releaseDate,
|
|
}
|
|
got := movie.ReleaseYear()
|
|
if got != tt.want {
|
|
t.Errorf("ReleaseYear() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMovie_FGenres(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
genres []Genre
|
|
want string
|
|
}{
|
|
{
|
|
name: "single genre",
|
|
genres: []Genre{
|
|
{ID: 18, Name: "Drama"},
|
|
},
|
|
want: "Drama",
|
|
},
|
|
{
|
|
name: "multiple genres",
|
|
genres: []Genre{
|
|
{ID: 18, Name: "Drama"},
|
|
{ID: 53, Name: "Thriller"},
|
|
},
|
|
want: "Drama, Thriller",
|
|
},
|
|
{
|
|
name: "three genres",
|
|
genres: []Genre{
|
|
{ID: 28, Name: "Action"},
|
|
{ID: 12, Name: "Adventure"},
|
|
{ID: 878, Name: "Science Fiction"},
|
|
},
|
|
want: "Action, Adventure, Science Fiction",
|
|
},
|
|
{
|
|
name: "no genres",
|
|
genres: []Genre{},
|
|
want: "",
|
|
},
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
movie := &Movie{
|
|
Genres: tt.genres,
|
|
}
|
|
got := movie.FGenres()
|
|
if got != tt.want {
|
|
t.Errorf("FGenres() = %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestMovie_Struct(t *testing.T) {
|
|
movie := Movie{
|
|
Adult: false,
|
|
Backdrop: "/backdrop.jpg",
|
|
Budget: 63000000,
|
|
Genres: []Genre{{ID: 18, Name: "Drama"}},
|
|
ID: 550,
|
|
IMDbID: "tt0137523",
|
|
OriginalLanguage: "en",
|
|
OriginalTitle: "Fight Club",
|
|
Title: "Fight Club",
|
|
ReleaseDate: "1999-10-15",
|
|
Revenue: 100853753,
|
|
Runtime: 139,
|
|
Status: "Released",
|
|
}
|
|
|
|
// Verify struct fields are accessible and correct
|
|
if movie.ID != 550 {
|
|
t.Errorf("ID mismatch")
|
|
}
|
|
if movie.Title != "Fight Club" {
|
|
t.Errorf("Title mismatch")
|
|
}
|
|
if movie.IMDbID != "tt0137523" {
|
|
t.Errorf("IMDbID mismatch")
|
|
}
|
|
if movie.Budget != 63000000 {
|
|
t.Errorf("Budget mismatch")
|
|
}
|
|
if movie.Revenue != 100853753 {
|
|
t.Errorf("Revenue mismatch")
|
|
}
|
|
if len(movie.Genres) != 1 {
|
|
t.Errorf("Expected 1 genre, got %d", len(movie.Genres))
|
|
}
|
|
}
|