...

Source file src/github.com/go-kivik/kivik/v4/mockdb/meets.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  	"encoding/json"
    17  	"reflect"
    18  
    19  	kivik "github.com/go-kivik/kivik/v4"
    20  )
    21  
    22  func meets(a, e expectation) bool {
    23  	if reflect.TypeOf(a).Elem().Name() != reflect.TypeOf(e).Elem().Name() {
    24  		return false
    25  	}
    26  	// Skip the DB test for the dbo() method
    27  	if _, ok := e.(*ExpectedDB); !ok {
    28  		if !dbMeetsExpectation(a.dbo(), e.dbo()) {
    29  			return false
    30  		}
    31  	}
    32  	if !optionsMeetExpectation(a.opts(), e.opts()) {
    33  		return false
    34  	}
    35  	return a.met(e)
    36  }
    37  
    38  func dbMeetsExpectation(a, e *DB) bool {
    39  	if e == nil {
    40  		return true
    41  	}
    42  	e.mu.RLock()
    43  	defer e.mu.RUnlock()
    44  	a.mu.RLock()
    45  	defer a.mu.RUnlock()
    46  	return e.name == a.name && e.id == a.id
    47  }
    48  
    49  func optionsMeetExpectation(a, e kivik.Option) bool {
    50  	if e == nil {
    51  		return true
    52  	}
    53  
    54  	return reflect.DeepEqual(convertOptions(e), convertOptions(a))
    55  }
    56  
    57  // convertOptions converts a to a slice of options, for easier comparison
    58  func convertOptions(a kivik.Option) []kivik.Option {
    59  	if a == nil {
    60  		return nil
    61  	}
    62  	t := reflect.TypeOf(a)
    63  	if t.Kind() != reflect.Slice {
    64  		return []kivik.Option{a}
    65  	}
    66  	v := reflect.ValueOf(a)
    67  	result := make([]kivik.Option, 0, v.Len())
    68  	for i := 0; i < v.Len(); i++ {
    69  		opt := v.Index(i)
    70  		if !opt.IsNil() {
    71  			result = append(result, convertOptions(opt.Interface().(kivik.Option))...)
    72  		}
    73  	}
    74  	return result
    75  }
    76  
    77  func jsonMeets(e, a interface{}) bool {
    78  	eJSON, _ := json.Marshal(e)
    79  	aJSON, _ := json.Marshal(a)
    80  	var eI, aI interface{}
    81  	_ = json.Unmarshal(eJSON, &eI)
    82  	_ = json.Unmarshal(aJSON, &aI)
    83  	return reflect.DeepEqual(eI, aI)
    84  }
    85  

View as plain text