From 5384b1c5c9027edfd2e732719ba35950048d4853 Mon Sep 17 00:00:00 2001 From: Yuri Rudman Date: Wed, 7 Aug 2019 15:26:09 -0400 Subject: [PATCH] fixed filter_for_schedule to return Hash and added rspec for it as well as fixing different typos --- app/models/miq_widget.rb | 18 +++++++----------- spec/models/miq_widget_spec.rb | 15 +++++++++++---- 2 files changed, 18 insertions(+), 15 deletions(-) diff --git a/app/models/miq_widget.rb b/app/models/miq_widget.rb index 3069089aef9..3fc35c57009 100644 --- a/app/models/miq_widget.rb +++ b/app/models/miq_widget.rb @@ -493,7 +493,7 @@ def self.sync_from_hash(attrs) end def filter_for_schedule - %["=" => {"field" => "MiqWidget-id", "value" => #{id}}] + {"=" => {"field" => "MiqWidget-id", "value" => id}} end def sync_schedule(schedule_info) @@ -515,7 +515,7 @@ def sync_schedule(schedule_info) raise _("Unsupported interval '%{interval}'") % {:interval => interval} end - sched = existing_scheduler + sched = existing_schedule sched ||= MiqSchedule.create!( :name => description, :description => description, @@ -537,20 +537,16 @@ def sync_schedule(schedule_info) sched end - def existing_scheduler + def existing_schedule return nil if (sched = MiqSchedule.find_by(:name => description)).nil? # return existing sheduler if filter referr to the same widget - return sched if sched.filter == filter_for_schedule + return sched if sched.filter.exp == filter_for_schedule # change name of existed schedule in case it is in use - - suff = Time.new.utc.to_s - _log.warn("Schedule #{sched.name} already exists, renaming it to `#{sched.name} #{suff}`") - sched.name = "#{sched.name} #{suff}" - sched.description = "#{sched.description} #{suff}" - sched.save - + suffix = Time.new.utc.to_s + _log.warn("Schedule #{sched.name} already exists, renaming it to `#{sched.name} #{suffix}`") + sched.update(:name => "#{sched.name} #{suffix}", :description => "#{sched.description} #{suffix}") nil end diff --git a/spec/models/miq_widget_spec.rb b/spec/models/miq_widget_spec.rb index 43f08e03cdb..c3e1f182da6 100644 --- a/spec/models/miq_widget_spec.rb +++ b/spec/models/miq_widget_spec.rb @@ -56,11 +56,18 @@ ')) end + describe "#filter_for_schedule" do + it "returns Hash object representing valid MiqExpression" do + exp = MiqExpression.new(@widget_chart_vendor_and_guest_os.filter_for_schedule) + expect(exp.valid?).to be_truthy + end + end + describe "#sync_schedule" do let(:schedule) do filter = @widget_chart_vendor_and_guest_os.filter_for_schedule - FactoryBot.create(:miq_schedule, :filter => filter, - :resource_type => "MiqWidget", :name => @widget_chart_vendor_and_guest_os.name) + FactoryBot.create(:miq_schedule, :filter => MiqExpression.new(filter), :resource_type => "MiqWidget", + :name => @widget_chart_vendor_and_guest_os.name) end it "uses existing schedule if link between widget and schedule broken" do @@ -71,8 +78,8 @@ expect(@widget_chart_vendor_and_guest_os.miq_schedule.id).to eq(schedule.id) end - it "rename existing scheduler by adding timestamp go name if existing scheduler use different filter" do - schedule.update(:filter => "\"=\" => {\"field\" => \"MiqWidget-id\", \"value\" => 9999}") + it "rename existing scheduler by adding timestamp to name if existing scheduler use different filter" do + schedule.update(:filter => MiqExpression.new("=" => {"field" => "MiqWidget-id", "value" => 9999})) time_now = Time.now.utc Timecop.freeze(time_now) { @widget_chart_vendor_and_guest_os.sync_schedule(:run_at => schedule.run_at) }