Раскладка файлов после распознавния
This commit is contained in:
@@ -0,0 +1,172 @@
|
||||
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 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)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user