Skip to content

Commit

Permalink
Collection item update tests
Browse files Browse the repository at this point in the history
  • Loading branch information
fbacall committed Jul 24, 2023
1 parent 5bbc374 commit 712bb3e
Showing 1 changed file with 105 additions and 0 deletions.
105 changes: 105 additions & 0 deletions test/controllers/collections_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,111 @@ class CollectionsControllerTest < ActionController::TestCase
assert_redirected_to collection_path(assigns(:collection))
end

test "should add items to collection" do
sign_in users(:regular_user)
collection = collections(:with_resources)
assert_difference('CollectionItem.count', 3) do
assert_difference('collection.events.count', 1) do
assert_difference('collection.materials.count', 2) do
patch :update, params: { id: collection.id, collection: { items_attributes: {
'0' => { resource_type: 'Material', resource_id: materials(:biojs).id, order: 2, comment: 'hello' },
'1' => { resource_type: 'Event', resource_id: events(:one).id, order: 1, comment: 'hello!' },
'2' => { resource_type: 'Material', resource_id: materials(:interpro).id, order: 1, comment: 'hello!!' }
} } }

mats = assigns(:collection).material_items
assert_equal 2, mats.length
assert_equal 1, mats[0].order
assert_equal 'hello!!', mats[0].comment
assert_equal materials(:interpro), mats[0].resource
assert_equal 2, mats[1].order
assert_equal 'hello', mats[1].comment
assert_equal materials(:biojs), mats[1].resource

events = assigns(:collection).event_items
assert_equal 1, events.length
assert_equal 1, events[0].order
assert_equal 'hello!', events[0].comment
assert_equal events(:one), events[0].resource
end
end
end
end

test "should remove items from collection" do
sign_in users(:regular_user)
collection = collections(:with_resources)
ci1 = collection.items.create!(resource: materials(:biojs), order: 2, comment: 'hello')
ci2 = collection.items.create!(resource: materials(:interpro), order: 1, comment: 'hello!!')
ci3 = collection.items.create!(resource: events(:one), order: 1, comment: 'hello!')


assert_difference('CollectionItem.count', -1) do
assert_difference('collection.materials.count', -1) do
assert_no_difference('collection.events.count') do
patch :update, params: { id: collection.id, collection: { items_attributes: {
'0' => { id: ci1.id, resource_type: 'Material', resource_id: materials(:biojs).id, order: 2, comment: 'hello' },
'1' => { id: ci3.id, resource_type: 'Event', resource_id: events(:one).id, order: 1, comment: 'hello!' },
'2' => { id: ci2.id, resource_type: 'Material', resource_id: materials(:interpro).id, order: 1, comment: 'hello!!', _destroy: '1' }
} } }

mats = assigns(:collection).material_items
assert_equal ci1.id, mats[0].id
assert_equal 1, mats.length
assert_equal 1, mats[0].order
assert_equal 'hello', mats[0].comment
assert_equal materials(:biojs), mats[0].resource

events = assigns(:collection).event_items
assert_equal 1, events.length
assert_equal ci3.id, events[0].id
assert_equal 1, events[0].order
assert_equal 'hello!', events[0].comment
assert_equal events(:one), events[0].resource
end
end
end
end

test "should modify items in collection" do
sign_in users(:regular_user)
collection = collections(:with_resources)
ci1 = collection.items.create!(resource: materials(:biojs), order: 2, comment: 'hello')
ci2 = collection.items.create!(resource: materials(:interpro), order: 1, comment: 'hello!!')
ci3 = collection.items.create!(resource: events(:one), order: 1, comment: 'hello!')

assert_no_difference('CollectionItem.count') do
assert_no_difference('collection.materials.count') do
assert_no_difference('collection.events.count') do
patch :update, params: { id: collection.id, collection: { items_attributes: {
'0' => { id: ci1.id, resource_type: 'Material', resource_id: materials(:biojs).id, order: 1, comment: 'hello world' },
'1' => { id: ci3.id, resource_type: 'Event', resource_id: events(:one).id, order: 1, comment: 'hello world!' },
'2' => { id: ci2.id, resource_type: 'Material', resource_id: materials(:interpro).id, order: 2, comment: 'hello world!!' }
} } }

mats = assigns(:collection).material_items
assert_equal 2, mats.length
assert_equal ci1.id, mats[0].id
assert_equal 1, mats[0].order
assert_equal 'hello world', mats[0].comment
assert_equal materials(:biojs), mats[0].resource
assert_equal ci2.id, mats[1].id
assert_equal 2, mats[1].order
assert_equal 'hello world!!', mats[1].comment
assert_equal materials(:interpro), mats[1].resource

events = assigns(:collection).event_items
assert_equal 1, events.length
assert_equal ci3.id, events[0].id
assert_equal 1, events[0].order
assert_equal 'hello world!', events[0].comment
assert_equal events(:one), events[0].resource
end
end
end
end


#UPDATE_CURATE TEST
test 'should add and remove elements' do
sign_in @collection.user
Expand Down

0 comments on commit 712bb3e

Please sign in to comment.