-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstorage_test.go
144 lines (115 loc) · 3.48 KB
/
storage_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
package i18n
import (
"fmt"
"testing"
"golang.org/x/text/language"
. "github.com/smartystreets/goconvey/convey"
)
func TestInMemoryStorage(t *testing.T) {
t.Parallel()
Convey("Given an empty memory store", t, func() {
storage := NewInMemoryStorage()
Convey("When the default language is set", func() {
storage.SetDefaultLanguage(language.English)
Convey("Then the default language should be correct", func() {
lang, err := storage.DefaultLanguage()
So(err, ShouldBeNil)
So(lang.String(), ShouldEqual, language.English.String())
})
})
Convey("When the supported languages is set", func() {
storage.StoreSupportedLanguage(language.English)
storage.StoreSupportedLanguage(language.Spanish)
storage.StoreSupportedLanguage(language.French)
storage.DeleteSupportedLanguage(language.French)
Convey("Then the supported languages should be correct", func() {
langs, err := storage.SupportedLanguages()
So(err, ShouldBeNil)
So(langs, ShouldHaveLength, 2)
So(langs, ShouldContainLanguage, language.Spanish)
So(langs, ShouldContainLanguage, language.English)
})
})
Convey("When an item is added to the memory store", func() {
expected := &Translation{
Lang: language.English,
Key: "SomeKey",
Value: "SomeValue",
}
err := storage.Store(expected)
So(err, ShouldBeNil)
Convey("Then it should be accessable", func() {
results, err := storage.GetAll()
So(err, ShouldBeNil)
So(results, ShouldHaveLength, 1)
So(results[0], ShouldResemble, expected)
})
})
Convey("When an item is added twice to the memory store", func() {
expected := &Translation{
Lang: language.English,
Key: "SomeKey",
Value: "SomeValue",
}
replacement := &Translation{
Lang: language.English,
Key: "SomeKey",
Value: "SomeOtherValue",
}
err := storage.Store(expected)
So(err, ShouldBeNil)
err = storage.Store(replacement)
So(err, ShouldBeNil)
Convey("Then only one should exists", func() {
results, err := storage.GetAll()
So(err, ShouldBeNil)
So(results, ShouldHaveLength, 1)
})
Convey("Then the value should be updated", func() {
results, err := storage.GetAll()
So(err, ShouldBeNil)
So(results[0], ShouldResemble, replacement)
})
})
})
Convey("Given a populated storage", t, func() {
storage := NewInMemoryStorage()
err := storage.Store(&Translation{
Lang: language.English,
Key: "SomeKey",
Value: "SomeValue",
})
So(err, ShouldBeNil)
Convey("When an item is deleted from the storage", func() {
storage.Delete(&Translation{
Lang: language.English,
Key: "SomeKey",
Value: "SomeValue",
})
Convey("The item should not exists in the storage", func() {
results, err := storage.GetAll()
So(err, ShouldBeNil)
So(results, ShouldHaveLength, 0)
})
})
})
}
func ShouldContainLanguage(actual interface{}, expected ...interface{}) string {
haystack, ok := actual.([]language.Tag)
if !ok {
return "This assertion requires the actual value to be of type []language.Tag"
}
if len(expected) != 1 {
return "This assertion requires exactly 1 comparison values (you provided 0)."
}
needle, ok := expected[0].(language.Tag)
if !ok {
return "This assertion requires the comparison value to be of type language.Tag"
}
for _, item := range haystack {
if item.String() == needle.String() {
return ""
}
}
return fmt.Sprintf("Expected collection to contain %s but it did not!", needle.String())
}