github.com/hamba/avro@v1.8.0/schema_json_test.go (about)

     1  package avro_test
     2  
     3  import (
     4  	"encoding/json"
     5  	"strconv"
     6  	"testing"
     7  
     8  	"github.com/hamba/avro"
     9  	"github.com/stretchr/testify/assert"
    10  	"github.com/stretchr/testify/require"
    11  )
    12  
    13  func TestSchema_JSON(t *testing.T) {
    14  	tests := []struct {
    15  		input string
    16  		json  string
    17  	}{
    18  		{
    19  			input: `"null"`,
    20  			json:  `"null"`,
    21  		},
    22  		{
    23  			input: `{"type":"null"}`,
    24  			json:  `"null"`,
    25  		},
    26  		{
    27  			input: `"boolean"`,
    28  			json:  `"boolean"`,
    29  		},
    30  		{
    31  			input: `{"type":"boolean"}`,
    32  			json:  `"boolean"`,
    33  		},
    34  		{
    35  			input: `"int"`,
    36  			json:  `"int"`,
    37  		},
    38  		{
    39  			input: `{"type":"int"}`,
    40  			json:  `"int"`,
    41  		},
    42  		{
    43  			input: `{"type":"int","logicalType":"date"}`,
    44  			json:  `{"type":"int","logicalType":"date"}`,
    45  		},
    46  		{
    47  			input: `{"type":"int","logicalType":"time-millis"}`,
    48  			json:  `{"type":"int","logicalType":"time-millis"}`,
    49  		},
    50  		{
    51  			input: `{"type":"int"}`,
    52  			json:  `"int"`,
    53  		},
    54  		{
    55  			input: `"long"`,
    56  			json:  `"long"`,
    57  		},
    58  		{
    59  			input: `{"type":"long"}`,
    60  			json:  `"long"`,
    61  		},
    62  		{
    63  			input: `{"type":"long","logicalType":"time-micros"}`,
    64  			json:  `{"type":"long","logicalType":"time-micros"}`,
    65  		},
    66  		{
    67  			input: `{"type":"long","logicalType":"timestamp-millis"}`,
    68  			json:  `{"type":"long","logicalType":"timestamp-millis"}`,
    69  		},
    70  		{
    71  			input: `{"type":"long","logicalType":"timestamp-millis"}`,
    72  			json:  `{"type":"long","logicalType":"timestamp-millis"}`,
    73  		},
    74  		{
    75  			input: `"float"`,
    76  			json:  `"float"`,
    77  		},
    78  		{
    79  			input: `{"type":"float"}`,
    80  			json:  `"float"`,
    81  		},
    82  		{
    83  			input: `"double"`,
    84  			json:  `"double"`,
    85  		},
    86  		{
    87  			input: `{"type":"double"}`,
    88  			json:  `"double"`,
    89  		},
    90  		{
    91  			input: `"bytes"`,
    92  			json:  `"bytes"`,
    93  		},
    94  		{
    95  			input: `{"type":"bytes"}`,
    96  			json:  `"bytes"`,
    97  		},
    98  		{
    99  			input: `{"type":"bytes","logicalType":"decimal","precision":4,"scale":2}`,
   100  			json:  `{"type":"bytes","logicalType":"decimal","precision":4,"scale":2}`,
   101  		},
   102  		{
   103  			input: `{"type":"bytes","logicalType":"decimal","precision":4,"scale":0}`,
   104  			json:  `{"type":"bytes","logicalType":"decimal","precision":4}`,
   105  		},
   106  		{
   107  			input: `"string"`,
   108  			json:  `"string"`,
   109  		},
   110  		{
   111  			input: `{"type":"string"}`,
   112  			json:  `"string"`,
   113  		},
   114  		{
   115  			input: `{"type":"string","logicalType":"uuid"}`,
   116  			json:  `{"type":"string","logicalType":"uuid"}`,
   117  		},
   118  		{
   119  			input: `[  ]`,
   120  			json:  `[]`,
   121  		},
   122  		{
   123  			input: `[ "int"  ]`,
   124  			json:  `["int"]`,
   125  		},
   126  		{
   127  			input: `[ "int" , {"type":"boolean"} ]`,
   128  			json:  `["int","boolean"]`,
   129  		},
   130  		{
   131  			input: `{"fields":[], "type":"error", "name":"foo"}`,
   132  			json:  `{"name":"foo","type":"error","fields":[]}`,
   133  		},
   134  		{
   135  			input: `{"fields":[], "type":"record", "name":"foo"}`,
   136  			json:  `{"name":"foo","type":"record","fields":[]}`,
   137  		},
   138  		{
   139  			input: `{"fields":[], "type":"record", "name":"foo", "namespace":"x.y"}`,
   140  			json:  `{"name":"x.y.foo","type":"record","fields":[]}`,
   141  		},
   142  		{
   143  			input: `{"fields":[], "type":"record", "name":"a.b.foo", "namespace":"x.y"}`,
   144  			json:  `{"name":"a.b.foo","type":"record","fields":[]}`,
   145  		},
   146  		{
   147  			input: `{"fields":[], "type":"record", "name":"foo", "doc":"Useful info"}`,
   148  			json:  `{"name":"foo","type":"record","fields":[]}`,
   149  		},
   150  		{
   151  			input: `{"fields":[], "type":"record", "name":"foo", "aliases":["foo","bar"]}`,
   152  			json:  `{"name":"foo","type":"record","fields":[]}`,
   153  		},
   154  		{
   155  			input: `{"fields":[], "type":"record", "name":"foo", "doc":"foo", "aliases":["foo","bar"]}`,
   156  			json:  `{"name":"foo","type":"record","fields":[]}`,
   157  		},
   158  		{
   159  			input: `{"fields":[{"type":{"type":"boolean"}, "name":"f1"}], "type":"record", "name":"foo"}`,
   160  			json:  `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean"}]}`,
   161  		},
   162  		{
   163  			input: `
   164  { "fields":[{"type":"boolean", "aliases":[], "name":"f1", "default":true},
   165             {"order":"descending","name":"f2","doc":"Hello","type":"int"}],
   166   "type":"record", "name":"foo"
   167  }`,
   168  			json: `{"name":"foo","type":"record","fields":[{"name":"f1","type":"boolean","default":true},{"name":"f2","type":"int"}]}`,
   169  		},
   170  		{
   171  			input: `{"type":"enum", "name":"foo", "symbols":["A1"]}`,
   172  			json:  `{"name":"foo","type":"enum","symbols":["A1"]}`,
   173  		},
   174  		{
   175  			input: `{"namespace":"x.y.z", "type":"enum", "name":"foo", "doc":"foo bar", "symbols":["A1", "A2"]}`,
   176  			json:  `{"name":"x.y.z.foo","type":"enum","symbols":["A1","A2"]}`,
   177  		},
   178  		{
   179  			input: `{"name":"foo","type":"fixed","size":15}`,
   180  			json:  `{"name":"foo","type":"fixed","size":15}`,
   181  		},
   182  		{
   183  			input: `{"name":"foo","type":"fixed","logicalType":"duration","size":12}`,
   184  			json:  `{"name":"foo","type":"fixed","size":12,"logicalType":"duration"}`,
   185  		},
   186  		{
   187  			input: `{"name":"foo","type":"fixed","logicalType":"decimal","size":12,"precision":4,"scale":2}`,
   188  			json:  `{"name":"foo","type":"fixed","size":12,"logicalType":"decimal","precision":4,"scale":2}`,
   189  		},
   190  		{
   191  			input: `{"name":"foo","type":"fixed","logicalType":"decimal","size":12,"precision":4,"scale":0}`,
   192  			json:  `{"name":"foo","type":"fixed","size":12,"logicalType":"decimal","precision":4}`,
   193  		},
   194  		{
   195  			input: `{"namespace":"x.y.z", "type":"fixed", "name":"foo", "doc":"foo bar", "size":32}`,
   196  			json:  `{"name":"x.y.z.foo","type":"fixed","size":32}`,
   197  		},
   198  		{
   199  			input: `{ "items":{"type":"null"}, "type":"array"}`,
   200  			json:  `{"type":"array","items":"null"}`,
   201  		},
   202  		{
   203  			input: `{ "values":"string", "type":"map"}`,
   204  			json:  `{"type":"map","values":"string"}`,
   205  		},
   206  		{
   207  			input: `
   208  
   209   {"name":"PigValue","type":"record",
   210    "fields":[{"name":"value", "type":["null", "int", "long", "PigValue"]}]}
   211  `,
   212  			json: `{"name":"PigValue","type":"record","fields":[{"name":"value","type":["null","int","long","PigValue"]}]}`,
   213  		},
   214  		{
   215  			input: `{
   216  				"type":"record",
   217  				"namespace": "org.hamba.avro",
   218  				"name":"X",
   219    				"fields":[
   220  					{"name":"value", "type":{
   221  						"type":"record",
   222  						"name":"Y",
   223  						"fields":[
   224  							{"name":"value", "type":"string"}
   225  						]
   226  					}}
   227  				]
   228  			}`,
   229  			json: `{"name":"org.hamba.avro.X","type":"record","fields":[{"name":"value","type":{"name":"org.hamba.avro.Y","type":"record","fields":[{"name":"value","type":"string"}]}}]}`,
   230  		},
   231  		{
   232  			input: `{
   233  				"type":"record",
   234  				"namespace": "org.hamba.avro",
   235  				"name":"X",
   236    				"fields":[
   237  					{"name":"value", "type":{
   238  						"type":"enum",
   239  						"name":"Y",
   240  						"symbols":["TEST"]
   241  					}}
   242  				]
   243  			}`,
   244  			json: `{"name":"org.hamba.avro.X","type":"record","fields":[{"name":"value","type":{"name":"org.hamba.avro.Y","type":"enum","symbols":["TEST"]}}]}`,
   245  		},
   246  		{
   247  			input: `{
   248  				"type":"record",
   249  				"namespace": "org.hamba.avro",
   250  				"name":"X",
   251    				"fields":[
   252  					{"name":"value", "type":{
   253  						"type":"fixed",
   254  						"name":"Y",
   255  						"size":15
   256  					}}
   257  				]
   258  			}`,
   259  			json: `{"name":"org.hamba.avro.X","type":"record","fields":[{"name":"value","type":{"name":"org.hamba.avro.Y","type":"fixed","size":15}}]}`,
   260  		},
   261  		{
   262  			input: `{
   263  				"type":"record",
   264  				"namespace": "org.hamba.avro",
   265  				"name":"X",
   266    				"fields":[
   267  					{"name":"union_no_def","type":["null", "int"]},
   268  					{"name":"union_with_def","type":["null", "string"],"default": null}
   269  				]
   270  			}`,
   271  			json: `{"name":"org.hamba.avro.X","type":"record","fields":[{"name":"union_no_def","type":["null","int"]},{"name":"union_with_def","type":["null","string"],"default":null}]}`,
   272  		},
   273  	}
   274  
   275  	for i, test := range tests {
   276  		test := test
   277  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   278  			s, err := avro.Parse(test.input)
   279  			require.NoError(t, err)
   280  
   281  			b, err := json.Marshal(s)
   282  
   283  			require.NoError(t, err)
   284  			assert.Equal(t, test.json, string(b))
   285  		})
   286  	}
   287  }