-
Notifications
You must be signed in to change notification settings - Fork 8.4k
Expand file tree
/
Copy pathsetting_service_initialize_test.go
More file actions
91 lines (74 loc) · 2.77 KB
/
Copy pathsetting_service_initialize_test.go
File metadata and controls
91 lines (74 loc) · 2.77 KB
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
//go:build unit
package service
import (
"context"
"testing"
"github.com/Wei-Shaw/sub2api/internal/config"
"github.com/stretchr/testify/require"
)
type settingInitRepoStub struct {
values map[string]string
updates map[string]string
}
func (s *settingInitRepoStub) Get(context.Context, string) (*Setting, error) {
panic("unexpected Get call")
}
func (s *settingInitRepoStub) GetValue(context.Context, string) (string, error) {
panic("unexpected GetValue call")
}
func (s *settingInitRepoStub) Set(context.Context, string, string) error {
panic("unexpected Set call")
}
func (s *settingInitRepoStub) GetMultiple(context.Context, []string) (map[string]string, error) {
panic("unexpected GetMultiple call")
}
func (s *settingInitRepoStub) SetMultiple(_ context.Context, settings map[string]string) error {
s.updates = make(map[string]string, len(settings))
for key, value := range settings {
s.updates[key] = value
s.values[key] = value
}
return nil
}
func (s *settingInitRepoStub) GetAll(context.Context) (map[string]string, error) {
result := make(map[string]string, len(s.values))
for key, value := range s.values {
result[key] = value
}
return result, nil
}
func (s *settingInitRepoStub) Delete(context.Context, string) error {
panic("unexpected Delete call")
}
func TestSettingService_InitializeDefaultSettings_BackfillsWithoutOverwriting(t *testing.T) {
repo := &settingInitRepoStub{
values: map[string]string{
SettingKeyRegistrationEnabled: "false",
SettingKeySiteName: "Custom Site",
},
}
svc := NewSettingService(repo, &config.Config{})
require.NoError(t, svc.InitializeDefaultSettings(context.Background()))
require.NotContains(t, repo.updates, SettingKeyRegistrationEnabled)
require.NotContains(t, repo.updates, SettingKeySiteName)
require.Equal(t, "false", repo.values[SettingKeyRegistrationEnabled])
require.Equal(t, "Custom Site", repo.values[SettingKeySiteName])
require.Contains(t, repo.updates, SettingKeyPromoCodeEnabled)
require.Contains(t, repo.updates, SettingKeyTableDefaultPageSize)
}
func TestSettingService_InitializeDefaultSettings_PreservesExistingDefaultKeys(t *testing.T) {
repo := &settingInitRepoStub{
values: map[string]string{
SettingKeyRegistrationEnabled: "false",
SettingKeyPromoCodeEnabled: "false",
SettingKeyTableDefaultPageSize: "50",
},
}
svc := NewSettingService(repo, &config.Config{})
require.NoError(t, svc.InitializeDefaultSettings(context.Background()))
require.NotContains(t, repo.updates, SettingKeyRegistrationEnabled)
require.NotContains(t, repo.updates, SettingKeyPromoCodeEnabled)
require.NotContains(t, repo.updates, SettingKeyTableDefaultPageSize)
require.Equal(t, "false", repo.values[SettingKeyPromoCodeEnabled])
require.Equal(t, "50", repo.values[SettingKeyTableDefaultPageSize])
}