Skip to content

Commit 734daa9

Browse files
committed
feat: adds unit tests and remove prints
1 parent bc430bb commit 734daa9

File tree

4 files changed

+100
-15
lines changed

4 files changed

+100
-15
lines changed

models/avatars/avatar_test.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,14 +19,14 @@ const gravatarSource = "https://secure.gravatar.com/avatar/"
1919
func disableGravatar(t *testing.T) {
2020
err := system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableFederatedAvatar.DynKey(): "false"})
2121
assert.NoError(t, err)
22-
// EnableGravatar.DynKey == picture.disable_gravatar for backwards compatability; .Value will flip correctly but the true value here is misleading
23-
err = system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableGravatar.DynKey(): "true"})
22+
// EnableGravatar.SelectFrom == picture.disable_gravatar for backwards compatability; .Value will flip correctly but the true value here is counterintuitive
23+
err = system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableGravatar.SelectFromKey(): "true"})
2424
assert.NoError(t, err)
2525
}
2626

2727
func enableGravatar(t *testing.T) {
28-
// EnableGravatar.DynKey == picture.disable_gravatar for backwards compatability; .Value will flip correctly but the false value here is misleading
29-
err := system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableGravatar.DynKey(): "false"})
28+
// EnableGravatar.SelectFrom == picture.disable_gravatar for backwards compatability; .Value will flip correctly but the false value here is counterintuitive
29+
err := system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableGravatar.SelectFromKey(): "false"})
3030
assert.NoError(t, err)
3131
setting.GravatarSource = gravatarSource
3232
}

modules/setting/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,8 @@ type ConfigStruct struct {
5858
}
5959

6060
var (
61-
defaultConfig *ConfigStruct
62-
ConfigOnce sync.Once
61+
defaultConfig *ConfigStruct
62+
defaultConfigOnce sync.Once
6363
)
6464

