github.com/go-kivik/kivik/v4@v4.3.2/attachments123_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 "errors" 19 "io" 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 24 "github.com/go-kivik/kivik/v4/driver" 25 "github.com/go-kivik/kivik/v4/int/mock" 26 ) 27 28 func TestAttachmentsIterator(t *testing.T) { 29 t.Parallel() 30 31 want := []string{"a", "b", "c"} 32 var idx int 33 atts := &AttachmentsIterator{ 34 atti: &mock.Attachments{ 35 NextFunc: func(att *driver.Attachment) error { 36 if idx >= len(want) { 37 return io.EOF 38 } 39 att.Filename = want[idx] 40 idx++ 41 return nil 42 }, 43 }, 44 } 45 46 ids := []string{} 47 for att, err := range atts.Iterator() { 48 if err != nil { 49 t.Fatalf("Unexpected error: %s", err) 50 } 51 ids = append(ids, att.Filename) 52 } 53 if diff := cmp.Diff(want, ids); diff != "" { 54 t.Errorf("Unexpected attachments: %s", diff) 55 } 56 } 57 58 func TestAttachmentsIteratorError(t *testing.T) { 59 t.Parallel() 60 61 atts := &AttachmentsIterator{ 62 atti: &mock.Attachments{ 63 NextFunc: func(*driver.Attachment) error { 64 return errors.New("failure") 65 }, 66 }, 67 } 68 69 for _, err := range atts.Iterator() { 70 if err == nil { 71 t.Fatal("Expected error") 72 } 73 return 74 } 75 t.Fatal("Expected an error during iteration") 76 } 77 78 func TestAttachmentsIteratorBreak(t *testing.T) { 79 t.Parallel() 80 81 var closed bool 82 atts := &AttachmentsIterator{ 83 atti: &mock.Attachments{ 84 NextFunc: func(*driver.Attachment) error { 85 return nil 86 }, 87 }, 88 onClose: func() { 89 closed = true 90 }, 91 } 92 93 for _, err := range atts.Iterator() { 94 if err != nil { 95 t.Fatalf("Unexpected error: %s", err) 96 } 97 break 98 } 99 if !closed { 100 t.Errorf("Expected iterator to be closed") 101 } 102 }