...

Source file src/github.com/go-kivik/kivik/v4/mockdb/meets_test.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  	"testing"
    17  
    18  	"github.com/google/go-cmp/cmp"
    19  	"gitlab.com/flimzy/testy"
    20  
    21  	"github.com/go-kivik/kivik/v4"
    22  )
    23  
    24  func TestDBMeetsExpectation(t *testing.T) {
    25  	type tst struct {
    26  		exp      *DB
    27  		act      *DB
    28  		expected bool
    29  	}
    30  	tests := testy.NewTable()
    31  	tests.Add("different name", tst{
    32  		exp:      &DB{name: "foo"},
    33  		act:      &DB{name: "bar"},
    34  		expected: false,
    35  	})
    36  	tests.Add("different id", tst{
    37  		exp:      &DB{name: "foo", id: 123},
    38  		act:      &DB{name: "foo", id: 321},
    39  		expected: false,
    40  	})
    41  	tests.Add("no db", tst{
    42  		expected: true,
    43  	})
    44  	tests.Add("match", tst{
    45  		exp:      &DB{name: "foo", id: 123},
    46  		act:      &DB{name: "foo", id: 123},
    47  		expected: true,
    48  	})
    49  	tests.Run(t, func(t *testing.T, test tst) {
    50  		result := dbMeetsExpectation(test.exp, test.act)
    51  		if result != test.expected {
    52  			t.Errorf("Unexpected result: %T", result)
    53  		}
    54  	})
    55  }
    56  
    57  func Test_convertOptions(t *testing.T) {
    58  	tests := []struct {
    59  		name string
    60  		in   kivik.Option
    61  		want []kivik.Option
    62  	}{
    63  		{
    64  			name: "nil input",
    65  			in:   nil,
    66  			want: nil,
    67  		},
    68  		{
    69  			name: "one items",
    70  			in:   kivik.Rev("x"),
    71  			want: []kivik.Option{kivik.Rev("x")},
    72  		},
    73  		{
    74  			name: "multiOptions",
    75  			in:   multiOptions{kivik.Rev("a"), kivik.Rev("b")},
    76  			want: []kivik.Option{kivik.Rev("a"), kivik.Rev("b")},
    77  		},
    78  		{
    79  			name: "nested multiOptions",
    80  			in: multiOptions{
    81  				multiOptions{kivik.Rev("a"), kivik.Rev("b")},
    82  				multiOptions{kivik.Rev("c"), kivik.Rev("d")},
    83  			},
    84  			want: []kivik.Option{kivik.Rev("a"), kivik.Rev("b"), kivik.Rev("c"), kivik.Rev("d")},
    85  		},
    86  		{
    87  			name: "nil value",
    88  			in:   kivik.Option(nil),
    89  			want: nil,
    90  		},
    91  		{
    92  			name: "filter nil values",
    93  			in: multiOptions{
    94  				multiOptions{kivik.Rev("a"), kivik.Option(nil), kivik.Rev("b"), nil, nil},
    95  				multiOptions{kivik.Option(nil), kivik.Rev("c"), kivik.Rev("d"), kivik.Option(nil)},
    96  			},
    97  			want: []kivik.Option{kivik.Rev("a"), kivik.Rev("b"), kivik.Rev("c"), kivik.Rev("d")},
    98  		},
    99  	}
   100  
   101  	for _, tt := range tests {
   102  		t.Run(tt.name, func(t *testing.T) {
   103  			got := convertOptions(tt.in)
   104  			if d := cmp.Diff(tt.want, got); d != "" {
   105  				t.Error(d)
   106  			}
   107  		})
   108  	}
   109  }
   110  

View as plain text