Commit 9503be63c331a6b14d86c9f4adf9680be5371030
Committed by
Gabriela Navarro
1 parent
84fb263e
Exists in
master
and in
5 other branches
Add library validations and tests
Signed-off-by: Luiz Matos <luizff.matos@gmail.com> Signed-off-by: Gustavo Jaruga <darksshades@gmail.com>
Showing
2 changed files
with
18 additions
and
1 deletions
Show diff stats
lib/library.rb
| @@ -4,8 +4,10 @@ class Library < ActiveRecord::Base | @@ -4,8 +4,10 @@ class Library < ActiveRecord::Base | ||
| 4 | 4 | ||
| 5 | attr_accessible :name, :version, :license, :software_info_id | 5 | attr_accessible :name, :version, :license, :software_info_id |
| 6 | 6 | ||
| 7 | - #TODO: review validations | ||
| 8 | validate :validate_name, :validate_version, :validate_license | 7 | validate :validate_name, :validate_version, :validate_license |
| 8 | + validates :name, length: { within: 0..20 } | ||
| 9 | + validates_length_of :version, maximum: 20, too_long: _("Library is too long (maximum is 20 characters)") | ||
| 10 | + validates_length_of :license, maximum: 20, too_long: _("Library is too long (maximum is 20 characters)") | ||
| 9 | 11 | ||
| 10 | def validate_name | 12 | def validate_name |
| 11 | self.errors.add(:name, _("can't be blank")) if self.name.blank? && self.errors[:name].blank? | 13 | self.errors.add(:name, _("can't be blank")) if self.name.blank? && self.errors[:name].blank? |
test/unit/library_validation_test.rb
| @@ -31,4 +31,19 @@ class LibraryValidationTest < ActiveSupport::TestCase | @@ -31,4 +31,19 @@ class LibraryValidationTest < ActiveSupport::TestCase | ||
| 31 | @library.license = "" | 31 | @library.license = "" |
| 32 | assert !@library.save | 32 | assert !@library.save |
| 33 | end | 33 | end |
| 34 | + | ||
| 35 | + should "Don't save Library if name is too long" do | ||
| 36 | + @library.name = "A too long name to be a valid name for library" | ||
| 37 | + assert !@library.save | ||
| 38 | + end | ||
| 39 | + | ||
| 40 | + should "Don't save Library if version is too long" do | ||
| 41 | + @library.version = "A too long version to be a valid version for library" | ||
| 42 | + assert !@library.save | ||
| 43 | + end | ||
| 44 | + | ||
| 45 | + should "Don't save Library if license is too long" do | ||
| 46 | + @library.license = "A too long license to be a valid license for library" | ||
| 47 | + assert !@library.save | ||
| 48 | + end | ||
| 34 | end | 49 | end |