aboutsummaryrefslogtreecommitdiffstats
path: root/worker/middleware/foldermapper_test.go
blob: 0b4f6dba0ca246674af64d26a07a04b3c59d7d79 (plain) (blame)
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
package middleware

import (
	"reflect"
	"testing"
)

func TestFolderMap_Apply(t *testing.T) {
	tests := []struct {
		name    string
		mapping map[string]string
		order   []string
		input   []string
		want    []string
	}{
		{
			name:    "strict single folder mapping",
			mapping: map[string]string{"Drafts": "INBOX/Drafts"},
			order:   []string{"Drafts"},
			input:   []string{"INBOX/Drafts"},
			want:    []string{"Drafts"},
		},
		{
			name:    "prefix mapping with * suffix",
			mapping: map[string]string{"Prefix/": "INBOX/*"},
			order:   []string{"Prefix/"},
			input:   []string{"INBOX", "INBOX/Test1", "INBOX/Test2", "Archive"},
			want:    []string{"INBOX", "Prefix/Test1", "Prefix/Test2", "Archive"},
		},
		{
			name:    "remove prefix with * in key",
			mapping: map[string]string{"*": "INBOX/*"},
			order:   []string{"*"},
			input:   []string{"INBOX", "INBOX/Test1", "INBOX/Test2", "Archive"},
			want:    []string{"INBOX", "Test1", "Test2", "Archive"},
		},
		{
			name: "remove two prefixes with * in keys",
			mapping: map[string]string{
				"*":  "INBOX/*",
				"**": "PROJECT/*",
			},
			order: []string{"*", "**"},
			input: []string{"INBOX", "INBOX/Test1", "INBOX/Test2", "Archive", "PROJECT/sub1", "PROJECT/sub2"},
			want:  []string{"INBOX", "Test1", "Test2", "Archive", "sub1", "sub2"},
		},
		{
			name: "multiple, sequential mappings",
			mapping: map[string]string{
				"Archive/existing": "Archive*",
				"Archive":          "Archivum*",
			},
			order: []string{"Archive/existing", "Archive"},
			input: []string{"Archive", "Archive/sub", "Archivum", "Archivum/year1"},
			want:  []string{"Archive/existing", "Archive/existing/sub", "Archive", "Archive/year1"},
		},
	}

	for i, test := range tests {
		fm := &folderMap{
			mapping: test.mapping,
			order:   test.order,
		}
		var result []string
		for _, in := range test.input {
			result = append(result, fm.Apply(in))
		}
		if !reflect.DeepEqual(result, test.want) {
			t.Errorf("test (%d: %s) failed: want '%v' but got '%v'",
				i, test.name, test.want, result)
		}
	}
}

func TestFolderMap_createFolder(t *testing.T) {
	tests := []struct {
		name  string
		table map[string]string
		input string
		want  string
	}{
		{
			name:  "create normal folder",
			table: map[string]string{"Drafts": "INBOX/Drafts"},
			input: "INBOX/Drafts2",
			want:  "INBOX/Drafts2",
		},
		{
			name:  "create mapped folder",
			table: map[string]string{"Drafts": "INBOX/Drafts"},
			input: "Drafts/Sub",
			want:  "INBOX/Drafts/Sub",
		},
	}

	for i, test := range tests {
		result := createFolder(test.table, test.input)
		if result != test.want {
			t.Errorf("test (%d: %s) failed: want '%v' but got '%v'",
				i, test.name, test.want, result)
		}
	}
}