github.com/go-kivik/kivik/v4@v4.3.2/updates123_test.go (about) 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 TestDBUpdatesIterator(t *testing.T) { 30 t.Parallel() 31 32 want := []string{"a", "b", "c"} 33 var idx int 34 updates := newDBUpdates(context.Background(), nil, &mock.DBUpdates{ 35 NextFunc: func(u *driver.DBUpdate) error { 36 if idx >= len(want) { 37 return io.EOF 38 } 39 u.DBName = want[idx] 40 idx++ 41 return nil 42 }, 43 }) 44 45 ids := []string{} 46 for update, err := range updates.Iterator() { 47 if err != nil { 48 t.Fatalf("Unexpected error: %s", err) 49 } 50 ids = append(ids, update.DBName) 51 } 52 53 if diff := cmp.Diff(want, ids); diff != "" { 54 t.Errorf("Unexpected updates: %s", diff) 55 } 56 } 57 58 func TestDBUpdatesIteratorError(t *testing.T) { 59 t.Parallel() 60 61 updates := newDBUpdates(context.Background(), nil, &mock.DBUpdates{ 62 NextFunc: func(*driver.DBUpdate) error { 63 return errors.New("Failure") 64 }, 65 }) 66 67 for _, err := range updates.Iterator() { 68 if err == nil { 69 t.Fatal("Expected error") 70 } 71 return 72 } 73 74 t.Fatal("Expected an error during iteration") 75 } 76 77 func TestDBUpdatesIteratorBreak(t *testing.T) { 78 t.Parallel() 79 80 updates := newDBUpdates(context.Background(), nil, &mock.DBUpdates{ 81 NextFunc: func(*driver.DBUpdate) error { 82 return nil 83 }, 84 }) 85 86 for _, err := range updates.Iterator() { 87 if err != nil { 88 t.Fatalf("Unexpected error: %s", err) 89 } 90 break 91 } 92 93 if updates.iter.state != stateClosed { 94 t.Errorf("Expected iterator to be closed") 95 } 96 }