...

Source file src/github.com/go-kivik/kivik/v4/couchdb/util_test.go

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

     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 couchdb
    14  
    15  import (
    16  	"encoding/json"
    17  	"net/http"
    18  	"testing"
    19  
    20  	"gitlab.com/flimzy/testy"
    21  
    22  	internal "github.com/go-kivik/kivik/v4/int/errors"
    23  )
    24  
    25  func TestDeJSONify(t *testing.T) {
    26  	tests := []struct {
    27  		name     string
    28  		input    interface{}
    29  		expected interface{}
    30  		status   int
    31  		err      string
    32  	}{
    33  		{
    34  			name:     "string",
    35  			input:    `{"foo":"bar"}`,
    36  			expected: map[string]interface{}{"foo": "bar"},
    37  		},
    38  		{
    39  			name:     "[]byte",
    40  			input:    []byte(`{"foo":"bar"}`),
    41  			expected: map[string]interface{}{"foo": "bar"},
    42  		},
    43  		{
    44  			name:     "json.RawMessage",
    45  			input:    json.RawMessage(`{"foo":"bar"}`),
    46  			expected: map[string]interface{}{"foo": "bar"},
    47  		},
    48  		{
    49  			name:     "map",
    50  			input:    map[string]string{"foo": "bar"},
    51  			expected: map[string]string{"foo": "bar"},
    52  		},
    53  		{
    54  			name:   "invalid JSON string",
    55  			input:  `{"foo":"\C"}`,
    56  			status: http.StatusBadRequest,
    57  			err:    "invalid character 'C' in string escape code",
    58  		},
    59  	}
    60  	for _, test := range tests {
    61  		t.Run(test.name, func(t *testing.T) {
    62  			result, err := deJSONify(test.input)
    63  			if d := internal.StatusErrorDiff(test.err, test.status, err); d != "" {
    64  				t.Error(d)
    65  			}
    66  			if d := testy.DiffInterface(test.expected, result); d != nil {
    67  				t.Error(d)
    68  			}
    69  		})
    70  	}
    71  }
    72  

View as plain text