-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapi_test.go
More file actions
171 lines (144 loc) · 4.15 KB
/
api_test.go
File metadata and controls
171 lines (144 loc) · 4.15 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
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
package fig
import (
"context"
"errors"
"testing"
"time"
)
type testConfig struct {
Host string `env:"TEST_FIG_API_HOST" default:"localhost"`
Port int `env:"TEST_FIG_API_PORT" default:"8080"`
Debug bool `env:"TEST_FIG_API_DEBUG"`
Timeout time.Duration `env:"TEST_FIG_API_TIMEOUT" default:"30s"`
Tags []string `env:"TEST_FIG_API_TAGS"`
Password string `secret:"test/password"`
}
func TestLoad_Defaults(t *testing.T) {
var cfg testConfig
if err := Load(&cfg); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Host != "localhost" {
t.Errorf("Host = %q, want %q", cfg.Host, "localhost")
}
if cfg.Port != 8080 {
t.Errorf("Port = %d, want %d", cfg.Port, 8080)
}
if cfg.Timeout != 30*time.Second {
t.Errorf("Timeout = %v, want %v", cfg.Timeout, 30*time.Second)
}
}
func TestLoad_EnvOverride(t *testing.T) {
t.Setenv("TEST_FIG_API_HOST", "example.com")
t.Setenv("TEST_FIG_API_PORT", "9000")
t.Setenv("TEST_FIG_API_DEBUG", "true")
t.Setenv("TEST_FIG_API_TAGS", "api,v2,prod")
var cfg testConfig
if err := Load(&cfg); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Host != "example.com" {
t.Errorf("Host = %q, want %q", cfg.Host, "example.com")
}
if cfg.Port != 9000 {
t.Errorf("Port = %d, want %d", cfg.Port, 9000)
}
if !cfg.Debug {
t.Error("Debug should be true")
}
if len(cfg.Tags) != 3 || cfg.Tags[0] != "api" {
t.Errorf("Tags = %v, want [api v2 prod]", cfg.Tags)
}
}
func TestLoad_SecretProvider(t *testing.T) {
provider := &mockProvider{secrets: map[string]string{"test/password": "secret123"}}
var cfg testConfig
if err := Load(&cfg, provider); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Password != "secret123" {
t.Errorf("Password = %q, want %q", cfg.Password, "secret123")
}
}
type requiredConfig struct {
Name string `env:"TEST_FIG_REQUIRED_NAME" required:"true"`
}
func TestLoad_Required(t *testing.T) {
var cfg requiredConfig
err := Load(&cfg)
if err == nil {
t.Fatal("expected error for missing required field")
}
var fieldErr *FieldError
if !errors.As(err, &fieldErr) {
t.Fatalf("expected FieldError, got %T", err)
}
if fieldErr.Field != "Name" {
t.Errorf("FieldError.Field = %q, want %q", fieldErr.Field, "Name")
}
}
type nestedDatabaseConfig struct {
Host string `env:"TEST_FIG_NESTED_DB_HOST" default:"db.local"`
Port int `env:"TEST_FIG_NESTED_DB_PORT" default:"5432"`
}
type nestedAppConfig struct {
Name string `env:"TEST_FIG_NESTED_APP_NAME" default:"myapp"`
Database nestedDatabaseConfig
}
func TestLoad_NestedStruct(t *testing.T) {
var cfg nestedAppConfig
if err := Load(&cfg); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Name != "myapp" {
t.Errorf("Name = %q, want %q", cfg.Name, "myapp")
}
if cfg.Database.Host != "db.local" {
t.Errorf("Database.Host = %q, want %q", cfg.Database.Host, "db.local")
}
if cfg.Database.Port != 5432 {
t.Errorf("Database.Port = %d, want %d", cfg.Database.Port, 5432)
}
}
type validatingConfig struct {
Port int `env:"TEST_FIG_VALIDATE_PORT" default:"8080"`
}
func (c *validatingConfig) Validate() error {
if c.Port <= 0 {
return errors.New("port must be positive")
}
return nil
}
func TestLoad_Validator(t *testing.T) {
t.Setenv("TEST_FIG_VALIDATE_PORT", "-1")
var cfg validatingConfig
err := Load(&cfg)
if err == nil {
t.Fatal("expected validation error")
}
if err.Error() != "port must be positive" {
t.Errorf("unexpected error: %v", err)
}
}
func TestLoadContext(t *testing.T) {
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
var cfg testConfig
if err := LoadContext(ctx, &cfg); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Host != "localhost" {
t.Errorf("Host = %q, want %q", cfg.Host, "localhost")
}
}
func TestLoadContext_WithProvider(t *testing.T) {
ctx := context.Background()
provider := &mockProvider{secrets: map[string]string{"test/password": "ctx-secret"}}
var cfg testConfig
if err := LoadContext(ctx, &cfg, provider); err != nil {
t.Fatalf("unexpected error: %v", err)
}
if cfg.Password != "ctx-secret" {
t.Errorf("Password = %q, want %q", cfg.Password, "ctx-secret")
}
}