diff options
author | danielsdeleo <dan@opscode.com> | 2012-10-19 16:14:31 -0700 |
---|---|---|
committer | danielsdeleo <dan@opscode.com> | 2012-10-19 16:14:31 -0700 |
commit | 06b4061054c2c1226cb1b0512e8e826ff1b721c9 (patch) | |
tree | 253db91ee0679bf3e84c61c9554c862946ea141b /chef/spec | |
parent | d90433ae23ec93dc9e9804c721911462c7a1b331 (diff) | |
parent | bf103e81ee679d6a24cab0b6c100e069abf4eb78 (diff) | |
download | chef-06b4061054c2c1226cb1b0512e8e826ff1b721c9.tar.gz |
Merge branch 'CHEF-2737'
Diffstat (limited to 'chef/spec')
-rw-r--r-- | chef/spec/unit/mixin/deep_merge_spec.rb | 510 |
1 files changed, 19 insertions, 491 deletions
diff --git a/chef/spec/unit/mixin/deep_merge_spec.rb b/chef/spec/unit/mixin/deep_merge_spec.rb index 21be4974c6..cbc9b1544f 100644 --- a/chef/spec/unit/mixin/deep_merge_spec.rb +++ b/chef/spec/unit/mixin/deep_merge_spec.rb @@ -36,14 +36,14 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do it "tests merging an hash w/array into blank hash" do hash_src = {'id' => '2'} hash_dst = {} - @dm.deep_merge!(hash_src.dup, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) + @dm.deep_merge!(hash_src.dup, hash_dst) hash_dst.should == hash_src end it "tests merging an hash w/array into blank hash" do hash_src = {'region' => {'id' => ['227', '2']}} hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) + @dm.deep_merge!(hash_src, hash_dst) hash_dst.should == hash_src end @@ -82,13 +82,6 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do hash_dst.should == {"property" => ["2","4","1","3"]} end - it "tests hashes holding array (sorted)" do - hash_src = {"property" => ["1","3"]} - hash_dst = {"property" => ["2","4"]} - @dm.deep_merge!(hash_src, hash_dst, {:sort_merged_arrays => true}) - hash_dst.should == {"property" => ["1","2","3","4"]} - end - it "tests hashes holding hashes holding arrays (array with duplicate elements is merged with dest then src" do hash_src = {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["1", "4+"]}} hash_dst = {"property" => {"bedroom_count" => ["3", "2"], "bathroom_count" => ["2"]}} @@ -103,13 +96,6 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do hash_dst.should == {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["2","1","4+"]}} end - it "tests hash holding hash holding array v string (string is NOT overwritten by array)" do - hash_src = {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["1", "4+"]}} - hash_dst = {"property" => {"bedroom_count" => "3", "bathroom_count" => ["2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true}) - hash_dst.should == {"property" => {"bedroom_count" => "3", "bathroom_count" => ["2","1","4+"]}} - end - it "tests hash holding hash holding string v array (array is overwritten by string)" do hash_src = {"property" => {"bedroom_count" => "3", "bathroom_count" => ["1", "4+"]}} hash_dst = {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["2"]}} @@ -117,13 +103,6 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do hash_dst.should == {"property" => {"bedroom_count" => "3", "bathroom_count" => ["2","1","4+"]}} end - it "tests hash holding hash holding string v array (array does NOT overwrite string)" do - hash_src = {"property" => {"bedroom_count" => "3", "bathroom_count" => ["1", "4+"]}} - hash_dst = {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true}) - hash_dst.should == {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["2","1","4+"]}} - end - it "tests hash holding hash holding hash v array (array is overwritten by hash)" do hash_src = {"property" => {"bedroom_count" => {"king_bed" => 3, "queen_bed" => 1}, "bathroom_count" => ["1", "4+"]}} hash_dst = {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["2"]}} @@ -131,13 +110,6 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do hash_dst.should == {"property" => {"bedroom_count" => {"king_bed" => 3, "queen_bed" => 1}, "bathroom_count" => ["2","1","4+"]}} end - it "tests hash holding hash holding hash v array (array is NOT overwritten by hash)" do - hash_src = {"property" => {"bedroom_count" => {"king_bed" => 3, "queen_bed" => 1}, "bathroom_count" => ["1", "4+"]}} - hash_dst = {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true}) - hash_dst.should == {"property" => {"bedroom_count" => ["1", "2"], "bathroom_count" => ["2","1","4+"]}} - end - it "tests 3 hash layers holding integers (integers are overwritten by source)" do hash_src = {"property" => {"bedroom_count" => {"king_bed" => 3, "queen_bed" => 1}, "bathroom_count" => ["1", "4+"]}} hash_dst = {"property" => {"bedroom_count" => {"king_bed" => 2, "queen_bed" => 4}, "bathroom_count" => ["2"]}} @@ -159,13 +131,6 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do hash_dst.should == {"property" => "1"} end - it "tests 1 hash NOT overwriting 3 hash layers holding arrays of int" do - hash_src = {"property" => "1"} - hash_dst = {"property" => {"bedroom_count" => {"king_bed" => [2], "queen_bed" => [4]}, "bathroom_count" => ["2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true}) - hash_dst.should == {"property" => {"bedroom_count" => {"king_bed" => [2], "queen_bed" => [4]}, "bathroom_count" => ["2"]}} - end - it "tests 3 hash layers holding arrays of int (arrays are merged) but second hash's array is overwritten" do hash_src = {"property" => {"bedroom_count" => {"king_bed" => [3], "queen_bed" => [1]}, "bathroom_count" => "1"}} hash_dst = {"property" => {"bedroom_count" => {"king_bed" => [2], "queen_bed" => [4]}, "bathroom_count" => ["2"]}} @@ -173,13 +138,6 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do hash_dst.should == {"property" => {"bedroom_count" => {"king_bed" => [2,3], "queen_bed" => [4,1]}, "bathroom_count" => "1"}} end - it "tests 3 hash layers holding arrays of int (arrays are merged) but second hash's array is NOT overwritten" do - hash_src = {"property" => {"bedroom_count" => {"king_bed" => [3], "queen_bed" => [1]}, "bathroom_count" => "1"}} - hash_dst = {"property" => {"bedroom_count" => {"king_bed" => [2], "queen_bed" => [4]}, "bathroom_count" => ["2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true}) - hash_dst.should == {"property" => {"bedroom_count" => {"king_bed" => [2,3], "queen_bed" => [4,1]}, "bathroom_count" => ["2"]}} - end - it "tests 3 hash layers holding arrays of int, but one holds int. This one overwrites, but the rest merge" do hash_src = {"property" => {"bedroom_count" => {"king_bed" => 3, "queen_bed" => [1]}, "bathroom_count" => ["1"]}} hash_dst = {"property" => {"bedroom_count" => {"king_bed" => [2], "queen_bed" => [4]}, "bathroom_count" => ["2"]}} @@ -215,31 +173,6 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do hash_dst.should == {"property" => {"bedroom_count" => {2=>3, "king_bed" => [3]}, "bathroom_count" => ["1"]}} end - it "tests parameter management for knockout_prefix and overwrite unmergable" do - hash_src = {"x" => 1} - hash_dst = {"y" => 2} - - lambda { - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => ""}) - }.should raise_error(Chef::Mixin::DeepMerge::InvalidParameter) - - lambda { - @dm.deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true, :knockout_prefix => ""}) - }.should raise_error(Chef::Mixin::DeepMerge::InvalidParameter) - - lambda { - @dm.deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true, :knockout_prefix => @field_ko_prefix}) - }.should raise_error(Chef::Mixin::DeepMerge::InvalidParameter) - - lambda { - @dm.deep_merge!(@dm.deep_merge!(hash_src, hash_dst)) - }.should_not raise_error(Chef::Mixin::DeepMerge::InvalidParameter) - - lambda { - @dm.deep_merge!(hash_src, hash_dst, {:preserve_unmergeables => true}) - }.should_not raise_error(Chef::Mixin::DeepMerge::InvalidParameter) - end - it "tests hash holding arrays of arrays" do hash_src = {["1", "2", "3"] => ["1", "2"]} hash_dst = {["4", "5"] => ["3"]} @@ -247,421 +180,27 @@ describe Chef::Mixin::DeepMerge, "deep_merge!" do hash_dst.should == {["1","2","3"] => ["1", "2"], ["4", "5"] => ["3"]} end - it "tests merging of hash with blank hash, and make sure that source array split still functions" do - hash_src = {'property' => {'bedroom_count' => ["1","2,3"]}} - hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'property' => {'bedroom_count' => ["1","2","3"]}} - end - it "tests merging of hash with blank hash, and make sure that source array split does not function when turned off" do hash_src = {'property' => {'bedroom_count' => ["1","2,3"]}} hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) + @dm.deep_merge!(hash_src, hash_dst) hash_dst.should == {'property' => {'bedroom_count' => ["1","2,3"]}} end - it "tests merging into a blank hash with overwrite_unmergeables turned on" do + it "tests merging into a blank hash" do hash_src = {"action"=>"browse", "controller"=>"results"} hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == hash_src - end - - # KNOCKOUT_PREFIX testing - # the next few tests are looking for correct behavior from specific real-world params/session merges - # using the custom modifiers built for param/session merges - - [nil, ","].each do |ko_split| - it "tests typical params/session style hash with knockout_merge elements" do - hash_src = {"property"=>{"bedroom_count"=>[@field_ko_prefix+":1", "2", "3"]}} - hash_dst = {"property"=>{"bedroom_count"=>["1", "2", "3"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ko_split}) - hash_dst.should == {"property"=>{"bedroom_count"=>["2", "3"]}} - end - - it "tests typical params/session style hash with knockout_merge elements" do - hash_src = {"property"=>{"bedroom_count"=>[@field_ko_prefix+":1", "2", "3"]}} - hash_dst = {"property"=>{"bedroom_count"=>["3"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ko_split}) - hash_dst.should == {"property"=>{"bedroom_count"=>["3","2"]}} - end - - it "tests typical params/session style hash with knockout_merge elements" do - hash_src = {"property"=>{"bedroom_count"=>[@field_ko_prefix+":1", "2", "3"]}} - hash_dst = {"property"=>{"bedroom_count"=>["4"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ko_split}) - hash_dst.should == {"property"=>{"bedroom_count"=>["4","2","3"]}} - end - - it "tests typical params/session style hash with knockout_merge elements" do - hash_src = {"property"=>{"bedroom_count"=>[@field_ko_prefix+":1", "2", "3"]}} - hash_dst = {"property"=>{"bedroom_count"=>[@field_ko_prefix+":1", "4"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ko_split}) - hash_dst.should == {"property"=>{"bedroom_count"=>["4","2","3"]}} - end - - it "tests typical params/session style hash with knockout_merge elements" do - hash_src = {"amenity"=>{"id"=>[@field_ko_prefix+":1", @field_ko_prefix+":2", "3", "4"]}} - hash_dst = {"amenity"=>{"id"=>["1", "2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ko_split}) - hash_dst.should == {"amenity"=>{"id"=>["3","4"]}} - end - end - - it "tests special params/session style hash with knockout_merge elements in form src: [\"1\",\"2\"] dest:[\"@ko:1,@ko:2\", \"3,4\"]" do - hash_src = {"amenity"=>{"id"=>[@field_ko_prefix+":1,"+@field_ko_prefix+":2", "3,4"]}} - hash_dst = {"amenity"=>{"id"=>["1", "2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"amenity"=>{"id"=>["3","4"]}} - end - - it "tests same as previous but without ko_split value, this merge should fail" do - hash_src = {"amenity"=>{"id"=>[@field_ko_prefix+":1,"+@field_ko_prefix+":2", "3,4"]}} - hash_dst = {"amenity"=>{"id"=>["1", "2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) - hash_dst.should == {"amenity"=>{"id"=>["1","2","3,4"]}} - end - - it "tests special params/session style hash with knockout_merge elements in form src: [\"1\",\"2\"] dest:[\"@ko:1,@ko:2\", \"3,4\"]" do - hash_src = {"amenity"=>{"id"=>[@field_ko_prefix+":1,2", "3,4", @field_ko_prefix+":5", "6"]}} - hash_dst = {"amenity"=>{"id"=>["1", "2"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"amenity"=>{"id"=>["2","3","4","6"]}} - end - - it "tests special params/session style hash with knockout_merge elements in form src: [\"@ko:1,@ko:2\", \"3,4\", \"@ko:5\", \"6\"] dest:[\"1,2\", \"3,4\"]" do - hash_src = {"amenity"=>{"id"=>["#{@field_ko_prefix}:1,#{@field_ko_prefix}:2", "3,4", "#{@field_ko_prefix}:5", "6"]}} - hash_dst = {"amenity"=>{"id"=>["1", "2", "3", "4"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"amenity"=>{"id"=>["3","4","6"]}} - end - - it "unamed upstream - tbd" do - hash_src = {"url_regions"=>[], "region"=>{"ids"=>["227,233"]}, "action"=>"browse", "task"=>"browse", "controller"=>"results"} - hash_dst = {"region"=>{"ids"=>["227"]}} - @dm.deep_merge!(hash_src.dup, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"url_regions"=>[], "region"=>{"ids"=>["227","233"]}, "action"=>"browse", "task"=>"browse", "controller"=>"results"} - end - - it "unamed upstream - tbd" do - hash_src = {"region"=>{"ids"=>[@field_ko_prefix,"227"], "id"=>"230"}} - hash_dst = {"region"=>{"ids"=>["227", "233", "324", "230", "230"], "id"=>"230"}} - @dm.deep_merge!(hash_src, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"region"=>{"ids"=>["227"], "id"=>"230"}} - end - - it "unamed upstream - tbd" do - hash_src = {"region"=>{"ids"=>[@field_ko_prefix,"227", "232", "233"], "id"=>"232"}} - hash_dst = {"region"=>{"ids"=>["227", "233", "324", "230", "230"], "id"=>"230"}} - @dm.deep_merge!(hash_src, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"region"=>{"ids"=>["227", "232", "233"], "id"=>"232"}} - end - - it "unamed upstream - tbd" do - hash_src = {"region"=>{"ids"=>["#{@field_ko_prefix},227,232,233"], "id"=>"232"}} - hash_dst = {"region"=>{"ids"=>["227", "233", "324", "230", "230"], "id"=>"230"}} - @dm.deep_merge!(hash_src, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"region"=>{"ids"=>["227", "232", "233"], "id"=>"232"}} - end - - it "unamed upstream - tbd" do - hash_src = {"region"=>{"ids"=>["#{@field_ko_prefix},227,232","233"], "id"=>"232"}} - hash_dst = {"region"=>{"ids"=>["227", "233", "324", "230", "230"], "id"=>"230"}} - @dm.deep_merge!(hash_src, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"region"=>{"ids"=>["227", "232", "233"], "id"=>"232"}} - end - - it "unamed upstream - tbd" do - hash_src = {"region"=>{"ids"=>["#{@field_ko_prefix},227"], "id"=>"230"}} - hash_dst = {"region"=>{"ids"=>["227", "233", "324", "230", "230"], "id"=>"230"}} - @dm.deep_merge!(hash_src, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"region"=>{"ids"=>["227"], "id"=>"230"}} - end - - it "unamed upstream - tbd" do - hash_src = {"region"=>{"ids"=>["#{@field_ko_prefix},227"], "id"=>"230"}} - hash_dst = {"region"=>{"ids"=>["227", "233", "324", "230", "230"], "id"=>"230"}, "action"=>"browse", "task"=>"browse", "controller"=>"results", "property_order_by"=>"property_type.descr"} - @dm.deep_merge!(hash_src, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"region"=>{"ids"=>["227"], "id"=>"230"}, "action"=>"browse", "task"=>"browse", - "controller"=>"results", "property_order_by"=>"property_type.descr"} - end - - it "unamed upstream - tbd" do - hash_src = {"query_uuid"=>"6386333d-389b-ab5c-8943-6f3a2aa914d7", "region"=>{"ids"=>["#{@field_ko_prefix},227"], "id"=>"230"}} - hash_dst = {"query_uuid"=>"6386333d-389b-ab5c-8943-6f3a2aa914d7", "url_regions"=>[], "region"=>{"ids"=>["227", "233", "324", "230", "230"], "id"=>"230"}, "action"=>"browse", "task"=>"browse", "controller"=>"results", "property_order_by"=>"property_type.descr"} - @dm.deep_merge!(hash_src, hash_dst, {:overwrite_unmergeables => true, :knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"query_uuid" => "6386333d-389b-ab5c-8943-6f3a2aa914d7", "url_regions"=>[], - "region"=>{"ids"=>["227"], "id"=>"230"}, "action"=>"browse", "task"=>"browse", - "controller"=>"results", "property_order_by"=>"property_type.descr"} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => @field_ko_prefix} - hash_dst = {"amenity" => "1"} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => ""} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => [@field_ko_prefix]} - hash_dst = {"amenity" => "1"} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => []} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => @field_ko_prefix} - hash_dst = {"amenity" => ["1"]} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => ""} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => [@field_ko_prefix]} - hash_dst = {"amenity" => ["1"]} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => []} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => [@field_ko_prefix]} - hash_dst = {"amenity" => "1"} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => []} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => [@field_ko_prefix, "2"]} - hash_dst = {'amenity' => ["1", "3", "7+"]} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => ["2"]} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => [@field_ko_prefix, "2"]} - hash_dst = {'amenity' => "5"} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => ['2']} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => @field_ko_prefix} - hash_dst = {"amenity"=>{"id"=>["1", "2", "3", "4"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => ""} - end - - it "tests knock out entire dest hash if \"@ko\" is passed for source" do - hash_src = {'amenity' => [@field_ko_prefix]} - hash_dst = {"amenity"=>{"id"=>["1", "2", "3", "4"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'amenity' => []} - end - - it "tests knock out dest array if \"@ko\" is passed for source" do - hash_src = {"region" => {'ids' => @field_ko_prefix}} - hash_dst = {"region"=>{"ids"=>["1", "2", "3", "4"]}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' => {'ids' => ""}} - end - - it "tests knock out dest array but leave other elements of hash intact" do - hash_src = {"region" => {'ids' => @field_ko_prefix}} - hash_dst = {"region"=>{"ids"=>["1", "2", "3", "4"], 'id'=>'11'}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' => {'ids' => "", 'id'=>'11'}} - end - - it "tests knock out entire tree of dest hash" do - hash_src = {"region" => @field_ko_prefix} - hash_dst = {"region"=>{"ids"=>["1", "2", "3", "4"], 'id'=>'11'}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' => ""} - end - - it "tests knock out entire tree of dest hash - retaining array format" do - hash_src = {"region" => {'ids' => [@field_ko_prefix]}} - hash_dst = {"region"=>{"ids"=>["1", "2", "3", "4"], 'id'=>'11'}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' => {'ids' => [], 'id'=>'11'}} - end - - it "tests knock out entire tree of dest hash & replace with new content" do - hash_src = {"region" => {'ids' => ["2", @field_ko_prefix, "6"]}} - hash_dst = {"region"=>{"ids"=>["1", "2", "3", "4"], 'id'=>'11'}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' => {'ids' => ["2", "6"], 'id'=>'11'}} - end - - it "tests knock out entire tree of dest hash & replace with new content" do - hash_src = {"region" => {'ids' => ["7", @field_ko_prefix, "6"]}} - hash_dst = {"region"=>{"ids"=>["1", "2", "3", "4"], 'id'=>'11'}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' => {'ids' => ["7", "6"], 'id'=>'11'}} - end - - it "tests edge test: make sure that when we turn off knockout_prefix that all values are processed correctly" do - hash_src = {"region" => {'ids' => ["7", @field_ko_prefix, "2", "6,8"]}} - hash_dst = {"region"=>{"ids"=>["1", "2", "3", "4"], 'id'=>'11'}} - @dm.deep_merge!(hash_src, hash_dst, {:unpack_arrays => ","}) - hash_dst.should == {'region' => {'ids' => ["1", "2", "3", "4", "7", @field_ko_prefix, "6", "8"], 'id'=>'11'}} - end - - it "tests edge test 2: make sure that when we turn off source array split that all values are processed correctly" do - hash_src = {"region" => {'ids' => ["7", "3", @field_ko_prefix, "6,8"]}} - hash_dst = {"region"=>{"ids"=>["1", "2", "3", "4"], 'id'=>'11'}} @dm.deep_merge!(hash_src, hash_dst) - hash_dst.should == {'region' => {'ids' => ["1", "2", "3", "4", "7", @field_ko_prefix, "6,8"], 'id'=>'11'}} - end - - it "tests Example: src = {'key' => \"@ko:1\"}, dst = {'key' => \"1\"} -> merges to {'key' => \"\"}" do - hash_src = {"amenity"=>@field_ko_prefix+":1"} - hash_dst = {"amenity"=>"1"} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) - hash_dst.should == {"amenity"=>""} - end - - it "tests Example: src = {'key' => \"@ko:1\"}, dst = {'key' => \"2\"} -> merges to {'key' => \"\"}" do - hash_src = {"amenity"=>@field_ko_prefix+":1"} - hash_dst = {"amenity"=>"2"} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) - hash_dst.should == {"amenity"=>""} - end - - it "tests Example: src = {'key' => \"@ko:1\"}, dst = {'key' => \"1\"} -> merges to {'key' => \"\"}" do - hash_src = {"amenity"=>[@field_ko_prefix+":1"]} - hash_dst = {"amenity"=>"1"} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) - hash_dst.should == {"amenity"=>[]} - end - - it "tests Example: src = {'key' => \"@ko:1\"}, dst = {'key' => \"1\"} -> merges to {'key' => \"\"}" do - hash_src = {"amenity"=>[@field_ko_prefix+":1"]} - hash_dst = {"amenity"=>["1"]} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) - hash_dst.should == {"amenity"=>[]} - end - - it "tests Example: src = {'key' => \"@ko:1\"}, dst = {'key' => \"1\"} -> merges to {'key' => \"\"}" do - hash_src = {"amenity"=>@field_ko_prefix+":1"} - hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) - hash_dst.should == {"amenity"=>""} - end - - it "tests Example: src = {'key' => \"@ko:1\"}, dst = {'key' => \"1\"} -> merges to {'key' => \"\"}" do - hash_src = {"amenity"=>@field_ko_prefix+":1"} - hash_dst = {"amenity"=>["1"]} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) - hash_dst.should == {"amenity"=>""} + hash_dst.should == hash_src end it "tests are unmerged hashes passed unmodified w/out :unpack_arrays?" do hash_src = {"amenity"=>{"id"=>["26,27"]}} hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix}) + @dm.deep_merge!(hash_src, hash_dst) hash_dst.should == {"amenity"=>{"id"=>["26,27"]}} end - it "tests hash should be merged" do - hash_src = {"amenity"=>{"id"=>["26,27"]}} - hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"amenity"=>{"id"=>["26","27"]}} - end - - it "tests second merge of same values should result in no change in output" do - hash_src = {"amenity"=>{"id"=>["26,27"]}} - hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"amenity"=>{"id"=>["26","27"]}} - end - - it "tests hashes with knockout values are suppressed" do - hash_src = {"amenity"=>{"id"=>["#{@field_ko_prefix}:26,#{@field_ko_prefix}:27,28"]}} - hash_dst = {} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"amenity"=>{"id"=>["28"]}} - end - - it "unamed upstream - tbd" do - hash_src= {'region' =>{'ids'=>[@field_ko_prefix]}, 'query_uuid' => 'zzz'} - hash_dst= {'region' =>{'ids'=>['227','2','3','3']}, 'query_uuid' => 'zzz'} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' =>{'ids'=>[]}, 'query_uuid' => 'zzz'} - end - - it "unamed upstream - tbd" do - hash_src= {'region' =>{'ids'=>[@field_ko_prefix]}, 'query_uuid' => 'zzz'} - hash_dst= {'region' =>{'ids'=>['227','2','3','3'], 'id' => '3'}, 'query_uuid' => 'zzz'} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' =>{'ids'=>[], 'id'=>'3'}, 'query_uuid' => 'zzz'} - end - - it "unamed upstream - tbd" do - hash_src= {'region' =>{'ids'=>[@field_ko_prefix]}, 'query_uuid' => 'zzz'} - hash_dst= {'region' =>{'muni_city_id' => '2244', 'ids'=>['227','2','3','3'], 'id'=>'3'}, 'query_uuid' => 'zzz'} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' =>{'muni_city_id' => '2244', 'ids'=>[], 'id'=>'3'}, 'query_uuid' => 'zzz'} - end - - it "unamed upstream - tbd" do - hash_src= {'region' =>{'ids'=>[@field_ko_prefix], 'id' => '5'}, 'query_uuid' => 'zzz'} - hash_dst= {'region' =>{'muni_city_id' => '2244', 'ids'=>['227','2','3','3'], 'id'=>'3'}, 'query_uuid' => 'zzz'} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' =>{'muni_city_id' => '2244', 'ids'=>[], 'id'=>'5'}, 'query_uuid' => 'zzz'} - end - - it "unamed upstream - tbd" do - hash_src= {'region' =>{'ids'=>[@field_ko_prefix, '227'], 'id' => '5'}, 'query_uuid' => 'zzz'} - hash_dst= {'region' =>{'muni_city_id' => '2244', 'ids'=>['227','2','3','3'], 'id'=>'3'}, 'query_uuid' => 'zzz'} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' =>{'muni_city_id' => '2244', 'ids'=>['227'], 'id'=>'5'}, 'query_uuid' => 'zzz'} - end - - it "unamed upstream - tbd" do - hash_src= {'region' =>{'muni_city_id' => @field_ko_prefix, 'ids'=>@field_ko_prefix, 'id'=>'5'}, 'query_uuid' => 'zzz'} - hash_dst= {'region' =>{'muni_city_id' => '2244', 'ids'=>['227','2','3','3'], 'id'=>'3'}, 'query_uuid' => 'zzz'} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' =>{'muni_city_id' => '', 'ids'=>'', 'id'=>'5'}, 'query_uuid' => 'zzz'} - end - - it "unamed upstream - tbd" do - hash_src= {'region' =>{'muni_city_id' => @field_ko_prefix, 'ids'=>[@field_ko_prefix], 'id'=>'5'}, 'query_uuid' => 'zzz'} - hash_dst= {'region' =>{'muni_city_id' => '2244', 'ids'=>['227','2','3','3'], 'id'=>'3'}, 'query_uuid' => 'zzz'} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' =>{'muni_city_id' => '', 'ids'=>[], 'id'=>'5'}, 'query_uuid' => 'zzz'} - end - - it "unamed upstream - tbd" do - hash_src= {'region' =>{'muni_city_id' => @field_ko_prefix, 'ids'=>[@field_ko_prefix,'227'], 'id'=>'5'}, 'query_uuid' => 'zzz'} - hash_dst= {'region' =>{'muni_city_id' => '2244', 'ids'=>['227','2','3','3'], 'id'=>'3'}, 'query_uuid' => 'zzz'} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {'region' =>{'muni_city_id' => '', 'ids'=>['227'], 'id'=>'5'}, 'query_uuid' => 'zzz'} - end - - it "unamed upstream - tbd" do - hash_src = {"muni_city_id"=>@field_ko_prefix, "id"=>""} - hash_dst = {"muni_city_id"=>"", "id"=>""} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"muni_city_id"=>"", "id"=>""} - end - - it "unamed upstream - tbd" do - hash_src = {"region"=>{"muni_city_id"=>@field_ko_prefix, "id"=>""}} - hash_dst = {"region"=>{"muni_city_id"=>"", "id"=>""}} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"region"=>{"muni_city_id"=>"", "id"=>""}} - end - - it "unamed upstream - tbd" do - hash_src = {"query_uuid"=>"a0dc3c84-ec7f-6756-bdb0-fff9157438ab", "url_regions"=>[], "region"=>{"muni_city_id"=>@field_ko_prefix, "id"=>""}, "property"=>{"property_type_id"=>"", "search_rate_min"=>"", "search_rate_max"=>""}, "task"=>"search", "run_query"=>"Search"} - hash_dst = {"query_uuid"=>"a0dc3c84-ec7f-6756-bdb0-fff9157438ab", "url_regions"=>[], "region"=>{"muni_city_id"=>"", "id"=>""}, "property"=>{"property_type_id"=>"", "search_rate_min"=>"", "search_rate_max"=>""}, "task"=>"search", "run_query"=>"Search"} - @dm.deep_merge!(hash_src, hash_dst, {:knockout_prefix => @field_ko_prefix, :unpack_arrays => ","}) - hash_dst.should == {"query_uuid"=>"a0dc3c84-ec7f-6756-bdb0-fff9157438ab", "url_regions"=>[], "region"=>{"muni_city_id"=>"", "id"=>""}, "property"=>{"property_type_id"=>"", "search_rate_min"=>"", "search_rate_max"=>""}, "task"=>"search", "run_query"=>"Search"} - end - it "tests hash of array of hashes" do hash_src = {"item" => [{"1" => "3"}, {"2" => "4"}]} hash_dst = {"item" => [{"3" => "5"}]} @@ -745,42 +284,31 @@ describe Chef::Mixin::DeepMerge do ret.should == {"property" => ["1","2","3","4","5","6"]} end - it "should not knockout matching array value when merging arrays within hashes" do - hash_dst = {"property" => ["2","4"]} - hash_src = {"property" => ["1","!merge:4"]} - hash_src_no_colon = {"property" => ["1","!merge"]} - @dm.merge(hash_dst, hash_src).should == {"property" => ["2", "4", "1", "!merge:4"]} - @dm.merge(hash_dst, hash_src_no_colon).should == {"property" => ["2", "4", "1", "!merge"]} - end end describe "role_merge" do - it "should knockout matching array value when merging arrays within hashes" do + it "errors out if knockout merge use is detected in an array" do hash_dst = {"property" => ["2","4"]} hash_src = {"property" => ["1","!merge:4"]} - @dm.role_merge(hash_dst, hash_src).should == {"property" => ["2","1"]} + lambda {@dm.role_merge(hash_dst, hash_src)}.should raise_error(Chef::Mixin::DeepMerge::InvalidSubtractiveMerge) end - it "should knockout all array values when merging arrays within hashes, leaving 2" do - hash_dst = {"property" => ["2","4"]} - hash_src = {"property" => ["!merge:","1","2"]} - hash_src_no_colon = {"property" => ["!merge","1","2"]} - @dm.role_merge(hash_dst, hash_src).should == {"property" => ["1","2"]} - @dm.role_merge(hash_dst, hash_src_no_colon).should == {"property" => ["1","2"]} + it "errors out if knockout merge use is detected in an array (reversed merge order)" do + hash_dst = {"property" => ["1","!merge:4"]} + hash_src = {"property" => ["2","4"]} + lambda {@dm.role_merge(hash_dst, hash_src)}.should raise_error(Chef::Mixin::DeepMerge::InvalidSubtractiveMerge) end - it "should knockout all array values when merging arrays within hashes, leaving 0" do + it "errors out if knockout merge use is detected in a string" do hash_dst = {"property" => ["2","4"]} - hash_src = {"property" => ["!merge:"]} - hash_src_no_colon = {"property" => ["!merge"]} - @dm.role_merge(hash_dst, hash_src).should == {"property" => []} - @dm.role_merge(hash_dst, hash_src_no_colon).should == {"property" => []} + hash_src = {"property" => "!merge"} + lambda {@dm.role_merge(hash_dst, hash_src)}.should raise_error(Chef::Mixin::DeepMerge::InvalidSubtractiveMerge) end - it "should knockout matching array value when merging arrays within hashes" do - hash_dst = {"property" => ["2","4"]} - hash_src = {"property" => ["1","!merge:4"]} - @dm.role_merge(hash_dst, hash_src).should == {"property" => ["2","1"]} + it "errors out if knockout merge use is detected in a string (reversed merge order)" do + hash_dst = {"property" => "!merge"} + hash_src= {"property" => ["2","4"]} + lambda {@dm.role_merge(hash_dst, hash_src)}.should raise_error(Chef::Mixin::DeepMerge::InvalidSubtractiveMerge) end end end |