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 | 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 < 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
test/fixtures/boxes.yml
test/unit/block_test.rb
| ... | ... | @@ -4,7 +4,55 @@ class BlockTest < 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