...

Source file src/github.com/go-kivik/kivik/v4/mockdb/updates.go

Documentation: github.com/go-kivik/kivik/v4/mockdb

     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 mockdb
    14  
    15  import (
    16  	"context"
    17  	"time"
    18  
    19  	"github.com/go-kivik/kivik/v4/driver"
    20  )
    21  
    22  // Updates is a mocked collection of database updates.
    23  type Updates struct {
    24  	iter
    25  	lastSeq    string
    26  	lastSeqErr error
    27  }
    28  
    29  type driverDBUpdates struct {
    30  	context.Context
    31  	*Updates
    32  }
    33  
    34  func coalesceDBUpdates(updates *Updates) *Updates {
    35  	if updates != nil {
    36  		return updates
    37  	}
    38  	return &Updates{}
    39  }
    40  
    41  var _ driver.DBUpdates = &driverDBUpdates{}
    42  
    43  func (u *driverDBUpdates) Next(update *driver.DBUpdate) error {
    44  	result, err := u.unshift(u.Context)
    45  	if err != nil {
    46  		return err
    47  	}
    48  	*update = *result.(*driver.DBUpdate)
    49  	return nil
    50  }
    51  
    52  func (u *driverDBUpdates) LastSeq() (string, error) {
    53  	return u.lastSeq, u.lastSeqErr
    54  }
    55  
    56  // CloseError sets an error to be returned when the updates iterator is closed.
    57  func (u *Updates) CloseError(err error) *Updates {
    58  	u.closeErr = err
    59  	return u
    60  }
    61  
    62  // AddUpdateError adds an error to be returned during update iteration.
    63  func (u *Updates) AddUpdateError(err error) *Updates {
    64  	u.resultErr = err
    65  	return u
    66  }
    67  
    68  // AddUpdate adds a database update to be returned by the DBUpdates iterator. If
    69  // AddUpdateError has been set, this method will panic.
    70  func (u *Updates) AddUpdate(update *driver.DBUpdate) *Updates {
    71  	if u.resultErr != nil {
    72  		panic("It is invalid to set more updates after AddUpdateError is defined.")
    73  	}
    74  	u.push(&item{item: update})
    75  	return u
    76  }
    77  
    78  // AddDelay adds a delay before the next iteration will complete.
    79  func (u *Updates) AddDelay(delay time.Duration) *Updates {
    80  	u.push(&item{delay: delay})
    81  	return u
    82  }
    83  
    84  // LastSeq sets the LastSeq value to be returned by the DBUpdates iterator.
    85  func (u *Updates) LastSeq(lastSeq string) *Updates {
    86  	u.lastSeq = lastSeq
    87  	return u
    88  }
    89  
    90  // LastSeqError sets the error value to be returned when LastSeq is called.
    91  func (u *Updates) LastSeqError(err error) *Updates {
    92  	u.lastSeqErr = err
    93  	return u
    94  }
    95  
    96  // Final converts the Updates object to a driver.DBUpdates. This method is
    97  // intended for use within WillExecute() to return results.
    98  func (u *Updates) Final() driver.DBUpdates {
    99  	return &driverDBUpdates{Updates: u}
   100  }
   101  

View as plain text