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

     1  package avro
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/stretchr/testify/assert"
     7  )
     8  
     9  func TestName_NameAndNamespace(t *testing.T) {
    10  	n, _ := newName("bar", "foo")
    11  
    12  	assert.Equal(t, "bar", n.Name())
    13  	assert.Equal(t, "foo", n.Namespace())
    14  	assert.Equal(t, "foo.bar", n.FullName())
    15  }
    16  
    17  func TestName_QualifiedName(t *testing.T) {
    18  	n, _ := newName("foo.bar", "test")
    19  
    20  	assert.Equal(t, "bar", n.Name())
    21  	assert.Equal(t, "foo", n.Namespace())
    22  	assert.Equal(t, "foo.bar", n.FullName())
    23  }
    24  
    25  func TestName_InvalidNameFirstChar(t *testing.T) {
    26  	_, err := newName("+bar", "foo")
    27  
    28  	assert.Error(t, err)
    29  }
    30  
    31  func TestName_InvalidNameOtherChar(t *testing.T) {
    32  	_, err := newName("bar+", "foo")
    33  
    34  	assert.Error(t, err)
    35  }
    36  
    37  func TestName_InvalidNamespaceFirstChar(t *testing.T) {
    38  	_, err := newName("bar", "+foo")
    39  
    40  	assert.Error(t, err)
    41  }
    42  
    43  func TestName_InvalidNamespaceOtherChar(t *testing.T) {
    44  	_, err := newName("bar", "foo+")
    45  
    46  	assert.Error(t, err)
    47  }
    48  
    49  func TestProperties_AddPropDoesNotAddReservedProperties(t *testing.T) {
    50  	p := properties{reserved: []string{"test"}}
    51  
    52  	p.AddProp("test", "foo")
    53  
    54  	assert.Nil(t, p.Prop("test"))
    55  }
    56  
    57  func TestProperties_AddPropDoesNotOverwriteProperties(t *testing.T) {
    58  	p := properties{}
    59  
    60  	p.AddProp("test", "foo")
    61  	p.AddProp("test", "bar")
    62  
    63  	assert.Equal(t, "foo", p.Prop("test"))
    64  }
    65  
    66  func TestProperties_PropGetsFromEmptySet(t *testing.T) {
    67  	p := properties{}
    68  
    69  	assert.Nil(t, p.Prop("test"))
    70  }
    71  
    72  func TestIsValidDefault(t *testing.T) {
    73  	tests := []struct {
    74  		name     string
    75  		schemaFn func() Schema
    76  		def      interface{}
    77  		want     interface{}
    78  		wantOk   bool
    79  	}{
    80  
    81  		{
    82  			name: "Null",
    83  			schemaFn: func() Schema {
    84  				return &NullSchema{}
    85  			},
    86  			def:    nil,
    87  			want:   nullDefault,
    88  			wantOk: true,
    89  		},
    90  		{
    91  			name: "Null Invalid Type",
    92  			schemaFn: func() Schema {
    93  				return &NullSchema{}
    94  			},
    95  			def:    "test",
    96  			wantOk: false,
    97  		},
    98  		{
    99  			name: "String",
   100  			schemaFn: func() Schema {
   101  				return NewPrimitiveSchema(String, nil)
   102  			},
   103  			def:    "test",
   104  			want:   "test",
   105  			wantOk: true,
   106  		},
   107  		{
   108  			name: "String Invalid Type",
   109  			schemaFn: func() Schema {
   110  				return NewPrimitiveSchema(String, nil)
   111  			},
   112  			def:    1,
   113  			wantOk: false,
   114  		},
   115  		{
   116  			name: "Bytes",
   117  			schemaFn: func() Schema {
   118  				return NewPrimitiveSchema(Bytes, nil)
   119  			},
   120  			def:    "test",
   121  			want:   "test",
   122  			wantOk: true,
   123  		},
   124  		{
   125  			name: "Bytes Invalid Type",
   126  			schemaFn: func() Schema {
   127  				return NewPrimitiveSchema(Bytes, nil)
   128  			},
   129  			def:    1,
   130  			wantOk: false,
   131  		},
   132  		{
   133  			name: "Enum",
   134  			schemaFn: func() Schema {
   135  				s, _ := NewEnumSchema("foo", "", []string{"BAR"})
   136  				return s
   137  			},
   138  			def:    "test",
   139  			want:   "test",
   140  			wantOk: true,
   141  		},
   142  		{
   143  			name: "Enum Invalid Type",
   144  			schemaFn: func() Schema {
   145  				s, _ := NewEnumSchema("foo", "", []string{"BAR"})
   146  				return s
   147  			},
   148  			def:    1,
   149  			wantOk: false,
   150  		},
   151  		{
   152  			name: "Fixed",
   153  			schemaFn: func() Schema {
   154  				s, _ := NewFixedSchema("foo", "", 1, nil)
   155  				return s
   156  			},
   157  			def:    "test",
   158  			want:   "test",
   159  			wantOk: true,
   160  		},
   161  		{
   162  			name: "Fixed Invalid Type",
   163  			schemaFn: func() Schema {
   164  				s, _ := NewFixedSchema("foo", "", 1, nil)
   165  				return s
   166  			},
   167  			def:    1,
   168  			wantOk: false,
   169  		},
   170  		{
   171  			name: "Boolean",
   172  			schemaFn: func() Schema {
   173  				return NewPrimitiveSchema(Boolean, nil)
   174  			},
   175  			def:    true,
   176  			want:   true,
   177  			wantOk: true,
   178  		},
   179  		{
   180  			name: "Boolean Invalid Type",
   181  			schemaFn: func() Schema {
   182  				return NewPrimitiveSchema(Boolean, nil)
   183  			},
   184  			def:    1,
   185  			wantOk: false,
   186  		},
   187  		{
   188  			name: "Int",
   189  			schemaFn: func() Schema {
   190  				return NewPrimitiveSchema(Int, nil)
   191  			},
   192  			def:    1,
   193  			want:   1,
   194  			wantOk: true,
   195  		},
   196  		{
   197  			name: "Int Int8",
   198  			schemaFn: func() Schema {
   199  				return NewPrimitiveSchema(Int, nil)
   200  			},
   201  			def:    int8(1),
   202  			want:   1,
   203  			wantOk: true,
   204  		},
   205  		{
   206  			name: "Int Int16",
   207  			schemaFn: func() Schema {
   208  				return NewPrimitiveSchema(Int, nil)
   209  			},
   210  			def:    int16(1),
   211  			want:   1,
   212  			wantOk: true,
   213  		},
   214  		{
   215  			name: "Int Int32",
   216  			schemaFn: func() Schema {
   217  				return NewPrimitiveSchema(Int, nil)
   218  			},
   219  			def:    int32(1),
   220  			want:   1,
   221  			wantOk: true,
   222  		},
   223  		{
   224  			name: "Int Float64",
   225  			schemaFn: func() Schema {
   226  				return NewPrimitiveSchema(Int, nil)
   227  			},
   228  			def:    float64(1),
   229  			want:   1,
   230  			wantOk: true,
   231  		},
   232  		{
   233  			name: "Int Invalid Type",
   234  			schemaFn: func() Schema {
   235  				return NewPrimitiveSchema(Int, nil)
   236  			},
   237  			def:    "test",
   238  			wantOk: false,
   239  		},
   240  		{
   241  			name: "Long",
   242  			schemaFn: func() Schema {
   243  				return NewPrimitiveSchema(Long, nil)
   244  			},
   245  			def:    int64(1),
   246  			want:   int64(1),
   247  			wantOk: true,
   248  		},
   249  		{
   250  			name: "Long Float64",
   251  			schemaFn: func() Schema {
   252  				return NewPrimitiveSchema(Long, nil)
   253  			},
   254  			def:    float64(1),
   255  			want:   int64(1),
   256  			wantOk: true,
   257  		},
   258  		{
   259  			name: "Long Invalid Type",
   260  			schemaFn: func() Schema {
   261  				return NewPrimitiveSchema(Long, nil)
   262  			},
   263  			def:    "test",
   264  			wantOk: false,
   265  		},
   266  		{
   267  			name: "Float",
   268  			schemaFn: func() Schema {
   269  				return NewPrimitiveSchema(Float, nil)
   270  			},
   271  			def:    float32(1),
   272  			want:   float32(1),
   273  			wantOk: true,
   274  		},
   275  		{
   276  			name: "Float Float64",
   277  			schemaFn: func() Schema {
   278  				return NewPrimitiveSchema(Float, nil)
   279  			},
   280  			def:    float64(1),
   281  			want:   float32(1),
   282  			wantOk: true,
   283  		},
   284  		{
   285  			name: "Float Invalid Type",
   286  			schemaFn: func() Schema {
   287  				return NewPrimitiveSchema(Float, nil)
   288  			},
   289  			def:    "test",
   290  			wantOk: false,
   291  		},
   292  		{
   293  			name: "Double",
   294  			schemaFn: func() Schema {
   295  				return NewPrimitiveSchema(Double, nil)
   296  			},
   297  			def:    float64(1),
   298  			want:   float64(1),
   299  			wantOk: true,
   300  		},
   301  		{
   302  			name: "Double Invalid Type",
   303  			schemaFn: func() Schema {
   304  				return NewPrimitiveSchema(Double, nil)
   305  			},
   306  			def:    "test",
   307  			wantOk: false,
   308  		},
   309  	}
   310  
   311  	for _, tt := range tests {
   312  		t.Run(tt.name, func(t *testing.T) {
   313  			got, ok := isValidDefault(tt.schemaFn(), tt.def)
   314  
   315  			assert.Equal(t, tt.wantOk, ok)
   316  			if ok {
   317  				assert.Equal(t, tt.want, got)
   318  			}
   319  		})
   320  	}
   321  }
   322  
   323  func TestRecursionError_Error(t *testing.T) {
   324  	err := recursionError{}
   325  
   326  	assert.Equal(t, "", err.Error())
   327  }
   328  
   329  func TestSchema_FingerprintUsingCaches(t *testing.T) {
   330  	schema := NewPrimitiveSchema(String, nil)
   331  
   332  	want, _ := schema.FingerprintUsing(CRC64Avro)
   333  
   334  	got, _ := schema.FingerprintUsing(CRC64Avro)
   335  
   336  	value, ok := schema.cache.Load(CRC64Avro)
   337  	assert.True(t, ok)
   338  	assert.Equal(t, want, value)
   339  	assert.Equal(t, want, got)
   340  }