Passed
Push — master ( 70cd71...7d7337 )
by Valentin
02:17
created

cmd/writecmd/operations/mapper_test.go   A

Size/Duplication

Total Lines 110
Duplicated Lines 0 %

Importance

Changes 0
Metric Value
cc 19
eloc 72
dl 0
loc 110
rs 10
c 0
b 0
f 0

3 Methods

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