6565
func initDefaultConfig() {
@@ -77,7 +77,7 @@ func initDefaultConfig() {
7777
}
7878

7979
func Config() *ConfigStruct {
80-
ConfigOnce.Do(initDefaultConfig)
80+
defaultConfigOnce.Do(initDefaultConfig)
8181
return defaultConfig
8282
}
8383

modules/setting/config/value.go

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ package config
55

66
import (
77
"context"
8-
"fmt"
98
"sync"
109

1110
"code.gitea.io/gitea/modules/json"
@@ -50,7 +49,6 @@ func (value *Value[T]) invertBoolStr(val string) (inverted string) {
5049
func (value *Value[T]) invert(val T) (v T) {
5150
v = val
5251
if value.flipBoolean {
53-
fmt.Printf("Flipping boolean value '%v'...\n", val)
5452
// if value is of type bool
5553
if _, ok := any(val).(bool); ok {
5654
// invert the boolean value upon retrieval
@@ -151,8 +149,6 @@ func (value *Value[any]) SetValue(val string) error {
151149
panic("no config dyn value getter")
152150
}
153151

154-
fmt.Printf("Setting value '%s' with old key '%s' using key '%s'\n", val, value.selectFromKey, value.dynKey)
155-
156152
if value.flipBoolean {
157153
return ds.SetValue(ctx, value.getKey(), value.invertBoolStr(val))
158154
}

modules/setting/config/value_test.go

Lines changed: 93 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ func TestValue_parse(t *testing.T) {
2424
}
2525
for _, tt := range tests {
2626
t.Run(tt.name, func(t *testing.T) {
27-
value := ValueJSON[bool]("picture.disable_gravatar").WithFileConfig(CfgSecKey{Sec: "picture", Key: "DISABLE_GRAVATAR"}).Invert()
27+
value := ValueJSON[bool]("picture.disable_gravatar").Invert()
2828
got := value.parse(tt.key, tt.valStr)
2929

3030
if got != tt.want {
@@ -42,12 +42,12 @@ func TestValue_getKey(t *testing.T) {
4242
}{
4343
{
4444
name: "Custom dynKey name",
45-
valueClass: ValueJSON[bool]("picture.enable_gravatar").SelectFrom("picture.disable_gravatar").WithFileConfig(CfgSecKey{Sec: "", Key: ""}),
46-
want: "picture.enable_gravatar",
45+
valueClass: ValueJSON[bool]("picture.enable_gravatar").SelectFrom("picture.disable_gravatar"),
46+
want: "picture.disable_gravatar",
4747
},
4848
{
4949
name: "Normal dynKey name",
50-
valueClass: ValueJSON[bool]("picture.disable_gravatar").WithFileConfig(CfgSecKey{Sec: "", Key: ""}),
50+
valueClass: ValueJSON[bool]("picture.disable_gravatar"),
5151
want: "picture.disable_gravatar",
5252
},
5353
}
@@ -61,3 +61,92 @@ func TestValue_getKey(t *testing.T) {
6161
})
6262
}
6363
}
64+
65+
func TestValue_invert(t *testing.T) {
66+
tests := []struct {
67+
name string // description of this test case
68+
// Named input parameters for target function.
69+
valueClass *Value[bool]
70+
want bool
71+
}{
72+
{
73+
name: "Invert typed true",
74+
valueClass: ValueJSON[bool]("picture.enable_gravatar").WithDefault(true).Invert(),
75+
want: false,
76+
},
77+
{
78+
name: "Invert typed false",
79+
valueClass: ValueJSON[bool]("picture.enable_gravatar").WithDefault(false).Invert(),
80+
want: true,
81+
},
82+
{
83+
name: "Invert typed Does not invert",
84+
valueClass: ValueJSON[bool]("picture.enable_gravatar").WithDefault(false),
85+
want: false,
86+
},
87+
}
88+
for _, tt := range tests {
89+
t.Run(tt.name, func(t *testing.T) {
90+
got := tt.valueClass.invert(tt.valueClass.def)
91+
92+
if got != tt.want {
93+
t.Errorf("invert() = %v, want %v", got, tt.want)
94+
}
95+
})
96+
}
97+
}
98+
99+
func TestValue_invertBoolStr(t *testing.T) {
100+
tests := []struct {
101+
name string // description of this test case
102+
// Named input parameters for target function.
103+
valueClass *Value[bool]
104+
val string
105+
want string
106+
}{
107+
{
108+
name: "Invert boolean string true",
109+
valueClass: ValueJSON[bool]("picture.enable_gravatar"),
110+
val: "true",
111+
want: "false",
112+
},
113+
{
114+
name: "Invert boolean string false",
115+
valueClass: ValueJSON[bool]("picture.enable_gravatar"),
116+
val: "false",
117+
want: "true",
118+
},
119+
}
120+
for _, tt := range tests {
121+
t.Run(tt.name, func(t *testing.T) {
122+
got := tt.valueClass.invertBoolStr(tt.val)
123+
if got != tt.want {
124+
t.Errorf("invertBoolStr() = %v, want %v", got, tt.want)
125+
}
126+
})
127+
}
128+
}
129+
130+
func TestValue_SelectFromKey(t *testing.T) {
131+
tests := []struct {
132+
name string // description of this test case
133+
// Named input parameters for target function.
134+
valueClass *Value[bool]
135+
want string
136+
}{
137+
{
138+
name: "SelectFrom set and get",
139+
valueClass: ValueJSON[bool]("picture.enable_gravatar").SelectFrom("picture.disable_gravatar"),
140+
want: "picture.disable_gravatar",
141+
},
142+
}
143+
for _, tt := range tests {
144+
t.Run(tt.name, func(t *testing.T) {
145+
got := tt.valueClass.SelectFromKey()
146+
147+
if got != tt.want {
148+
t.Errorf("SelectFromKey() = %v, want %v", got, tt.want)
149+
}
150+
})
151+
}
152+
}

0 commit comments

Comments
 (0)