github.com/hamba/avro/v2@v2.22.1-0.20240518180522-aff3955acf7d/config_internal_test.go (about) 1 package avro 2 3 import ( 4 "testing" 5 6 "github.com/modern-go/reflect2" 7 "github.com/stretchr/testify/assert" 8 ) 9 10 func TestConfig_Freeze(t *testing.T) { 11 api := Config{ 12 TagKey: "test", 13 BlockLength: 2, 14 }.Freeze() 15 cfg := api.(*frozenConfig) 16 17 assert.Equal(t, "test", cfg.getTagKey()) 18 assert.Equal(t, 2, cfg.getBlockLength()) 19 } 20 21 func TestConfig_ReusesDecoders(t *testing.T) { 22 type testObj struct { 23 A int64 `avro:"a"` 24 } 25 26 api := Config{ 27 TagKey: "test", 28 BlockLength: 2, 29 }.Freeze() 30 cfg := api.(*frozenConfig) 31 32 schema := MustParse(`{ 33 "type": "record", 34 "name": "test", 35 "fields" : [ 36 {"name": "a", "type": "long"} 37 ] 38 }`) 39 typ := reflect2.TypeOfPtr(&testObj{}) 40 41 dec1 := cfg.DecoderOf(schema, typ) 42 dec2 := cfg.DecoderOf(schema, typ) 43 44 assert.Same(t, dec1, dec2) 45 } 46 47 func TestConfig_ReusesDecoders_WithWriterFingerprint(t *testing.T) { 48 type testObj struct { 49 A int64 `avro:"a"` 50 B string `avro:"b"` 51 } 52 sch := `{ 53 "type": "record", 54 "name": "test", 55 "fields" : [ 56 {"name": "a", "type": "long"}, 57 {"name": "a", "type": "string", "default": "foo"} 58 ] 59 }` 60 typ := reflect2.TypeOfPtr(&testObj{}) 61 62 api := Config{ 63 TagKey: "test", 64 BlockLength: 2, 65 }.Freeze() 66 cfg := api.(*frozenConfig) 67 68 schema1 := MustParse(sch) 69 schema2 := MustParse(sch) 70 fp := [32]byte{1, 2, 3} 71 schema2.(*RecordSchema).writerFingerprint = &fp 72 73 dec1 := cfg.DecoderOf(schema1, typ) 74 dec2 := cfg.DecoderOf(schema2, typ) 75 76 assert.NotSame(t, dec1, dec2) 77 } 78 79 func TestConfig_ReusesDecoders_WithEnum(t *testing.T) { 80 sch := `{ 81 "type": "enum", 82 "name": "test.enum", 83 "symbols": ["foo"], 84 "default": "foo" 85 }` 86 typ := reflect2.TypeOfPtr(new(string)) 87 88 api := Config{ 89 TagKey: "test", 90 BlockLength: 2, 91 }.Freeze() 92 cfg := api.(*frozenConfig) 93 94 schema1 := MustParse(sch) 95 schema2 := MustParse(sch) 96 schema2.(*EnumSchema).encodedSymbols = []string{"foo", "bar"} 97 fp := schema1.Fingerprint() 98 schema2.(*EnumSchema).writerFingerprint = &fp 99 100 dec1 := cfg.DecoderOf(schema1, typ) 101 dec2 := cfg.DecoderOf(schema2, typ) 102 103 assert.NotSame(t, dec1, dec2) 104 } 105 106 func TestConfig_DisableCache_DoesNotReuseDecoders(t *testing.T) { 107 type testObj struct { 108 A int64 `avro:"a"` 109 } 110 111 api := Config{ 112 TagKey: "test", 113 BlockLength: 2, 114 DisableCaching: true, 115 }.Freeze() 116 cfg := api.(*frozenConfig) 117 118 schema := MustParse(`{ 119 "type": "record", 120 "name": "test", 121 "fields" : [ 122 {"name": "a", "type": "long"} 123 ] 124 }`) 125 typ := reflect2.TypeOfPtr(&testObj{}) 126 127 dec1 := cfg.DecoderOf(schema, typ) 128 dec2 := cfg.DecoderOf(schema, typ) 129 130 assert.NotSame(t, dec1, dec2) 131 } 132 133 func TestConfig_ReusesEncoders(t *testing.T) { 134 type testObj struct { 135 A int64 `avro:"a"` 136 } 137 138 api := Config{ 139 TagKey: "test", 140 BlockLength: 2, 141 }.Freeze() 142 cfg := api.(*frozenConfig) 143 144 schema := MustParse(`{ 145 "type": "record", 146 "name": "test", 147 "fields" : [ 148 {"name": "a", "type": "long"} 149 ] 150 }`) 151 typ := reflect2.TypeOfPtr(testObj{}) 152 153 enc1 := cfg.EncoderOf(schema, typ) 154 enc2 := cfg.EncoderOf(schema, typ) 155 156 assert.Same(t, enc1, enc2) 157 } 158 159 func TestConfig_DisableCache_DoesNotReuseEncoders(t *testing.T) { 160 type testObj struct { 161 A int64 `avro:"a"` 162 } 163 164 api := Config{ 165 TagKey: "test", 166 BlockLength: 2, 167 DisableCaching: true, 168 }.Freeze() 169 cfg := api.(*frozenConfig) 170 171 schema := MustParse(`{ 172 "type": "record", 173 "name": "test", 174 "fields" : [ 175 {"name": "a", "type": "long"} 176 ] 177 }`) 178 typ := reflect2.TypeOfPtr(testObj{}) 179 180 enc1 := cfg.EncoderOf(schema, typ) 181 enc2 := cfg.EncoderOf(schema, typ) 182 183 assert.NotSame(t, enc1, enc2) 184 }