Browse Source

added tests

Stephen McQuay 3 years ago
parent
commit
035375996e
No known key found for this signature in database

+ 299
- 0
arrange_test.go View File

@@ -0,0 +1,299 @@
1
+package arrange
2
+
3
+import (
4
+	"fmt"
5
+	"io/ioutil"
6
+	"os"
7
+	"path/filepath"
8
+	"testing"
9
+	"time"
10
+)
11
+
12
+func TestFileMove(t *testing.T) {
13
+	wd, err := os.Getwd()
14
+	if err != nil {
15
+		t.Fatal(err)
16
+	}
17
+	tmp, err := ioutil.TempDir("", "arrange-tests-")
18
+	if err != nil {
19
+		t.Fatal(err)
20
+	}
21
+	defer func() {
22
+		os.RemoveAll(tmp)
23
+	}()
24
+	if err := PrepOutput(tmp); err != nil {
25
+		t.Fatal(err)
26
+	}
27
+
28
+	tests := []struct {
29
+		path     string
30
+		expected []string
31
+		ts       time.Time
32
+	}{
33
+		{
34
+			path: filepath.Join(wd, "testdata", "lenna.png"),
35
+			expected: []string{
36
+				"date/2012/10/1350815400000000000.png",
37
+				"content/81/4a0034f5549e957ee61360d87457e5.png",
38
+			},
39
+			ts: time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
40
+		},
41
+		{
42
+			path: filepath.Join(wd, "testdata", "valid.jpg"),
43
+			expected: []string{
44
+				"date/2012/10/1350815400000000001.jpg",
45
+				"content/77/d2a6bf840622331df62963174df72d.jpg",
46
+			},
47
+			ts: time.Date(2012, 10, 21, 10, 30, 0, 1, time.UTC),
48
+		},
49
+		{
50
+			path: filepath.Join(wd, "testdata", "stott.gif"),
51
+			expected: []string{
52
+				"date/2012/10/1350815400000000001.gif",
53
+				"content/9e/f476f6e1ee9ecf64ca443eb63e4655.gif",
54
+			},
55
+			ts: time.Date(2012, 10, 21, 10, 30, 0, 1, time.UTC),
56
+		},
57
+		{
58
+			path: filepath.Join(wd, "testdata", "exif-decode-error.jpg"),
59
+			expected: []string{
60
+				"date/2006/03/1143508767000000000.jpg",
61
+				"content/4e/08875f21249becbc18bc978b35a9b4.jpg",
62
+			},
63
+			ts: time.Date(2012, 10, 21, 10, 30, 0, 2, time.UTC),
64
+		},
65
+		{
66
+			path: filepath.Join(wd, "testdata", "no-exif-but-good.jpg"),
67
+			expected: []string{
68
+				"date/2012/10/1350815400000000003.jpg",
69
+				"content/a2/7735c61b375da9012a33dbdb548274.jpg",
70
+			},
71
+			ts: time.Date(2012, 10, 21, 10, 30, 0, 3, time.UTC),
72
+		},
73
+		{
74
+			path: filepath.Join(wd, "testdata", "bad.mov"),
75
+			expected: []string{
76
+				"date/2012/10/1350815400000000004.mov",
77
+				"content/d4/1d8cd98f00b204e9800998ecf8427e.mov",
78
+			},
79
+			ts: time.Date(2012, 10, 21, 10, 30, 0, 4, time.UTC),
80
+		},
81
+	}
82
+	for _, test := range tests {
83
+		if err := os.Chtimes(test.path, test.ts, test.ts); err != nil {
84
+			t.Fatalf("chtime fail: %v", err)
85
+		}
86
+		m, err := _parse(test.path)
87
+		if err != nil {
88
+			t.Fatalf("problem parsing known good png: %v", err)
89
+		}
90
+		if err := m.Move(tmp); err != nil {
91
+			t.Fatalf("problem moving file into place: %v", err)
92
+		}
93
+		for _, p := range test.expected {
94
+			if _, err := os.Stat(filepath.Join(tmp, p)); os.IsNotExist(err) {
95
+				t.Errorf("could not find expected file %q: %v", p, err)
96
+			}
97
+		}
98
+	}
99
+}
100
+
101
+func TestBadFiles(t *testing.T) {
102
+	wd, err := os.Getwd()
103
+	if err != nil {
104
+		t.Fatal(err)
105
+	}
106
+	tmp, err := ioutil.TempDir("", "arrange-tests-")
107
+	if err != nil {
108
+		t.Fatal(err)
109
+	}
110
+	defer func() {
111
+		os.RemoveAll(tmp)
112
+	}()
113
+	if err := PrepOutput(tmp); err != nil {
114
+		t.Fatal(err)
115
+	}
116
+
117
+	tests := []struct {
118
+		path string
119
+	}{
120
+		{
121
+			path: filepath.Join(wd, "testdata", "bad-link"),
122
+		},
123
+		{
124
+			path: filepath.Join(wd, "testdata", "not.a.jpg"),
125
+		},
126
+		{
127
+			path: filepath.Join(wd, "testdata", "not.a.png"),
128
+		},
129
+		{
130
+			path: filepath.Join(wd, "testdata", "too-many-links.jpg"),
131
+		},
132
+	}
133
+	for _, test := range tests {
134
+		_, err := _parse(test.path)
135
+		if err == nil {
136
+			t.Fatalf("should have had an error in parse of %q, got nil", test.path)
137
+		}
138
+	}
139
+}
140
+
141
+func TestMoveCollision(t *testing.T) {
142
+	wd, err := os.Getwd()
143
+	if err != nil {
144
+		t.Fatal(err)
145
+	}
146
+	tmp, err := ioutil.TempDir("", "arrange-tests-")
147
+	if err != nil {
148
+		t.Fatal(err)
149
+	}
150
+	defer func() {
151
+		os.RemoveAll(tmp)
152
+	}()
153
+	if err := PrepOutput(tmp); err != nil {
154
+		t.Fatal(err)
155
+	}
156
+	ts := time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC)
157
+	media := []Media{
158
+		Media{
159
+			Path:      filepath.Join(wd, "testdata", "a.mov"),
160
+			Hash:      "60b725f10c9c85c70d97880dfe8191b3",
161
+			Time:      ts,
162
+			Extension: ".mov",
163
+		},
164
+		Media{
165
+			Path:      filepath.Join(wd, "testdata", "b.mov"),
166
+			Hash:      "3b5d5c3712955042212316173ccf37be",
167
+			Time:      ts,
168
+			Extension: ".mov",
169
+		},
170
+		Media{
171
+			Path:      filepath.Join(wd, "testdata", "c.mov"),
172
+			Hash:      "2cd6ee2c70b0bde53fbe6cac3c8b8bb1",
173
+			Time:      ts,
174
+			Extension: ".mov",
175
+		},
176
+	}
177
+
178
+	for _, m := range media {
179
+		if err := m.Move(tmp); err != nil {
180
+			t.Fatalf("move: %v", err)
181
+		}
182
+	}
183
+
184
+	expected := []string{
185
+		"date/2012/10/1350815400000000000_0001.mov",
186
+		"date/2012/10/1350815400000000000_0000.mov",
187
+		"date/2012/10/1350815400000000000.mov",
188
+	}
189
+	for _, p := range expected {
190
+		if _, err := os.Stat(filepath.Join(tmp, p)); os.IsNotExist(err) {
191
+			t.Errorf("could not find expected file %q: %v", p, err)
192
+		}
193
+	}
194
+}
195
+
196
+func TestSundry(t *testing.T) {
197
+	fmt.Sprintf("%v", NotMedia{"hi"})
198
+	fmt.Sprintf("%v", Dup{"hi"})
199
+}
200
+
201
+func TestFlow(t *testing.T) {
202
+	wd, err := os.Getwd()
203
+	if err != nil {
204
+		t.Fatal(err)
205
+	}
206
+	tmp, err := ioutil.TempDir("", "arrange-tests-")
207
+	if err != nil {
208
+		t.Fatal(err)
209
+	}
210
+	defer func() {
211
+		os.RemoveAll(tmp)
212
+	}()
213
+	if err := PrepOutput(tmp); err != nil {
214
+		t.Fatal(err)
215
+	}
216
+
217
+	files := []struct {
218
+		path string
219
+		ts   time.Time
220
+	}{
221
+		{
222
+			path: filepath.Join(wd, "testdata", "a.mov"),
223
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
224
+		},
225
+		{
226
+			path: filepath.Join(wd, "testdata", "b.mov"),
227
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 1, time.UTC),
228
+		},
229
+		{
230
+			path: filepath.Join(wd, "testdata", "c.mov"),
231
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 2, time.UTC),
232
+		},
233
+		{
234
+			path: filepath.Join(wd, "testdata", "bad.mov"),
235
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 3, time.UTC),
236
+		},
237
+
238
+		{
239
+			path: filepath.Join(wd, "testdata", "no-exif-but-good.jpg"),
240
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
241
+		},
242
+		{
243
+			path: filepath.Join(wd, "testdata", "exif-decode-error.jpg"),
244
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 1, time.UTC),
245
+		},
246
+		{
247
+			path: filepath.Join(wd, "testdata", "valid.jpg"),
248
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 2, time.UTC),
249
+		},
250
+
251
+		{
252
+			path: filepath.Join(wd, "testdata", "stott.gif"),
253
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
254
+		},
255
+		{
256
+			path: filepath.Join(wd, "testdata", "lenna.png"),
257
+			ts:   time.Date(2012, 10, 21, 10, 30, 0, 0, time.UTC),
258
+		},
259
+	}
260
+	for _, f := range files {
261
+		if err := os.Chtimes(f.path, f.ts, f.ts); err != nil {
262
+			t.Errorf("failure to chtime: %v", err)
263
+		}
264
+	}
265
+
266
+	work := Source(filepath.Join(wd, "testdata"))
267
+	streams := []<-chan Media{}
268
+
269
+	for w := 0; w < 4; w++ {
270
+		streams = append(streams, Parse(work))
271
+	}
272
+
273
+	for err := range Move(Merge(streams), tmp) {
274
+		if err != nil {
275
+			t.Errorf("unexpected error: %v")
276
+		}
277
+	}
278
+
279
+	expected := []string{
280
+		"date/2012/10/1350815400000000000.mov",
281
+		"date/2012/10/1350815400000000001.mov",
282
+		"date/2012/10/1350815400000000002.mov",
283
+		"date/2012/10/1350815400000000003.mov",
284
+
285
+		"date/2012/10/1350815400000000000.jpg",
286
+		"date/2012/10/1350815400000000002.jpg",
287
+		// this one has valid exif
288
+		"date/2006/03/1143508767000000000.jpg",
289
+
290
+		"date/2012/10/1350815400000000000.gif",
291
+		"date/2012/10/1350815400000000000.png",
292
+	}
293
+
294
+	for _, p := range expected {
295
+		if _, err := os.Stat(filepath.Join(tmp, p)); os.IsNotExist(err) {
296
+			t.Errorf("could not find expected file %q: %v", p, err)
297
+		}
298
+	}
299
+}

