From bb51ea9db5738e525e6609643f1d5ef32e234e66 Mon Sep 17 00:00:00 2001 From: lukemelia Date: Mon, 19 Jun 2006 04:04:20 +0000 Subject: [PATCH] Get all the tests passing again, in preparation to add another validation on the project and context models. git-svn-id: http://www.rousette.org.uk/svn/tracks-repos/trunk@264 a4c988fc-2ded-0310-b66e-134b36920a42 --- tracks/app/controllers/todo_controller.rb | 26 +++++++-------- tracks/app/models/project.rb | 2 +- tracks/db/schema.rb | 29 +++++++++-------- .../test/functional/login_controller_test.rb | 18 +++++------ tracks/test/unit/project_test.rb | 32 +++++++++++++++++-- tracks/test/unit/todo_test.rb | 6 ++-- tracks/test/unit/user_test.rb | 4 +-- 7 files changed, 72 insertions(+), 45 deletions(-) diff --git a/tracks/app/controllers/todo_controller.rb b/tracks/app/controllers/todo_controller.rb index e3c69a39..7d3606e6 100644 --- a/tracks/app/controllers/todo_controller.rb +++ b/tracks/app/controllers/todo_controller.rb @@ -67,23 +67,23 @@ class TodoController < ApplicationController self.init # we have to do this again to update @todos @up_count = @todos.reject { |x| x.done? or x.context.hide? }.size.to_s end - return if request.xhr? - # fallback for standard requests - if @saved - flash["notice"] = 'Added new next action.' - redirect_to :action => 'list' - else - flash["warning"] = 'The next action was not added. Please try again.' - redirect_to :action => 'list' + respond_to do |wants| + # if @saved + # flash["notice"] = 'Added new next action.' + # else + # flash["warning"] = 'The next action was not added. Please try again.' + # end + wants.html { redirect_to :action => 'list' } + wants.js + wants.xml { render :xml => @item.to_xml( :root => 'todo', :except => :user_id ) } end rescue - if request.xhr? # be sure to include an error.rjs - render :action => 'error' - else - flash["warning"] = 'An error occurred on the server.' - render :action => 'list' + respond_to do |wants| + wants.html { render :action => 'list' } # flash["warning"] = 'An error occurred on the server.' render :action => 'list' }# flash["warning"] = 'An error occurred on the server.' # render :action => 'list' + wants.js { render :action => 'error' } + wants.xml { render :text => 'An error occurred on the server.' + $! } end end diff --git a/tracks/app/models/project.rb b/tracks/app/models/project.rb index d0873a18..2cbaa720 100644 --- a/tracks/app/models/project.rb +++ b/tracks/app/models/project.rb @@ -10,7 +10,7 @@ class Project < ActiveRecord::Base # Project name must not be empty # and must be less than 255 bytes validates_presence_of :name, :message => "project must have a name" - validates_length_of :name, :maximum => 255, :message => "project name must be less than %d" + validates_length_of :name, :maximum => 255, :message => "project name must be less than 256 characters" validates_uniqueness_of :name, :message => "already exists", :scope =>"user_id" def self.list_of(isdone=0) diff --git a/tracks/db/schema.rb b/tracks/db/schema.rb index 7bfab182..1b9608ac 100644 --- a/tracks/db/schema.rb +++ b/tracks/db/schema.rb @@ -5,26 +5,27 @@ ActiveRecord::Schema.define(:version => 8) do create_table "contexts", :force => true do |t| - t.column "name", :string, :null => false - t.column "position", :integer, :null => false + t.column "name", :string, :default => "", :null => false + t.column "position", :integer, :default => 0, :null => false t.column "hide", :boolean, :default => false - t.column "user_id", :integer, :default => 1 + t.column "user_id", :integer, :default => 1, :null => false end create_table "notes", :force => true do |t| - t.column "user_id", :integer, :null => false - t.column "project_id", :integer, :null => false + t.column "user_id", :integer, :default => 1, :null => false + t.column "project_id", :integer, :default => 0, :null => false t.column "body", :text t.column "created_at", :datetime t.column "updated_at", :datetime end create_table "projects", :force => true do |t| - t.column "name", :string, :null => false - t.column "position", :integer, :null => false + t.column "name", :string, :default => "", :null => false + t.column "position", :integer, :default => 0, :null => false t.column "done", :boolean, :default => false - t.column "user_id", :integer, :default => 1 - t.column "description", :text + t.column "user_id", :integer, :default => 1, :null => false + t.column "description", :string, :default => "" + t.column "linkurl", :string, :default => "" end create_table "sessions", :force => true do |t| @@ -36,22 +37,22 @@ ActiveRecord::Schema.define(:version => 8) do add_index "sessions", ["session_id"], :name => "sessions_session_id_index" create_table "todos", :force => true do |t| - t.column "context_id", :integer, :null => false + t.column "context_id", :integer, :default => 0, :null => false t.column "project_id", :integer - t.column "description", :string, :null => false + t.column "description", :string, :default => "", :null => false t.column "notes", :text t.column "done", :boolean, :default => false, :null => false t.column "created_at", :datetime t.column "due", :date t.column "completed", :datetime - t.column "user_id", :integer, :default => 1 + t.column "user_id", :integer, :default => 1, :null => false t.column "type", :string, :default => "Immediate", :null => false t.column "show_from", :date end create_table "users", :force => true do |t| - t.column "login", :string, :limit => 80, :null => false - t.column "password", :string, :limit => 40, :null => false + t.column "login", :string, :limit => 80, :default => "", :null => false + t.column "password", :string, :limit => 40, :default => "", :null => false t.column "word", :string t.column "is_admin", :boolean, :default => false, :null => false t.column "preferences", :text diff --git a/tracks/test/functional/login_controller_test.rb b/tracks/test/functional/login_controller_test.rb index b669f1bd..4d046752 100644 --- a/tracks/test/functional/login_controller_test.rb +++ b/tracks/test/functional/login_controller_test.rb @@ -32,7 +32,7 @@ class LoginControllerTest < Test::Unit::TestCase user = login('admin', 'abracadabra', 'on') assert_equal user.id, @response.session['user_id'] assert_equal user.login, "admin" - assert_equal user.is_admin, 1 + assert user.is_admin assert_equal "Login successful: session will not expire.", flash['notice'] assert_redirect_url "http://#{@request.host}/bogus/location" end @@ -42,7 +42,7 @@ class LoginControllerTest < Test::Unit::TestCase user = login('jane','sesame', 'off') assert_equal user.id, @response.session['user_id'] assert_equal user.login, "jane" - assert_equal user.is_admin, 0 + assert !user.is_admin assert_equal "Login successful: session will expire after 1 hour of inactivity.", flash['notice'] assert_redirected_to :controller => 'todo', :action => 'list' end @@ -79,19 +79,19 @@ class LoginControllerTest < Test::Unit::TestCase # def test_create admin = login('admin', 'abracadabra', 'on') - assert_equal admin.is_admin, 1 + assert admin.is_admin newbie = create('newbie', 'newbiepass') assert_equal "Signup successful for user newbie.", flash['notice'] assert_redirected_to :controller => 'todo', :action => 'list' assert_valid newbie get :logout # logout the admin user assert_equal newbie.login, "newbie" - assert_equal newbie.is_admin, 0 + assert !newbie.is_admin assert_not_nil newbie.preferences # have user preferences been created? user = login('newbie', 'newbiepass', 'on') # log in the new user assert_redirected_to :controller => 'todo', :action => 'list' assert_equal 'newbie', user.login - assert_equal user.is_admin, 0 + assert !user.is_admin num_users = User.find(:all) assert_equal num_users.length, 3 end @@ -100,7 +100,7 @@ class LoginControllerTest < Test::Unit::TestCase # def test_create_by_non_admin non_admin = login('jane', 'sesame', 'on') - assert_equal non_admin.is_admin, 0 + assert !non_admin.is_admin post :signup, :user => {:login => 'newbie2', :password => 'newbiepass2', :password_confirmation => 'newbiepass2'} assert_template 'login/nosignup' @@ -114,7 +114,7 @@ class LoginControllerTest < Test::Unit::TestCase def test_create_with_invalid_password admin = login('admin', 'abracadabra', 'on') - assert_equal admin.is_admin, 1 + assert admin.is_admin assert_equal admin.id, @response.session['user_id'] post :create, :user => {:login => 'newbie', :password => '', :password_confirmation => ''} num_users = User.find(:all) @@ -124,7 +124,7 @@ class LoginControllerTest < Test::Unit::TestCase def test_create_with_invalid_user admin = login('admin', 'abracadabra', 'on') - assert_equal admin.is_admin, 1 + assert admin.is_admin assert_equal admin.id, @response.session['user_id'] post :create, :user => {:login => 'n', :password => 'newbiepass', :password_confirmation => 'newbiepass'} num_users = User.find(:all) @@ -136,7 +136,7 @@ class LoginControllerTest < Test::Unit::TestCase # def test_validate_uniqueness_of_login admin = login('admin', 'abracadabra', 'on') - assert_equal admin.is_admin, 1 + assert admin.is_admin assert_equal admin.id, @response.session['user_id'] post :create, :user => {:login => 'jane', :password => 'newbiepass', :password_confirmation => 'newbiepass'} num_users = User.find(:all) diff --git a/tracks/test/unit/project_test.rb b/tracks/test/unit/project_test.rb index 42820373..c669f549 100644 --- a/tracks/test/unit/project_test.rb +++ b/tracks/test/unit/project_test.rb @@ -3,8 +3,34 @@ require File.dirname(__FILE__) + '/../test_helper' class ProjectTest < Test::Unit::TestCase fixtures :projects - # Replace this with your real tests. - def test_truth - assert true + def setup + @timemachine = Project.find(1) + @moremoney = Project.find(2) end + + def test_validate_presence_of_name + assert_equal "Build a working time machine", @timemachine.name + @timemachine.name = "" + assert !@timemachine.save + assert_equal 1, @timemachine.errors.count + assert_equal "project must have a name", @timemachine.errors.on(:name) + end + + def test_validate_name_is_less_than_256 + assert_equal "Build a working time machine", @timemachine.name + @timemachine.name = "a"*256 + assert !@timemachine.save + assert_equal 1, @timemachine.errors.count + assert_equal "project name must be less than 256 characters", @timemachine.errors.on(:name) + end + + def test_validate_name_is_unique + assert_equal "Build a working time machine", @timemachine.name + newproj = Project.new + newproj.name = "Build a working time machine" + assert !newproj.save + assert_equal 1, newproj.errors.count + assert_equal "already exists", newproj.errors.on(:name) + end + end diff --git a/tracks/test/unit/todo_test.rb b/tracks/test/unit/todo_test.rb index d556267a..86c0633b 100644 --- a/tracks/test/unit/todo_test.rb +++ b/tracks/test/unit/todo_test.rb @@ -18,8 +18,8 @@ class TodoTest < Test::Unit::TestCase assert_equal 2, @not_completed1.project_id assert_equal "Call Bill Gates to find out how much he makes per day", @not_completed1.description assert_nil @not_completed1.notes - assert_equal 0, @not_completed1.done - assert_equal 1.week.ago.to_s(:db), @not_completed1.created_at.strftime("%Y-%m-%d %H:%M:%S") + assert !@not_completed1.done + assert_equal 1.week.ago.strftime("%Y-%m-%d %H:%M"), @not_completed1.created_at.strftime("%Y-%m-%d %H:%M") assert_equal 2.week.from_now.strftime("%Y-%m-%d"), @not_completed1.due.strftime("%Y-%m-%d") assert_nil @not_completed1.completed assert_equal 1, @not_completed1.user_id @@ -27,7 +27,7 @@ class TodoTest < Test::Unit::TestCase def test_completed assert_kind_of Todo, @completed - assert_equal 1, @completed.done + assert @completed.done assert_not_nil @completed.completed end diff --git a/tracks/test/unit/user_test.rb b/tracks/test/unit/user_test.rb index c9bba10d..546696e4 100644 --- a/tracks/test/unit/user_test.rb +++ b/tracks/test/unit/user_test.rb @@ -18,7 +18,7 @@ class UserTest < Test::Unit::TestCase assert_equal "admin", @admin_user.login assert_equal "#{Digest::SHA1.hexdigest("#{SALT}--abracadabra--")}", @admin_user.password assert_not_nil @admin_user.word - assert_equal 1, @admin_user.is_admin + assert @admin_user.is_admin end # Test a non-admin user model @@ -28,7 +28,7 @@ class UserTest < Test::Unit::TestCase assert_equal "jane", @other_user.login assert_equal "#{Digest::SHA1.hexdigest("#{SALT}--sesame--")}", @other_user.password assert_not_nil @other_user.word - assert_equal 0, @other_user.is_admin + assert !@other_user.is_admin end # ============================================