From 4a0e7486081c359ae2e4600c1dda472d23d0caa7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniel=20Garc=C3=ADa?= Date: Mon, 14 Aug 2023 09:27:59 -0500 Subject: [PATCH] test registering a driver with options --- driver_internal_test.go | 67 +++++++++++++++++++++++++++++++++++++++++ 1 file changed, 67 insertions(+) diff --git a/driver_internal_test.go b/driver_internal_test.go index 96b6436..20d4b6a 100644 --- a/driver_internal_test.go +++ b/driver_internal_test.go @@ -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