...

Source file src/github.com/go-kivik/kivik/v4/document_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  	"encoding/json"
    17  	"os"
    18  	"testing"
    19  
    20  	"gitlab.com/flimzy/testy"
    21  )
    22  
    23  func TestDocumentMarshalJSON(t *testing.T) {
    24  	tests := testy.NewTable()
    25  	tests.Add("no attachments", &document{
    26  		ID:  "foo",
    27  		Rev: "1-xxx",
    28  		Data: map[string]interface{}{
    29  			"foo": "bar",
    30  		},
    31  	})
    32  	tests.Add("attachment", func(t *testing.T) interface{} {
    33  		f, err := os.Open("testdata/foo.txt")
    34  		if err != nil {
    35  			t.Fatal(err)
    36  		}
    37  
    38  		return &document{
    39  			ID:  "foo",
    40  			Rev: "1-xxx",
    41  			Attachments: &Attachments{
    42  				"foo.txt": &Attachment{
    43  					ContentType: "text/plain",
    44  					Content:     f,
    45  				},
    46  			},
    47  		}
    48  	})
    49  
    50  	tests.Run(t, func(t *testing.T, doc *document) {
    51  		result, err := json.Marshal(doc)
    52  		if err != nil {
    53  			t.Fatal(err)
    54  		}
    55  		if d := testy.DiffAsJSON(&testy.File{Path: "testdata/" + testy.Stub(t)}, result); d != nil {
    56  			t.Error(d)
    57  		}
    58  	})
    59  }
    60  
    61  func TestNormalDocUnmarshalJSON(t *testing.T) {
    62  	tests := testy.NewTable()
    63  	tests.Add("no extra fields", `{
    64          "_id":"foo"
    65      }`)
    66  	tests.Add("extra fields", `{
    67          "_id":"foo",
    68          "foo":"bar"
    69      }`)
    70  	tests.Add("attachment stub", `{
    71          "_id":"foo",
    72          "foo":"bar",
    73          "_attachments":{
    74              "foo.txt":{
    75                  "content_type":"text/plain",
    76                  "stub":true
    77              }
    78          }
    79      }`)
    80  	tests.Add("attachment", `{
    81          "_id":"foo",
    82          "foo":"bar",
    83          "_attachments":{
    84              "foo.txt":{
    85                  "content_type":"text/plain",
    86                  "data":"VGVzdGluZwo="
    87              }
    88          }
    89      }`)
    90  
    91  	tests.Run(t, func(t *testing.T, in string) {
    92  		result := new(document)
    93  		if err := json.Unmarshal([]byte(in), &result); err != nil {
    94  			t.Fatal(err)
    95  		}
    96  		if d := testy.DiffAsJSON(&testy.File{Path: "testdata/" + testy.Stub(t)}, result); d != nil {
    97  			t.Error(d)
    98  		}
    99  	})
   100  }
   101  

View as plain text