Skip to content

Commit

Permalink
Merge pull request #16978 from eclarizio/BZ1531316
Browse files Browse the repository at this point in the history
Use correct comparison for multi-value default value inclusion rules
(cherry picked from commit 6056dc8)

https://bugzilla.redhat.com/show_bug.cgi?id=1531316
  • Loading branch information
gmcculloug authored and simaishi committed Feb 9, 2018
1 parent 6c851de commit 35c564c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 18 deletions.
14 changes: 14 additions & 0 deletions app/models/dialog_field_drop_down_list.rb
Original file line number Diff line number Diff line change
Expand Up @@ -55,4 +55,18 @@ def automate_key_name
return super unless force_multi_value
MiqAeEngine.create_automation_attribute_array_key(super)
end

private

def default_value_included?(values_list)
if force_multi_value
return false if default_value.blank?
converted_values_list = values_list.collect { |value_pair| value_pair[0].send(value_modifier) }
converted_default_values = JSON.parse(default_value).collect { |value| value.send(value_modifier) }
overlap = converted_values_list & converted_default_values
!overlap.empty?
else
super(values_list)
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -25,14 +25,14 @@
context "when allowed values are given" do
let(:param_options) do
constraint = OrchestrationTemplate::OrchestrationParameterAllowed.new(:allowed_values => %w(val1 val2), :allow_multiple => true)
{:default_value => 'val1', :constraints => [constraint]}
{:default_value => '["val1"]', :constraints => [constraint]}
end

it "creates a dropdown field with pairs of values" do
assert_field(test_field(dialog),
DialogFieldDropDownList,
:name => "param_user",
:default_value => "val1",
:default_value => "[\"val1\"]",
:values => [%w(val1 val1), %w(val2 val2)],
:reconfigurable => true,
:force_multi_value => true)
Expand Down
64 changes: 48 additions & 16 deletions spec/models/dialog_field_drop_down_list_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -136,33 +136,65 @@
end
end

context "#initialize_with_values" do
before(:each) do
@df.values = [%w(3 X), %w(2 Y), %w(1 Z)]
@df.load_values_on_init = true
end
it "#automate_key_name" do
expect(@df.automate_key_name).to eq("dialog_drop_down_list")
end
end

describe "#initialize_with_values" do
let(:dialog_field) { described_class.new(:options => {:force_multi_value => force_multi_value}) }

before do
dialog_field.values = [%w(3 X), %w(2 Y), %w(1 Z)]
dialog_field.load_values_on_init = true
end

context "when force_multi_value is not true" do
let(:force_multi_value) { false }

it "uses the nil as the default value" do
@df.default_value = nil
@df.initialize_with_values({})
expect(@df.value).to eq(nil)
dialog_field.default_value = nil
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq(nil)
end

it "with default value" do
@df.default_value = "1"
@df.initialize_with_values({})
expect(@df.value).to eq("1")
dialog_field.default_value = "1"
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq("1")
end

it "uses the nil when there is a non-matching default value" do
@df.default_value = "4"
@df.initialize_with_values({})
expect(@df.value).to eq(nil)
dialog_field.default_value = "4"
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq(nil)
end
end

it "#automate_key_name" do
expect(@df.automate_key_name).to eq("dialog_drop_down_list")
context "when force_multi_value is true" do
let(:force_multi_value) { true }

context "when the default values are included in the value list" do
before do
dialog_field.default_value = "[\"3\", \"2\"]"
end

it "uses the default value" do
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq("[\"3\", \"2\"]")
end
end

context "when the default values are not included in the value list" do
before do
dialog_field.default_value = "[\"4\"]"
end

it "uses nil" do
dialog_field.initialize_with_values({})
expect(dialog_field.value).to eq(nil)
end
end
end
end

Expand Down

0 comments on commit 35c564c

Please sign in to comment.