Commit 44d63989a58938a3f13c1d68e59d8b6e3c2c2503
1 parent
9459a9d5
Exists in
master
and in
23 other branches
ActionItem0: validation of block and box models
git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@34 3f533792-8f58-4932-b0fe-aaf55b0a4547
Showing
7 changed files
with
80 additions
and
4 deletions
Show diff stats
app/models/block.rb
| 1 | class Block < ActiveRecord::Base | 1 | class Block < ActiveRecord::Base |
| 2 | + belongs_to :box | ||
| 3 | + | ||
| 4 | + #<tt>position</tt> codl not be nil and must be an integer | ||
| 5 | + validates_numericality_of :position, :only_integer => true , :message => _('%{fn} must be composed only of integers') | ||
| 6 | + | ||
| 7 | + # A block must be associated to a box | ||
| 8 | + validates_presence_of :box_id | ||
| 9 | + | ||
| 2 | end | 10 | end |
app/models/box.rb
| @@ -2,8 +2,8 @@ class Box < ActiveRecord::Base | @@ -2,8 +2,8 @@ class Box < ActiveRecord::Base | ||
| 2 | belongs_to :owner, :polymorphic => true | 2 | belongs_to :owner, :polymorphic => true |
| 3 | 3 | ||
| 4 | #we cannot have two boxs with the same number to the same owner | 4 | #we cannot have two boxs with the same number to the same owner |
| 5 | - validates_uniqueness_of :number, :scope => :owner | 5 | + validates_uniqueness_of :number, :scope => [:owner_type, :owner_id] |
| 6 | 6 | ||
| 7 | - #<tt>number</tt> must be a integer | 7 | + #<tt>number</tt> could not be nil and must be an integer |
| 8 | validates_numericality_of :number, :only_integer => true, :message => _('%{fn} must be composed only of integers.') | 8 | validates_numericality_of :number, :only_integer => true, :message => _('%{fn} must be composed only of integers.') |
| 9 | end | 9 | end |
app/models/user.rb
test/fixtures/boxes.yml
| @@ -2,9 +2,15 @@ | @@ -2,9 +2,15 @@ | ||
| 2 | one: | 2 | one: |
| 3 | id: 1 | 3 | id: 1 |
| 4 | number: 1 | 4 | number: 1 |
| 5 | + owner_type: 'User' | ||
| 6 | + owner_id: 1 | ||
| 5 | two: | 7 | two: |
| 6 | id: 2 | 8 | id: 2 |
| 7 | number: 2 | 9 | number: 2 |
| 10 | + owner_type: 'User' | ||
| 11 | + owner_id: 1 | ||
| 8 | three: | 12 | three: |
| 9 | id: 3 | 13 | id: 3 |
| 10 | number: 3 | 14 | number: 3 |
| 15 | + owner_type: 'User' | ||
| 16 | + owner_id: 1 |
test/unit/block_test.rb
| @@ -4,7 +4,55 @@ class BlockTest < Test::Unit::TestCase | @@ -4,7 +4,55 @@ class BlockTest < Test::Unit::TestCase | ||
| 4 | fixtures :blocks | 4 | fixtures :blocks |
| 5 | 5 | ||
| 6 | # Replace this with your real tests. | 6 | # Replace this with your real tests. |
| 7 | - def test_truth | ||
| 8 | - assert true | 7 | + def test_create |
| 8 | + count = Block.count | ||
| 9 | + b = Block.new | ||
| 10 | + assert !b.valid? | ||
| 11 | + assert b.errors.invalid?(:box_id) | ||
| 12 | + assert b.errors.invalid?(:position) | ||
| 13 | + | ||
| 14 | + u = User.new | ||
| 15 | + assert u.save | ||
| 16 | + box = Box.new | ||
| 17 | + box.owner = u | ||
| 18 | + box.number = 1000 | ||
| 19 | + assert box.save | ||
| 20 | + b.box = box | ||
| 21 | + assert !b.valid? | ||
| 22 | + assert b.errors.invalid?(:position) | ||
| 23 | + | ||
| 24 | + b.position=1 | ||
| 25 | + assert b.save | ||
| 26 | + | ||
| 27 | + assert_equal count + 1, Block.count | ||
| 28 | + end | ||
| 29 | + | ||
| 30 | + def test_box_presence | ||
| 31 | + b = Block.new | ||
| 32 | + b.position = 1000 | ||
| 33 | + assert !b.valid? | ||
| 34 | + assert b.errors.invalid?(:box_id) | ||
| 35 | + | ||
| 36 | + u = User.new | ||
| 37 | + assert u.save | ||
| 38 | + box = Box.new | ||
| 39 | + box.owner = u | ||
| 40 | + box.number = 1000 | ||
| 41 | + assert box.save | ||
| 42 | + b.box = box | ||
| 43 | + assert b.valid? | ||
| 44 | + | ||
| 9 | end | 45 | end |
| 46 | + | ||
| 47 | + def test_destroy | ||
| 48 | + b = Block.find(1) | ||
| 49 | + assert b.destroy | ||
| 50 | + end | ||
| 51 | + | ||
| 52 | + def test_valid_fixtures | ||
| 53 | + Block.find_all.each do |b| | ||
| 54 | + assert b.valid? | ||
| 55 | + end | ||
| 56 | + end | ||
| 57 | + | ||
| 10 | end | 58 | end |
test/unit/box_test.rb
| @@ -43,4 +43,10 @@ class BoxTest < Test::Unit::TestCase | @@ -43,4 +43,10 @@ class BoxTest < Test::Unit::TestCase | ||
| 43 | assert b.errors.invalid?(:number) | 43 | assert b.errors.invalid?(:number) |
| 44 | end | 44 | end |
| 45 | 45 | ||
| 46 | + def test_presence_number | ||
| 47 | + b = Box.new(:number => nil) | ||
| 48 | + assert !b.valid? | ||
| 49 | + assert b.errors.invalid?(:number) | ||
| 50 | + end | ||
| 51 | + | ||
| 46 | end | 52 | end |