Passed
Push — master ( a410ee...468f5b )
by Valentin
01:51
created

operations.TestReadCSV   D

Complexity

Conditions 13

Size

Total Lines 45
Code Lines 31

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 13
eloc 31
nop 1
dl 0
loc 45
rs 4.2
c 0
b 0
f 0

How to fix   Complexity   

Complexity

Complex classes like operations.TestReadCSV often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes.

Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.

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