-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatautil_test.go
199 lines (183 loc) · 4.98 KB
/
datautil_test.go
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
package imgproc_test
import (
"bytes"
"io"
"io/ioutil"
"net/http"
"net/http/httptest"
"os"
"path"
"strings"
"testing"
"time"
"github.com/anatolym/imgproc"
)
func TestNewFileSrcErrors(t *testing.T) {
if _, err := imgproc.NewFileSrc("testdata/some_not_existing_file.txt", 1); err == nil ||
!strings.Contains(string(err.Error()), "not exists") {
t.Errorf("Got error '%s', expected error 'not exists'", err)
}
}
func TestNewFileSrc(t *testing.T) {
_, err := imgproc.NewFileSrc("testdata/url_list.txt", 1)
if err != nil {
t.Errorf("Got error '%s', expected nil", err)
}
}
func TestNewCsvResult(t *testing.T) {
resultFile := path.Join(os.TempDir(), "tst.csv")
if _, err := imgproc.NewCsvResult(resultFile, 0); err == nil ||
!strings.Contains(string(err.Error()), "Column number should be above 0") {
t.Errorf("Got error '%s', expected error 'Column number should be above 0'", err)
}
if _, err := imgproc.NewCsvResult(resultFile, -1); err == nil ||
!strings.Contains(string(err.Error()), "Column number should be above 0") {
t.Errorf("Got error '%s', expected error 'Column number should be above 0'", err)
}
}
func TestCsvResult(t *testing.T) {
resultFile := path.Join(os.TempDir(), "tst.csv")
res := imgproc.ResultItem{
Name: "http://example.com/123",
Results: imgproc.ColorList{
imgproc.Color{Hex: "FF0000", Count: 1},
imgproc.Color{Hex: "00FF00", Count: 1},
imgproc.Color{Hex: "0000FF", Count: 1}},
}
cases := []struct {
colNum int
contentWant string
}{
{3, "http://example.com/123,#FF0000,#00FF00,#0000FF,\n"},
{1, "http://example.com/123,#FF0000,\n"},
{5, "http://example.com/123,#FF0000,#00FF00,#0000FF,,,\n"},
}
for _, c := range cases {
csv, err := imgproc.NewCsvResult(resultFile, c.colNum)
if err != nil {
t.Errorf("Got error '%s', expected nil", err)
}
if err := csv.Add(&res); err != nil {
t.Errorf("Got error '%s', expected nil", err)
}
content, err := ioutil.ReadFile(resultFile)
if err != nil {
t.Errorf("Got error '%s', expected nil", err)
}
if string(content) != c.contentWant {
t.Errorf("Got content '%s', wanted '%s'", content, c.contentWant)
}
if err := os.Remove(resultFile); err != nil {
t.Errorf("Got error '%s', expected nil", err)
}
}
}
func prepFileServer(t *testing.T) *httptest.Server {
ts := httptest.NewServer(http.FileServer(http.Dir("testdata")))
// Checking test server returns correct data.
res, err := http.Get(ts.URL + "/gopher.png")
if err != nil {
t.Fatalf("Got error '%s', expected nil", err)
}
respImg, err := ioutil.ReadAll(res.Body)
res.Body.Close()
if err != nil {
t.Fatalf("Got error '%s', expected nil", err)
}
if respImg == nil {
t.Fatal("Got nil , expected non-empty response body")
}
fileImg, err := ioutil.ReadFile("testdata/gopher.png")
if err != nil {
t.Fatalf("Got error '%s', expected nil", err)
}
if !bytes.Equal(respImg, fileImg) {
t.Fatal("Files are different")
}
return ts
}
func TestGetImgItemCh(t *testing.T) {
ts := prepFileServer(t)
defer ts.Close()
files := []string{
"gopher.png",
"img-1-color.jpg",
"img-3-colors-with-gradient.png",
"img-4-colors.png",
"img-gradient.png",
}
tmpfile, err := ioutil.TempFile(os.TempDir(), "example.txt")
if err != nil {
t.Fatal(err)
}
defer os.Remove(tmpfile.Name()) // clean up
// Filling tmpfile with image urls.
urls := make([]string, len(files))
for i, fn := range files {
urls[i] = ts.URL + "/" + fn
io.WriteString(tmpfile, urls[i]+"\n")
}
if err := tmpfile.Close(); err != nil {
t.Fatal(err)
}
src, e := imgproc.NewFileSrc(tmpfile.Name(), 1)
if e != nil {
t.Errorf("Got error '%s', expected nil", e)
}
// Checking image channel returns all the imagas
done := make(chan struct{})
defer close(done)
readCh := func() map[string]struct{} {
imgCh := src.GetImgItemCh(done)
var lastImgName string
timeoutC := time.After(1 * time.Second)
gotUrls := map[string]struct{}{}
for {
select {
case img, ok := <-imgCh:
if !ok {
return gotUrls
}
lastImgName = img.Name
gotUrls[img.Name] = struct{}{}
t.Logf("processing img '%s'", img.Name)
case <-timeoutC:
t.Errorf("timed out on img '%s'", lastImgName)
return gotUrls
}
}
}
gotUrls := readCh()
for _, url := range urls {
if _, ok := gotUrls[url]; !ok {
t.Errorf("Url '%s' did not show up in the channel", url)
}
}
}
func TestProcessItems(t *testing.T) {
gopherImg, err := ioutil.ReadFile("testdata/gopher.png")
if err != nil {
t.Fatalf("Got error '%s', expected nil", err)
}
imgs := []imgproc.ImgItem{
{Name: "name1", Data: gopherImg},
}
imgItemCh := make(imgproc.ImgItemCh)
go func() {
for _, img := range imgs {
imgItemCh <- &img
}
close(imgItemCh)
}()
done := make(chan struct{})
defer close(done)
results := make(map[string]struct{})
for res := range imgproc.ProcessItems(done, imgItemCh, 1, 1) {
results[res.Name] = struct{}{}
}
for _, img := range imgs {
if _, ok := results[img.Name]; !ok {
t.Errorf("Img '%s' did not show up in the channel", img.Name)
}
}
}