@@ -4,6 +4,11 @@ import (
44 "errors"
55 "strings"
66 "testing"
7+
8+ "github.com/go-sql-driver/mysql"
9+ "github.com/jackc/pgx/v4/stdlib"
10+ v5 "github.com/jackc/pgx/v5/stdlib"
11+ "github.com/lib/pq"
712)
813
914const (
@@ -27,7 +32,7 @@ func TestAvailableDriversAreRegistered(t *testing.T) {
2732 }
2833}
2934
30- func TestCantRegisterADriverWithoutAFactory (t * testing.T ) {
35+ func TestCantRegisterADriverWithNilFactory (t * testing.T ) {
3136 unregisterAllDrivers ()
3237 defer func () {
3338 if r := recover (); r == nil {
@@ -40,6 +45,23 @@ func TestCantRegisterADriverWithoutAFactory(t *testing.T) {
4045 }
4146}
4247
48+ func TestRegisterAllWithANilFactoryPanics (t * testing.T ) {
49+ tmpAvailableDrivers := availableDrivers
50+ availableDrivers = map [string ]factory {
51+ "pgx" : nil ,
52+ }
53+
54+ defer func () {
55+ if r := recover (); r == nil {
56+ t .Fatal ("expected a panic when attempting to create a driver" )
57+ }
58+
59+ availableDrivers = tmpAvailableDrivers
60+ }()
61+
62+ registerAllDrivers ()
63+ }
64+
4365func TestCanRegisterAValidFactory (t * testing.T ) {
4466 unregisterAllDrivers ()
4567 defer func () {
@@ -69,9 +91,20 @@ func TestCantRegisterMultipleFactoriesWithTheSameName(t *testing.T) {
6991 t .Fatal (err )
7092 }
7193
72- if err := Register (driverName , fn ); err == nil {
94+ err := Register (driverName , fn )
95+ if err == nil {
7396 t .Fatal ("expected an error registering a duplicate driver but didn't get one" )
7497 }
98+
99+ expectedError := errFactoryAlreadyRegistered {driverName }
100+
101+ if err .Error () != expectedError .Error () {
102+ t .Fatalf (
103+ "expected error to be %s but got %s instead" ,
104+ expectedError .Error (),
105+ err .Error (),
106+ )
107+ }
75108 d := drivers ()
76109 if len (d ) != 1 || d [0 ] != driverName {
77110 t .Fatalf ("expected one driver to be registered but got %d" , len (d ))
@@ -128,3 +161,44 @@ func TestCantCreateMissingDriver(t *testing.T) {
128161 t .Fatalf ("expected 'invalid Driver name' in error but got %s" , err )
129162 }
130163}
164+
165+ func TestCanCreateAllDrivers (t * testing.T ) {
166+ registerAllDrivers ()
167+ for k := range availableDrivers {
168+ d , err := CreateDriver (k )
169+ if err != nil {
170+ t .Fatal (err )
171+ }
172+
173+ switch k {
174+ case "pgx" :
175+ if driver , ok := d .Driver .(* v5.Driver ); ! ok {
176+ t .Fatalf (
177+ "expected pgx factory to create a v5 *stdlib.Driver but got a %T instead" ,
178+ driver ,
179+ )
180+ }
181+ case "pgxv4" :
182+ if driver , ok := d .Driver .(* stdlib.Driver ); ! ok {
183+ t .Fatalf (
184+ "expected pgxv4 factory to create a v4 *stdlib.Driver but got a %T instead" ,
185+ driver ,
186+ )
187+ }
188+ case "pq" :
189+ if driver , ok := d .Driver .(* pq.Driver ); ! ok {
190+ t .Fatalf (
191+ "expected pq factory to create a *pq.Driver but got a %T instead" ,
192+ driver ,
193+ )
194+ }
195+ case "mysql" :
196+ if driver , ok := d .Driver .(* mysql.MySQLDriver ); ! ok {
197+ t .Fatalf (
198+ "expected mysql factory to create a *mysql.MySQLDriver but got a %T instead" ,
199+ driver ,
200+ )
201+ }
202+ }
203+ }
204+ }
0 commit comments