subscription_library_test.go
| 1 | package service |
| 2 | |
| 3 | import ( |
| 4 | "os" |
| 5 | "path/filepath" |
| 6 | "testing" |
| 7 | ) |
| 8 | |
| 9 | func TestFindByVideoID(t *testing.T) { |
| 10 | lib, dir := newLibrary(t) |
| 11 | |
| 12 | writeItem(t, dir, "alpha", "name = \"Alpha\"\nvideo_id = \"aaa\"\nextractor = \"youtube\"\n", nil) |
| 13 | writeItem(t, dir, "beta", "name = \"Beta\"\nvideo_id = \"bbb\"\nextractor = \"youtube\"\n", nil) |
| 14 | // Same id but different extractor must not match. |
| 15 | writeItem(t, dir, "gamma", "name = \"Gamma\"\nvideo_id = \"aaa\"\nextractor = \"vimeo\"\n", nil) |
| 16 | |
| 17 | got, ok := lib.FindByVideoID(dir, "youtube", "aaa") |
| 18 | if !ok || filepath.Base(got) != "alpha" { |
| 19 | t.Fatalf("expected to find alpha, got %q ok=%v", got, ok) |
| 20 | } |
| 21 | |
| 22 | if _, ok := lib.FindByVideoID(dir, "youtube", "zzz"); ok { |
| 23 | t.Error("expected no match for unknown id") |
| 24 | } |
| 25 | // Incomplete key never matches. |
| 26 | if _, ok := lib.FindByVideoID(dir, "", "aaa"); ok { |
| 27 | t.Error("expected no match for empty extractor") |
| 28 | } |
| 29 | } |
| 30 | |
| 31 | func TestPruneToIDSet(t *testing.T) { |
| 32 | lib, dir := newLibrary(t) |
| 33 | |
| 34 | writeItem(t, dir, "keep", "name = \"Keep\"\nvideo_id = \"k1\"\nextractor = \"youtube\"\n", nil) |
| 35 | writeItem(t, dir, "drop", "name = \"Drop\"\nvideo_id = \"d1\"\nextractor = \"youtube\"\n", nil) |
| 36 | // An item without identity must never be pruned (we can't positively id it). |
| 37 | writeItem(t, dir, "noid", "name = \"NoId\"\n", nil) |
| 38 | |
| 39 | keep := map[string]bool{"youtube:k1": true} |
| 40 | removed, err := lib.PruneToIDSet(dir, keep) |
| 41 | if err != nil { |
| 42 | t.Fatalf("prune: %v", err) |
| 43 | } |
| 44 | if removed != 1 { |
| 45 | t.Errorf("expected 1 removed, got %d", removed) |
| 46 | } |
| 47 | if _, err := os.Stat(filepath.Join(dir, "drop")); !os.IsNotExist(err) { |
| 48 | t.Error("expected 'drop' to be removed") |
| 49 | } |
| 50 | if _, err := os.Stat(filepath.Join(dir, "keep")); err != nil { |
| 51 | t.Error("expected 'keep' to survive") |
| 52 | } |
| 53 | if _, err := os.Stat(filepath.Join(dir, "noid")); err != nil { |
| 54 | t.Error("expected unidentified item to survive") |
| 55 | } |
| 56 | } |
| 57 |