module PlatformSdk module SpecSupport RSpec.shared_examples "DataPipelineable" do let(:data) do { action: } .merge(record.attributes.symbolize_keys) .merge(record.pipeline_additional_attributes) .except(*record.pipeline_excluded_attributes) end 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(: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 do Timecop.return end context 'after_create callbacks' do let(:action) { 'created' } it "defines an after_create callback" do record.save! expect(data_pipeline_client).to have_received(:post).with(pipeline_payload) end end context 'before_destroy callbacks' do let(:action) { 'destroyed' } it "defines an after_destroy callback" do record.save! record.destroy expect(data_pipeline_client).to have_received(:post).with(pipeline_payload) end end context 'after_update callbacks' do let(:action) { 'modified' } it "defines an after_update callback" do record.save! column = column_to_update(record) update_record(record, column) expect(data_pipeline_client).to have_received(:post).with(pipeline_payload) end end def column_to_update(record) [:string, :integer, :boolean].each do |type| columns_to_update = record.class.columns.select { |c| c.sql_type_metadata.type == type && !c.name.match?(/_type$/) && c.name != 'id' } return columns_to_update.first.name if columns_to_update.any? end 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 end end end