...
1
2
3
4
5
6
7
8
9
10
11
12
13
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 }
97
View as plain text