github.com/go-kivik/kivik/v4@v4.3.2/internal/registry/registry_test.go (about) 1 // Licensed under the Apache License, Version 2.0 (the "License"); you may not 2 // use this file except in compliance with the License. You may obtain a copy of 3 // the License at 4 // 5 // http://www.apache.org/licenses/LICENSE-2.0 6 // 7 // Unless required by applicable law or agreed to in writing, software 8 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 9 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the 10 // License for the specific language governing permissions and limitations under 11 // the License. 12 13 package registry 14 15 import ( 16 "sync" 17 "testing" 18 19 "gitlab.com/flimzy/testy" 20 21 "github.com/go-kivik/kivik/v4/driver" 22 "github.com/go-kivik/kivik/v4/int/mock" 23 ) 24 25 // to protect the registry from concurrent tests 26 var registryMU sync.Mutex 27 28 func TestRegister(t *testing.T) { 29 registryMU.Lock() 30 defer registryMU.Unlock() 31 t.Run("nil driver", func(t *testing.T) { 32 t.Cleanup(func() { 33 drivers = make(map[string]driver.Driver) 34 }) 35 p := func() (p interface{}) { 36 defer func() { 37 p = recover() 38 }() 39 Register("foo", nil) 40 return "" 41 }() 42 if p.(string) != "kivik: Register driver is nil" { 43 t.Errorf("Unexpected panic: %v", p) 44 } 45 }) 46 47 t.Run("duplicate driver", func(t *testing.T) { 48 t.Cleanup(func() { 49 drivers = make(map[string]driver.Driver) 50 }) 51 p := func() (p interface{}) { 52 defer func() { 53 p = recover() 54 }() 55 Register("foo", &mock.Driver{}) 56 Register("foo", &mock.Driver{}) 57 return "" 58 }() 59 if p.(string) != "kivik: Register called twice for driver foo" { 60 t.Errorf("Unexpected panic: %v", p) 61 } 62 }) 63 64 t.Run("success", func(t *testing.T) { 65 t.Cleanup(func() { 66 drivers = make(map[string]driver.Driver) 67 }) 68 p := func() (p interface{}) { 69 defer func() { 70 p = recover() 71 }() 72 Register("foo", &mock.Driver{}) 73 return "" 74 }() 75 if p != nil { 76 t.Errorf("Unexpected panic: %v", p) 77 } 78 expected := map[string]driver.Driver{ 79 "foo": &mock.Driver{}, 80 } 81 if d := testy.DiffInterface(expected, drivers); d != nil { 82 t.Error(d) 83 } 84 }) 85 }