aboutsummaryrefslogtreecommitdiffstats
path: root/queue/item_test.go
blob: 8e6375ba8aa8fca788d0ce1b3843fa92932c05ee (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package queue

import (
	"os"
	"regexp"
	"testing"
	"text/template"
	"time"

	"github.com/mpolden/lftpq/parser"
)

func newTestItem(remotePath string, itemParser itemParser) Item {
	item, _ := newItem(remotePath, time.Time{}, itemParser)
	return item
}

func showItemParser() itemParser {
	return itemParser{
		parser:   parser.Show,
		template: template.Must(template.New("t").Parse("/tmp/{{ .Name }}/S{{ .Season }}/")),
	}
}

func movieItemParser() itemParser {
	return itemParser{
		parser:   parser.Movie,
		template: template.Must(template.New("t").Parse("/tmp/{{ .Year }}/{{ .Name }}/")),
	}
}

func TestNewItemShow(t *testing.T) {
	item := newTestItem("/foo/The.Wire.S03E01", showItemParser())
	if expected := "/tmp/The.Wire/S3/The.Wire.S03E01"; item.LocalPath != expected {
		t.Fatalf("Expected %q, got %q", expected, item.LocalPath)
	}
}

func TestNewItemMovie(t *testing.T) {
	item := newTestItem("/foo/Apocalypse.Now.1979", movieItemParser())
	if expected := "/tmp/1979/Apocalypse.Now/Apocalypse.Now.1979"; item.LocalPath != expected {
		t.Fatalf("Expected %q, got %q", expected, item.LocalPath)
	}
}

func TestNewItemDefaultParser(t *testing.T) {
	tmpl := itemParser{parser: parser.Default, template: template.Must(template.New("t").Parse("/tmp/"))}
	item := newTestItem("/foo/The.Wire.S03E01", tmpl)
	if expected := "/tmp/The.Wire.S03E01"; item.LocalPath != expected {
		t.Fatalf("Expected %s, got %s", expected, item.LocalPath)
	}
}

func TestNewItemUnparsable(t *testing.T) {
	_, err := newItem("/foo/bar", time.Time{}, showItemParser())
	if err == nil {
		t.Fatal("Expected error")
	}
}

func TestNewItemWithReplacements(t *testing.T) {
	tmpl := showItemParser()
	tmpl.replacements = []Replacement{
		{pattern: regexp.MustCompile("_"), Replacement: "."},
		{pattern: regexp.MustCompile(`\.Of\.`), Replacement: ".of."},
		{pattern: regexp.MustCompile(`\.the\.`), Replacement: ".The."},
		{pattern: regexp.MustCompile(`\.And\.`), Replacement: ".and."},
	}
	var tests = []struct {
		in  Item
		out string
	}{
		{newTestItem("/foo/Game.Of.Thrones.S01E01", tmpl), "Game.of.Thrones"},
		{newTestItem("/foo/Fear.the.Walking.Dead.S01E01", tmpl), "Fear.The.Walking.Dead"},
		{newTestItem("/foo/Halt.And.Catch.Fire.S01E01", tmpl), "Halt.and.Catch.Fire"},
		{newTestItem("/foo/Top_Gear.01x01", tmpl), "Top.Gear"},
	}
	for _, tt := range tests {
		if tt.in.Media.Name != tt.out {
			t.Errorf("Expected %q, got %q", tt.out, tt.in.Media.Name)
		}
	}
}

func TestLocalPath(t *testing.T) {
	var tests = []struct {
		remotePath string
		template   string
		out        string
	}{
		{"/remote/foo", "/local/", "/local/foo"},
		{"/remote/bar", "/local/bar", "/local/bar"},
	}
	for _, tt := range tests {
		itemParser := itemParser{
			parser:   parser.Default,
			template: template.Must(template.New("t").Parse(tt.template)),
		}
		item := newTestItem(tt.remotePath, itemParser)
		if item.LocalPath != tt.out {
			t.Errorf("Expected %q, got %q", tt.out, item.LocalPath)
		}
	}
}

func TestAccept(t *testing.T) {
	item := Item{}
	item.accept("foo")
	if !item.Transfer {
		t.Error("Expected true")
	}
	if expected := "foo"; item.Reason != expected {
		t.Errorf("Expected %q, got %q", expected, item.Reason)
	}
}

func TestReject(t *testing.T) {
	item := Item{}
	item.reject("bar")
	if item.Transfer {
		t.Error("Expected false")
	}
	if expected := "bar"; item.Reason != expected {
		t.Errorf("Expected %q, got %q", expected, item.Reason)
	}
}

func TestIsEmpty(t *testing.T) {
	readDir := func(dirname string) ([]os.FileInfo, error) {
		if dirname == "/tmp/bar" {
			return []os.FileInfo{file{}}, nil
		}
		return nil, nil
	}
	var tests = []struct {
		in  Item
		out bool
	}{
		{Item{LocalPath: "/tmp/foo"}, true},
		{Item{LocalPath: "/tmp/bar"}, false},
	}
	for _, tt := range tests {
		if got := tt.in.isEmpty(readDir); got != tt.out {
			t.Errorf("Expected %t, got %t", tt.out, got)
		}
	}
}