@@ -8,14 +8,15 @@ import (
8
8
"errors"
9
9
"fmt"
10
10
"github.com/go-feature-flag/flag-management/server/dao"
11
+ daoerr "github.com/go-feature-flag/flag-management/server/dao/err"
11
12
"github.com/go-feature-flag/flag-management/server/dao/pgimpl"
13
+ "github.com/go-feature-flag/flag-management/server/model"
12
14
"github.com/go-feature-flag/flag-management/server/testutils"
13
15
"os"
14
16
"strconv"
15
17
"testing"
16
18
"time"
17
19
18
- "github.com/go-feature-flag/flag-management/server/model"
19
20
"github.com/golang-migrate/migrate/v4"
20
21
"github.com/golang-migrate/migrate/v4/database/postgres"
21
22
_ "github.com/golang-migrate/migrate/v4/source/file"
@@ -95,84 +96,29 @@ func getPostgresDao(t *testing.T, pgContainer *testcontainerPostgres.PostgresCon
95
96
}
96
97
97
98
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
- }
105
99
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
110
104
}{
111
105
{
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" ,
145
110
},
146
111
{
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" ,
169
116
},
170
117
}
171
118
172
119
for _ , tt := range tests {
173
120
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 )
176
122
tt .wantErr (t , err )
177
123
if err != nil {
178
124
assert .Equal (t , tt .wantErrMessage , err .Error ())
0 commit comments