Skip to content

Commit fe4d19f

Browse files
fix issue in test dao pg
Signed-off-by: Thomas Poignant <thomas.poignant@gofeatureflag.org>
1 parent 6c43d65 commit fe4d19f

File tree

4 files changed

+21
-71
lines changed

4 files changed

+21
-71
lines changed

Makefile

+1-1
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ setup-env:
4545
## Test:
4646
test: test-server
4747
test-server: ## Run the tests of the project
48-
$(GOTEST) -v -race ./...
48+
$(GOTEST) -v -tags=docker -race ./...
4949

5050
## Coverage:
5151
coverage: coverage-server ## Run all the coverage on your project

server/cmd/goff_management_api_cmd_test.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ func TestNewGOFeatureFlagManagementAPICommand(t *testing.T) {
3131
name: "should have an error when creating a new GOFeatureFlagManagementAPICommand with no access to DB",
3232
options: cmd.APICommandOptions{},
3333
wantErr: assert.Error,
34-
expectedErrMsg: "error while initializing dependencies: impossible to initialize database connection: impossible to connect to the database: dial tcp [::1]:5432: connect: connection refused",
34+
expectedErrMsg: "error while initializing dependencies: impossible to initialize database connection: connection string is empty",
3535
},
3636
}
3737
for _, tt := range tests {

server/dao/pgimpl/postgres_impl.go

+4
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,10 @@ import (
1515
)
1616

1717
func NewPostgresDao(connectionString string) (dao.FlagStorage, error) {
18+
if connectionString == "" {
19+
return nil, fmt.Errorf("connection string is empty")
20+
}
21+
1822
conn, err := sqlx.Connect("postgres", connectionString)
1923
if err != nil {
2024
return nil, fmt.Errorf("impossible to connect to the database: %w", err)

server/dao/pgimpl/postgres_impl_test.go

+15-69
Original file line numberDiff line numberDiff line change
@@ -8,14 +8,15 @@ import (
88
"errors"
99
"fmt"
1010
"github.com/go-feature-flag/flag-management/server/dao"
11+
daoerr "github.com/go-feature-flag/flag-management/server/dao/err"
1112
"github.com/go-feature-flag/flag-management/server/dao/pgimpl"
13+
"github.com/go-feature-flag/flag-management/server/model"
1214
"github.com/go-feature-flag/flag-management/server/testutils"
1315
"os"
1416
"strconv"
1517
"testing"
1618
"time"
1719

18-
"github.com/go-feature-flag/flag-management/server/model"
1920
"github.com/golang-migrate/migrate/v4"
2021
"github.com/golang-migrate/migrate/v4/database/postgres"
2122
_ "github.com/golang-migrate/migrate/v4/source/file"
@@ -95,84 +96,29 @@ func getPostgresDao(t *testing.T, pgContainer *testcontainerPostgres.PostgresCon
9596
}
9697

9798
func TestNewPostgresDao(t *testing.T) {
98-
type args struct {
99-
host string
100-
port int
101-
dbName string
102-
user string
103-
password string
104-
}
10599
tests := []struct {
106-
name string
107-
args args
108-
wantErr assert.ErrorAssertionFunc
109-
wantErrMessage string
100+
name string
101+
connectionString string
102+
wantErr assert.ErrorAssertionFunc
103+
wantErrMessage string
110104
}{
111105
{
112-
name: "should return an error for an empty host",
113-
args: args{
114-
host: "",
115-
port: 5432,
116-
dbName: "test",
117-
user: "user",
118-
password: "password",
119-
},
120-
wantErr: assert.Error,
121-
wantErrMessage: "impossible to connect to the database: pq: password authentication failed for user \"user\"",
122-
},
123-
{
124-
name: "should return an error for an empty port",
125-
args: args{
126-
host: "localhost",
127-
dbName: "test",
128-
user: "user",
129-
password: "password",
130-
},
131-
wantErr: assert.Error,
132-
wantErrMessage: "impossible to connect to the database: dial tcp [::1]:0: connect: can't assign requested address",
133-
},
134-
{
135-
name: "should return an error for an empty dbName",
136-
args: args{
137-
host: "localhost",
138-
port: 5432,
139-
dbName: "",
140-
user: "user",
141-
password: "password",
142-
},
143-
wantErr: assert.Error,
144-
wantErrMessage: "impossible to connect to the database: pq: password authentication failed for user \"user\"",
106+
name: "should return an error for an empty connection string",
107+
connectionString: "postgres://user:password@localhost:5434/test?sslmode=disable",
108+
wantErr: assert.Error,
109+
wantErrMessage: "impossible to connect to the database: dial tcp [::1]:5434: connect: connection refused",
145110
},
146111
{
147-
name: "should return an error for an empty password",
148-
args: args{
149-
host: "localhost",
150-
port: 5432,
151-
dbName: "test",
152-
user: "user",
153-
password: "",
154-
},
155-
wantErr: assert.Error,
156-
wantErrMessage: "impossible to connect to the database: pq: password authentication failed for user \"user\"",
157-
},
158-
{
159-
name: "should return an error for a not available DB",
160-
args: args{
161-
host: "localhost",
162-
port: 5432,
163-
dbName: "test",
164-
user: "user",
165-
password: "password",
166-
},
167-
wantErr: assert.Error,
168-
wantErrMessage: "impossible to connect to the database: pq: password authentication failed for user \"user\"",
112+
name: "should return an error for a not available DB",
113+
connectionString: "postgres://user:password@localhost:1111/test?sslmode=disable",
114+
wantErr: assert.Error,
115+
wantErrMessage: "impossible to connect to the database: dial tcp [::1]:1111: connect: connection refused",
169116
},
170117
}
171118

172119
for _, tt := range tests {
173120
t.Run(tt.name, func(t *testing.T) {
174-
connectionString := fmt.Sprintf("postgres://%s:%s@%s:%d/%s?sslmode=disable", tt.args.user, tt.args.password, tt.args.host, tt.args.port, tt.args.dbName)
175-
_, err := pgimpl.NewPostgresDao(connectionString)
121+
_, err := pgimpl.NewPostgresDao(tt.connectionString)
176122
tt.wantErr(t, err)
177123
if err != nil {
178124
assert.Equal(t, tt.wantErrMessage, err.Error())

0 commit comments

Comments
 (0)