From a4e86f00c78389bdc550b5b8c84a27c48cd6d950 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Fri, 26 Jul 2024 12:13:04 -0400 Subject: [PATCH 1/2] Update MiqAeMethod specs to remove @ variables --- spec/factories/miq_ae_method.rb | 1 + spec/models/miq_ae_method_spec.rb | 114 ++++++++++++------------------ 2 files changed, 47 insertions(+), 68 deletions(-) diff --git a/spec/factories/miq_ae_method.rb b/spec/factories/miq_ae_method.rb index 16920263483..3a0e247b46c 100644 --- a/spec/factories/miq_ae_method.rb +++ b/spec/factories/miq_ae_method.rb @@ -3,6 +3,7 @@ sequence(:name) { |n| "miq_ae_method#{seq_padded_for_sorting(n)}" } language { "ruby" } location { "inline" } + scope { "instance" } transient do params { {} } diff --git a/spec/models/miq_ae_method_spec.rb b/spec/models/miq_ae_method_spec.rb index 0e4af5e7ce7..d67a7d31722 100644 --- a/spec/models/miq_ae_method_spec.rb +++ b/spec/models/miq_ae_method_spec.rb @@ -1,52 +1,44 @@ RSpec.describe MiqAeMethod do let(:user) { FactoryBot.create(:user_with_group) } - it "should return editable as false if the parent namespace/class is not editable" do - n1 = FactoryBot.create(:miq_ae_system_domain, :tenant => user.current_tenant) - c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo") - f1 = FactoryBot.create(:miq_ae_method, - :class_id => c1.id, - :name => "foo_method", - :scope => "instance", - :language => "ruby", - :location => "inline") - expect(f1.editable?(user)).to be_falsey - end + let(:sys_domain) { FactoryBot.create(:miq_ae_system_domain) } + let(:domain) { FactoryBot.create(:miq_ae_domain) } + let(:sub_domain) { FactoryBot.create(:miq_ae_namespace, :parent => domain) } - it "should return editable as true if the parent namespace/class is editable" do - n1 = FactoryBot.create(:miq_ae_domain, :tenant => user.current_tenant) - c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo") - f1 = FactoryBot.create(:miq_ae_method, - :class_id => c1.id, - :name => "foo_method", - :scope => "instance", - :language => "ruby", - :location => "inline") - expect(f1.editable?(user)).to be_truthy + let(:sys_class) { FactoryBot.create(:miq_ae_class, :namespace_id => sys_domain.id) } + let(:reg_class) { FactoryBot.create(:miq_ae_class, :namespace_id => domain.id) } + let(:sub_class) { FactoryBot.create(:miq_ae_class, :namespace_id => sub_domain.id) } + + describe "#editable" do + it "should return editable as false if the parent namespace/class is not editable" do + f1 = FactoryBot.create(:miq_ae_method, + :class_id => sys_class.id, + :name => "foo_method") + expect(f1.editable?(user)).to be_falsey + end + + it "should return editable as true if the parent namespace/class is editable" do + f1 = FactoryBot.create(:miq_ae_method, + :class_id => reg_class.id, + :name => "foo_method") + expect(f1.editable?(user)).to be_truthy + end end - it "should lookup method" do - n1 = FactoryBot.create(:miq_ae_system_domain, :tenant => user.current_tenant) - c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo") - f1 = FactoryBot.create(:miq_ae_method, - :class_id => c1.id, - :name => "foo_method", - :scope => "instance", - :language => "ruby", - :location => "inline") - expect(f1.editable?(user)).to be_falsey - - expect(MiqAeMethod.lookup_by_class_id_and_name(c1.id, "foo_method")).to eq(f1) + describe ".lookup_by_class_id_and_name" do + it "should lookup method" do + f1 = FactoryBot.create(:miq_ae_method, + :class_id => sys_class.id, + :name => "foo_method") + expect(f1.editable?(user)).to be_falsey + + expect(MiqAeMethod.lookup_by_class_id_and_name(sys_class.id, "foo_method")).to eq(f1) + end end it "doesn’t access database when unchanged model is saved" do - n1 = FactoryBot.create(:miq_ae_system_domain, :tenant => user.current_tenant) - c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo") f1 = FactoryBot.create(:miq_ae_method, - :class_id => c1.id, - :name => "foo_method", - :scope => "instance", - :language => "ruby", - :location => "inline") + :class_id => reg_class.id, + :name => "foo_method") expect { f1.valid? }.not_to make_database_queries end @@ -85,15 +77,13 @@ end context "#copy" do - let(:d2) { FactoryBot.create(:miq_ae_domain, :name => "domain2", :priority => 2) } - let(:ns1) { FactoryBot.create(:miq_ae_namespace, :name => "ns1", :parent => @d1) } - let(:m1) { FactoryBot.create(:miq_ae_method, :class_id => @cls1.id, :name => "foo_method1", :scope => "instance", :language => "ruby", :location => "inline") } - let(:m2) { FactoryBot.create(:miq_ae_method, :class_id => @cls1.id, :name => "foo_method2", :scope => "instance", :language => "ruby", :location => "inline") } - before do - @d1 = FactoryBot.create(:miq_ae_domain, :name => "domain1", :parent => nil, :priority => 1) - @cls1 = FactoryBot.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id) - @ns2 = FactoryBot.create(:miq_ae_namespace, :name => "ns2", :parent => d2) - end + let(:d1) { FactoryBot.create(:miq_ae_domain, :priority => 1) } + let(:d2) { FactoryBot.create(:miq_ae_domain, :priority => 2) } + let(:ns1) { FactoryBot.create(:miq_ae_namespace, :parent => d1) } + let(:ns2) { FactoryBot.create(:miq_ae_namespace, :parent => d2) } + let(:m1) { FactoryBot.create(:miq_ae_method, :class_id => cls1.id, :scope => "instance") } + let(:m2) { FactoryBot.create(:miq_ae_method, :class_id => cls1.id, :scope => "instance") } + let(:cls1) { FactoryBot.create(:miq_ae_class, :name => "cls1", :namespace_id => ns1.id) } it "copies instances under specified namespace" do options = { @@ -109,7 +99,7 @@ it "copy instances under same namespace raise error when class exists" do options = { - :domain => @d1.name, + :domain => d1.name, :namespace => ns1.fqname, :overwrite_location => false, :ids => [m1.id, m2.id] @@ -120,7 +110,7 @@ it "replaces instances under same namespace when class exists" do options = { :domain => d2.name, - :namespace => @ns2.name, + :namespace => ns2.name, :overwrite_location => true, :ids => [m1.id, m2.id] } @@ -162,28 +152,16 @@ end it "#domain" do - d1 = FactoryBot.create(:miq_ae_system_domain, :name => 'dom1', :priority => 10) - n1 = FactoryBot.create(:miq_ae_namespace, :name => 'ns1', :parent => d1) - c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo") m1 = FactoryBot.create(:miq_ae_method, - :class_id => c1.id, - :name => "foo_method", - :scope => "instance", - :language => "ruby", - :location => "inline") - expect(m1.domain.name).to eql('dom1') + :class_id => sub_class.id, + :name => "foo_method") + expect(m1.domain).to eql(domain) end it "#to_export_yaml" do - d1 = FactoryBot.create(:miq_ae_system_domain, :name => 'dom1', :priority => 10) - n1 = FactoryBot.create(:miq_ae_namespace, :name => 'ns1', :parent => d1) - c1 = FactoryBot.create(:miq_ae_class, :namespace_id => n1.id, :name => "foo") m1 = FactoryBot.create(:miq_ae_method, - :class_id => c1.id, - :name => "foo_method", - :scope => "instance", - :language => "ruby", - :location => "inline") + :class_id => sub_class.id, + :name => "foo_method") result = m1.to_export_yaml expect(result['name']).to eql('foo_method') From 5d2ddfe7d268fa1a78a2eb4398da3e70eb132e6e Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Thu, 25 Jul 2024 09:42:18 -0400 Subject: [PATCH 2/2] Add MiqAeMethod scopes for name and path search --- app/models/miq_ae_method.rb | 6 ++++++ spec/models/miq_ae_method_spec.rb | 30 ++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/app/models/miq_ae_method.rb b/app/models/miq_ae_method.rb index 6a4ea5d3295..64b4fbcc962 100644 --- a/app/models/miq_ae_method.rb +++ b/app/models/miq_ae_method.rb @@ -30,6 +30,12 @@ class MiqAeMethod < ApplicationRecord AVAILABLE_SCOPES = ["class", "instance"] validates_inclusion_of :scope, :in => AVAILABLE_SCOPES + # finds by name or namespace. not domain + # @param [nil,String] search + scope :name_path_search, lambda { |search| + search.present? ? where(arel_table[:relative_path].matches("%#{search}%")) : where({}) + } + def self.available_languages AVAILABLE_LANGUAGES end diff --git a/spec/models/miq_ae_method_spec.rb b/spec/models/miq_ae_method_spec.rb index d67a7d31722..2136790d674 100644 --- a/spec/models/miq_ae_method_spec.rb +++ b/spec/models/miq_ae_method_spec.rb @@ -170,4 +170,34 @@ expect(keys.exclude?('options')).to be_truthy expect(keys.exclude?('embedded_methods')).to be_truthy end + + describe ".name_path_search" do + it "matches name" do + m1 = FactoryBot.create(:miq_ae_method, "name" => "match", :class_id => sub_class.id) + FactoryBot.create(:miq_ae_method, "name" => "nope", :class_id => sys_class.id) + + expect(MiqAeMethod.name_path_search("match")).to eq([m1]) + end + + it "matches a name with a %" do + m1 = FactoryBot.create(:miq_ae_method, "name" => "10_is_bigger", :class_id => sub_class.id) + FactoryBot.create(:miq_ae_method, "name" => "nope", :class_id => sys_class.id) + + expect(MiqAeMethod.name_path_search("10%_big")).to eq([m1]) + end + + it "matches path" do + m1 = FactoryBot.create(:miq_ae_method, "name" => "match", :class_id => sub_class.id) + FactoryBot.create(:miq_ae_method, "name" => "nope", :class_id => sys_class.id) + + expect(MiqAeMethod.name_path_search(sub_domain.name)).to eq([m1]) + end + + it "searches all when blank" do + m1 = FactoryBot.create(:miq_ae_method, "name" => "match", :class_id => sub_class.id) + m2 = FactoryBot.create(:miq_ae_method, "name" => "nope", :class_id => sys_class.id) + + expect(MiqAeMethod.name_path_search(nil)).to match_array([m1, m2]) + end + end end