Files
meilisearch-showcase/internal/domain/catalog/movie_test.go
T

69 lines
1.6 KiB
Go

package catalog_test
import (
"errors"
"testing"
"git.jokresner.de/johannes/meilisearch-showcase/internal/domain/catalog"
)
func TestNewMovieInvariants(t *testing.T) {
cases := []struct {
name string
id catalog.MovieID
title string
year int
rating float64
wantErr bool
}{
{"ok", "1", "Alien", 1979, 8.5, false},
{"empty id", "", "Alien", 1979, 8.5, true},
{"blank title", "1", " ", 1979, 8.5, true},
{"pre-cinema", "1", "Alien", 1800, 8.5, true},
{"rating too high", "1", "Alien", 1979, 11, true},
{"rating negative", "1", "Alien", 1979, -1, true},
}
for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
m, err := catalog.NewMovie(tc.id, tc.title, "", nil, tc.year, tc.rating)
if tc.wantErr {
if !errors.Is(err, catalog.ErrInvalidMovie) {
t.Fatalf("want ErrInvalidMovie, got %v", err)
}
return
}
if err != nil {
t.Fatalf("unexpected error: %v", err)
}
if m.Title() != "Alien" {
t.Fatalf("title not trimmed/kept: %q", m.Title())
}
})
}
}
func TestGenresAreCopied(t *testing.T) {
m, err := catalog.NewMovie("1", "Alien", "", []string{"Horror", " ", "SciFi"}, 1979, 8.5)
if err != nil {
t.Fatal(err)
}
g := m.Genres()
if len(g) != 2 {
t.Fatalf("blank genre not dropped: %v", g)
}
g[0] = "mutated"
if m.Genres()[0] != "Horror" {
t.Fatal("Genres() leaked internal slice")
}
}
func TestQueryNormalize(t *testing.T) {
q := catalog.Query{Limit: 0, Offset: -5}.Normalize()
if q.Limit != 20 || q.Offset != 0 {
t.Fatalf("bad defaults: %+v", q)
}
if (catalog.Query{Limit: 1000}).Normalize().Limit != 20 {
t.Fatal("limit not capped")
}
}