Passed
Push — master ( 2df545...289a86 )
by Valentin
02:00
created

operations.testMapPayload   B

Complexity

Conditions 7

Size

Total Lines 32
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 7
eloc 23
nop 1
dl 0
loc 32
rs 7.9279
c 0
b 0
f 0
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/vars"
7
	"reflect"
8
	"testing"
9
)
10
11
func testMapPayload(t *testing.T) {
12
	dict := configuration.Load(config.DictConfig{}, "./../../../dict.yaml").(config.DictConfig)
13
	columns := readColumns([]string{"", "keywords", "", "title", "test", "XMP:Marked"}, dict)
14
15
	type check struct {
16
		data []string
17
		has  map[string]interface{}
18
		miss []string
19
	}
20
21
	set := []check{
22
		{
23
			[]string{"name1", "keyword1,keyword2,keyword3", "empty1", "title1", "test1"},
24
			map[string]interface{}{"IPTC:Keywords": "keyword1,keyword2,keyword3", "IPTC:Headline": "title1", "XMP:Marked": false},
25
			[]string{"test", ""},
26
		},
27
		{
28
			[]string{"name2", "keyword4", "empty2", "", "", "true"},
29
			map[string]interface{}{"IPTC:Keywords": "keyword4", "XMP:Marked": true},
30
			[]string{"IPTC:Headline"},
31
		},
32
	}
33
34
	for i, s := range set {
35
		payload := mapPayload(columns, s.data)
36
		tags := payload.Tags()
37
38
		for name, val := range s.has {
39
			if v, ok := tags.Tag(name); !ok {
40
				t.Errorf("payload mismatch (line `%d`):\n\nexp `%s` `%v`", i, name, val)
41
			} else if v != val {
42
				t.Errorf("payload mismatch (line `%d`) for `%s`:\ngot `%v`\nexp `%v`", i, name, v, val)
43
			}
44
		}
45
	}
46
}
47
48
func TestReadColumns(t *testing.T) {
49
	type tag struct {
50
		pos   int
51
		name  string
52
		found bool
53
	}
54
	type check struct {
55
		cols []string
56
		exp  []tag
57
	}
58
59
	set := []check{
60
		{[]string{"abc", "Keywords"}, []tag{
61
			{1, "keywords", true},
62
		}},
63
		{[]string{"", "keywords "}, []tag{
64
			{1, "IPTC:Keywords", true},
65
		}},
66
		{[]string{"", "XMP:Description"}, []tag{
67
			{1, "description", true},
68
		}},
69
		{[]string{"", "keywords", "", "description", "", ""}, []tag{
70
			{1, "keywords", true},
71
			{3, "description", true},
72
		}},
73
		{[]string{"", "keywords", "test"}, []tag{
74
			{1, "keywords", true},
75
			{2, "test", false},
76
		}},
77
	}
78
79
	dict := configuration.Load(config.DictConfig{}, "./../../../dict.yaml").(config.DictConfig)
80
81
	for i, s := range set {
82
		read := readColumns(s.cols, dict)
83
		for j, checkTag := range s.exp {
84
			foundCheckTag, found := dict.Find(checkTag.name)
85
			if found != checkTag.found {
86
				t.Errorf("columns mismatch (line `%d`, column `%d`):\ngot `%t` `%+v`\nexp `%t` `%+v`", i, j, found, foundCheckTag, checkTag.found, checkTag)
87
			} else if found {
88
				mapTag, ok := read[checkTag.pos]
89
				if !ok {
90
					t.Errorf("columns not found (line `%d`, column `%d`):\nexp `%+v`", i, j, foundCheckTag)
91
				} else if !tagsEqual(foundCheckTag, mapTag) {
92
					t.Errorf("column tags not equal (line `%d`, column `%d`):\ngot `%+v`\nexp `%+v`", i, j, mapTag, foundCheckTag)
93
				}
94
			}
95
		}
96
	}
97
}
98
99
func tagsEqual(t1, t2 vars.Tag) bool {
100
	if t1.Key() != t2.Key() {
101
		return false
102
	}
103
104
	if !reflect.DeepEqual(t1.Map(), t2.Map()) {
105
		return false
106
	}
107
108
	return true
109
}
110