Skip to content

Commit d211587

Browse files
committed
fixed tests / add tests for validation
1 parent c2a6d0f commit d211587

File tree

9 files changed

+172
-207
lines changed

9 files changed

+172
-207
lines changed

client/middleware/3rd_party_logger.go

Lines changed: 0 additions & 145 deletions
This file was deleted.

client/middleware/README.md

Lines changed: 0 additions & 31 deletions
This file was deleted.

configurator/.env.tests

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
HOST=0.0.0.0
2+
PORT=8086

configurator/configuration_test.go

Lines changed: 45 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,60 @@
11
package configurator
22

33
import (
4+
"fmt"
5+
"os"
46
"testing"
57
)
68

7-
type Config struct {
8-
Host string `required:"false" default:"localhost" split_words:"true"`
9-
Port uint16 `default:"5432" split_words:"true"`
10-
User string `required:"true" split_words:"true"`
11-
Password string `required:"true" split_words:"true"`
12-
}
13-
149
// will load .env automatically
1510
// and check if all required envs are exists
1611
func TestNewConfiguration(t *testing.T) {
12+
type Config struct {
13+
Host string `required:"false" default:"localhost" split_words:"true"`
14+
Port uint16 `default:"5432" split_words:"true"`
15+
User string `required:"true" split_words:"true"`
16+
Password string `required:"true" split_words:"true"`
17+
}
18+
1719
cfg := &Config{}
1820
err := NewConfiguration(cfg, "DB")
1921
if err != nil {
2022
t.Fatalf("cannot process %s", err.Error())
2123
}
2224
}
25+
26+
// will load .env automatically
27+
// and check if all required envs are exists
28+
func TestAlternativeEnvPath(t *testing.T) {
29+
type ServerConfig struct {
30+
Host string `required:"true"`
31+
Port uint16 `required:"true"`
32+
}
33+
34+
os.Setenv("ENV_FILE", ".env.tests")
35+
cfg := &ServerConfig{}
36+
err := NewConfiguration(cfg, "")
37+
if err != nil {
38+
t.Fatalf("cannot process %s", err.Error())
39+
}
40+
}
41+
42+
// will load .env automatically
43+
// and check if all required envs are exists
44+
func TestRequiredEnvs(t *testing.T) {
45+
type ServerConfig struct {
46+
Host string `required:"true"`
47+
Port int16 `required:"true"`
48+
}
49+
defer func() {
50+
if r := recover(); r != nil {
51+
fmt.Println("Recovered in f", r)
52+
}
53+
}()
54+
55+
cfg := &ServerConfig{}
56+
err := NewConfiguration(cfg, "")
57+
if err == nil {
58+
t.Errorf("no env set, should return an error")
59+
}
60+
}

db/db_logging_test.go

