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