1
0

Test pass: test_destroy

Expand Targets #update success flash and failure handling
Add Target index table checks in modification actions tests
This commit is contained in:
cryptogopher 2021-04-29 01:30:13 +02:00
parent 5b80272ae0
commit 4eda035e47
5 changed files with 76 additions and 10 deletions

View File

@ -6,7 +6,7 @@ class TargetsController < ApplicationController
include Concerns::Finders include Concerns::Finders
before_action :find_goal_by_goal_id, before_action :find_goal_by_goal_id,
only: [:index, :new, :create, :edit, :update, :toggle_exposure] only: [:index, :new, :create, :edit, :update, :destroy, :toggle_exposure]
before_action :find_quantity_by_quantity_id, only: [:toggle_exposure, :subthresholds] before_action :find_quantity_by_quantity_id, only: [:toggle_exposure, :subthresholds]
before_action :authorize before_action :authorize
#before_action :set_view_params #before_action :set_view_params
@ -54,17 +54,27 @@ class TargetsController < ApplicationController
params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from } params[:goal][:targets_attributes].each { |ta| ta[:effective_from] = @effective_from }
if @goal.update(targets_params) if @goal.update(targets_params)
flash.now[:notice] = t('.success') count = @goal.targets.target.count { |t| t.previous_changes.present? }
flash.now[:notice] = t('.success', count: count)
prepare_targets prepare_targets
render :index
else else
@targets = @goal.targets.where(id: targets_params[:targets_attributes].pluck(:id)) @targets = @goal.targets.where(id: targets_params[:targets_attributes].pluck(:id))
@targets += @goal.targets.select(&:changed_for_autosave?) @targets += @goal.targets.target.select(&:changed_for_autosave?)
.each { |t| t.thresholds.new unless t.thresholds.present? } .each { |t| t.thresholds.new unless t.thresholds.present? }
render :edit render :edit
end end
end end
def destroy def destroy
@effective_from = params[:date]
@targets = @goal.targets.where(effective_from: @effective_from)
count = @targets.destroy_all.length
if @targets.all?(&:destroyed?)
flash.now[:notice] = t('.success', count: count)
else
flash.now[:error] = t('.failure')
end
end end
def reapply def reapply

View File

@ -63,6 +63,7 @@ module Validations::NestedUniqueness
end end
end end
if records if records
# TODO: reset collction, not proxy
collection.proxy_association.reset collection.proxy_association.reset
records.each { |r| collection.proxy_association.add_to_target(r) } records.each { |r| collection.proxy_association.add_to_target(r) }
end end

View File

@ -0,0 +1,3 @@
<% if @targets.all?(&:destroyed?) %>
$('tr[id=date-<%= @effective_from %>]').nextUntil('tr.primary').addBack().remove();
<% end %>

View File

@ -122,6 +122,15 @@ en:
zero: "No targets specified" zero: "No targets specified"
one: "Created 1 target" one: "Created 1 target"
other: "Created %{count} targets" other: "Created %{count} targets"
update:
success:
one: "Successfully updated target"
other: "Successfully updated targets"
destroy:
success:
one: "Deleted 1 target"
other: "Deleted %{count} targets"
failure: "Some targets have not been deleted"
meals: meals:
contextual: contextual:
link_new_meal: 'New meal' link_new_meal: 'New meal'

View File