Lines changed: 25 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -197,56 +197,57 @@ func TestLogger_DBQuery_ERROR(t *testing.T) {
197197
actualLogs := ReadStdout(stdout)
198198
expected := `
199199
{
200-
"level": "error",
201200
"component": "db",
202-
"error": "ERROR: column \"asd\" does not exist (SQLSTATE 42703)",
203201
"data": {
204202
"args": [],
205203
"err": {
206-
"Severity": "ERROR",
207204
"Code": "42703",
208-
"Message": "column \"asd\" does not exist",
205+
"ColumnName": "",
206+
"ConstraintName": "",
207+
"DataTypeName": "",
209208
"Detail": "",
209+
"File": "parse_relation.c",
210210
"Hint": "",
211-
"Position": 8,
212211
"InternalPosition": 0,
213212
"InternalQuery": "",
214-
"Where": "",
213+
"Line": 3722,
214+
"Message": "column \"asd\" does not exist",
215+
"Position": 8,
216+
"Routine": "errorMissingColumn",
215217
"SchemaName": "",
218+
"Severity": "ERROR",
219+
"SeverityUnlocalized": "ERROR",
216220
"TableName": "",
217-
"ColumnName": "",
218-
"DataTypeName": "",
219-
"ConstraintName": "",
220-
"File": "parse_relation.c",
221-
"Line": "%any%",
222-
"Routine": "errorMissingColumn"
221+
"Where": ""
223222
},
224223
"pid": "%any%",
225224
"sql": "select asd;",
226225
"time": "%any%"
227226
},
228-
"severity": "ERROR",
227+
"error": "ERROR: column \"asd\" does not exist (SQLSTATE 42703)",
228+
"level": "error",
229+
"message": "column \"asd\" does not exist",
229230
"serviceContext": {
230-
"service": "test-db",
231-
"version": "v1.0.0",
232-
"user": "test-test-test",
233-
"request_id": "req-1",
234-
"msg_id": "msg-1",
235231
"httpRequest": {
236232
"method": "POST",
237-
"url": "https://test",
238-
"userAgent": "test-agent",
239233
"referrer": "test-Referrer",
234+
"remoteIp": "0.0.0.0",
240235
"responseStatusCode": 0,
241-
"remoteIp": "0.0.0.0"
236+
"url": "https://test",
237+
"userAgent": "test-agent"
242238
},
239+
"msg_id": "msg-1",
240+
"request_id": "req-1",
241+
"service": "test-db",
243242
"sourceReference": {
244243
"repository": "https://github",
245244
"revisionId": "1111"
246-
}
245+
},
246+
"user": "test-test-test",
247+
"version": "v1.0.0"
247248
},
248-
"time": "%any%",
249-
"message": "column \"asd\" does not exist"
249+
"severity": "ERROR",
250+
"time": "%any%"
250251
}
251252
`
252253
actual := actualLogs[len(actualLogs)-1]

validator/go.mod

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,16 +5,22 @@ go 1.22.2
55
require (
66
github.com/go-playground/validator/v10 v10.22.0
77
github.com/pkg/errors v0.9.1
8+
github.com/stretchr/testify v1.9.0
89
github.com/webdevelop-pro/go-common/response v0.0.0-20240709190542-6fc8edc6e2da
910
)
1011

1112
require (
13+
github.com/davecgh/go-spew v1.1.1 // indirect
1214
github.com/gabriel-vasile/mimetype v1.4.3 // indirect
1315
github.com/go-playground/locales v0.14.1 // indirect
1416
github.com/go-playground/universal-translator v0.18.1 // indirect
1517
github.com/leodido/go-urn v1.4.0 // indirect
18+
github.com/pmezard/go-difflib v1.0.0 // indirect
19+
github.com/webdevelop-pro/go-common/misc v0.0.0-20240709190542-6fc8edc6e2da // indirect
20+
github.com/webdevelop-pro/go-common/tests v0.0.0-20240710053503-c2a6d0f99fed // indirect
1621
golang.org/x/crypto v0.19.0 // indirect
1722
golang.org/x/net v0.21.0 // indirect
1823
golang.org/x/sys v0.17.0 // indirect
1924
golang.org/x/text v0.14.0 // indirect
25+
gopkg.in/yaml.v3 v3.0.1 // indirect
2026
)

validator/go.sum

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,14 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
1818
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
1919
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
2020
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
21+
github.com/stretchr/testify v1.9.0 h1:HtqpIVDClZ4nwg75+f6Lvsy/wHu+3BoSGCbBAcpTsTg=
22+
github.com/stretchr/testify v1.9.0/go.mod h1:r2ic/lqez/lEtzL7wO/rwa5dbSLXVDPFyf8C91i36aY=
23+
github.com/webdevelop-pro/go-common/misc v0.0.0-20240709190542-6fc8edc6e2da h1:52xUrPcn5XKa/+DWMYATwhFEzVZlAXMZnWexuDb7kxQ=
24+
github.com/webdevelop-pro/go-common/misc v0.0.0-20240709190542-6fc8edc6e2da/go.mod h1:rWON4xXHLd2YPtk0dsR1mpscY88LJDr+UawAV0fU2n8=
2125
github.com/webdevelop-pro/go-common/response v0.0.0-20240709190542-6fc8edc6e2da h1:7iWqnpWgEumJUCjKSNLxNzV53hPyQsbxhNO1nYfR83E=
2226
github.com/webdevelop-pro/go-common/response v0.0.0-20240709190542-6fc8edc6e2da/go.mod h1:OClsHgTVyGEjiOdxOJ/s3fwmHlLbcFRKM+RzOzHXmpE=
27+
github.com/webdevelop-pro/go-common/tests v0.0.0-20240710053503-c2a6d0f99fed h1:Wmhc7LeX/kQBk+S+LWksYPFXctl6FYFCnaM5uV5Ecvo=
28+
github.com/webdevelop-pro/go-common/tests v0.0.0-20240710053503-c2a6d0f99fed/go.mod h1:qHHawYTo8EOZOrl78IjxUWJL8cSJuNjS65GMBUbvE88=
2329
golang.org/x/crypto v0.19.0 h1:ENy+Az/9Y1vSrlrvBSyna3PITt4tiZLf7sgCjZBX7Wo=
2430
golang.org/x/crypto v0.19.0/go.mod h1:Iy9bg/ha4yyC70EfRS8jz+B6ybOBKMaSxLj6P6oBDfU=
2531
golang.org/x/net v0.21.0 h1:AQyQV4dYCvJ7vGmJyKki9+PBdyvhkSd8EIx/qb0AYv4=
@@ -28,5 +34,7 @@ golang.org/x/sys v0.17.0 h1:25cE3gD+tdBA7lp7QfhuV+rJiE9YXTcS3VG1SqssI/Y=
2834
golang.org/x/sys v0.17.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
2935
golang.org/x/text v0.14.0 h1:ScX5w1eTa3QqT8oi6+ziP7dTV1S2+ALU0bI+0zXKWiQ=
3036
golang.org/x/text v0.14.0/go.mod h1:18ZOQIKpY8NJVqYksKHtTdi31H5itFRjB5/qKTNYzSU=
37+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
38+
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
3139
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
3240
gopkg.in/yaml.v3 v3.0.1/go.mod h1:K4uyk7z7BCEPqu6E+C64Yfv1cQ7kz7rIZviUmN+EgEM=

validator/validate.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ const (
1818
MsgGt = "greater than %s"
1919
MsgOneOf = "must be one of: %s"
2020
MsgEq = "must be equal to: %s"
21+
MsgSSN = "is a valid social security number: %s"
2122
)
2223

2324
type FieldError struct {
@@ -56,6 +57,8 @@ func beautifulMsg(fe validator.FieldError) string {
5657
return fmt.Sprintf(MsgOneOf, fe.Param())
5758
case "eq":
5859
return fmt.Sprintf(MsgEq, fe.Param())
60+
case "ssn":
61+
return fmt.Sprintf(MsgSSN, fe.Value())
5962
}
6063
return fe.Error() // default error
6164
}

0 commit comments

Comments
 (0)