github.com/kaleido-io/firefly@v0.0.0-20210622132723-8b4b6aacb971/internal/database/sqlcommon/provider_mock_test.go (about)

     1  // Copyright © 2021 Kaleido, Inc.
     2  //
     3  // SPDX-License-Identifier: Apache-2.0
     4  //
     5  // Licensed under the Apache License, Version 2.0 (the "License");
     6  // you may not use this file except in compliance with the License.
     7  // You may obtain a copy of the License at
     8  //
     9  //     http://www.apache.org/licenses/LICENSE-2.0
    10  //
    11  // Unless required by applicable law or agreed to in writing, software
    12  // distributed under the License is distributed on an "AS IS" BASIS,
    13  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    14  // See the License for the specific language governing permissions and
    15  // limitations under the License.
    16  
    17  package sqlcommon
    18  
    19  import (
    20  	"context"
    21  	"database/sql"
    22  	"fmt"
    23  
    24  	"github.com/DATA-DOG/go-sqlmock"
    25  	sq "github.com/Masterminds/squirrel"
    26  	migratedb "github.com/golang-migrate/migrate/v4/database"
    27  	"github.com/kaleido-io/firefly/internal/config"
    28  	"github.com/kaleido-io/firefly/mocks/databasemocks"
    29  	"github.com/kaleido-io/firefly/pkg/database"
    30  )
    31  
    32  // testProvider uses the datadog mocking framework
    33  type mockProvider struct {
    34  	SQLCommon
    35  	callbacks    *databasemocks.Callbacks
    36  	capabilities *database.Capabilities
    37  	prefix       config.Prefix
    38  
    39  	mockDB *sql.DB
    40  	mdb    sqlmock.Sqlmock
    41  
    42  	fakePSQLInsert          bool
    43  	openError               error
    44  	getMigrationDriverError error
    45  }
    46  
    47  func newMockProvider() *mockProvider {
    48  	mp := &mockProvider{
    49  		prefix: config.NewPluginConfig("unittest.mockdb"),
    50  	}
    51  	mp.SQLCommon.InitPrefix(mp, mp.prefix)
    52  	mp.mockDB, mp.mdb, _ = sqlmock.New()
    53  	return mp
    54  }
    55  
    56  // init is a convenience to init for tests that aren't testing init itself
    57  func (mp *mockProvider) init() (*mockProvider, sqlmock.Sqlmock) {
    58  	_ = mp.Init(context.Background(), mp, mp.prefix, mp.callbacks, mp.capabilities)
    59  	return mp, mp.mdb
    60  }
    61  
    62  func (mp *mockProvider) Name() string {
    63  	return "mockdb"
    64  }
    65  
    66  func (mp *mockProvider) PlaceholderFormat() sq.PlaceholderFormat {
    67  	return sq.Dollar
    68  }
    69  
    70  func (mp *mockProvider) UpdateInsertForSequenceReturn(insert sq.InsertBuilder) (sq.InsertBuilder, bool) {
    71  	if mp.fakePSQLInsert {
    72  		return insert.Suffix(" RETURNING seq"), true
    73  	}
    74  	return insert, false
    75  }
    76  
    77  func (mp *mockProvider) SequenceField(tableName string) string {
    78  	if tableName != "" {
    79  		return fmt.Sprintf("%s.seq", tableName)
    80  	}
    81  	return "seq"
    82  }
    83  
    84  func (mp *mockProvider) Open(url string) (*sql.DB, error) {
    85  	return mp.mockDB, mp.openError
    86  }
    87  
    88  func (mp *mockProvider) GetMigrationDriver(db *sql.DB) (migratedb.Driver, error) {
    89  	return nil, mp.getMigrationDriverError
    90  }