-
Notifications
You must be signed in to change notification settings - Fork 2
/
formatTypes_test.go
165 lines (138 loc) · 3.53 KB
/
formatTypes_test.go
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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package astra
import (
"github.com/stretchr/testify/require"
"strings"
"testing"
)
func TestWithCustomTypeMapping(t *testing.T) {
t.Run("adds custom type mapping", func(t *testing.T) {
service := &Service{
CustomTypeMapping: make(map[string]TypeFormat),
}
WithCustomTypeMapping(map[string]TypeFormat{
"old": {
Type: "string",
Format: "new",
},
})(service)
require.Equal(t, TypeFormat{
Type: "string",
Format: "new",
}, service.CustomTypeMapping["old"])
})
t.Run("overwrites existing type mapping", func(t *testing.T) {
service := &Service{
CustomTypeMapping: map[string]TypeFormat{
"old": {
Type: "string",
Format: "old",
},
},
}
WithCustomTypeMapping(map[string]TypeFormat{
"old": {
Type: "string",
Format: "new",
},
})(service)
require.Equal(t, TypeFormat{
Type: "string",
Format: "new",
}, service.CustomTypeMapping["old"])
})
t.Run("overwrites existing type mapping set from function", func(t *testing.T) {
service := &Service{
CustomTypeMapping: make(map[string]TypeFormat),
}
WithCustomTypeMapping(map[string]TypeFormat{
"old": {
Type: "string",
Format: "old",
},
})(service)
require.Equal(t, TypeFormat{
Type: "string",
Format: "old",
}, service.CustomTypeMapping["old"])
WithCustomTypeMapping(map[string]TypeFormat{
"old": {
Type: "string",
Format: "new",
},
})(service)
require.Equal(t, TypeFormat{
Type: "string",
Format: "new",
}, service.CustomTypeMapping["old"])
})
}
func TestWithCustomTypeMappingSingle(t *testing.T) {
t.Run("adds custom type mapping", func(t *testing.T) {
service := &Service{
CustomTypeMapping: make(map[string]TypeFormat),
}
WithCustomTypeMappingSingle("old", "string", "new")(service)
require.Equal(t, TypeFormat{
Type: "string",
Format: "new",
}, service.CustomTypeMapping["old"])
})
t.Run("overwrites existing type mapping", func(t *testing.T) {
service := &Service{
CustomTypeMapping: map[string]TypeFormat{
"old": {
Type: "string",
Format: "old",
},
},
}
WithCustomTypeMappingSingle("old", "string", "new")(service)
require.Equal(t, TypeFormat{
Type: "string",
Format: "new",
}, service.CustomTypeMapping["old"])
})
}
func TestService_GetTypeMapping(t *testing.T) {
t.Run("sets full type mapping if it doesn't exist initially", func(t *testing.T) {
service := &Service{
CustomTypeMapping: make(map[string]TypeFormat),
}
require.Empty(t, service.fullTypeMapping)
service.GetTypeMapping("test", "test")
require.NotEmpty(t, service.fullTypeMapping)
})
t.Run("returns the type mapping for the given custom key", func(t *testing.T) {
service := &Service{
fullTypeMapping: map[string]TypeFormat{
"test.test": {
Type: "string",
Format: "test",
},
},
}
result, ok := service.GetTypeMapping("test", "test")
require.True(t, ok)
require.Equal(t, TypeFormat{
Type: "string",
Format: "test",
}, result)
})
t.Run("returns predefined types for type maps", func(t *testing.T) {
service := &Service{}
for k, v := range PredefinedTypeMap {
t.Run(k, func(t *testing.T) {
splitKey := strings.Split(k, ".")
var result TypeFormat
var ok bool
if len(splitKey) > 1 {
result, ok = service.GetTypeMapping(splitKey[len(splitKey)-1], strings.Join(splitKey[0:len(splitKey)-1], "."))
} else {
result, ok = service.GetTypeMapping(splitKey[0], "")
}
require.True(t, ok)
require.Equal(t, v, result)
})
}
})
}