github.com/Ali-iotechsys/sqlboiler/v4@v4.0.0-20221208124957-6aec9a5f1f71/drivers/registration_test.go (about) 1 package drivers 2 3 import ( 4 "testing" 5 6 "github.com/volatiletech/sqlboiler/v4/importers" 7 ) 8 9 type testRegistrationDriver struct{} 10 11 func (t testRegistrationDriver) Assemble(config Config) (*DBInfo, error) { 12 return &DBInfo{ 13 Tables: nil, 14 Dialect: Dialect{}, 15 }, nil 16 } 17 18 func (t testRegistrationDriver) Templates() (map[string]string, error) { 19 return nil, nil 20 } 21 22 func (t testRegistrationDriver) Imports() (importers.Collection, error) { 23 return importers.Collection{}, nil 24 } 25 26 func TestRegistration(t *testing.T) { 27 mock := testRegistrationDriver{} 28 RegisterFromInit("mock1", mock) 29 30 if d, ok := registeredDrivers["mock1"]; !ok { 31 t.Error("driver was not found") 32 } else if d != mock { 33 t.Error("got the wrong driver back") 34 } 35 } 36 37 func TestBinaryRegistration(t *testing.T) { 38 RegisterBinary("mock2", "/bin/true") 39 40 if d, ok := registeredDrivers["mock2"]; !ok { 41 t.Error("driver was not found") 42 } else if string(d.(binaryDriver)) != "/bin/true" { 43 t.Error("got the wrong driver back") 44 } 45 } 46 47 func TestBinaryFromArgRegistration(t *testing.T) { 48 RegisterBinaryFromCmdArg("/bin/true/mock5") 49 50 if d, ok := registeredDrivers["mock5"]; !ok { 51 t.Error("driver was not found") 52 } else if string(d.(binaryDriver)) != "/bin/true/mock5" { 53 t.Error("got the wrong driver back") 54 } 55 } 56 57 func TestGetDriver(t *testing.T) { 58 didYouPanic := false 59 60 RegisterBinary("mock4", "/bin/true") 61 62 func() { 63 defer func() { 64 if r := recover(); r != nil { 65 didYouPanic = true 66 } 67 }() 68 69 _ = GetDriver("mock4") 70 }() 71 72 if didYouPanic { 73 t.Error("expected not to panic when fetching a driver that's known") 74 } 75 76 func() { 77 defer func() { 78 if r := recover(); r != nil { 79 didYouPanic = true 80 } 81 }() 82 83 _ = GetDriver("notpresentdriver") 84 }() 85 86 if !didYouPanic { 87 t.Error("expected to recover from a panic") 88 } 89 } 90 91 func TestReregister(t *testing.T) { 92 didYouPanic := false 93 94 func() { 95 defer func() { 96 if r := recover(); r != nil { 97 didYouPanic = true 98 } 99 }() 100 101 RegisterBinary("mock3", "/bin/true") 102 RegisterBinary("mock3", "/bin/true") 103 }() 104 105 if !didYouPanic { 106 t.Error("expected to recover from a panic") 107 } 108 }