github.com/prebid/prebid-server/v2@v2.18.0/stored_requests/backends/db_provider/db_provider_mock.go (about)

     1  package db_provider
     2  
     3  import (
     4  	"context"
     5  	"database/sql"
     6  	"reflect"
     7  
     8  	"github.com/DATA-DOG/go-sqlmock"
     9  	"github.com/prebid/prebid-server/v2/config"
    10  )
    11  
    12  func NewDbProviderMock() (*DbProviderMock, sqlmock.Sqlmock, error) {
    13  	db, mock, err := sqlmock.New()
    14  	if err != nil {
    15  		return nil, nil, err
    16  	}
    17  
    18  	provider := &DbProviderMock{
    19  		db:   db,
    20  		mock: mock,
    21  	}
    22  
    23  	return provider, mock, nil
    24  }
    25  
    26  type DbProviderMock struct {
    27  	db   *sql.DB
    28  	mock sqlmock.Sqlmock
    29  }
    30  
    31  func (provider DbProviderMock) Config() config.DatabaseConnection {
    32  	return config.DatabaseConnection{}
    33  }
    34  
    35  func (provider DbProviderMock) ConnString() (string, error) {
    36  	return "", nil
    37  }
    38  
    39  func (provider DbProviderMock) Open() error {
    40  	return nil
    41  }
    42  
    43  func (provider DbProviderMock) Close() error {
    44  	return nil
    45  }
    46  
    47  func (provider DbProviderMock) Ping() error {
    48  	return nil
    49  }
    50  
    51  func (provider DbProviderMock) PrepareQuery(template string, params ...QueryParam) (query string, args []interface{}) {
    52  	for _, param := range params {
    53  		if reflect.TypeOf(param.Value).Kind() == reflect.Slice {
    54  			idList := param.Value.([]interface{})
    55  			args = append(args, idList...)
    56  		} else {
    57  			args = append(args, param.Value)
    58  		}
    59  	}
    60  	return template, args
    61  }
    62  
    63  func (provider DbProviderMock) QueryContext(ctx context.Context, template string, params ...QueryParam) (*sql.Rows, error) {
    64  	query, args := provider.PrepareQuery(template, params...)
    65  
    66  	return provider.db.QueryContext(ctx, query, args...)
    67  }