...

Source file src/github.com/go-kivik/kivik/v4/session_test.go

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

     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 kivik
    14  
    15  import (
    16  	"context"
    17  	"errors"
    18  	"net/http"
    19  	"testing"
    20  
    21  	"gitlab.com/flimzy/testy"
    22  
    23  	"github.com/go-kivik/kivik/v4/driver"
    24  	"github.com/go-kivik/kivik/v4/int/mock"
    25  )
    26  
    27  func TestSession(t *testing.T) {
    28  	tests := []struct {
    29  		name     string
    30  		client   driver.Client
    31  		closed   bool
    32  		expected interface{}
    33  		status   int
    34  		err      string
    35  	}{
    36  		{
    37  			name:   "driver doesn't implement Sessioner",
    38  			client: &mock.Client{},
    39  			status: http.StatusNotImplemented,
    40  			err:    "kivik: driver does not support sessions",
    41  		},
    42  		{
    43  			name: "driver returns error",
    44  			client: &mock.Sessioner{
    45  				SessionFunc: func(context.Context) (*driver.Session, error) {
    46  					return nil, errors.New("session error")
    47  				},
    48  			},
    49  			status: http.StatusInternalServerError,
    50  			err:    "session error",
    51  		},
    52  		{
    53  			name: "good response",
    54  			client: &mock.Sessioner{
    55  				SessionFunc: func(context.Context) (*driver.Session, error) {
    56  					return &driver.Session{
    57  						Name:  "curly",
    58  						Roles: []string{"stooges"},
    59  					}, nil
    60  				},
    61  			},
    62  			expected: &Session{
    63  				Name:  "curly",
    64  				Roles: []string{"stooges"},
    65  			},
    66  		},
    67  		{
    68  			name:   "closed",
    69  			closed: true,
    70  			status: http.StatusServiceUnavailable,
    71  			err:    "kivik: client closed",
    72  		},
    73  	}
    74  	for _, test := range tests {
    75  		t.Run(test.name, func(t *testing.T) {
    76  			client := &Client{
    77  				driverClient: test.client,
    78  				closed:       test.closed,
    79  			}
    80  			session, err := client.Session(context.Background())
    81  			var errMsg string
    82  			if err != nil {
    83  				errMsg = err.Error()
    84  			}
    85  			if errMsg != test.err {
    86  				t.Errorf("Unexpected error: %s", errMsg)
    87  			}
    88  			if err != nil {
    89  				return
    90  			}
    91  			if d := testy.DiffInterface(test.expected, session); d != nil {
    92  				t.Error(d)
    93  			}
    94  		})
    95  	}
    96  }
    97  

View as plain text