...

Source file src/github.com/go-kivik/kivik/v4/x/fsdb/changes_test.go

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

     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 fs
    14  
    15  import (
    16  	"context"
    17  	"io"
    18  	"testing"
    19  
    20  	"gitlab.com/flimzy/testy"
    21  
    22  	"github.com/go-kivik/kivik/v4"
    23  	"github.com/go-kivik/kivik/v4/driver"
    24  	internal "github.com/go-kivik/kivik/v4/int/errors"
    25  )
    26  
    27  func TestChanges(t *testing.T) {
    28  	type tt struct {
    29  		db      *db
    30  		options kivik.Option
    31  		status  int
    32  		err     string
    33  	}
    34  	tests := testy.NewTable()
    35  	tests.Add("success", tt{
    36  		db: &db{
    37  			client: &client{root: "testdata"},
    38  			dbPath: "testdata/db_foo",
    39  			dbName: "db_foo",
    40  		},
    41  	})
    42  	tests.Add("repl failure", tt{
    43  		db: &db{
    44  			client: &client{root: ""},
    45  			dbPath: "./testdata/source",
    46  			dbName: "source",
    47  		},
    48  		err:    `open testdata/source: [Nn]o such file or directory`,
    49  		status: 500,
    50  	})
    51  
    52  	tests.Run(t, func(t *testing.T, tt tt) {
    53  		changes, err := tt.db.Changes(context.TODO(), tt.options)
    54  		if d := internal.StatusErrorDiffRE(tt.err, tt.status, err); d != "" {
    55  			t.Error(d)
    56  		}
    57  		if err != nil {
    58  			return
    59  		}
    60  		t.Cleanup(func() {
    61  			_ = changes.Close()
    62  		})
    63  		result := make(map[string]driver.Change)
    64  		ch := &driver.Change{}
    65  		for {
    66  			if err := changes.Next(ch); err != nil {
    67  				if err == io.EOF {
    68  					break
    69  				}
    70  				t.Fatal(err)
    71  			}
    72  			result[ch.ID] = *ch
    73  		}
    74  		if d := testy.DiffAsJSON(testy.Snapshot(t), result); d != nil {
    75  			t.Error(d)
    76  		}
    77  	})
    78  }
    79  

View as plain text