// TestCreateOpenFail ensures that errors which occur while opening or closing // a database are handled properly. func TestCreateOpenFail(t *testing.T) { // bogusCreateDB is a function which acts as a bogus create and open // driver function that intentionally returns a failure which can be // detected. dbType := "createopenfail" openError := fmt.Errorf("failed to create or open database for "+ "database type [%v]", dbType) bogusCreateDB := func(args ...interface{}) (database.Db, error) { return nil, openError } // Create and add driver that intentionally fails when created or opened // to ensure errors on database open and create are handled properly. driver := database.DriverDB{ DbType: dbType, CreateDB: bogusCreateDB, OpenDB: bogusCreateDB, } database.AddDBDriver(driver) // Ensure creating a database with the new type fails with the expected // error. _, err := database.CreateDB(dbType, "createfailtest") if err != openError { t.Errorf("TestCreateOpenFail: expected error not received - "+ "got: %v, want %v", err, openError) return } // Ensure opening a database with the new type fails with the expected // error. _, err = database.OpenDB(dbType, "openfailtest") if err != openError { t.Errorf("TestCreateOpenFail: expected error not received - "+ "got: %v, want %v", err, openError) return } }
// TestAddDuplicateDriver ensures that adding a duplicate driver does not // overwrite an existing one. func TestAddDuplicateDriver(t *testing.T) { supportedDBs := database.SupportedDBs() if len(supportedDBs) == 0 { t.Errorf("TestAddDuplicateDriver: No backends to test") return } dbType := supportedDBs[0] // bogusCreateDB is a function which acts as a bogus create and open // driver function and intentionally returns a failure that can be // detected if the interface allows a duplicate driver to overwrite an // existing one. bogusCreateDB := func(args ...interface{}) (database.Db, error) { return nil, fmt.Errorf("duplicate driver allowed for database "+ "type [%v]", dbType) } // Create a driver that tries to replace an existing one. Set its // create and open functions to a function that causes a test failure if // they are invoked. driver := database.DriverDB{ DbType: dbType, CreateDB: bogusCreateDB, OpenDB: bogusCreateDB, } database.AddDBDriver(driver) // Ensure creating a database of the type that we tried to replace // doesn't fail (if it does, it indicates the driver was erroneously // replaced). _, teardown, err := createDB(dbType, "dupdrivertest", true) if err != nil { t.Errorf("TestAddDuplicateDriver: %v", err) return } teardown() }
func init() { database.AddDBDriver(self) }
func init() { driver := database.DriverDB{DbType: "memdb", CreateDB: CreateDB, OpenDB: OpenDB} database.AddDBDriver(driver) }