From 069993916c8a321cd605f11290aa04a3d5714d39 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Tue, 11 Apr 2017 12:40:09 +0200 Subject: [PATCH 1/3] Refactor: Extract spec scope and use common before section. --- spec/models/miq_event_definition_spec.rb | 30 ++++++++++++++---------- 1 file changed, 17 insertions(+), 13 deletions(-) diff --git a/spec/models/miq_event_definition_spec.rb b/spec/models/miq_event_definition_spec.rb index c23f30aa1a2..82acbce4cda 100644 --- a/spec/models/miq_event_definition_spec.rb +++ b/spec/models/miq_event_definition_spec.rb @@ -125,24 +125,28 @@ def create_set!(name) expect(event.definition).to be_nil end - it "won't update an event with a definition (keyed as a string)" do - described_class.seed_default_events - name = described_class.first.name - attributes = {"name" => name, "definition" => {:event => {:message => "`rm -rf /super/secret/file`"}}} + context 'with defaults in db' do + before do + described_class.seed_default_events + end - event, = described_class.import_from_hash(attributes) + it "won't update an event with a definition (keyed as a string)" do + name = described_class.first.name + attributes = {"name" => name, "definition" => {:event => {:message => "`rm -rf /super/secret/file`"}}} - expect(event.definition).to be_nil - end + event, = described_class.import_from_hash(attributes) - it "won't update an event with a definition (keyed as a symbol)" do - described_class.seed_default_events - name = described_class.first.name - attributes = {"name" => name, :definition => {:event => {:message => "`rm -rf /super/secret/file`"}}} + expect(event.definition).to be_nil + end - event, = described_class.import_from_hash(attributes) + it "won't update an event with a definition (keyed as a symbol)" do + name = described_class.first.name + attributes = {"name" => name, :definition => {:event => {:message => "`rm -rf /super/secret/file`"}}} - expect(event.definition).to be_nil + event, = described_class.import_from_hash(attributes) + + expect(event.definition).to be_nil + end end end end From 3a17fbd78bacafc445c714fcbcde328ab0d5fa98 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Tue, 11 Apr 2017 12:41:22 +0200 Subject: [PATCH 2/3] Ensure that MiqEventDefinitionSets are in before calling seed_default_events, that will enable us to be more strict within the method -> more validations for db/fixtures Fail early, fail fast. --- spec/models/miq_event_definition_spec.rb | 1 + 1 file changed, 1 insertion(+) diff --git a/spec/models/miq_event_definition_spec.rb b/spec/models/miq_event_definition_spec.rb index 82acbce4cda..8da1063a5af 100644 --- a/spec/models/miq_event_definition_spec.rb +++ b/spec/models/miq_event_definition_spec.rb @@ -127,6 +127,7 @@ def create_set!(name) context 'with defaults in db' do before do + MiqEventDefinitionSet.seed described_class.seed_default_events end From 76d4220dab4e25ec1b0e2e7589ba659ca3c08aee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0imon=20Luka=C5=A1=C3=ADk?= Date: Tue, 11 Apr 2017 11:31:44 +0200 Subject: [PATCH 3/3] Avoid dozens of extra selects in seed_default_events MiqEventDefinitionSet is static union. It is inefficient to use multiple selects instead of a single one. On my system `100.times { MiqEventDefinition.seed_default_events }` gives :gear: | rows | selects | time | % ------ | ----- | ------- | ---- | --- Before | 16900 | 16900 | 204s | 100% After | 1300 | 100 | 93s | 45% --- app/models/miq_event_definition.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/app/models/miq_event_definition.rb b/app/models/miq_event_definition.rb index a7d4f5d8f0d..9ffcd9b995f 100644 --- a/app/models/miq_event_definition.rb +++ b/app/models/miq_event_definition.rb @@ -122,6 +122,7 @@ def self.seed end def self.seed_default_events + event_sets = MiqEventDefinitionSet.all.index_by(&:name) fname = File.join(FIXTURE_DIR, "#{to_s.pluralize.underscore}.csv") CSV.foreach(fname, :headers => true, :skip_lines => /^#/, :skip_blanks => true) do |csv_row| event = csv_row.to_hash @@ -139,7 +140,7 @@ def self.seed_default_events end end - es = MiqEventDefinitionSet.find_by(:name => set_type) + es = event_sets[set_type] rec.memberof.each { |old_set| rec.make_not_memberof(old_set) unless old_set == es } # handle changes in set membership es.add_member(rec) if es && !es.members.include?(rec) end