github.com/dolthub/go-mysql-server@v0.18.0/sql/types/json_encode_test.go (about)

     1  package types
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  )
     7  
     8  func TestMarshalToMySqlString(t *testing.T) {
     9  	tests := []struct {
    10  		name     string
    11  		val      interface{}
    12  		expected string
    13  	}{
    14  		{
    15  			name: "simple",
    16  			val: map[string]interface{}{
    17  				"foo": "bar",
    18  			},
    19  			expected: `{"foo": "bar"}`,
    20  		},
    21  		{
    22  			name: "nested",
    23  			val: map[string]interface{}{
    24  				"foo": map[string]interface{}{
    25  					"bar": "baz",
    26  				},
    27  			},
    28  			expected: `{"foo": {"bar": "baz"}}`,
    29  		},
    30  		{
    31  			name:     "array",
    32  			val:      []interface{}{"foo", "bar", "baz"},
    33  			expected: `["foo", "bar", "baz"]`,
    34  		},
    35  		{
    36  			name: "array of maps",
    37  			val: []interface{}{
    38  				map[string]interface{}{
    39  					"foo": "bar",
    40  				},
    41  				map[string]interface{}{
    42  					"baz": "qux",
    43  				},
    44  				map[string]interface{}{
    45  					"str":     "str",
    46  					"float64": 1.0,
    47  					"float32": float32(1.0),
    48  					"int64":   int64(-1),
    49  					"int32":   int32(-1),
    50  					"int16":   int16(-1),
    51  					"int8":    int8(-1),
    52  					"int":     -1,
    53  					"uint64":  uint64(1),
    54  					"uint32":  uint32(1),
    55  					"uint16":  uint16(1),
    56  					"uint8":   uint8(1),
    57  					"bool":    true,
    58  				},
    59  			},
    60  			expected: `[{"foo": "bar"}, {"baz": "qux"}, {"int": -1, "str": "str", "bool": true, "int8": -1, "int16": -1, "int32": -1, "int64": -1, "uint8": 1, "uint16": 1, "uint32": 1, "uint64": 1, "float32": 1, "float64": 1}]`,
    61  		},
    62  		{
    63  			name: "map of strings",
    64  			val: map[string]string{
    65  				"foo": "bar",
    66  				"baz": "qux",
    67  			},
    68  			expected: `{"baz": "qux", "foo": "bar"}`,
    69  		},
    70  		{
    71  			name: "map of timestamps",
    72  			val: map[string]interface{}{
    73  				"a": time.Date(2023, 1, 2, 3, 4, 5, 6, time.UTC),
    74  				"b": time.Date(2023, 6, 5, 4, 3, 2, 1, time.UTC),
    75  			},
    76  			expected: `{"a": "2023-01-02T03:04:05Z", "b": "2023-06-05T04:03:02Z"}`,
    77  		},
    78  		{
    79  			name: "string formatting",
    80  			val: []string{
    81  				`simple`,
    82  				`With "quotes"`,
    83  				`With "quotes" not at the end`,
    84  				`with 'single quotes'`,
    85  				`with
    86  newlines
    87  `,
    88  				`with \n escaped newline`,
    89  				`with	`,
    90  			},
    91  			expected: `["simple", "With \"quotes\"", "With \"quotes\" not at the end", "with 'single quotes'", "with\nnewlines\n", "with \\n escaped newline", "with\t"]`,
    92  		},
    93  		{
    94  			name: "complicated string",
    95  			val: []string{
    96  				`{
    97  	"nested": "json",
    98  	"nested_escapedQuotes": "here \"you\" go"
    99  }`},
   100  			expected: `["{\n\t\"nested\": \"json\",\n\t\"nested_escapedQuotes\": \"here \\\"you\\\" go\"\n}"]`,
   101  		},
   102  	}
   103  
   104  	for _, test := range tests {
   105  		t.Run(test.name, func(t *testing.T) {
   106  			actual, err := marshalToMySqlString(test.val)
   107  			if err != nil {
   108  				t.Fatal(err)
   109  			}
   110  
   111  			if actual != test.expected {
   112  				t.Errorf("expected %s, got %s", test.expected, actual)
   113  			}
   114  		})
   115  	}
   116  }