github.com/pachyderm/pachyderm@v1.13.4/src/server/pkg/serde/serde_test.go (about)

     1  package serde
     2  
     3  import (
     4  	"bytes"
     5  	"strings"
     6  	"testing"
     7  
     8  	"github.com/pachyderm/pachyderm/src/client/pkg/require"
     9  )
    10  
    11  func s(text []byte) string {
    12  	return string(bytes.TrimSpace(text))
    13  }
    14  
    15  func TestJSONBasic(t *testing.T) {
    16  	type foo struct {
    17  		A, B string
    18  	}
    19  	encoded := []byte(`{"A":"first","B":"second"}`)
    20  
    21  	var f foo
    22  	require.NoError(t, DecodeYAML(encoded, &f))
    23  	require.Equal(t, foo{"first", "second"}, f)
    24  	out, err := EncodeJSON(f)
    25  	require.NoError(t, err)
    26  	require.Equal(t, s(encoded), s(out))
    27  }
    28  
    29  func TestTags(t *testing.T) {
    30  	type foo struct {
    31  		A string `json:"alpha"`
    32  		B string `json:"beta"`
    33  	}
    34  	encoded := []byte(`{"alpha":"first","beta":"second"}`)
    35  
    36  	var f foo
    37  	require.NoError(t, DecodeYAML(encoded, &f))
    38  	require.Equal(t, foo{"first", "second"}, f)
    39  	out, err := EncodeJSON(f)
    40  	require.NoError(t, err)
    41  	require.Equal(t, s(encoded), s(out))
    42  }
    43  
    44  func TestYAMLBasic(t *testing.T) {
    45  	type foo struct {
    46  		A, B string
    47  	}
    48  	encoded := []byte("A: first\nB: second\n")
    49  
    50  	var f foo
    51  	require.NoError(t, DecodeYAML(encoded, &f))
    52  	require.Equal(t, foo{"first", "second"}, f)
    53  	out, err := EncodeYAML(f)
    54  	require.NoError(t, err)
    55  	require.Equal(t, s(encoded), s(out))
    56  }
    57  
    58  func TestJSONTagsWhenDecodingYAML(t *testing.T) {
    59  	type foo struct {
    60  		A string `json:"alpha"`
    61  		B string `json:"beta"`
    62  	}
    63  	encoded := []byte("alpha: first\nbeta: second\n")
    64  
    65  	var f foo
    66  	require.NoError(t, DecodeYAML(encoded, &f))
    67  	require.Equal(t, foo{"first", "second"}, f)
    68  	out, err := EncodeYAML(f)
    69  	require.NoError(t, err)
    70  	require.Equal(t, s(encoded), s(out))
    71  }
    72  
    73  func TestDecodeYAMLTransform(t *testing.T) {
    74  	type foo struct {
    75  		A string
    76  		B string
    77  	}
    78  	encoded := []byte("A: first\nC: third\n")
    79  
    80  	var f foo
    81  	d := NewYAMLDecoder(bytes.NewReader(encoded))
    82  	require.NoError(t, d.DecodeTransform(&f, func(m map[string]interface{}) error {
    83  		m["B"] = "second"
    84  		delete(m, "C")
    85  		return nil
    86  	}))
    87  	require.Equal(t, foo{"first", "second"}, f)
    88  
    89  	var buf bytes.Buffer
    90  	e := NewYAMLEncoder(&buf)
    91  	require.NoError(t, e.EncodeTransform(f, func(m map[string]interface{}) error {
    92  		m["C"] = "third"
    93  		delete(m, "B")
    94  		return nil
    95  	}))
    96  	require.Equal(t, s(encoded), strings.TrimSpace(buf.String()))
    97  }
    98  
    99  func TestEncodeOptions(t *testing.T) {
   100  	type foo struct {
   101  		A string
   102  		B string
   103  	}
   104  	data := struct {
   105  		F foo
   106  	}{
   107  		F: foo{"first", "second"},
   108  	}
   109  
   110  	var buf bytes.Buffer
   111  	e := NewYAMLEncoder(&buf, WithIndent(3)) // unusual indent, to demo option
   112  	require.NoError(t, e.Encode(data))
   113  	expected := []byte("F:\n   A: first\n   B: second\n")
   114  	// don't trim space to ensure leading space is the same
   115  	require.Equal(t, string(expected), buf.String())
   116  }
   117  
   118  // TODO(msteffen) add proto tests
   119  
   120  // TODO(msteffen) add proto tests