@ -13,15 +13,22 @@ class TargetsTest < BodyTrackingSystemTestCase
def test_index_binding_goal def test_index_binding_goal
goal = @project.goals.binding goal = @project.goals.binding
assert_not_equal 0, goal.targets.count assert_not_equal 0, goal.targets.count
visit goal_targets_path(goal) visit goal_targets_path(goal)
assert_selector 'table#targets tbody tr', within 'table#targets tbody' do
count: goal.targets.distinct.pluck(:effective_from).count dates = goal.targets.distinct.pluck(:effective_from)
assert_selector 'tr', count: dates.count
dates.each do |date|
assert_selector 'td', text: date
end
end
end end
def test_index_binding_goal_without_targets def test_index_binding_goal_without_targets
goal = @project.goals.binding goal = @project.goals.binding
goal.targets.delete_all goal.targets.delete_all
assert_equal 0, goal.targets.count assert_equal 0, goal.targets.count
visit goal_targets_path(goal) visit goal_targets_path(goal)
assert_current_path goal_targets_path(goal) assert_current_path goal_targets_path(goal)
assert_selector 'div#targets', visible: :yes, exact_text: t(:label_no_data) assert_selector 'div#targets', visible: :yes, exact_text: t(:label_no_data)
@ -58,6 +65,10 @@ class TargetsTest < BodyTrackingSystemTestCase
assert_selector 'table#targets thead th' assert_selector 'table#targets thead th'
end end
def test_show
# TODO
end
def test_new_binding_target def test_new_binding_target
visit goal_targets_path(@project.goals.binding) visit goal_targets_path(@project.goals.binding)
assert_no_selector 'form#new-target-form' assert_no_selector 'form#new-target-form'
@ -112,8 +123,11 @@ class TargetsTest < BodyTrackingSystemTestCase
assert_equal threshold_unit, t.thresholds.first.unit assert_equal threshold_unit, t.thresholds.first.unit
assert_no_selector 'form#new-target-form' assert_no_selector 'form#new-target-form'
assert_selector 'table#targets tbody tr', assert_selector 'div.flash.notice'
count: goal.targets.distinct.pluck(:effective_from).count within 'table#targets tbody' do
assert_selector 'tr', count: goal.targets.distinct.pluck(:effective_from).count
assert_selector 'td', text: date
end
end end
def test_create_binding_target_when_binding_goal_does_not_exist def test_create_binding_target_when_binding_goal_does_not_exist
@ -336,9 +350,9 @@ class TargetsTest < BodyTrackingSystemTestCase
end end
click_on t(:button_save) click_on t(:button_save)
end
end
assert_no_selector 'div#errorExplanation' assert_no_selector 'div#errorExplanation'
end
end
target.reload target.reload
assert_equal date, target.effective_from assert_equal date, target.effective_from
@ -350,6 +364,12 @@ class TargetsTest < BodyTrackingSystemTestCase
assert_equal t_value, target.thresholds[index].value assert_equal t_value, target.thresholds[index].value
assert_equal t_unit, target.thresholds[index].unit assert_equal t_unit, target.thresholds[index].unit
end end
assert_selector 'div.flash.notice'
within 'table#targets tbody' do
assert_no_selector 'td', text: target_date
assert_selector 'td', text: date
end
end end
def test_update_swap_targets def test_update_swap_targets
@ -373,9 +393,9 @@ class TargetsTest < BodyTrackingSystemTestCase
select2.select quantity1 select2.select quantity1
click_on t(:button_save) click_on t(:button_save)
end
end
assert_no_selector 'div#errorExplanation' assert_no_selector 'div#errorExplanation'
end
end
target1.reload target1.reload
target2.reload target2.reload
@ -405,15 +425,38 @@ class TargetsTest < BodyTrackingSystemTestCase
end end
click_on t(:button_save) click_on t(:button_save)
end
end
assert_no_selector 'div#errorExplanation' assert_no_selector 'div#errorExplanation'
end
end
new_target = Target.last new_target = Target.last
assert new_target.quantity, target.quantity assert new_target.quantity, target.quantity
assert_raises(ActiveRecord::RecordNotFound) { target.reload } assert_raises(ActiveRecord::RecordNotFound) { target.reload }
end end
def test_destroy
goal = @project.goals.binding
date = goal.targets.distinct.pluck(:effective_from).sample
targets = goal.targets.where(effective_from: date)
visit goal_targets_path(goal)
assert_difference 'Goal.count' => 0, 'Target.count' => - targets.length,
'Threshold.count' => - targets.sum { |t| t.thresholds.length } do
find('td', text: date).ancestor('tr').click_link t(:button_delete)
end
assert_empty goal.targets.reload.where(effective_from: date)
assert_selector 'div.flash.notice'
within 'table#targets tbody' do
assert_selector 'tr', count: goal.targets.distinct.pluck(:effective_from).count
assert_no_selector 'td', text: date
end
end
def test_reapply
# TODO
end
def fill_thresholds(thresholds) def fill_thresholds(thresholds)
thresholds.each do |threshold| thresholds.each do |threshold|
t_quantity, t_value, t_unit = threshold t_quantity, t_value, t_unit = threshold