|
1
|
|
|
package operations |
|
2
|
|
|
|
|
3
|
|
|
import ( |
|
4
|
|
|
"github.com/vvval/go-metadata-scanner/config" |
|
5
|
|
|
"github.com/vvval/go-metadata-scanner/configuration" |
|
6
|
|
|
"github.com/vvval/go-metadata-scanner/util" |
|
7
|
|
|
"github.com/vvval/go-metadata-scanner/vars/metadata" |
|
8
|
|
|
"strings" |
|
9
|
|
|
"testing" |
|
10
|
|
|
) |
|
11
|
|
|
|
|
12
|
|
|
func TestReadCSV(t *testing.T) { |
|
13
|
|
|
filename := "./fixtures/file1.csv" |
|
14
|
|
|
file := util.MustOpenReadonlyFile(filename) |
|
15
|
|
|
defer file.Close() |
|
16
|
|
|
|
|
17
|
|
|
dict := configuration.Load(config.DictConfig{}, "./../../../dict.yaml").(config.DictConfig) |
|
18
|
|
|
|
|
19
|
|
|
type check struct { |
|
20
|
|
|
filename string |
|
21
|
|
|
lists []string |
|
22
|
|
|
tags []string |
|
23
|
|
|
miss []string |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
expected := map[string]check{ |
|
27
|
|
|
"file.jpg": {"file.jpg", []string{}, []string{"IPTC:Headline", "IPTC:Keywords", "XMP:Marked"}, []string{"EXIF:ImageDescription", "IPTC:Caption-Abstract"}}, |
|
28
|
|
|
"file2.jpg": {"file2.jpg", []string{"IPTC:Keywords"}, []string{"XMP:Headline", "XMP:Subject"}, []string{"XMP:Description", "some other description"}}, |
|
29
|
|
|
} |
|
30
|
|
|
var output []check |
|
31
|
|
|
|
|
32
|
|
|
ReadCSV(util.GetCSVReader(file, ','), dict, func(filename string, payload metadata.Payload) { |
|
33
|
|
|
var tags, miss []string |
|
34
|
|
|
for s, v := range payload.Tags() { |
|
35
|
|
|
tags = append(tags, s) |
|
36
|
|
|
if vs, ok := v.(string); ok { |
|
37
|
|
|
if len(vs) == 0 { |
|
38
|
|
|
miss = append(miss, s) |
|
39
|
|
|
} |
|
40
|
|
|
} else if v != nil { |
|
41
|
|
|
miss = append(miss, s) |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
output = append(output, check{filename, payload.ListTags(), tags, miss}) |
|
46
|
|
|
}) |
|
47
|
|
|
|
|
48
|
|
|
for i, o := range output { |
|
49
|
|
|
e, ok := expected[o.filename] |
|
50
|
|
|
if !ok { |
|
51
|
|
|
t.Errorf("read unexpected result (line `%d`) for filename `%s`", i, o.filename) |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
for _, l := range e.lists { |
|
55
|
|
|
if !inArray(l, o.lists) { |
|
56
|
|
|
t.Errorf("read expected lists not found (line `%d`) for filename `%s`:\ntag %s\ngot: %+v", i, o.filename, l, o.lists) |
|
57
|
|
|
} |
|
58
|
|
|
} |
|
59
|
|
|
} |
|
60
|
|
|
} |
|
61
|
|
|
|
|
62
|
|
|
func inArray(el string, arr []string) bool { |
|
63
|
|
|
for _, val := range arr { |
|
64
|
|
|
if strings.EqualFold(el, val) { |
|
65
|
|
|
return true |
|
66
|
|
|
} |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
return false |
|
70
|
|
|
} |
|
71
|
|
|
|