-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdefaultManager_test.go
261 lines (208 loc) · 5.98 KB
/
defaultManager_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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
package i18n
import (
"github.com/chrstphlbr/resource"
"github.com/chrstphlbr/testHelpers"
"os"
"testing"
"time"
)
const (
filesDirectory = "./temp"
greetingJson = `{
"hello": {
"en": "hello",
"de": "hallo",
"ru": "Привет"
}
}`
greetingFileName = "./temp/greeting.json"
)
func setUp(t *testing.T) (repo resource.Repository) {
os.Mkdir(filesDirectory, 0700)
testHelpers.CreateFile(t, greetingFileName, greetingJson)
repo = resource.NewFileRepository(filesDirectory)
return
}
func tearDown(t *testing.T) {
testHelpers.RemoveFile(t, filesDirectory)
}
func TestSetGetLanguage(t *testing.T) {
const lanaguage = "en"
manager := &DefaultManager{}
manager.SetLanguage(lanaguage)
if manager.GetLanguage() != "en" {
t.Fatalf("did not set default language (\"%s\") to \"%s\"", manager.GetDefaultLanguage(), lanaguage)
}
}
func TestSetGetDefaultLanguage(t *testing.T) {
const defaultLanaguage = "en"
manager := &DefaultManager{}
manager.SetDefaultLanguage(defaultLanaguage)
if manager.GetDefaultLanguage() != "en" {
t.Fatalf("did not set default language (\"%s\") to \"%s\"", manager.GetDefaultLanguage(), defaultLanaguage)
}
}
func TestConstructMapping(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
checkElement := func(t *testing.T, mapping keys, element string) *values {
el, ok := mapping[element]
if !ok {
t.Fatal("element (hello) from file not included in mapping")
}
return &el
}
checkValue := func(t *testing.T, values values, element string, expectedResult string) {
el, ok := values[element]
if !ok {
t.Fatalf("value (%s) from file not included in mapping", element)
} else if el != expectedResult {
t.Fatalf("%s (%s) has not the correct value (%s)", element, el, expectedResult)
}
}
before := time.Now()
// run constructMapping
manager.constructMapping()
// check if time (mappingConstructed) is set and after before
if manager.mappingConstructed.IsZero() {
t.Fatal("mappingConstructed is not set")
} else if !manager.mappingConstructed.After(before) {
t.Fatalf("mappingConstructed is set (%v), but not after before (%v)", manager.mappingConstructed, before)
}
// check mapping
if manager.mapping == nil {
t.Fatal("mapping not initialized")
}
// check if there is just one element
mappingLength := len(manager.mapping)
const expectedLength = 1
if mappingLength != expectedLength {
t.Fatalf("mapping length wrong (%d), expected %d element(s)", mappingLength, expectedLength)
}
el := checkElement(t, manager.mapping, "hello")
checkValue(t, *el, "en", "hello")
checkValue(t, *el, "de", "hallo")
checkValue(t, *el, "ru", "Привет")
t.Logf("mapping: %+v", manager.mapping)
}
func TestGetByLanguageFound(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
manager.SetLanguage("en")
const (
vEn = "hello"
)
value, err := manager.GetByLanguage("hello")
if err != nil {
t.Fatalf("should have found value (err: %v)", err)
} else if value != vEn {
t.Fatalf("found value (%s), but expected \"%s\"", value, vEn)
}
}
func TestGetFound(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
manager.SetDefaultLanguage("en")
const (
vEn = "hello"
vDe = "hallo"
vRu = "Привет"
)
value, err := manager.Get("hello", "en")
if err != nil {
t.Fatalf("should have found value (err: %v)", err)
} else if value != vEn {
t.Fatalf("found value (%s), but expected \"%s\"", value, vEn)
}
value, err = manager.Get("hello", "de")
if err != nil {
t.Fatalf("should have found value (err: %v)", err)
} else if value != vDe {
t.Fatalf("found value (%s), but expected \"%s\"", value, vDe)
}
value, err = manager.Get("hello", "ru")
if err != nil {
t.Fatalf("should have found value (err: %v)", err)
} else if value != vRu {
t.Fatalf("found value (%s), but expected \"%s\"", value, vRu)
}
}
func TestGetFoundWithWrongDefaultLang(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
manager.SetDefaultLanguage("es")
const vEn = "hello"
value, err := manager.Get("hello", "en")
if err != nil {
t.Fatalf("should have found value (err: %v)", err)
} else if value != vEn {
t.Fatalf("found value (%s), but expected \"%s\"", value, vEn)
}
}
func TestGetWrongKey(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
value, err := manager.Get("huhu", "en")
if err == nil {
t.Fatalf("result found (%s). should have returned error", value)
}
}
func TestGetWrongLanguage(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
value, err := manager.Get("hello", "es")
if err == nil {
t.Fatalf("result found (%s). should have returned error", value)
}
}
func TestGetWrongLangButDefaultLang(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
manager.SetDefaultLanguage("en")
const vExp = "hello"
value, err := manager.Get("hello", "es")
if err != nil {
t.Fatalf("returned error despite available default language: %v", err)
} else if value != vExp {
t.Fatalf("result found (%s), but expected \"%s\"", value, vExp)
}
}
func TestGetWrongLangAndWrongDefaultLang(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
manager.SetDefaultLanguage("es")
value, err := manager.Get("hello", "hu")
if err == nil {
t.Fatalf("result found (%s). should have returned error", value)
}
}
func TestGetAll(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
values, err := manager.GetAll("hello")
if err != nil {
t.Fatalf("error but there should not be one: %v", err)
}
length := len(values)
if length != 3 {
t.Fatalf("should have exactly 3 values but has %d", length)
}
}
func TestGetAllError(t *testing.T) {
repo := setUp(t)
defer tearDown(t)
manager := NewDefaultManager(repo)
values, err := manager.GetAll("huhu")
if err == nil {
t.Fatalf("did not return but returned values (%v)", values)
}
}