...

Source file src/github.com/go-kivik/kivik/v4/changes123_test.go

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

     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  //go:build go1.23
    14  
    15  package kivik
    16  
    17  import (
    18  	"context"
    19  	"errors"
    20  	"io"
    21  	"testing"
    22  
    23  	"github.com/google/go-cmp/cmp"
    24  
    25  	"github.com/go-kivik/kivik/v4/driver"
    26  	"github.com/go-kivik/kivik/v4/int/mock"
    27  )
    28  
    29  func TestChangesIterator(t *testing.T) {
    30  	t.Parallel()
    31  
    32  	want := []string{"a", "b", "c"}
    33  	var idx int
    34  	changes := newChanges(context.Background(), nil, &mock.Changes{
    35  		NextFunc: func(ch *driver.Change) error {
    36  			if idx >= len(want) {
    37  				return io.EOF
    38  			}
    39  			ch.ID = want[idx]
    40  			idx++
    41  			return nil
    42  		},
    43  	})
    44  
    45  	ids := []string{}
    46  	for change, err := range changes.Iterator() {
    47  		if err != nil {
    48  			t.Fatalf("Unexpected error: %s", err)
    49  		}
    50  		ids = append(ids, change.ID)
    51  	}
    52  	if diff := cmp.Diff(want, ids); diff != "" {
    53  		t.Errorf("Unexpected changes: %s", diff)
    54  	}
    55  }
    56  
    57  func TestChangesIteratorError(t *testing.T) {
    58  	t.Parallel()
    59  
    60  	changes := newChanges(context.Background(), nil, &mock.Changes{
    61  		NextFunc: func(*driver.Change) error {
    62  			return errors.New("failure")
    63  		},
    64  	})
    65  
    66  	for _, err := range changes.Iterator() {
    67  		if err == nil {
    68  			t.Fatal("Expected error")
    69  		}
    70  		return
    71  	}
    72  	t.Fatal("Expected an error during iteration")
    73  }
    74  
    75  func TestChangesIteratorBreak(t *testing.T) {
    76  	t.Parallel()
    77  
    78  	changes := newChanges(context.Background(), nil, &mock.Changes{
    79  		NextFunc: func(ch *driver.Change) error {
    80  			return nil
    81  		},
    82  	})
    83  
    84  	for _, err := range changes.Iterator() {
    85  		if err != nil {
    86  			t.Fatalf("Unexpected error: %s", err)
    87  		}
    88  		break
    89  	}
    90  	if changes.iter.state != stateClosed {
    91  		t.Errorf("Expected iterator to be closed")
    92  	}
    93  }
    94  

View as plain text