spec/messaging_spec.rb in avro_turf-1.16.0 vs spec/messaging_spec.rb in avro_turf-1.17.0

- old
+ new

@@ -34,10 +34,28 @@ } ] } AVSC end + + let(:city_message) { { "name" => "Paris" } } + let(:city_schema_json) do + <<-AVSC + { + "name": "city", + "type": "record", + "fields": [ + { + "type": "string", + "name": "name" + } + ] + } + AVSC + end + + let(:city_schema) { Avro::Schema.parse(city_schema_json) } let(:schema) { Avro::Schema.parse(schema_json) } before do FileUtils.mkdir_p("spec/schemas") end @@ -47,10 +65,11 @@ FakeConfluentSchemaRegistryServer.clear end before do define_schema "person.avsc", schema_json + define_schema "city.avsc", city_schema_json end shared_examples_for "encoding and decoding with the schema from schema store" do it "encodes and decodes messages" do data = avro.encode(message, schema_name: "person") @@ -90,10 +109,20 @@ it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry' do expect { avro.encode(message, subject: 'missing', version: 1) }.to raise_error(AvroTurf::SchemaNotFoundError) end + it 'raises AvroTurf::SchemaNotFoundError when the schema does not exist on registry and register_schemas false' do + expect { avro.encode(city_message, schema_name: 'city', register_schemas: false) }. + to raise_error(AvroTurf::SchemaNotFoundError, "Schema with structure: #{city_schema} not found on registry") + end + + it 'encodes with register_schemas false when the schema exists on the registry' do + data = avro.encode(message, schema_name: 'person', register_schemas: false) + expect(avro.decode(data, schema_name: 'person')).to eq message + end + it 'caches parsed schemas for decoding' do data = avro.encode(message, subject: 'person', version: 1) avro.decode(data) allow(Avro::Schema).to receive(:parse).and_call_original expect(avro.decode(data)).to eq message @@ -359,9 +388,37 @@ allow(registry).to receive(:fetch).with(schema_id).and_return(schema_json) end it 'gets schema from registry' do expect(subject).to eq([schema, schema_id]) + end + end + + context 'using fetch_schema_by_body' do + let(:subject_name) { 'city' } + let(:schema_name) { 'city' } + let(:namespace) { 'namespace' } + let(:city_schema_id) { 125 } + let(:city_schema_data) do + { + "subject" => subject_name, + "version" => 123, + "id" => city_schema_id, + "schema" => city_schema + } + end + + subject(:fetch_schema_by_body) do + avro.fetch_schema_by_body(schema_name: schema_name, namespace: namespace, subject: subject_name) + end + + before do + allow(schema_store).to receive(:find).with(schema_name, namespace).and_return(city_schema) + allow(registry).to receive(:check).with(subject_name, city_schema).and_return(city_schema_data) + end + + it 'gets schema from registry' do + expect(fetch_schema_by_body).to eq([city_schema, city_schema_id]) end end context 'using register_schema' do let(:schema_name) { 'schema_name' }