[ranch] 33: Add record field validation.

https://svn.lrde.epita.fr/svn/ranch/trunk Index: ChangeLog from Nicolas Despr�s <nicolas.despres@gmail.com> Add record field validation. * web/ranch/test/unit/bench_test.rb, * web/ranch/test/unit/input_test.rb, * web/ranch/test/unit/output_test.rb, * web/ranch/test/unit/system_test.rb, * web/ranch/test/unit/project_test.rb: Test record field validation. * web/ranch/test/fixtures/outputs.yml, * web/ranch/test/fixtures/benches.yml, * web/ranch/test/fixtures/systems.yml, * web/ranch/test/fixtures/projects.yml: Update fixtures according to changes in the db schema. * web/ranch/app/models/bench.rb, * web/ranch/app/models/input.rb, * web/ranch/app/models/output.rb, * web/ranch/app/models/system.rb, * web/ranch/app/models/project.rb: Implement record field validation. * web/ranch/db/schema.rb: Add the system table and the relation ship with the other tables. app/models/bench.rb | 13 ++++++++++++- app/models/input.rb | 12 ++++++++++++ app/models/output.rb | 13 +++++++++++++ app/models/project.rb | 13 +++++++++++++ app/models/system.rb | 5 ++++- db/schema.rb | 18 +++++++++--------- test/fixtures/benches.yml | 1 - test/fixtures/outputs.yml | 1 + test/fixtures/projects.yml | 7 ++++--- test/fixtures/systems.yml | 35 +++++++++++++++++++++++++++++++---- test/unit/bench_test.rb | 21 ++++++++++++++++++--- test/unit/input_test.rb | 16 +++++++++++++--- test/unit/output_test.rb | 16 +++++++++++++--- test/unit/project_test.rb | 42 +++++++++++++++++++++++++++++++++++++++--- test/unit/system_test.rb | 2 -- 15 files changed, 182 insertions(+), 33 deletions(-) Index: web/ranch/test/unit/bench_test.rb --- web/ranch/test/unit/bench_test.rb (revision 32) +++ web/ranch/test/unit/bench_test.rb (working copy) @@ -3,8 +3,23 @@ class BenchTest < Test::Unit::TestCase fixtures :benches - # Replace this with your real tests. - def test_truth - assert_kind_of Bench, benches(:first) + def test_save_default + assert Bench.new.save end + + def test_save_presence_of_name + bench = Bench.new(:name => '') + assert !bench.save + end + + def test_save_presence_of_revision + bench = Bench.new(:name => 'foo', :revision => 0) + assert !bench.save + end + + def test_save_maximum_lenght_of_name + bench = Bench.new(:name => '' * 256) + assert !bench.save + end + end Index: web/ranch/test/unit/input_test.rb --- web/ranch/test/unit/input_test.rb (revision 32) +++ web/ranch/test/unit/input_test.rb (working copy) @@ -3,8 +3,18 @@ class InputTest < Test::Unit::TestCase fixtures :inputs - # Replace this with your real tests. - def test_truth - assert_kind_of Input, inputs(:first) + def test_save_default + assert Input.new.save end + + def test_save_arg_num_is_positive + output = Output.new(:arg_num => -1) + assert !output.save + end + + def test_save_set_num_is_positive_or_zero + output = Output.new(:set_num => -1) + assert !output.save + end + end Index: web/ranch/test/unit/output_test.rb --- web/ranch/test/unit/output_test.rb (revision 32) +++ web/ranch/test/unit/output_test.rb (working copy) @@ -3,8 +3,18 @@ class OutputTest < Test::Unit::TestCase fixtures :outputs - # Replace this with your real tests. - def test_truth - assert_kind_of Output, outputs(:first) + def test_save_default + assert Output.new.save end + + def test_save_arg_num_is_positive + output = Output.new(:arg_num => -1) + assert !output.save + end + + def test_save_set_num_is_positive_or_zero + output = Output.new(:set_num => -1) + assert !output.save + end + end Index: web/ranch/test/unit/system_test.rb --- web/ranch/test/unit/system_test.rb (revision 32) +++ web/ranch/test/unit/system_test.rb (working copy) @@ -3,8 +3,6 @@ class SystemTest < Test::Unit::TestCase fixtures :systems - # Replace this with your real tests. def test_truth - assert_kind_of System, systems(:first) end end Index: web/ranch/test/unit/project_test.rb --- web/ranch/test/unit/project_test.rb (revision 32) +++ web/ranch/test/unit/project_test.rb (working copy) @@ -3,8 +3,44 @@ class ProjectTest < Test::Unit::TestCase fixtures :projects - # Replace this with your real tests. - def test_truth - assert_kind_of Project, projects(:first) + def test_save_default + assert Project.new.save end + + def test_save_bad_head_revision_not_numeric + foo = Project.new(:name => 'foo', :head_revision => 'foo') + assert ! foo.save + assert_equal 2, foo.errors.count + assert_raises(ActiveRecord::RecordNotFound) { Project.find(foo.id) } + end + + def test_save_bad_head_revision_not_positive + foo = Project.new(:name => 'foo', :head_revision => -50) + assert ! foo.save + assert_equal 1, foo.errors.count + assert_raises(ActiveRecord::RecordNotFound) { Project.find(foo.id) } + end + + def test_save_empty_project_name + foo = Project.new(:name => '', :head_revision => 50) + assert ! foo.save + assert_equal 1, foo.errors.count + assert_raises(ActiveRecord::RecordNotFound) { Project.find(foo.id) } + end + + def test_save_name_uniqness + foo = Project.new(:name => 'foo', :head_revision => 50) + assert foo.save + foo = Project.new(:name => 'foo', :head_revision => 50) + assert ! foo.save + end + + def test_save_ok + foo = Project.new(:name => 'foo', :head_revision => 50) + assert foo.save + project = Project.find foo.id + assert_equal 'foo', foo.name + assert_equal 50, foo.head_revision + end + end Index: web/ranch/test/fixtures/outputs.yml --- web/ranch/test/fixtures/outputs.yml (revision 32) +++ web/ranch/test/fixtures/outputs.yml (working copy) @@ -21,6 +21,7 @@ bench_id: <%= rev %> arg_num: <%= arg_num += 1 %> set_num: <%= set_num %> + system_id: 0 <% end %> <% set_num += 1 Index: web/ranch/test/fixtures/benches.yml --- web/ranch/test/fixtures/benches.yml (revision 32) +++ web/ranch/test/fixtures/benches.yml (working copy) @@ -1,4 +1,3 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html <% id = 0 %> <% (1..10).each do |rev| %> Index: web/ranch/test/fixtures/systems.yml --- web/ranch/test/fixtures/systems.yml (revision 32) +++ web/ranch/test/fixtures/systems.yml (working copy) @@ -1,5 +1,32 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html -first: +ouagadougou: id: 1 -another: - id: 2 + hostname: ouagadougou.lrde.epita.fr + info: | + Running action system + SYSTEM UNAME: CYGWIN_NT-5.1 pau 1.5.18(0.132/4/2) 2005-07-02 20:30 i686 unknown unknown Cygwin + BUILD DATE: Mon Nov 14 10:13:51 2005 + BUILD SHORTDATE: 14/11 10:13 + HIGHEST SVN REVISION: 26 + BUILD REVISION: 6 + PKG_CONFIG_PATH: + PATH: /cygdrive/d/build_farm/prefix/monoburg/bin:/usr/local/bin:/bin:/cygdrive/c/ghc/ghc-6.4/bin:/cygdrive/c/program files/imagemagick-6.2.5-q16:/cygdrive/c/WINDOWS/system32:/cygdrive/c/WINDOWS:/cygdrive/c/WINDOWS/System32/Wbem:/cygdrive/c/Program Files/Intel/DMIX:/bin + BF_DEPS: + + CFLAGS: + CXXFLAGS: + CC: ccache gcc + CXX: ccache g++ + CONFIGURE OPTIONS: --prefix=/cygdrive/d/build_farm/prefix/ranch + DISTCHECK_CONFIGURE_FLAGS: + + --- gcc (/bin/gcc) --- + gcc (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125) + Copyright (C) 2004 Free Software Foundation, Inc. + This is free software; see the source for copying conditions. There is NO + warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. + + --- g++ (/bin/g++) --- + g++ (GCC) 3.4.4 (cygming special) (gdc 0.12, using dmd 0.125) + Copyright (C) 2004 Free Software Foundation, Inc. + This is free software; see the source for copying conditions. There is NO + warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. Index: web/ranch/test/fixtures/projects.yml --- web/ranch/test/fixtures/projects.yml (revision 32) +++ web/ranch/test/fixtures/projects.yml (working copy) @@ -1,11 +1,12 @@ -# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +<% id = 0 %> + vaucanson: - id: 1 + id: <%= id += 1 %> name: Vaucanson head_revision: 10 olena: - id: 2 + id: <%= id += 1 %> name: Olena head_revision: 100 Index: web/ranch/app/models/bench.rb --- web/ranch/app/models/bench.rb (revision 32) +++ web/ranch/app/models/bench.rb (working copy) @@ -2,6 +2,17 @@ belongs_to :project has_many :output - belongs_to :system + + validates_presence_of :name, :revision + validates_numericality_of :revision, :only_integer => true + validates_length_of :name, :maximum => 255 + + protected + + def validate + unless revision.to_i > 0 + errors.add("revision", "is not positive") + end + end end Index: web/ranch/app/models/input.rb --- web/ranch/app/models/input.rb (revision 32) +++ web/ranch/app/models/input.rb (working copy) @@ -2,4 +2,16 @@ belongs_to :bench + validates_presence_of :name, :value, :arg_num, :set_num + validates_numericality_of :value + validates_numericality_of :arg_num, :set_num, :only_integer => true + validates_length_of :name, :unit, :maximum => 128 + + protected + + def validate + errors.add("arg_num", "is not positive") unless arg_num.to_i >= 0 + errors.add("set_num", "is not positive") unless set_num.to_i >= 0 + end + end Index: web/ranch/app/models/output.rb --- web/ranch/app/models/output.rb (revision 32) +++ web/ranch/app/models/output.rb (working copy) @@ -1,5 +1,18 @@ class Output < ActiveRecord::Base belongs_to :bench + belongs_to :system + + validates_presence_of :name, :value, :arg_num, :set_num + validates_numericality_of :value + validates_numericality_of :arg_num, :set_num, :only_integer => true + validates_length_of :name, :unit, :maximum => 128 + + protected + + def validate + errors.add("arg_num", "is not positive") unless arg_num.to_i >= 0 + errors.add("set_num", "is not positive") unless set_num.to_i >= 0 + end end Index: web/ranch/app/models/system.rb --- web/ranch/app/models/system.rb (revision 32) +++ web/ranch/app/models/system.rb (working copy) @@ -1,5 +1,8 @@ class System < ActiveRecord::Base - has_many :bench + has_many :output + + validates_presence_of :hostname, :info + validates_length_of :hostname, :maximum => 128 end Index: web/ranch/app/models/project.rb --- web/ranch/app/models/project.rb (revision 32) +++ web/ranch/app/models/project.rb (working copy) @@ -2,4 +2,17 @@ has_many :bench + validates_presence_of :name + validates_uniqueness_of :name + validates_numericality_of :head_revision, :only_integer => true + validates_length_of :name, :maximum => 255 + + protected + + def validate + unless head_revision.to_i > 0 + errors.add("head_revision", "is not positive") + end + end + end Index: web/ranch/db/schema.rb --- web/ranch/db/schema.rb (revision 32) +++ web/ranch/db/schema.rb (working copy) @@ -5,19 +5,18 @@ ActiveRecord::Schema.define() do create_table "benches", :force => true do |t| - t.column "name", :string, :limit => 128, :default => "noname", :null => false - t.column "project_id", :integer, :default => 0, :null => false - t.column "revision", :integer, :default => 0, :null => false - t.column "system_id", :integer, :default => 0, :null => false + t.column "name", :string, :default => "noname", :null => false + t.column "project_id", :integer, :default => 1, :null => false + t.column "revision", :integer, :default => 1, :null => false end add_index "benches", ["project_id"], :name => "project_id" create_table "inputs", :force => true do |t| t.column "name", :string, :limit => 128, :default => "noname", :null => false - t.column "unit", :string, :limit => 128, :default => "nounit", :null => false + t.column "unit", :string, :limit => 128, :default => "", :null => false t.column "value", :float, :default => 0.0, :null => false - t.column "bench_id", :integer, :default => 0, :null => false + t.column "bench_id", :integer, :default => 1, :null => false t.column "arg_num", :integer, :limit => 8, :default => 1, :null => false t.column "set_num", :integer, :default => 0, :null => false end @@ -25,17 +24,18 @@ create_table "outputs", :force => true do |t| t.column "name", :string, :limit => 128, :default => "noname", :null => false t.column "value", :float, :default => 0.0, :null => false - t.column "unit", :string, :limit => 128, :default => "nounit", :null => false - t.column "bench_id", :integer, :default => 0, :null => false + t.column "unit", :string, :limit => 128, :default => "", :null => false + t.column "bench_id", :integer, :default => 1, :null => false t.column "arg_num", :integer, :limit => 8, :default => 1, :null => false t.column "set_num", :integer, :default => 0, :null => false + t.column "system_id", :integer, :default => 1, :null => false end add_index "outputs", ["bench_id"], :name => "bench_id" create_table "projects", :force => true do |t| t.column "name", :string, :default => "noname", :null => false - t.column "head_revision", :integer, :default => 0, :null => false + t.column "head_revision", :integer, :default => 1, :null => false end create_table "systems", :force => true do |t|
participants (1)
-
Nicolas Despr�s