234 lines
6.3 KiB
Go
234 lines
6.3 KiB
Go
package store
|
|
|
|
import (
|
|
"context"
|
|
"database/sql"
|
|
"testing"
|
|
)
|
|
|
|
func seedDownload(t *testing.T, st *Store) int64 {
|
|
t.Helper()
|
|
id, err := st.CreateDownload(context.Background(),
|
|
newDownloading("aabbccddeeff00112233445566778899aabbccdd"))
|
|
if err != nil {
|
|
t.Fatalf("seed download: %v", err)
|
|
}
|
|
return id
|
|
}
|
|
|
|
func TestCreateRecognition_AttemptsAndCurrent(t *testing.T) {
|
|
st := newTestStore(t)
|
|
ctx := context.Background()
|
|
dl := seedDownload(t, st)
|
|
|
|
id1, err := st.CreateRecognition(ctx, &Recognition{
|
|
DownloadID: dl,
|
|
MediaType: NullString("series"),
|
|
Title: NullString("Show"),
|
|
Year: sql.NullInt64{Int64: 2006, Valid: true},
|
|
Plan: NullString(`{"type":"series"}`),
|
|
}, []string{"нет матча в базе"})
|
|
if err != nil {
|
|
t.Fatalf("create #1: %v", err)
|
|
}
|
|
|
|
id2, err := st.CreateRecognition(ctx, &Recognition{
|
|
DownloadID: dl,
|
|
MediaType: NullString("movie"),
|
|
Title: NullString("Show v2"),
|
|
}, []string{"уточнено"})
|
|
if err != nil {
|
|
t.Fatalf("create #2: %v", err)
|
|
}
|
|
if id2 == id1 {
|
|
t.Fatal("ids must differ")
|
|
}
|
|
|
|
cur, err := st.GetCurrentRecognition(ctx, dl)
|
|
if err != nil {
|
|
t.Fatalf("get current: %v", err)
|
|
}
|
|
if cur.ID != id2 {
|
|
t.Errorf("current id = %d, want %d", cur.ID, id2)
|
|
}
|
|
if cur.AttemptNo != 2 {
|
|
t.Errorf("attempt_no = %d, want 2", cur.AttemptNo)
|
|
}
|
|
if !cur.IsCurrent {
|
|
t.Error("current recognition must have is_current = true")
|
|
}
|
|
if cur.Title.String != "Show v2" {
|
|
t.Errorf("title = %q", cur.Title.String)
|
|
}
|
|
if got := cur.ReasonList(); len(got) != 1 || got[0] != "уточнено" {
|
|
t.Errorf("reasons = %v", got)
|
|
}
|
|
}
|
|
|
|
func TestGetCurrentRecognition_None(t *testing.T) {
|
|
st := newTestStore(t)
|
|
dl := seedDownload(t, st)
|
|
cur, err := st.GetCurrentRecognition(context.Background(), dl)
|
|
if err != nil {
|
|
t.Fatalf("get current: %v", err)
|
|
}
|
|
if cur != nil {
|
|
t.Errorf("want nil, got %+v", cur)
|
|
}
|
|
}
|
|
|
|
func TestHints(t *testing.T) {
|
|
st := newTestStore(t)
|
|
ctx := context.Background()
|
|
dl := seedDownload(t, st)
|
|
|
|
for _, h := range []string{"второй сезон", "рус+англ дорожки"} {
|
|
if err := st.AddHint(ctx, dl, h); err != nil {
|
|
t.Fatalf("add hint: %v", err)
|
|
}
|
|
}
|
|
got, err := st.ListHints(ctx, dl)
|
|
if err != nil {
|
|
t.Fatalf("list hints: %v", err)
|
|
}
|
|
if len(got) != 2 || got[0] != "второй сезон" || got[1] != "рус+англ дорожки" {
|
|
t.Errorf("hints = %v", got)
|
|
}
|
|
}
|
|
|
|
func TestOverrides_Upsert(t *testing.T) {
|
|
st := newTestStore(t)
|
|
ctx := context.Background()
|
|
dl := seedDownload(t, st)
|
|
|
|
if err := st.SetOverride(ctx, dl, "media_type", "series"); err != nil {
|
|
t.Fatalf("set override: %v", err)
|
|
}
|
|
if err := st.SetOverride(ctx, dl, "media_type", "movie"); err != nil { // перезапись
|
|
t.Fatalf("override upsert: %v", err)
|
|
}
|
|
if err := st.SetOverride(ctx, dl, "ignored_files", `["sample.mkv"]`); err != nil {
|
|
t.Fatalf("set override 2: %v", err)
|
|
}
|
|
|
|
got, err := st.ListOverrides(ctx, dl)
|
|
if err != nil {
|
|
t.Fatalf("list overrides: %v", err)
|
|
}
|
|
if got["media_type"] != "movie" {
|
|
t.Errorf("media_type = %q, want movie (upsert)", got["media_type"])
|
|
}
|
|
if got["ignored_files"] != `["sample.mkv"]` {
|
|
t.Errorf("ignored_files = %q", got["ignored_files"])
|
|
}
|
|
}
|
|
|
|
func TestFileLinks_BatchLifecycle(t *testing.T) {
|
|
st := newTestStore(t)
|
|
ctx := context.Background()
|
|
dl := seedDownload(t, st)
|
|
|
|
batch := "batch-1"
|
|
links := []FileLink{
|
|
{DownloadID: dl, ApplyBatchID: batch, SrcPath: "/d/a.mkv", DstPath: "/m/A.mkv", Kind: "video", Status: "linked"},
|
|
{DownloadID: dl, ApplyBatchID: batch, SrcPath: "/d/a.srt", DstPath: "/m/A.ru.srt", Kind: "subtitle", Status: "linked"},
|
|
}
|
|
if err := st.CreateFileLinks(ctx, links); err != nil {
|
|
t.Fatalf("create links: %v", err)
|
|
}
|
|
|
|
latest, err := st.LatestBatchID(ctx, dl)
|
|
if err != nil || latest != batch {
|
|
t.Fatalf("latest batch = %q, %v", latest, err)
|
|
}
|
|
|
|
got, err := st.ListFileLinksByBatch(ctx, batch)
|
|
if err != nil {
|
|
t.Fatalf("list by batch: %v", err)
|
|
}
|
|
if len(got) != 2 || got[0].DstPath != "/m/A.mkv" {
|
|
t.Errorf("links = %+v", got)
|
|
}
|
|
|
|
if err := st.DeleteFileLinksByBatch(ctx, batch); err != nil {
|
|
t.Fatalf("delete batch: %v", err)
|
|
}
|
|
after, _ := st.ListFileLinksByBatch(ctx, batch)
|
|
if len(after) != 0 {
|
|
t.Errorf("links remain after delete: %+v", after)
|
|
}
|
|
}
|
|
|
|
func TestCandidates_Lifecycle(t *testing.T) {
|
|
st := newTestStore(t)
|
|
ctx := context.Background()
|
|
dl := seedDownload(t, st)
|
|
recID, err := st.CreateRecognition(ctx, &Recognition{DownloadID: dl}, nil)
|
|
if err != nil {
|
|
t.Fatalf("create recognition: %v", err)
|
|
}
|
|
|
|
cands := []MetadataCandidate{
|
|
{RecognitionID: recID, Provider: "tvdb", ProviderID: "269613",
|
|
Title: NullString("Fargo"), Year: sql.NullInt64{Int64: 2014, Valid: true}},
|
|
{RecognitionID: recID, Provider: "tmdb", ProviderID: "60622",
|
|
Title: NullString("Fargo")},
|
|
}
|
|
if err := st.CreateCandidates(ctx, cands); err != nil {
|
|
t.Fatalf("create candidates: %v", err)
|
|
}
|
|
|
|
got, err := st.ListCandidatesByRecognition(ctx, recID)
|
|
if err != nil {
|
|
t.Fatalf("list: %v", err)
|
|
}
|
|
if len(got) != 2 || got[0].Provider != "tvdb" || got[0].ProviderID != "269613" {
|
|
t.Fatalf("candidates = %+v", got)
|
|
}
|
|
|
|
chosenID := got[0].ID
|
|
if err := st.SetCandidateChosen(ctx, recID, chosenID); err != nil {
|
|
t.Fatalf("set chosen: %v", err)
|
|
}
|
|
got, _ = st.ListCandidatesByRecognition(ctx, recID)
|
|
for _, c := range got {
|
|
want := c.ID == chosenID
|
|
if c.Chosen != want {
|
|
t.Errorf("candidate %d chosen = %v, want %v", c.ID, c.Chosen, want)
|
|
}
|
|
}
|
|
|
|
// GetCandidate + переотметка.
|
|
single, err := st.GetCandidate(ctx, got[1].ID)
|
|
if err != nil || single == nil || single.Provider != "tmdb" {
|
|
t.Fatalf("get candidate = %+v, %v", single, err)
|
|
}
|
|
if err := st.SetCandidateChosen(ctx, recID, got[1].ID); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
got, _ = st.ListCandidatesByRecognition(ctx, recID)
|
|
if got[0].Chosen || !got[1].Chosen {
|
|
t.Errorf("re-choose failed: %+v", got)
|
|
}
|
|
}
|
|
|
|
func TestGetCandidate_None(t *testing.T) {
|
|
st := newTestStore(t)
|
|
c, err := st.GetCandidate(context.Background(), 999)
|
|
if err != nil || c != nil {
|
|
t.Errorf("want nil,nil; got %+v, %v", c, err)
|
|
}
|
|
}
|
|
|
|
func TestLatestBatchID_None(t *testing.T) {
|
|
st := newTestStore(t)
|
|
dl := seedDownload(t, st)
|
|
latest, err := st.LatestBatchID(context.Background(), dl)
|
|
if err != nil {
|
|
t.Fatalf("latest batch: %v", err)
|
|
}
|
|
if latest != "" {
|
|
t.Errorf("want empty, got %q", latest)
|
|
}
|
|
}
|