mirror of
https://github.com/hanzoai/mock.git
synced 2026-09-30 22:56:01 +00:00
56 lines
1.5 KiB
Go
56 lines
1.5 KiB
Go
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
// you may not use this file except in compliance with the License.
|
|
// You may obtain a copy of the License at
|
|
//
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
//
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
// See the License for the specific language governing permissions and
|
|
// limitations under the License.
|
|
|
|
package mock
|
|
|
|
import (
|
|
"fmt"
|
|
"sync"
|
|
|
|
"github.com/hanzo-ds/go"
|
|
"github.com/DATA-DOG/go-sqlmock"
|
|
)
|
|
|
|
var datastorePool *mockDatastoreDriver
|
|
|
|
type mockDatastoreDriver struct {
|
|
counter int
|
|
conns map[string]*datastoremock
|
|
sync.Mutex
|
|
}
|
|
|
|
func init() {
|
|
datastorePool = &mockDatastoreDriver{
|
|
conns: make(map[string]*datastoremock),
|
|
}
|
|
}
|
|
|
|
// NewDatastoreNative creates datastoremock database mock to manage expectations.
|
|
func NewDatastoreNative(options *datastore.Options) (*datastoremock, error) {
|
|
return NewDatastoreWithQueryMatcher(options, sqlmock.QueryMatcherEqual)
|
|
}
|
|
|
|
func NewDatastoreWithQueryMatcher(
|
|
options *datastore.Options,
|
|
queryMatcher sqlmock.QueryMatcher,
|
|
) (*datastoremock, error) {
|
|
datastorePool.Lock()
|
|
dsn := fmt.Sprintf("datastoremock_db_%d", datastorePool.counter)
|
|
datastorePool.counter++
|
|
|
|
cmock := &datastoremock{dsn: dsn, drv: datastorePool, ordered: true, queryMatcher: queryMatcher}
|
|
datastorePool.conns[dsn] = cmock
|
|
datastorePool.Unlock()
|
|
|
|
return cmock.open(options)
|
|
}
|