github.com/hamba/avro/v2@v2.22.1-0.20240518180522-aff3955acf7d/cmd/avrosv/main_test.go (about) 1 package main 2 3 import ( 4 "bytes" 5 "io" 6 "testing" 7 8 "github.com/hamba/avro/v2" 9 "github.com/stretchr/testify/assert" 10 ) 11 12 func TestAvroSv_RequiredFlags(t *testing.T) { 13 tests := []struct { 14 name string 15 args []string 16 wantExitCode int 17 }{ 18 { 19 name: "validates no schema is set", 20 args: []string{"avrosv"}, 21 wantExitCode: 1, 22 }, 23 { 24 name: "validates single schema is set", 25 args: []string{"avrosv", "some/file"}, 26 wantExitCode: 2, 27 }, 28 { 29 name: "validates multiple schemas are set", 30 args: []string{"avrosv", "some/file", "some/other"}, 31 wantExitCode: 2, 32 }, 33 } 34 35 for _, test := range tests { 36 test := test 37 t.Run(test.name, func(t *testing.T) { 38 got := realMain(test.args, io.Discard, io.Discard) 39 40 assert.Equal(t, test.wantExitCode, got) 41 }) 42 } 43 } 44 45 func TestAvroSv_ValidatesSchema(t *testing.T) { 46 tests := []struct { 47 name string 48 args []string 49 wantExitCode int 50 }{ 51 { 52 name: "validates a simple schema", 53 args: []string{"avrosv", "testdata/schema.avsc"}, 54 wantExitCode: 0, 55 }, 56 { 57 name: "does not validate a bad schema", 58 args: []string{"avrosv", "testdata/bad-schema.avsc"}, 59 wantExitCode: 2, 60 }, 61 { 62 name: "does not validate a schema with a bad default", 63 args: []string{"avrosv", "testdata/bad-default-schema.avsc"}, 64 wantExitCode: 2, 65 }, 66 { 67 name: "does not validate a schema with a reference to a missing schema", 68 args: []string{"avrosv", "testdata/withref-schema.avsc"}, 69 wantExitCode: 2, 70 }, 71 { 72 name: "validates a schema with a reference to an existing schema", 73 args: []string{"avrosv", "testdata/schema.avsc", "testdata/withref-schema.avsc"}, 74 wantExitCode: 0, 75 }, 76 } 77 78 for _, test := range tests { 79 test := test 80 t.Run(test.name, func(t *testing.T) { 81 avro.DefaultSchemaCache = &avro.SchemaCache{} // reset the schema cache 82 got := realMain(test.args, io.Discard, io.Discard) 83 84 assert.Equal(t, test.wantExitCode, got) 85 }) 86 } 87 } 88 89 func TestAvroSv_Verbose(t *testing.T) { 90 tests := []struct { 91 name string 92 args []string 93 wantStdout string 94 wantExitCode int 95 }{ 96 { 97 name: "dumps a simple schema", 98 args: []string{"avrosv", "-v", "testdata/schema.avsc"}, 99 wantStdout: "{\"name\":\"test\",\"type\":\"record\",\"fields\":[{\"name\":\"someString\",\"type\":\"string\"}]}\n", 100 wantExitCode: 0, 101 }, 102 { 103 name: "dumps a schema with a reference to an existing schema", 104 args: []string{"avrosv", "-v", "testdata/schema.avsc", "testdata/withref-schema.avsc"}, 105 wantStdout: "{\"name\":\"testref\",\"type\":\"record\",\"fields\":[{\"name\":\"someref\",\"type\":{\"name\":\"test\",\"type\":\"record\",\"fields\":[{\"name\":\"someString\",\"type\":\"string\"}]}}]}\n", 106 wantExitCode: 0, 107 }, 108 { 109 name: "does not dump any schema when the schema file is invalid", 110 args: []string{"avrosv", "-v", "testdata/bad-schema.avsc"}, 111 wantStdout: "", 112 wantExitCode: 2, 113 }, 114 } 115 116 for _, test := range tests { 117 test := test 118 t.Run(test.name, func(t *testing.T) { 119 var buf bytes.Buffer 120 121 avro.DefaultSchemaCache = &avro.SchemaCache{} // reset the schema cache 122 123 got := realMain(test.args, &buf, io.Discard) 124 125 assert.Equal(t, test.wantStdout, buf.String()) 126 assert.Equal(t, test.wantExitCode, got) 127 }) 128 } 129 }