github.com/dashpay/godash@v0.0.0-20160726055534-e038a21e0e3d/database/driver_test.go (about) 1 // Copyright (c) 2015-2016 The btcsuite developers 2 // Copyright (c) 2016 The Dash developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package database_test 7 8 import ( 9 "fmt" 10 "testing" 11 12 "github.com/dashpay/godash/database" 13 _ "github.com/dashpay/godash/database/ffldb" 14 ) 15 16 var ( 17 // ignoreDbTypes are types which should be ignored when running tests 18 // that iterate all supported DB types. This allows some tests to add 19 // bogus drivers for testing purposes while still allowing other tests 20 // to easily iterate all supported drivers. 21 ignoreDbTypes = map[string]bool{"createopenfail": true} 22 ) 23 24 // checkDbError ensures the passed error is a database.Error with an error code 25 // that matches the passed error code. 26 func checkDbError(t *testing.T, testName string, gotErr error, wantErrCode database.ErrorCode) bool { 27 dbErr, ok := gotErr.(database.Error) 28 if !ok { 29 t.Errorf("%s: unexpected error type - got %T, want %T", 30 testName, gotErr, database.Error{}) 31 return false 32 } 33 if dbErr.ErrorCode != wantErrCode { 34 t.Errorf("%s: unexpected error code - got %s (%s), want %s", 35 testName, dbErr.ErrorCode, dbErr.Description, 36 wantErrCode) 37 return false 38 } 39 40 return true 41 } 42 43 // TestAddDuplicateDriver ensures that adding a duplicate driver does not 44 // overwrite an existing one. 45 func TestAddDuplicateDriver(t *testing.T) { 46 supportedDrivers := database.SupportedDrivers() 47 if len(supportedDrivers) == 0 { 48 t.Errorf("no backends to test") 49 return 50 } 51 dbType := supportedDrivers[0] 52 53 // bogusCreateDB is a function which acts as a bogus create and open 54 // driver function and intentionally returns a failure that can be 55 // detected if the interface allows a duplicate driver to overwrite an 56 // existing one. 57 bogusCreateDB := func(args ...interface{}) (database.DB, error) { 58 return nil, fmt.Errorf("duplicate driver allowed for database "+ 59 "type [%v]", dbType) 60 } 61 62 // Create a driver that tries to replace an existing one. Set its 63 // create and open functions to a function that causes a test failure if 64 // they are invoked. 65 driver := database.Driver{ 66 DbType: dbType, 67 Create: bogusCreateDB, 68 Open: bogusCreateDB, 69 } 70 testName := "duplicate driver registration" 71 err := database.RegisterDriver(driver) 72 if !checkDbError(t, testName, err, database.ErrDbTypeRegistered) { 73 return 74 } 75 } 76 77 // TestCreateOpenFail ensures that errors which occur while opening or closing 78 // a database are handled properly. 79 func TestCreateOpenFail(t *testing.T) { 80 // bogusCreateDB is a function which acts as a bogus create and open 81 // driver function that intentionally returns a failure which can be 82 // detected. 83 dbType := "createopenfail" 84 openError := fmt.Errorf("failed to create or open database for "+ 85 "database type [%v]", dbType) 86 bogusCreateDB := func(args ...interface{}) (database.DB, error) { 87 return nil, openError 88 } 89 90 // Create and add driver that intentionally fails when created or opened 91 // to ensure errors on database open and create are handled properly. 92 driver := database.Driver{ 93 DbType: dbType, 94 Create: bogusCreateDB, 95 Open: bogusCreateDB, 96 } 97 database.RegisterDriver(driver) 98 99 // Ensure creating a database with the new type fails with the expected 100 // error. 101 _, err := database.Create(dbType) 102 if err != openError { 103 t.Errorf("expected error not received - got: %v, want %v", err, 104 openError) 105 return 106 } 107 108 // Ensure opening a database with the new type fails with the expected 109 // error. 110 _, err = database.Open(dbType) 111 if err != openError { 112 t.Errorf("expected error not received - got: %v, want %v", err, 113 openError) 114 return 115 } 116 } 117 118 // TestCreateOpenUnsupported ensures that attempting to create or open an 119 // unsupported database type is handled properly. 120 func TestCreateOpenUnsupported(t *testing.T) { 121 // Ensure creating a database with an unsupported type fails with the 122 // expected error. 123 testName := "create with unsupported database type" 124 dbType := "unsupported" 125 _, err := database.Create(dbType) 126 if !checkDbError(t, testName, err, database.ErrDbUnknownType) { 127 return 128 } 129 130 // Ensure opening a database with the an unsupported type fails with the 131 // expected error. 132 testName = "open with unsupported database type" 133 _, err = database.Open(dbType) 134 if !checkDbError(t, testName, err, database.ErrDbUnknownType) { 135 return 136 } 137 }