Started migrating the tests from issue into workpackage #1754

Starting with the validations, we're moving everything out of issue into
workpackage: Accompanying this, the tests are migrated into specs. This
branch concentrates on moving the validations.

  * added shoulda for simplified testing of validations
  * added date_validator to simplify&fix date-validation
  * moved the basic-validations (presence_of, date-validations) into the workpackage_basics_spec.rb
pull/384/head
Stefan Frank 11 years ago
parent 585507176c
commit a70d8d4012
  1. 3
      Gemfile
  2. 4
      Gemfile.lock
  3. 19
      app/models/issue.rb
  4. 10
      app/models/work_package.rb
  5. 6
      config/locales/de.yml
  6. 6
      config/locales/en.yml
  7. 43
      spec/models/work_package_basics_spec.rb

@ -31,6 +31,8 @@ gem 'htmldiff'
gem 'execjs'
gem 'therubyracer'
gem "date_validator"
# will need to be removed once we are on rails4 as it will be part of the rails4 core
gem 'strong_parameters'
@ -83,6 +85,7 @@ group :test do
# why in Gemfile? see: https://github.com/guard/guard-test
gem 'ruby-prof'
gem 'simplecov', ">= 0.8.pre"
gem "shoulda-matchers"
end
group :ldap do

@ -107,6 +107,8 @@ GEM
daemons (1.1.9)
dalli (2.6.4)
database_cleaner (1.0.1)
date_validator (0.7.0)
activemodel (>= 3)
debug_inspector (0.0.2)
debugger (1.6.0)
columnize (>= 0.3.1)
@ -362,6 +364,7 @@ DEPENDENCIES
cucumber-rails-training-wheels
dalli
database_cleaner
date_validator
delayed_job_active_record
execjs
factory_girl_rails (~> 4.0)
@ -411,6 +414,7 @@ DEPENDENCIES
select2-rails (~> 3.3.2)
selenium-webdriver
shoulda
shoulda-matchers
simplecov (>= 0.8.pre)
sqlite3
strong_parameters

@ -21,14 +21,6 @@ class Issue < WorkPackage
attr_protected :project_id, :author_id, :lft, :rgt
validates_presence_of :subject, :priority, :project, :type, :author, :status
validates_length_of :subject, :maximum => 255
validates_inclusion_of :done_ratio, :in => 0..100
validates_numericality_of :estimated_hours, :allow_nil => true
validate :validate_format_of_due_date
validate :validate_start_date_before_due_date
validate :validate_start_date_before_soonest_start_date
validate :validate_fixed_version_is_assignable
validate :validate_fixed_version_is_still_open
@ -178,17 +170,6 @@ class Issue < WorkPackage
Setting.issue_done_ratio == 'issue_field'
end
def validate_format_of_due_date
if self.due_date.nil? && @attributes['due_date'] && !@attributes['due_date'].empty?
errors.add :due_date, :not_a_date
end
end
def validate_start_date_before_due_date
if self.due_date and self.start_date and self.due_date < self.start_date
errors.add :due_date, :greater_than_start_date
end
end
def validate_start_date_before_soonest_start_date
if start_date && soonest_start && start_date < soonest_start

@ -15,6 +15,16 @@
class WorkPackage < ActiveRecord::Base
# validations
validates_presence_of :subject, :priority, :project, :type, :author, :status
validates_length_of :subject, :maximum => 255
validates_inclusion_of :done_ratio, :in => 0..100
validates_numericality_of :estimated_hours, :allow_nil => true
validates :start_date, :date => {:allow_blank => true}
validates :due_date, :date => {:after => :start_date, :message => :greater_than_start_date, :allow_blank => true}, :unless => Proc.new { |wp| wp.start_date.blank?}
#TODO Remove alternate inheritance column name once single table
# inheritance is no longer needed. The need for a different column name
# comes from Trackers becoming Types.

@ -176,6 +176,12 @@ de:
too_long: "ist zu lang (nicht mehr als %{count} Zeichen)"
too_short: "ist zu kurz (nicht weniger als %{count} Zeichen)"
wrong_length: "hat die falsche Länge (muss genau %{count} Zeichen haben)"
# locales for date_validator
not_a_date: "ist kein Datum"
after: "muss nach %{date} sein"
after_or_equal_to: "muss nach oder gleich %{date} sein"
before: "muss vor %{date} sein"
before_or_equal_to: "muss vor oder gleich %{date} sein"
models:
planning_element:
attributes:

@ -180,6 +180,12 @@ en:
too_long: "is too long (maximum is %{count} characters)"
too_short: "is too short (minimum is %{count} characters)"
wrong_length: "is the wrong length (should be %{count} characters)"
# translations for date-validator
not_a_date: "is not a date"
after: "must be after %{date}"
after_or_equal_to: "must be after or equal to %{date}"
before: "must be before %{date}"
before_or_equal_to: "must be before or equal to %{date}"
models:
planning_element:
attributes:

@ -0,0 +1,43 @@
#-- copyright
# OpenProject is a project management system.
#
# Copyright (C) 2012-2013 the OpenProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# See doc/COPYRIGHT.rdoc for more details.
#++
require 'spec_helper'
describe WorkPackage do
describe "validations" do
# validations
[:subject, :priority, :project, :type, :author, :status].each do |field|
it{ should validate_presence_of field}
end
it { should ensure_length_of(:subject).is_at_most 255 }
it { should ensure_inclusion_of(:done_ratio).in_range 0..100 }
it { should validate_numericality_of :estimated_hours}
it "validate, that start-date is before end-date" do
wp = FactoryGirl.build(:work_package, start_date: 1.day.from_now, due_date: Time.now)
expect(wp).to have(1).errors_on(:due_date)
end
it "validate, that correct formats are properly validated" do
wp = FactoryGirl.build(:work_package, start_date: "01/01/13", due_date: "31/01/13")
puts wp.valid?
puts wp.errors.full_messages
expect(wp).to have(0).errors_on(:start_date)
end
end
end
Loading…
Cancel
Save