...

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

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

     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 js
    14  
    15  package pouchdb
    16  
    17  import (
    18  	"context"
    19  	"testing"
    20  
    21  	"gitlab.com/flimzy/testy"
    22  
    23  	kivik "github.com/go-kivik/kivik/v4"
    24  	internal "github.com/go-kivik/kivik/v4/int/errors"
    25  	"github.com/go-kivik/kivik/v4/kiviktest/kt"
    26  )
    27  
    28  func TestChanges(t *testing.T) {
    29  	type tst struct {
    30  		opts            kivik.Option
    31  		status          int
    32  		err             string
    33  		changesErr      string
    34  		expectedIDs     []string
    35  		expectedLastSeq string
    36  		expectedPending int64
    37  	}
    38  	tests := testy.NewTable()
    39  
    40  	tests.Run(t, func(t *testing.T, test tst) {
    41  		ctx := context.Background()
    42  		client, err := kivik.New("pouch", "")
    43  		if err != nil {
    44  			t.Fatalf("Failed to connect to PouchDB/memdown driver: %s", err)
    45  		}
    46  		dbname := kt.TestDBName(t)
    47  		t.Cleanup(func() {
    48  			_ = client.DestroyDB(ctx, dbname)
    49  		})
    50  		if err := client.CreateDB(ctx, dbname); err != nil {
    51  			t.Fatalf("Failed to create db: %s", err)
    52  		}
    53  		db := client.DB(dbname)
    54  		changes := db.Changes(ctx, test.opts)
    55  		if d := internal.StatusErrorDiff(test.err, test.status, changes.Err()); d != "" {
    56  			t.Error(d)
    57  		}
    58  		results := []string{}
    59  		for changes.Next() {
    60  			results = append(results, changes.ID())
    61  		}
    62  		if err := changes.Err(); !testy.ErrorMatches(test.changesErr, err) {
    63  			t.Errorf("Unexpected error: %s", err)
    64  		}
    65  		if d := testy.DiffTextSlices(test.expectedIDs, results); d != nil {
    66  			t.Error(d)
    67  		}
    68  		meta, err := changes.Metadata()
    69  		if err != nil {
    70  			t.Fatal(err)
    71  		}
    72  		if ls := meta.LastSeq; ls != test.expectedLastSeq {
    73  			t.Errorf("Unexpected last_seq: %s", ls)
    74  		}
    75  		if p := meta.Pending; p != test.expectedPending {
    76  			t.Errorf("Unexpected pending count: %d", p)
    77  		}
    78  	})
    79  }
    80  

View as plain text