module PlatformSdk module SpecSupport RSpec.shared_examples "DataPipelineable" do let(:record) { build(described_class.to_s.underscore.to_sym) } let(:data_pipeline_client) { double(PlatformSdk::DataPipeline::Client) } before do allow(PlatformSdk::DataPipeline::Client).to receive(:new).and_return(data_pipeline_client) allow(data_pipeline_client).to receive(:post) Timecop.freeze(DateTime.parse('2024-06-09 04:20:00')) end after { Timecop.return } context 'after_create callbacks' do before { allow(record).to receive(:send_pipeline_create) } it "defines an after_create callback" do record.save! expect(record).to have_received(:send_pipeline_create) end end context 'before_destroy callbacks' do before do allow(record).to receive(:send_pipeline_create) allow(record).to receive(:send_pipeline_destroy) end it "defines an after_destroy callback" do record.save! record.destroy expect(record).to have_received(:send_pipeline_create) expect(record).to have_received(:send_pipeline_destroy) end end context 'after_update callbacks' do before do allow(record).to receive(:send_pipeline_update) record.save! end it "defines an after_update callback" do column = column_to_update(record) unless column.nil? update_record(record, column) expect(record).to have_received(:send_pipeline_update) end end end def column_to_update(record) %i[string integer boolean].each do |type| columns_to_update = record.class.columns.select { |c| c.sql_type_metadata.type == type && !c.name.match?(/(_id|_type)$/) && c.name != 'id' } return columns_to_update.first.name if columns_to_update.any? end nil end def update_record(record, column) new_record = build(described_class.to_s.underscore.to_sym) column_class = record[column] case column_class when Integer record.update!(column => 69) when [true, false].include?(column_class) record.update!(column => !record[column]) else record.update!(column => new_record[column]) end end describe '#pipeline_payload' do %w[created modified destroyed].each do |action| context "when action is #{action}" do let(:action) { action } let(:pipeline_payload) do { "noun": described_class.pipeline_noun, "identifiers": { "id": record.id }, "meta": described_class.pipeline_meta, "data": data, "envelope_version": '1.0.0', "message_timestamp": Time.current.utc.iso8601 } end let(:data) do record.attributes.symbolize_keys .merge(record.pipeline_additional_attributes) .except(*record.pipeline_excluded_attributes) .merge(action:) end it 'returns the expected payload' do expect(record.pipeline_payload(action)).to eq(pipeline_payload) end end end end end end end