Skip to content

Commit

Permalink
test registering a driver with options
Browse files Browse the repository at this point in the history
  • Loading branch information
daniel-garcia committed Aug 14, 2023
1 parent 8d70c79 commit 4a0e748
Showing 1 changed file with 67 additions and 0 deletions.
67 changes: 67 additions & 0 deletions driver_internal_test.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,76 @@
package hotload

import (
"database/sql/driver"
"fmt"
"reflect"
"strings"
"testing"
)

type testDriver struct {
options map[string]string
}

func (d *testDriver) Open(name string) (driver.Conn, error) {

parts := strings.Fields(name)
if len(parts) != 2 {
return nil, fmt.Errorf("not implemented")
}
switch parts[0] {
case "with_options":
return nil, fmt.Errorf(d.options[parts[1]])
default:
}
return nil, fmt.Errorf("directive not found")
}

func withConnectionStringOptions(options map[string]string) driverOption {
return func(di *driverInstance) {
di.options = options
}
}

func TestRegisterSQLDriverWithOptions(t *testing.T) {
type args struct {
name string
driver driver.Driver
options []driverOption
}
tests := []struct {
name string
args args
}{
{
name: "driver with an option",
args: args{
name: "test with options",
driver: &testDriver{},
options: []driverOption{
withConnectionStringOptions(map[string]string{"a": "b"}),
},
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
RegisterSQLDriver(tt.args.name, tt.args.driver, tt.args.options...)
mu.Lock()
defer mu.Unlock()

d, ok := sqlDrivers[tt.args.name]
if !ok {
t.Errorf("RegisterSQLDriver() did not register the driver")
}
gotOptions := d.driver.(*testDriver).options
if reflect.DeepEqual(gotOptions, tt.args.options) {
t.Errorf("RegisterSQLDriver() did not set the options")
}
})
}
}

func Test_mergeConnectionStringOptions(t *testing.T) {
type args struct {
dsn string
Expand Down

0 comments on commit 4a0e748

Please sign in to comment.