mirror of
https://github.com/go-gitea/gitea.git
synced 2025-11-08 16:12:19 +01:00
feat: adds unit tests and remove prints
This commit is contained in:
parent
bc430bb330
commit
734daa9684
@ -19,14 +19,14 @@ const gravatarSource = "https://secure.gravatar.com/avatar/"
|
||||
func disableGravatar(t *testing.T) {
|
||||
err := system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableFederatedAvatar.DynKey(): "false"})
|
||||
assert.NoError(t, err)
|
||||
// EnableGravatar.DynKey == picture.disable_gravatar for backwards compatability; .Value will flip correctly but the true value here is misleading
|
||||
err = system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableGravatar.DynKey(): "true"})
|
||||
// EnableGravatar.SelectFrom == picture.disable_gravatar for backwards compatability; .Value will flip correctly but the true value here is counterintuitive
|
||||
err = system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableGravatar.SelectFromKey(): "true"})
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
|
||||
func enableGravatar(t *testing.T) {
|
||||
// EnableGravatar.DynKey == picture.disable_gravatar for backwards compatability; .Value will flip correctly but the false value here is misleading
|
||||
err := system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableGravatar.DynKey(): "false"})
|
||||
// EnableGravatar.SelectFrom == picture.disable_gravatar for backwards compatability; .Value will flip correctly but the false value here is counterintuitive
|
||||
err := system_model.SetSettings(t.Context(), map[string]string{setting.Config().Picture.EnableGravatar.SelectFromKey(): "false"})
|
||||
assert.NoError(t, err)
|
||||
setting.GravatarSource = gravatarSource
|
||||
}
|
||||
|
||||
@ -58,8 +58,8 @@ type ConfigStruct struct {
|
||||
}
|
||||
|
||||
var (
|
||||
defaultConfig *ConfigStruct
|
||||
ConfigOnce sync.Once
|
||||
defaultConfig *ConfigStruct
|
||||
defaultConfigOnce sync.Once
|
||||
)
|
||||
|
||||
func initDefaultConfig() {
|
||||
@ -77,7 +77,7 @@ func initDefaultConfig() {
|
||||
}
|
||||
|
||||
func Config() *ConfigStruct {
|
||||
ConfigOnce.Do(initDefaultConfig)
|
||||
defaultConfigOnce.Do(initDefaultConfig)
|
||||
return defaultConfig
|
||||
}
|
||||
|
||||
|
||||
@ -5,7 +5,6 @@ package config
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"sync"
|
||||
|
||||
"code.gitea.io/gitea/modules/json"
|
||||
@ -50,7 +49,6 @@ func (value *Value[T]) invertBoolStr(val string) (inverted string) {
|
||||
func (value *Value[T]) invert(val T) (v T) {
|
||||
v = val
|
||||
if value.flipBoolean {
|
||||
fmt.Printf("Flipping boolean value '%v'...\n", val)
|
||||
// if value is of type bool
|
||||
if _, ok := any(val).(bool); ok {
|
||||
// invert the boolean value upon retrieval
|
||||
@ -151,8 +149,6 @@ func (value *Value[any]) SetValue(val string) error {
|
||||
panic("no config dyn value getter")
|
||||
}
|
||||
|
||||
fmt.Printf("Setting value '%s' with old key '%s' using key '%s'\n", val, value.selectFromKey, value.dynKey)
|
||||
|
||||
if value.flipBoolean {
|
||||
return ds.SetValue(ctx, value.getKey(), value.invertBoolStr(val))
|
||||
}
|
||||
|
||||
@ -24,7 +24,7 @@ func TestValue_parse(t *testing.T) {
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
value := ValueJSON[bool]("picture.disable_gravatar").WithFileConfig(CfgSecKey{Sec: "picture", Key: "DISABLE_GRAVATAR"}).Invert()
|
||||
value := ValueJSON[bool]("picture.disable_gravatar").Invert()
|
||||
got := value.parse(tt.key, tt.valStr)
|
||||
|
||||
if got != tt.want {
|
||||
@ -42,12 +42,12 @@ func TestValue_getKey(t *testing.T) {
|
||||
}{
|
||||
{
|
||||
name: "Custom dynKey name",
|
||||
valueClass: ValueJSON[bool]("picture.enable_gravatar").SelectFrom("picture.disable_gravatar").WithFileConfig(CfgSecKey{Sec: "", Key: ""}),
|
||||
want: "picture.enable_gravatar",
|
||||
valueClass: ValueJSON[bool]("picture.enable_gravatar").SelectFrom("picture.disable_gravatar"),
|
||||
want: "picture.disable_gravatar",
|
||||
},
|
||||
{
|
||||
name: "Normal dynKey name",
|
||||
valueClass: ValueJSON[bool]("picture.disable_gravatar").WithFileConfig(CfgSecKey{Sec: "", Key: ""}),
|
||||
valueClass: ValueJSON[bool]("picture.disable_gravatar"),
|
||||
want: "picture.disable_gravatar",
|
||||
},
|
||||
}
|
||||
@ -61,3 +61,92 @@ func TestValue_getKey(t *testing.T) {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValue_invert(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string // description of this test case
|
||||
// Named input parameters for target function.
|
||||
valueClass *Value[bool]
|
||||
want bool
|
||||
}{
|
||||
{
|
||||
name: "Invert typed true",
|
||||
valueClass: ValueJSON[bool]("picture.enable_gravatar").WithDefault(true).Invert(),
|
||||
want: false,
|
||||
},
|
||||
{
|
||||
name: "Invert typed false",
|
||||
valueClass: ValueJSON[bool]("picture.enable_gravatar").WithDefault(false).Invert(),
|
||||
want: true,
|
||||
},
|
||||
{
|
||||
name: "Invert typed Does not invert",
|
||||
valueClass: ValueJSON[bool]("picture.enable_gravatar").WithDefault(false),
|
||||
want: false,
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.valueClass.invert(tt.valueClass.def)
|
||||
|
||||
if got != tt.want {
|
||||
t.Errorf("invert() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValue_invertBoolStr(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string // description of this test case
|
||||
// Named input parameters for target function.
|
||||
valueClass *Value[bool]
|
||||
val string
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "Invert boolean string true",
|
||||
valueClass: ValueJSON[bool]("picture.enable_gravatar"),
|
||||
val: "true",
|
||||
want: "false",
|
||||
},
|
||||
{
|
||||
name: "Invert boolean string false",
|
||||
valueClass: ValueJSON[bool]("picture.enable_gravatar"),
|
||||
val: "false",
|
||||
want: "true",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.valueClass.invertBoolStr(tt.val)
|
||||
if got != tt.want {
|
||||
t.Errorf("invertBoolStr() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestValue_SelectFromKey(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string // description of this test case
|
||||
// Named input parameters for target function.
|
||||
valueClass *Value[bool]
|
||||
want string
|
||||
}{
|
||||
{
|
||||
name: "SelectFrom set and get",
|
||||
valueClass: ValueJSON[bool]("picture.enable_gravatar").SelectFrom("picture.disable_gravatar"),
|
||||
want: "picture.disable_gravatar",
|
||||
},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
got := tt.valueClass.SelectFromKey()
|
||||
|
||||
if got != tt.want {
|
||||
t.Errorf("SelectFromKey() = %v, want %v", got, tt.want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user