Skip to content

Commit 3ac3452

Browse files
committed
Refactor TestParseURL
This is in preparation for supporting query parameters in ParseURL: - use an expected *Options instance to execute assertions on - extract assertions into helper function - enable parallel testing - condense test table
1 parent 0982b38 commit 3ac3452

File tree

1 file changed

+88
-129
lines changed

1 file changed

+88
-129
lines changed

options_test.go

Lines changed: 88 additions & 129 deletions
Original file line numberDiff line numberDiff line change
@@ -4,157 +4,116 @@
44
package redis
55

66
import (
7+
"crypto/tls"
78
"errors"
89
"testing"
910
"time"
1011
)
1112

1213
func TestParseURL(t *testing.T) {
1314
cases := []struct {
14-
u string
15-
addr string
16-
db int
17-
tls bool
18-
err error
19-
user string
20-
pass string
15+
url string
16+
o *Options // expected value
17+
err error
2118
}{
2219
{
23-
"redis://localhost:123/1",
24-
"localhost:123",
25-
1, false, nil,
26-
"", "",
27-
},
28-
{
29-
"redis://localhost:123",
30-
"localhost:123",
31-
0, false, nil,
32-
"", "",
33-
},
34-
{
35-
"redis://localhost/1",
36-
"localhost:6379",
37-
1, false, nil,
38-
"", "",
39-
},
40-
{
41-
"redis://12345",
42-
"12345:6379",
43-
0, false, nil,
44-
"", "",
45-
},
46-
{
47-
"rediss://localhost:123",
48-
"localhost:123",
49-
0, true, nil,
50-
"", "",
51-
},
52-
{
53-
"redis://:bar@localhost:123",
54-
"localhost:123",
55-
0, false, nil,
56-
"", "bar",
57-
},
58-
{
59-
"redis://foo@localhost:123",
60-
"localhost:123",
61-
0, false, nil,
62-
"foo", "",
63-
},
64-
{
65-
"redis://foo:bar@localhost:123",
66-
"localhost:123",
67-
0, false, nil,
68-
"foo", "bar",
69-
},
70-
{
71-
"unix:///tmp/redis.sock",
72-
"/tmp/redis.sock",
73-
0, false, nil,
74-
"", "",
75-
},
76-
{
77-
"unix://foo:bar@/tmp/redis.sock",
78-
"/tmp/redis.sock",
79-
0, false, nil,
80-
"foo", "bar",
81-
},
82-
{
83-
"unix://foo:bar@/tmp/redis.sock?db=3",
84-
"/tmp/redis.sock",
85-
3, false, nil,
86-
"foo", "bar",
87-
},
88-
{
89-
"unix://foo:bar@/tmp/redis.sock?db=test",
90-
"/tmp/redis.sock",
91-
0, false, errors.New("redis: invalid database number: strconv.Atoi: parsing \"test\": invalid syntax"),
92-
"", "",
93-
},
94-
{
95-
"redis://localhost/?abc=123",
96-
"",
97-
0, false, errors.New("redis: no options supported"),
98-
"", "",
99-
},
100-
{
101-
"http://google.com",
102-
"",
103-
0, false, errors.New("redis: invalid URL scheme: http"),
104-
"", "",
105-
},
106-
{
107-
"redis://localhost/1/2/3/4",
108-
"",
109-
0, false, errors.New("redis: invalid URL path: /1/2/3/4"),
110-
"", "",
111-
},
112-
{
113-
"12345",
114-
"",
115-
0, false, errors.New("redis: invalid URL scheme: "),
116-
"", "",
117-
},
118-
{
119-
"redis://localhost/iamadatabase",
120-
"",
121-
0, false, errors.New(`redis: invalid database number: "iamadatabase"`),
122-
"", "",
20+
url: "redis://localhost:123/1",
21+
o: &Options{Addr: "localhost:123", DB: 1},
22+
}, {
23+
url: "redis://localhost:123",
24+
o: &Options{Addr: "localhost:123"},
25+
}, {
26+
url: "redis://localhost/1",
27+
o: &Options{Addr: "localhost:6379", DB: 1},
28+
}, {
29+
url: "redis://12345",
30+
o: &Options{Addr: "12345:6379"},
31+
}, {
32+
url: "rediss://localhost:123",
33+
o: &Options{Addr: "localhost:123", TLSConfig: &tls.Config{ /* no deep comparison */ }},
34+
}, {
35+
url: "redis://:bar@localhost:123",
36+
o: &Options{Addr: "localhost:123", Password: "bar"},
37+
}, {
38+
url: "redis://foo@localhost:123",
39+
o: &Options{Addr: "localhost:123", Username: "foo"},
40+
}, {
41+
url: "redis://foo:bar@localhost:123",
42+
o: &Options{Addr: "localhost:123", Username: "foo", Password: "bar"},
43+
}, {
44+
url: "unix:///tmp/redis.sock",
45+
o: &Options{Addr: "/tmp/redis.sock"},
46+
}, {
47+
url: "unix://foo:bar@/tmp/redis.sock",
48+
o: &Options{Addr: "/tmp/redis.sock", Username: "foo", Password: "bar"},
49+
}, {
50+
url: "unix://foo:bar@/tmp/redis.sock?db=3",
51+
o: &Options{Addr: "/tmp/redis.sock", Username: "foo", Password: "bar", DB: 3},
52+
}, {
53+
url: "unix://foo:bar@/tmp/redis.sock?db=test",
54+
err: errors.New(`redis: invalid database number: strconv.Atoi: parsing "test": invalid syntax`),
55+
}, {
56+
url: "redis://localhost/?abc=123",
57+
err: errors.New("redis: no options supported"),
58+
}, {
59+
url: "http://google.com",
60+
err: errors.New("redis: invalid URL scheme: http"),
61+
}, {
62+
url: "redis://localhost/1/2/3/4",
63+
err: errors.New("redis: invalid URL path: /1/2/3/4"),
64+
}, {
65+
url: "12345",
66+
err: errors.New("redis: invalid URL scheme: "),
67+
}, {
68+
url: "redis://localhost/iamadatabase",
69+
err: errors.New(`redis: invalid database number: "iamadatabase"`),
12370
},
12471
}
12572

126-
for _, c := range cases {
127-
t.Run(c.u, func(t *testing.T) {
128-
o, err := ParseURL(c.u)
129-
if c.err == nil && err != nil {
73+
for i := range cases {
74+
tc := cases[i]
75+
t.Run(tc.url, func(t *testing.T) {
76+
t.Parallel()
77+
78+
actual, err := ParseURL(tc.url)
79+
if tc.err == nil && err != nil {
13080
t.Fatalf("unexpected error: %q", err)
13181
return
13282
}
133-
if c.err != nil && err != nil {
134-
if c.err.Error() != err.Error() {
135-
t.Fatalf("got %q, expected %q", err, c.err)
83+
if tc.err != nil && err != nil {
84+
if tc.err.Error() != err.Error() {
85+
t.Fatalf("got %q, expected %q", err, tc.err)
13686
}
13787
return
13888
}
139-
if o.Addr != c.addr {
140-
t.Errorf("got %q, want %q", o.Addr, c.addr)
141-
}
142-
if o.DB != c.db {
143-
t.Errorf("got %q, expected %q", o.DB, c.db)
144-
}
145-
if c.tls && o.TLSConfig == nil {
146-
t.Errorf("got nil TLSConfig, expected a TLSConfig")
147-
}
148-
if o.Username != c.user {
149-
t.Errorf("got %q, expected %q", o.Username, c.user)
150-
}
151-
if o.Password != c.pass {
152-
t.Errorf("got %q, expected %q", o.Password, c.pass)
153-
}
89+
comprareOptions(t, actual, tc.o)
15490
})
15591
}
15692
}
15793

94+
func comprareOptions(t *testing.T, actual, expected *Options) {
95+
t.Helper()
96+
97+
if actual.Addr != expected.Addr {
98+
t.Errorf("got %q, want %q", actual.Addr, expected.Addr)
99+
}
100+
if actual.DB != expected.DB {
101+
t.Errorf("got %q, expected %q", actual.DB, expected.DB)
102+
}
103+
if actual.TLSConfig == nil && expected.TLSConfig != nil {
104+
t.Errorf("got nil TLSConfig, expected a TLSConfig")
105+
}
106+
if actual.TLSConfig != nil && expected.TLSConfig == nil {
107+
t.Errorf("got TLSConfig, expected no TLSConfig")
108+
}
109+
if actual.Username != expected.Username {
110+
t.Errorf("got %q, expected %q", actual.Username, expected.Username)
111+
}
112+
if actual.Password != expected.Password {
113+
t.Errorf("got %q, expected %q", actual.Password, expected.Password)
114+
}
115+
}
116+
158117
// Test ReadTimeout option initialization, including special values -1 and 0.
159118
// And also test behaviour of WriteTimeout option, when it is not explicitly set and use
160119
// ReadTimeout value.

0 commit comments

Comments
 (0)