...

Source file src/github.com/go-kivik/kivik/v4/int/mock/db.go

Documentation: github.com/go-kivik/kivik/v4/int/mock

     1  // Licensed under the Apache License, Version 2.0 (the "License"); you may not
     2  // use this file except in compliance with the License. You may obtain a copy of
     3  // the License at
     4  //
     5  //  http://www.apache.org/licenses/LICENSE-2.0
     6  //
     7  // Unless required by applicable law or agreed to in writing, software
     8  // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
     9  // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
    10  // License for the specific language governing permissions and limitations under
    11  // the License.
    12  
    13  package mock
    14  
    15  import (
    16  	"context"
    17  
    18  	"github.com/go-kivik/kivik/v4/driver"
    19  )
    20  
    21  // DB mocks a driver.DB
    22  type DB struct {
    23  	// ID is a unique identifier for the DB instance.
    24  	ID                   string
    25  	AllDocsFunc          func(ctx context.Context, options driver.Options) (driver.Rows, error)
    26  	GetFunc              func(ctx context.Context, docID string, options driver.Options) (*driver.Document, error)
    27  	PutFunc              func(ctx context.Context, docID string, doc interface{}, options driver.Options) (rev string, err error)
    28  	DeleteFunc           func(ctx context.Context, docID string, options driver.Options) (newRev string, err error)
    29  	StatsFunc            func(ctx context.Context) (*driver.DBStats, error)
    30  	CompactFunc          func(ctx context.Context) error
    31  	CompactViewFunc      func(ctx context.Context, ddocID string) error
    32  	ViewCleanupFunc      func(ctx context.Context) error
    33  	ChangesFunc          func(ctx context.Context, options driver.Options) (driver.Changes, error)
    34  	PutAttachmentFunc    func(ctx context.Context, docID string, att *driver.Attachment, options driver.Options) (newRev string, err error)
    35  	GetAttachmentFunc    func(ctx context.Context, docID, filename string, options driver.Options) (*driver.Attachment, error)
    36  	DeleteAttachmentFunc func(ctx context.Context, docID, filename string, options driver.Options) (newRev string, err error)
    37  	QueryFunc            func(context.Context, string, string, driver.Options) (driver.Rows, error)
    38  	CloseFunc            func() error
    39  }
    40  
    41  // DocCreator is a stub for a [github.com/go-kivik/v4/driver.DocCreator].
    42  type DocCreator struct {
    43  	DB
    44  	CreateDocFunc func(ctx context.Context, doc interface{}, options driver.Options) (docID, rev string, err error)
    45  }
    46  
    47  // CreateDoc calls db.CreateDocFunc
    48  func (db *DocCreator) CreateDoc(ctx context.Context, doc interface{}, opts driver.Options) (string, string, error) {
    49  	return db.CreateDocFunc(ctx, doc, opts)
    50  }
    51  
    52  // SecurityDB is a stub for a driver.SecurityDB.
    53  type SecurityDB struct {
    54  	DB
    55  	SecurityFunc    func(ctx context.Context) (*driver.Security, error)
    56  	SetSecurityFunc func(ctx context.Context, security *driver.Security) error
    57  }
    58  
    59  // Get calls db.GetFunc
    60  func (db *DB) Get(ctx context.Context, docID string, opts driver.Options) (*driver.Document, error) {
    61  	return db.GetFunc(ctx, docID, opts)
    62  }
    63  
    64  var _ driver.DB = &DB{}
    65  
    66  // AllDocs calls db.AllDocsFunc
    67  func (db *DB) AllDocs(ctx context.Context, options driver.Options) (driver.Rows, error) {
    68  	return db.AllDocsFunc(ctx, options)
    69  }
    70  
    71  // Put calls db.PutFunc
    72  func (db *DB) Put(ctx context.Context, docID string, doc interface{}, opts driver.Options) (string, error) {
    73  	return db.PutFunc(ctx, docID, doc, opts)
    74  }
    75  
    76  // Delete calls db.DeleteFunc
    77  func (db *DB) Delete(ctx context.Context, docID string, opts driver.Options) (string, error) {
    78  	return db.DeleteFunc(ctx, docID, opts)
    79  }
    80  
    81  // Stats calls db.StatsFunc
    82  func (db *DB) Stats(ctx context.Context) (*driver.DBStats, error) {
    83  	return db.StatsFunc(ctx)
    84  }
    85  
    86  // Compact calls db.CompactFunc
    87  func (db *DB) Compact(ctx context.Context) error {
    88  	return db.CompactFunc(ctx)
    89  }
    90  
    91  // CompactView calls db.CompactViewFunc
    92  func (db *DB) CompactView(ctx context.Context, docID string) error {
    93  	return db.CompactViewFunc(ctx, docID)
    94  }
    95  
    96  // ViewCleanup calls db.ViewCleanupFunc
    97  func (db *DB) ViewCleanup(ctx context.Context) error {
    98  	return db.ViewCleanupFunc(ctx)
    99  }
   100  
   101  // Security calls db.SecurityFunc
   102  func (db *SecurityDB) Security(ctx context.Context) (*driver.Security, error) {
   103  	return db.SecurityFunc(ctx)
   104  }
   105  
   106  // SetSecurity calls db.SetSecurityFunc
   107  func (db *SecurityDB) SetSecurity(ctx context.Context, security *driver.Security) error {
   108  	return db.SetSecurityFunc(ctx, security)
   109  }
   110  
   111  // Changes calls db.ChangesFunc
   112  func (db *DB) Changes(ctx context.Context, opts driver.Options) (driver.Changes, error) {
   113  	return db.ChangesFunc(ctx, opts)
   114  }
   115  
   116  // PutAttachment calls db.PutAttachmentFunc
   117  func (db *DB) PutAttachment(ctx context.Context, docID string, att *driver.Attachment, opts driver.Options) (string, error) {
   118  	return db.PutAttachmentFunc(ctx, docID, att, opts)
   119  }
   120  
   121  // GetAttachment calls db.GetAttachmentFunc
   122  func (db *DB) GetAttachment(ctx context.Context, docID, filename string, opts driver.Options) (*driver.Attachment, error) {
   123  	return db.GetAttachmentFunc(ctx, docID, filename, opts)
   124  }
   125  
   126  // DeleteAttachment calls db.DeleteAttachmentFunc
   127  func (db *DB) DeleteAttachment(ctx context.Context, docID, filename string, opts driver.Options) (string, error) {
   128  	return db.DeleteAttachmentFunc(ctx, docID, filename, opts)
   129  }
   130  
   131  // Query calls db.QueryFunc.
   132  func (db *DB) Query(ctx context.Context, ddoc, view string, opts driver.Options) (driver.Rows, error) {
   133  	return db.QueryFunc(ctx, ddoc, view, opts)
   134  }
   135  
   136  // OpenRever mocks a driver.DB and driver.OpenRever.
   137  type OpenRever struct {
   138  	*DB
   139  	OpenRevsFunc func(context.Context, string, []string, driver.Options) (driver.Rows, error)
   140  }
   141  
   142  var _ driver.OpenRever = (*OpenRever)(nil)
   143  
   144  // OpenRevs calls db.OpenRevsFunc.
   145  func (db *OpenRever) OpenRevs(ctx context.Context, docID string, revs []string, options driver.Options) (driver.Rows, error) {
   146  	return db.OpenRevsFunc(ctx, docID, revs, options)
   147  }
   148  
   149  // Finder mocks a driver.DB and driver.Finder
   150  type Finder struct {
   151  	*DB
   152  	CreateIndexFunc func(context.Context, string, string, interface{}, driver.Options) error
   153  	DeleteIndexFunc func(context.Context, string, string, driver.Options) error
   154  	FindFunc        func(context.Context, interface{}, driver.Options) (driver.Rows, error)
   155  	GetIndexesFunc  func(context.Context, driver.Options) ([]driver.Index, error)
   156  	ExplainFunc     func(context.Context, interface{}, driver.Options) (*driver.QueryPlan, error)
   157  }
   158  
   159  var _ driver.Finder = &Finder{}
   160  
   161  // CreateIndex calls db.CreateIndexFunc
   162  func (db *Finder) CreateIndex(ctx context.Context, ddoc, name string, index interface{}, options driver.Options) error {
   163  	return db.CreateIndexFunc(ctx, ddoc, name, index, options)
   164  }
   165  
   166  // DeleteIndex calls db.DeleteIndexFunc
   167  func (db *Finder) DeleteIndex(ctx context.Context, ddoc, name string, opts driver.Options) error {
   168  	return db.DeleteIndexFunc(ctx, ddoc, name, opts)
   169  }
   170  
   171  // Find calls db.FindFunc
   172  func (db *Finder) Find(ctx context.Context, query interface{}, opts driver.Options) (driver.Rows, error) {
   173  	return db.FindFunc(ctx, query, opts)
   174  }
   175  
   176  // GetIndexes calls db.GetIndexesFunc
   177  func (db *Finder) GetIndexes(ctx context.Context, opts driver.Options) ([]driver.Index, error) {
   178  	return db.GetIndexesFunc(ctx, opts)
   179  }
   180  
   181  // Explain calls db.ExplainFunc
   182  func (db *Finder) Explain(ctx context.Context, query interface{}, opts driver.Options) (*driver.QueryPlan, error) {
   183  	return db.ExplainFunc(ctx, query, opts)
   184  }
   185  
   186  // Flusher mocks a driver.DB and driver.Flusher
   187  type Flusher struct {
   188  	*DB
   189  	FlushFunc func(context.Context) error
   190  }
   191  
   192  var _ driver.Flusher = &Flusher{}
   193  
   194  // Flush calls db.FlushFunc
   195  func (db *Flusher) Flush(ctx context.Context) error {
   196  	return db.FlushFunc(ctx)
   197  }
   198  
   199  // RevGetter mocks a driver.DB and driver.RevGetter
   200  type RevGetter struct {
   201  	*DB
   202  	GetRevFunc func(context.Context, string, driver.Options) (string, error)
   203  }
   204  
   205  var _ driver.RevGetter = &RevGetter{}
   206  
   207  // GetRev calls db.GetRevFunc
   208  func (db *RevGetter) GetRev(ctx context.Context, docID string, opts driver.Options) (string, error) {
   209  	return db.GetRevFunc(ctx, docID, opts)
   210  }
   211  
   212  // Copier mocks a driver.DB and driver.Copier.
   213  type Copier struct {
   214  	*DB
   215  	CopyFunc func(context.Context, string, string, driver.Options) (string, error)
   216  }
   217  
   218  var _ driver.Copier = &Copier{}
   219  
   220  // Copy calls db.CopyFunc
   221  func (db *Copier) Copy(ctx context.Context, target, source string, options driver.Options) (string, error) {
   222  	return db.CopyFunc(ctx, target, source, options)
   223  }
   224  
   225  // AttachmentMetaGetter mocks a driver.DB and driver.AttachmentMetaGetter
   226  type AttachmentMetaGetter struct {
   227  	*DB
   228  	GetAttachmentMetaFunc func(ctx context.Context, docID, filename string, options driver.Options) (*driver.Attachment, error)
   229  }
   230  
   231  var _ driver.AttachmentMetaGetter = &AttachmentMetaGetter{}
   232  
   233  // GetAttachmentMeta calls db.GetAttachmentMetaFunc
   234  func (db *AttachmentMetaGetter) GetAttachmentMeta(ctx context.Context, docID, filename string, options driver.Options) (*driver.Attachment, error) {
   235  	return db.GetAttachmentMetaFunc(ctx, docID, filename, options)
   236  }
   237  
   238  // DesignDocer mocks a driver.DB and driver.DesignDocer
   239  type DesignDocer struct {
   240  	*DB
   241  	DesignDocsFunc func(context.Context, driver.Options) (driver.Rows, error)
   242  }
   243  
   244  var _ driver.DesignDocer = &DesignDocer{}
   245  
   246  // DesignDocs calls db.DesignDocsFunc
   247  func (db *DesignDocer) DesignDocs(ctx context.Context, options driver.Options) (driver.Rows, error) {
   248  	return db.DesignDocsFunc(ctx, options)
   249  }
   250  
   251  // LocalDocer mocks a driver.DB and driver.DesignDocer
   252  type LocalDocer struct {
   253  	*DB
   254  	LocalDocsFunc func(context.Context, driver.Options) (driver.Rows, error)
   255  }
   256  
   257  var _ driver.LocalDocer = &LocalDocer{}
   258  
   259  // LocalDocs calls db.LocalDocsFunc
   260  func (db *LocalDocer) LocalDocs(ctx context.Context, options driver.Options) (driver.Rows, error) {
   261  	return db.LocalDocsFunc(ctx, options)
   262  }
   263  
   264  // Purger mocks a driver.DB and driver.Purger
   265  type Purger struct {
   266  	*DB
   267  	PurgeFunc func(context.Context, map[string][]string) (*driver.PurgeResult, error)
   268  }
   269  
   270  var _ driver.Purger = &Purger{}
   271  
   272  // Purge calls db.PurgeFunc
   273  func (db *Purger) Purge(ctx context.Context, docMap map[string][]string) (*driver.PurgeResult, error) {
   274  	return db.PurgeFunc(ctx, docMap)
   275  }
   276  
   277  // BulkGetter mocks a driver.DB and driver.BulkGetter
   278  type BulkGetter struct {
   279  	*DB
   280  	BulkGetFunc func(context.Context, []driver.BulkGetReference, driver.Options) (driver.Rows, error)
   281  }
   282  
   283  var _ driver.BulkGetter = &BulkGetter{}
   284  
   285  // BulkGet calls db.BulkGetFunc
   286  func (db *BulkGetter) BulkGet(ctx context.Context, docs []driver.BulkGetReference, opts driver.Options) (driver.Rows, error) {
   287  	return db.BulkGetFunc(ctx, docs, opts)
   288  }
   289  
   290  // Close calls db.CloseFunc
   291  func (db *DB) Close() error {
   292  	if db != nil && db.CloseFunc != nil {
   293  		return db.CloseFunc()
   294  	}
   295  	return nil
   296  }
   297  
   298  // RevsDiffer mocks a driver.DB and driver.RevsDiffer.
   299  type RevsDiffer struct {
   300  	*BulkDocer
   301  	RevsDiffFunc func(context.Context, interface{}) (driver.Rows, error)
   302  }
   303  
   304  var _ driver.RevsDiffer = &RevsDiffer{}
   305  
   306  // RevsDiff calls db.RevsDiffFunc
   307  func (db *RevsDiffer) RevsDiff(ctx context.Context, revMap interface{}) (driver.Rows, error) {
   308  	return db.RevsDiffFunc(ctx, revMap)
   309  }
   310  
   311  // PartitionedDB mocks a driver.DB and a driver.PartitionedDB.
   312  type PartitionedDB struct {
   313  	*DB
   314  	PartitionStatsFunc func(context.Context, string) (*driver.PartitionStats, error)
   315  }
   316  
   317  // PartitionStats calls db.PartitionStatsFunc.
   318  func (db *PartitionedDB) PartitionStats(ctx context.Context, name string) (*driver.PartitionStats, error) {
   319  	return db.PartitionStatsFunc(ctx, name)
   320  }
   321  

View as plain text