spec/messaging_spec.rb in avro_turf-0.8.1 vs spec/messaging_spec.rb in avro_turf-0.9.0

- old
+ new

@@ -13,22 +13,12 @@ logger: logger ) } let(:message) { { "full_name" => "John Doe" } } - - before do - FileUtils.mkdir_p("spec/schemas") - end - - before do - stub_request(:any, /^#{registry_url}/).to_rack(FakeConfluentSchemaRegistryServer) - FakeConfluentSchemaRegistryServer.clear - end - - before do - define_schema "person.avsc", <<-AVSC + let(:schema_json) do + <<-AVSC { "name": "person", "type": "record", "fields": [ { @@ -38,11 +28,24 @@ ] } AVSC end - shared_examples_for "encoding and decoding" do + before do + FileUtils.mkdir_p("spec/schemas") + end + + before do + stub_request(:any, /^#{registry_url}/).to_rack(FakeConfluentSchemaRegistryServer) + FakeConfluentSchemaRegistryServer.clear + end + + before do + define_schema "person.avsc", 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") expect(avro.decode(data)).to eq message end @@ -58,12 +61,44 @@ expect(avro.decode(data)).to eq message expect(Avro::Schema).not_to have_received(:parse) end end - it_behaves_like "encoding and decoding" + shared_examples_for 'encoding and decoding with the schema from registry' do + before do + registry = AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) + registry.register('person', Avro::Schema.parse(schema_json)) + registry.register('people', Avro::Schema.parse(schema_json)) + end + it 'encodes and decodes messages' do + data = avro.encode(message, subject: 'person', version: 1) + expect(avro.decode(data)).to eq message + end + + it "allows specifying a reader's schema by subject and version" do + data = avro.encode(message, subject: 'person', version: 1) + expect(avro.decode(data, schema_name: 'person')).to eq message + end + + 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 '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 + expect(Avro::Schema).not_to have_received(:parse) + end + end + + it_behaves_like "encoding and decoding with the schema from schema store" + + it_behaves_like 'encoding and decoding with the schema from registry' + context "with a provided registry" do let(:registry) { AvroTurf::ConfluentSchemaRegistry.new(registry_url, logger: logger) } let(:avro) do AvroTurf::Messaging.new( @@ -71,12 +106,14 @@ schemas_path: "spec/schemas", logger: logger ) end - it_behaves_like "encoding and decoding" + it_behaves_like "encoding and decoding with the schema from schema store" + it_behaves_like 'encoding and decoding with the schema from registry' + it "uses the provided registry" do allow(registry).to receive(:register).and_call_original message = { "full_name" => "John Doe" } avro.encode(message, schema_name: "person") expect(registry).to have_received(:register).with("person", anything) @@ -99,10 +136,10 @@ schema_store: schema_store, logger: logger ) end - it_behaves_like "encoding and decoding" + it_behaves_like "encoding and decoding with the schema from schema store" it "uses the provided schema store" do allow(schema_store).to receive(:find).and_call_original avro.encode(message, schema_name: "person") expect(schema_store).to have_received(:find).with("person", nil)