1
0

Test pass: test_create_duplicated_targets_should_fail

This commit is contained in:
cryptogopher 2021-04-19 23:26:52 +02:00
parent 0b9401b089
commit 1b634dea2b
3 changed files with 46 additions and 13 deletions

View File

@ -20,12 +20,12 @@
<%= link_to t(".button_delete_target"), '#', class: 'icon icon-del',
style: (@targets.many? ? "" : "display:none"),
onclick: "deleteTarget(); return false;" %>
onclick: "deleteTarget(event); return false;" %>
</p>
<% end %>
<p>
<%= link_to t(".button_new_target"), '#', class: 'icon icon-add',
onclick: 'newTarget(); return false;' %>
onclick: 'newTarget(event); return false;' %>
</p>
<%= javascript_tag do %>
@ -37,7 +37,7 @@
$('p.target select:first-child[id$=__quantity_id]').trigger(jQuery.Event('change'));
})
function newTarget() {
function newTarget(event) {
var form = $(event.target).closest('form');
var row = form.find('p.target:visible:last');
var new_row = row.clone().insertAfter(row);
@ -48,7 +48,7 @@
form.find('p.target:visible a.icon-del').show();
}
function deleteTarget() {
function deleteTarget(event) {
var form = $(event.target).closest('form');
var row = $(event.target).closest('p.target');
if (row.find('input[id$=__id]').val()) {

View File

@ -6,6 +6,8 @@ ENV["RACK_ENV"] = "test"
require File.expand_path('../../../../test/application_system_test_case', __FILE__)
class BodyTrackingSystemTestCase < ApplicationSystemTestCase
#Selenium::WebDriver.logger.level = :debug
profile = Selenium::WebDriver::Firefox::Profile.new
profile['browser.download.dir'] = DOWNLOADS_PATH
profile['browser.download.folderList'] = 2

View File

@ -169,12 +169,13 @@ class TargetsTest < BodyTrackingSystemTestCase
def test_create_multiple_targets
end
def test_create_form_retains_data_on_failure
end
def test_create_duplicate_for_persisted_target_should_fail
# TODO: extend with item + scope
source = @project.targets.sample
threshold_quantity = @project.quantities.target.roots.sample
threshold_value = rand(-2000.0..2000.0).to_d(4)
threshold_unit = @project.units.sample
msg = t('activerecord.errors.models.target.attributes.base.duplicated_record')
assert_no_difference 'Target.count' do
visit goal_targets_path(source.goal)
@ -183,18 +184,48 @@ class TargetsTest < BodyTrackingSystemTestCase
fill_in t(:field_effective_from), with: source.effective_from
within 'p.target' do
select source.quantity.name
select threshold_quantity.name
fill_in with: threshold_value
select threshold_unit.shortname
select @project.quantities.target.roots.sample.name
fill_in with: rand(-2000.0..2000.0).to_d(4)
select @project.units.sample.shortname
end
click_on t(:button_create)
assert_selector :xpath, '//p[@class="target"]//preceding-sibling::div', text: msg
end
end
msg = I18n.t('activerecord.errors.models.target.attributes.base.duplicated_record')
assert_selector :xpath, '//form//p[@class="target"]//preceding-sibling::div', text: msg
end
def test_create_duplicated_targets_should_fail
date = Date.current + rand(-10..10).days
quantity = @project.quantities.except_targets
.joins("LEFT OUTER JOIN targets ON targets.quantity_id = quantities.id \
AND targets.effective_from = #{date}")
.where(targets: {id: nil}).sample
assert quantity
msg = t('activerecord.errors.models.target.attributes.base.duplicated_record')
assert_no_difference 'Target.count' do
visit goal_targets_path(@project.goals.binding)
click_link t('targets.contextual.link_new_target')
within 'form#new-target-form' do
fill_in t(:field_effective_from), with: date
within :xpath, '//p[@class="target"][1]' do
select quantity.name
select @project.quantities.target.roots.sample.name
fill_in with: rand(-2000.0..2000.0).to_d(4)
select @project.units.sample.shortname
end
click_link t('targets.form.button_new_target')
within :xpath, '//p[@class="target"][2]' do
select quantity.name
select @project.quantities.target.roots.sample.name
fill_in with: rand(-2000.0..2000.0).to_d(4)
select @project.units.sample.shortname
end
click_on t(:button_create)
assert_selector :xpath, '//p[@class="target"][last()]//preceding-sibling::div',
text: msg
end
end
end
def test_create_and_simultaneously_remove_persisted_duplicate
@ -212,7 +243,7 @@ class TargetsTest < BodyTrackingSystemTestCase
within find('td', text: target.effective_from).ancestor('tr') do
click_link t(:button_edit)
within find(:xpath, 'following-sibling::tr//form') do
within :xpath, 'following-sibling::tr//form' do
assert has_field?(t(:field_effective_from), with: target.effective_from)
# NOTE: this assumes target.quantity is unique which is not enforced now