+ 1
- 0
testdata/a.mov View File

@@ -0,0 +1 @@
1
+a

+ 12
- 0
testdata/attribution.md View File

@@ -0,0 +1,12 @@
1
+# lenna.png:
2
+
3
+By Original full portrait: "Playmate of the Month". Playboy Magazine. November
4
+1972, photographed by Dwight Hooker.This 512x512 electronic/mechanical scan of
5
+a section of the full portrait: Alexander Sawchuk and two others[1] - The
6
+USC-SIPI image database, Fair use,
7
+https://en.wikipedia.org/w/index.php?curid=20658476
8
+
9
+# valid.jpg
10
+
11
+By No machine-readable author provided. Webber assumed (based on copyright
12
+claims). [Public domain], via Wikimedia Commons

+ 1
- 0
testdata/b.mov View File

@@ -0,0 +1 @@
1
+b

+ 1
- 0
testdata/bad-link View File

@@ -0,0 +1 @@
1
+bad-link.jpg

+ 0
- 0
testdata/bad.mov View File


+ 1
- 0
testdata/c.mov View File

@@ -0,0 +1 @@
1
+c

BIN
testdata/exif-decode-error.jpg View File


BIN
testdata/lenna.png View File


BIN
testdata/no-exif-but-good.jpg View File


+ 0
- 0
testdata/not.a.jpg View File


+ 0
- 0
testdata/not.a.png View File


BIN
testdata/stott.gif View File


+ 1
- 0
testdata/too-many-links.jpg View File

@@ -0,0 +1 @@
1
+too-many-links.jpg

BIN
testdata/valid.jpg View File


Loading…
Cancel
Save