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
|
package thumbnail
import (
"context"
"os"
"testing"
"time"
"github.com/RanXom/galleryd/internal/gallery"
"github.com/RanXom/galleryd/internal/scanner"
"github.com/RanXom/galleryd/internal/testfixtures"
)
func TestGenerator(t *testing.T) {
cacheDir := t.TempDir()
generator, err := New(cacheDir)
if err != nil {
t.Fatal(err)
}
photo := gallery.Photo{
ID: "canon40d",
File: scanner.File{
Path: testfixtures.Canon40D(),
Root: "photos",
RelativePath: "Canon_40D.jpg",
ModTime: time.Now(),
},
}
t.Run("generates thumbnail", func(t *testing.T) {
thumb, err := generator.Generate(
context.Background(),
photo,
)
if err != nil {
t.Fatal(err)
}
if _, err := os.Stat(thumb.Path); err != nil {
t.Fatalf("thumbnail was not created: %v", err)
}
})
t.Run("reuses cached thumbnail", func(t *testing.T) {
first, err := generator.Generate(
context.Background(),
photo,
)
if err != nil {
t.Fatal(err)
}
info1, err := os.Stat(first.Path)
if err != nil {
t.Fatal(err)
}
time.Sleep(10 * time.Millisecond)
second, err := generator.Generate(
context.Background(),
photo,
)
if err != nil {
t.Fatal(err)
}
info2, err := os.Stat(second.Path)
if err != nil {
t.Fatal(err)
}
if !info1.ModTime().Equal(info2.ModTime()) {
t.Fatal("thumbnail was regenerated instead of reused")
}
})
t.Run("respects cancelled context", func(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
cancel()
_, err := generator.Generate(ctx, photo)
if err == nil {
t.Fatal("expected context cancellation")
}
})
}
|