github.com/willyham/dosa@v2.3.1-0.20171024181418-1e446d37ee71+incompatible/schema/avro/avro_test.go (about)

     1  // Copyright (c) 2017 Uber Technologies, Inc.
     2  //
     3  // Permission is hereby granted, free of charge, to any person obtaining a copy
     4  // of this software and associated documentation files (the "Software"), to deal
     5  // in the Software without restriction, including without limitation the rights
     6  // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
     7  // copies of the Software, and to permit persons to whom the Software is
     8  // furnished to do so, subject to the following conditions:
     9  //
    10  // The above copyright notice and this permission notice shall be included in
    11  // all copies or substantial portions of the Software.
    12  //
    13  // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
    14  // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
    15  // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
    16  // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
    17  // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
    18  // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
    19  // THE SOFTWARE.
    20  
    21  package avro
    22  
    23  import (
    24  	"testing"
    25  
    26  	"github.com/pkg/errors"
    27  	"github.com/stretchr/testify/assert"
    28  	"github.com/uber-go/dosa"
    29  )
    30  
    31  func createEntityDefinition() *dosa.EntityDefinition {
    32  	return &dosa.EntityDefinition{
    33  		Name: "test",
    34  		Columns: []*dosa.ColumnDefinition{
    35  			{
    36  				Name: "stringcol",
    37  				Type: dosa.String,
    38  			},
    39  			{
    40  				Name: "uuidcol",
    41  				Type: dosa.TUUID,
    42  			},
    43  			{
    44  				Name: "int32col",
    45  				Type: dosa.Int32,
    46  			},
    47  			{
    48  				Name: "longcol",
    49  				Type: dosa.Int64,
    50  			},
    51  			{
    52  				Name: "doublecol",
    53  				Type: dosa.Double,
    54  			},
    55  			{
    56  				Name: "blobcol",
    57  				Type: dosa.Blob,
    58  			},
    59  			{
    60  				Name: "boolcol",
    61  				Type: dosa.Bool,
    62  			},
    63  			{
    64  				Name: "timestampcol",
    65  				Type: dosa.Timestamp,
    66  			},
    67  		},
    68  		Indexes: map[string]*dosa.IndexDefinition{
    69  			"index1": {
    70  				Key: &dosa.PrimaryKey{
    71  					PartitionKeys: []string{"timestampcol"},
    72  				},
    73  			},
    74  		},
    75  		Key: &dosa.PrimaryKey{
    76  			PartitionKeys: []string{
    77  				"stringcol",
    78  				"uuidcol",
    79  			},
    80  			ClusteringKeys: []*dosa.ClusteringKey{
    81  				{
    82  					Name:       "doublecol",
    83  					Descending: false,
    84  				},
    85  				{
    86  					Name:       "boolcol",
    87  					Descending: true,
    88  				},
    89  			},
    90  		},
    91  	}
    92  }
    93  
    94  func TestToAvroSchema(t *testing.T) {
    95  	ed := createEntityDefinition()
    96  	fqn, err := dosa.ToFQN("xxx.tt.yy")
    97  	assert.NoError(t, err)
    98  	av, err := ToAvro(fqn, ed)
    99  	assert.NoError(t, err)
   100  	ed1, err := FromAvro(string(av))
   101  	assert.NoError(t, err)
   102  	assert.Equal(t, ed, ed1)
   103  }
   104  
   105  func TestDecodeFailure(t *testing.T) {
   106  	data := []struct {
   107  		Schema string
   108  		Err    error
   109  	}{
   110  		{
   111  			Schema: `{
   112  				"ClusteringKeys":[
   113  					{"Name":"doublecol","Descending":false},
   114  					{"Name":"boolcol","Descending":true}
   115  				],
   116  				"fields":[
   117  					{"dosaType":"String","name":"stringcol","type":"string"}
   118  				],
   119  				"name":"test",
   120  				"PartitionKeys":{},
   121  				"type":"record"
   122  			}`,
   123  			Err: errors.New("failed to parse partition keys"),
   124  		},
   125  		{
   126  			Schema: `{
   127  				"ClusteringKeys":[
   128  					{"Name":"doublecol","Descending":false},
   129  					{"Name":"boolcol","Descending":true}
   130  				],
   131  				"fields":[
   132  					{"dosaType":"String","name":"stringcol","type":"string"}]
   133  		 		,
   134  				"name":"test",
   135  				"PartitionKeys":[{},"uuidcol"],
   136  				"type":"record"
   137  			}`,
   138  			Err: errors.New("failed to parse partition keys"),
   139  		},
   140  		{
   141  			Schema: `{
   142  				"ClusteringKeys":{},
   143  				"fields":[
   144  					{"dosaType":"String","name":"stringcol","type":"string"}]
   145  		 		,
   146  				"name":"test",
   147  				"PartitionKeys":["stringcol","uuidcol"],
   148  				"type":"record"
   149  			}`,
   150  			Err: errors.New("clustering keys"),
   151  		},
   152  		{
   153  			Schema: `{
   154  				"ClusteringKeys":[
   155  					{"A":"doublecol","Descending":false}
   156  				],
   157  				"fields":[
   158  					{"dosaType":"String","name":"stringcol","type":"string"}]
   159  		 		,
   160  				"name":"test",
   161  				"PartitionKeys":["stringcol","uuidcol"],
   162  				"type":"record"
   163  			}`,
   164  			Err: errors.New("cannot find Name key in map"),
   165  		},
   166  		{
   167  			Schema: `{
   168  				"ClusteringKeys":[
   169  					{"Name":"doublecol","A":false}
   170  				],
   171  				"fields":[
   172  					{"dosaType":"String","name":"stringcol","type":"string"}]
   173  		 		,
   174  				"name":"test",
   175  				"PartitionKeys":["stringcol","uuidcol"],
   176  				"type":"record"
   177  			}`,
   178  			Err: errors.New("cannot find Descending key in map"),
   179  		},
   180  		{
   181  			Schema: `{
   182  				"k":[
   183  					{"Name":"doublecol","Descending":false}
   184  				],
   185  				"fields":[
   186  					{"dosaType":"String","name":"stringcol","type":"string"}]
   187  		 		,
   188  				"name":"test",
   189  				"PartitionKeys":["stringcol","uuidcol"],
   190  				"type":"record"
   191  			}`,
   192  			Err: errors.New("cannot find ClusteringKeys key"),
   193  		},
   194  		{
   195  			Schema: `{
   196  				"ClusteringKeys":[
   197  					{"Name":"doublecol","Descending":false}
   198  				],
   199  				"fields":[
   200  					{"dosaType":"String","name":"stringcol","type":"string"}]
   201  		 		,
   202  				"name":"test",
   203  				"p":["stringcol","uuidcol"],
   204  				"type":"record"
   205  			}`,
   206  			Err: errors.New("cannot find PartitionKeys key"),
   207  		},
   208  		{
   209  			Schema: `{
   210  				"ClusteringKeys":[
   211  					{"Name":"doublecol","Descending":false},
   212  					{"Name":"boolcol","Descending":true}
   213  				],
   214  				"fields":[
   215  					{"d":"String","name":"stringcol","type":"string"}
   216  		 		],
   217  				"name":"test",
   218  				"PartitionKeys":["stringcol","uuidcol"],
   219  				"type":"record"
   220  			}`,
   221  			Err: errors.New("cannot find dosaType key"),
   222  		},
   223  		{
   224  			Schema: `{
   225  				"ClusteringKeys":[
   226  					{"Name":"doublecol","Descending":false},
   227  					{"Name":"boolcol","Descending":true}
   228  				],
   229  				"fields":[
   230  					{"d":{},"name":"stringcol","type":"string"}
   231  		 		],
   232  				"name":"test",
   233  				"PartitionKeys":["stringcol","uuidcol"],
   234  				"type":"record"
   235  			}`,
   236  			Err: errors.New("cannot find dosaType key"),
   237  		},
   238  		{
   239  			Schema: `{
   240  				"ClusteringKeys":[
   241  					{"Name":"doublecol","Descending":false},
   242  					{"Name":"boolcol","Descending":true}
   243  				],
   244  				"fields":[
   245  					{"d":1,"name":"stringcol","type":"string"}
   246  		 		],
   247  				"name":"test",
   248  				"PartitionKeys":["stringcol","uuidcol"],
   249  				"type":"record"
   250  			}`,
   251  			Err: errors.New("cannot find dosaType key"),
   252  		},
   253  	}
   254  	for _, d := range data {
   255  		_, err := FromAvro(d.Schema)
   256  		assert.Contains(t, err.Error(), d.Err.Error())
   257  	}
   258  }