From e10c3dc59b3c3d665388a9fd9547ee86a6507acc Mon Sep 17 00:00:00 2001 From: Ahmed Ejaz Date: Fri, 18 Oct 2024 12:31:05 +0500 Subject: [PATCH] add specs for migration --- .../report_rendering_options_factory.rb | 7 +++ ..._item_name_to_product_in_od_report_spec.rb | 49 +++++++++++++++++++ 2 files changed, 56 insertions(+) create mode 100644 spec/factories/report_rendering_options_factory.rb create mode 100644 spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb diff --git a/spec/factories/report_rendering_options_factory.rb b/spec/factories/report_rendering_options_factory.rb new file mode 100644 index 0000000000..928656af30 --- /dev/null +++ b/spec/factories/report_rendering_options_factory.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +FactoryBot.define do + factory :orders_and_distributors_options, class: ReportRenderingOptions do + report_type { "orders_and_distributors" } + end +end diff --git a/spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb b/spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb new file mode 100644 index 0000000000..49c23f48b6 --- /dev/null +++ b/spec/migrations/20241011071014_update_item_name_to_product_in_od_report_spec.rb @@ -0,0 +1,49 @@ +# frozen_string_literal: true + +require 'spec_helper' +require_relative '../../db/migrate/20241011071014_update_item_name_to_product_in_od_report' + +RSpec.describe UpdateItemNameToProductInODReport, type: :migration do + let!(:report_option_without_item_name_product) do + create( + :orders_and_distributors_options, + options: { fields_to_show: ['other_field'] } + ) + end + + describe '#up' do + let!(:report_option_with_item_name) do + create( + :orders_and_distributors_options, + options: { fields_to_show: ['item_name', 'other_field'] } + ) + end + before { subject.up } + + it 'updates fields_to_show from item_name to product only if options have item_name' do + report_option_with_item_name.reload + expect(fields_to_show(report_option_with_item_name)).to eq(['other_field', 'product']) + expect(fields_to_show(report_option_without_item_name_product)).to eq(['other_field']) + end + end + + describe '#down' do + let!(:report_option_with_product) do + create( + :orders_and_distributors_options, + options: { fields_to_show: ['product', 'other_field'] } + ) + end + before { subject.down } + + it 'reverts fields_to_show from product to item_name only if options have product' do + report_option_with_product.reload + expect(fields_to_show(report_option_with_product)).to eq(['other_field', 'item_name']) + expect(fields_to_show(report_option_without_item_name_product)).to eq(['other_field']) + end + end + + def fields_to_show(report_options) + report_options.options[:fields_to_show] + end +end