Commit 44d63989a58938a3f13c1d68e59d8b6e3c2c2503

Authored by LeandroNunes
1 parent 9459a9d5

ActionItem0: validation of block and box models



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@34 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/block.rb
1 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 10 end
... ...
app/models/box.rb
... ... @@ -2,8 +2,8 @@ class Box &lt; ActiveRecord::Base
2 2 belongs_to :owner, :polymorphic => true
3 3  
4 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 8 validates_numericality_of :number, :only_integer => true, :message => _('%{fn} must be composed only of integers.')
9 9 end
... ...
app/models/user.rb
1 1 class User < ActiveRecord::Base
  2 + has_many :boxes, :as => :owner
2 3 end
... ...
test/fixtures/boxes.yml
... ... @@ -2,9 +2,15 @@
2 2 one:
3 3 id: 1
4 4 number: 1
  5 + owner_type: 'User'
  6 + owner_id: 1
5 7 two:
6 8 id: 2
7 9 number: 2
  10 + owner_type: 'User'
  11 + owner_id: 1
8 12 three:
9 13 id: 3
10 14 number: 3
  15 + owner_type: 'User'
  16 + owner_id: 1
... ...
test/fixtures/users.yml 0 → 100644
... ... @@ -0,0 +1,7 @@
  1 +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
  2 +one:
  3 + id: 1
  4 + name: 'Leandro Nunes dos Santos'
  5 +two:
  6 + id: 2
  7 + name: 'Antonio S. A. Terceiro'
... ...
test/unit/block_test.rb
... ... @@ -4,7 +4,55 @@ class BlockTest &lt; Test::Unit::TestCase
4 4 fixtures :blocks
5 5  
6 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 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 58 end
... ...
test/unit/box_test.rb
... ... @@ -43,4 +43,10 @@ class BoxTest &lt; Test::Unit::TestCase
43 43 assert b.errors.invalid?(:number)
44 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 52 end
... ...