Created
July 1, 2013 20:16
-
-
Save elja/5904180 to your computer and use it in GitHub Desktop.
specs
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
context 'Product Options Deletion' do | |
before(:all) do | |
any_instance_of(Website::Site) do |klass| | |
stub(klass).create_default_data { true } | |
end | |
@site = FactoryGirl.create(:site) | |
ActsAsTenant.current_tenant = @site | |
end | |
before(:each) do | |
@product = FactoryGirl.create(:website_product, :site => @site) | |
end | |
it 'should create product option/group proxy when option/group is created' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello' | |
} | |
}, | |
'1' => { | |
:product_option_attributes => { | |
:name => 'world', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(2) | |
end | |
it 'should create only one option/group proxy when group with new option in it is created' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'world', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(1) | |
@product.product_option_groups.count.should eq(1) | |
@product.product_option_groups.inject(0) { |accum, g| accum += g.product_options.count }.should eq(1) | |
end | |
it 'should create only one option/group proxy when group with existent option in it is created' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'hello', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(1) | |
@product.product_options.count.should eq(1) | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'test group', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_id => @product.product_options.reload.first.id | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_groups.count.should eq(1) | |
@product.product_option_groups.inject(0) { |accum, g| accum += g.product_options.count }.should eq(1) | |
end | |
it 'should create only one option/group proxy when existing group with new option in it is created' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_grouping_elements.first.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.first.id, | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'test', | |
:friendly_name => 'option' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(1) | |
@product.product_option_groups.count.should eq(1) | |
@product.product_option_groups.inject(0) { |accum, g| accum += g.product_options.count }.should eq(1) | |
end | |
it 'should remove product option/group proxy when existent option is moved from root into existent group' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello', | |
} | |
}, | |
'1' => { | |
:product_option_attributes => { | |
:name => 'hello', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(2) | |
@product.product_options.count.should eq(1) | |
@product.product_option_groups.count.should eq(1) | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_groups.first.product_option_grouping_element.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.first.id, | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_id => @product.product_options.reload.first.id | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(1) | |
@product.product_option_groups.count.should eq(1) | |
@product.product_option_groups.inject(0) { |accum, g| accum += g.product_options.count }.should eq(1) | |
@product.product_options.count.should eq(0) | |
end | |
it 'should create product proxy for option when existing option is moved out from group into root' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'test group', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'test', | |
:friendly_name => 'option' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(1) | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_id => @product.product_option_grouping_elements.first.product_option_group.product_options.first.id | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(2) | |
@product.product_option_groups.sum { |gr| gr.product_options.count }.should eq(0) | |
end | |
it 'should correctly move existent option from existent group to new group' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'test group', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'test', | |
:friendly_name => 'option' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'new group', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_id => @product.product_option_grouping_elements.first.product_option_group.product_options.first.id | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(2) | |
@product.product_option_groups.count.should eq(2) | |
@product.product_option_groups.inject(0) { |accum, g| accum += g.product_options.count }.should eq(1) | |
end | |
it 'should not create new option proxy when existent option is moved from one existent group to another' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello', | |
} | |
}, | |
'1' => { | |
:product_option_attributes => { | |
:name => 'hello', | |
:friendly_name => 'test' | |
} | |
}, | |
'2' => { | |
:product_option_group_attributes => { | |
:name => 'world' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(3) | |
@product.product_option_groups.count.should eq(2) | |
@product.product_options.count.should eq(1) | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_groups.find_by_name('hello').product_option_grouping_element.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.find_by_name('hello').id, | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_id => @product.product_options.find_by_name('hello').id | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(2) | |
@product.product_option_groups.count.should eq(2) | |
@product.product_option_groups.sum { |gr| gr.product_options.count }.should eq(1) | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_groups.find_by_name('world').product_option_grouping_element.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.find_by_name('world').id, | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_id => @product.product_option_groups.find_by_name('hello').product_group_option_connections.first.product_option.id | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(2) | |
@product.product_option_groups.count.should eq(2) | |
@product.product_option_groups.sum { |gr| gr.product_options.count }.should eq(1) | |
@product.product_option_groups.find_by_name('hello').product_group_option_connections.count.should eq(0) | |
@product.product_option_groups.find_by_name('world').product_group_option_connections.count.should eq(1) | |
end | |
it 'should remove option proxy when option is destroying' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'test option', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.reload | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_grouping_elements.first.id, | |
:product_option_attributes => { | |
:id => @product.product_option_grouping_elements.first.product_option.id, | |
:_destroy => true | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(0) | |
end | |
it 'should remove group proxy when group is destroying' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'test group' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.reload | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_grouping_elements.first.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.first.id, | |
:_destroy => true | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(0) | |
end | |
it 'should remove group\'s options when group is destroying' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'world', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.product_option_groups.count.should eq(1) | |
@product.product_option_groups.sum { |gr| gr.product_options.count }.should eq(1) | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_grouping_elements.first.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_grouping_elements.first.product_option_group.id, | |
:_destroy => true | |
} | |
} | |
} | |
} | |
expect { @product.update_attributes(update_attributes) }.to change { Website::ProductOption.count }.by(-1) | |
@product.should be_valid | |
end | |
it 'should remove connection when option is removing from group' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'world', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
@product.reload | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_grouping_elements.first.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_grouping_elements.first.product_option_group.id, | |
:product_group_option_connections_attributes => { | |
:id => @product.product_option_grouping_elements.first.product_option_group.product_group_option_connections.first.id, | |
:product_option_attributes => { | |
:id => @product.product_option_grouping_elements.first.product_option_group.product_options.first.id, | |
:_destroy => true | |
} | |
} | |
} | |
} | |
} | |
} | |
lambda { | |
lambda { @product.update_attributes(update_attributes) }.should change(Website::ProductOption, :count).by(-1) | |
}.should change(Website::ProductGroupOptionConnection, :count).by(-1) | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(1) | |
@product.product_option_groups.count.should eq(1) | |
end | |
it 'should not allow destroy proxy through nested attributes' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes) | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_grouping_elements.first.id, | |
:_destroy => true | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq(1) | |
end | |
it 'should not allow destroy group-option connection through nested attributes' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'hello', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'world', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_grouping_elements.first.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.find_by_name('hello').id, | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:id => @product.product_option_groups.find_by_name('hello').product_group_option_connections.first.id, | |
:_destroy => false | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_groups.first.product_group_option_connections.count.should eq 1 | |
end | |
it 'should correctly process option moving from one existent group to another with deleting first group' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'first group', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'option', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
}, | |
'1' => { | |
:product_option_group_attributes => { | |
:name => 'second group' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:id => @product.product_option_groups.find_by_name('first group').product_option_grouping_element.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.find_by_name('first group').id, | |
:_destroy => true | |
} | |
}, | |
'1' => { | |
:id => @product.product_option_groups.find_by_name('second group').product_option_grouping_element.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.find_by_name('second group').id, | |
:name => 'second group 2', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_id => @product.product_option_groups.find_by_name('first group').product_options.first.id | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq 1 | |
@product.product_option_groups.count.should eq 1 | |
@product.product_option_groups.sum { |group| group.product_options.count }.should eq 1 | |
end | |
it 'should correctly process existing option moving from one existing group to another with deleting second group' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'first group', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'option', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
}, | |
'1' => { | |
:product_option_group_attributes => { | |
:name => 'second group' | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'1' => { | |
:id => @product.product_option_groups.find_by_name('second group').product_option_grouping_element.id, | |
:product_option_group_attributes => { | |
:id => @product.product_option_groups.find_by_name('second group').id, | |
:name => 'second group 2', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_id => @product.product_option_groups.find_by_name('first group').product_options.first.id | |
} | |
}, | |
:_destroy => true | |
} | |
} | |
} | |
} | |
@product.update_attributes(update_attributes).should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq 1 | |
@product.product_option_groups.count.should eq 1 | |
@product.product_option_groups.sum { |group| group.product_options.count }.should eq 0 | |
end | |
it 'should correctly process existing option moving from existent group to new group with deleting new group' do | |
attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'first group', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_attributes => { | |
:name => 'option', | |
:friendly_name => 'test' | |
} | |
} | |
} | |
} | |
} | |
} | |
} | |
@product.update_attributes(attributes).should be_true | |
@product.should be_valid | |
update_attributes = { | |
:product_option_grouping_elements_attributes => { | |
'0' => { | |
:product_option_group_attributes => { | |
:name => 'second group', | |
:product_group_option_connections_attributes => { | |
'0' => { | |
:product_option_id => @product.product_option_groups.first.product_options.first.id | |
} | |
}, | |
:_destroy => true | |
} | |
} | |
} | |
} | |
@product.assign_attributes update_attributes | |
@product.save.should be_true | |
@product.should be_valid | |
@product.product_option_grouping_elements.count.should eq 1 | |
@product.product_option_groups.count.should eq 1 | |
@product.product_option_groups.sum { |group| group.product_options.count }.should eq 0 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment