# HG changeset patch # User Jittat Fakcharoenphol # Date 2010-03-02 18:33:21 # Node ID 3720a1af46c5570ed3633f73106bd8b8cea7cb8b # Parent 25b80877ebced107db4be463e3f8fda98a449b6a started cleaning up tests, fixed fixtures loading error, removed error when contest time limit is uninitialized diff --git a/app/controllers/main_controller.rb b/app/controllers/main_controller.rb --- a/app/controllers/main_controller.rb +++ b/app/controllers/main_controller.rb @@ -327,7 +327,7 @@ def update_user_start_time user = User.find(session[:user_id]) - UserContestStat.update_user_start_time(user) + user.update_start_time end def reject_announcement_refresh_when_logged_out diff --git a/app/models/configuration.rb b/app/models/configuration.rb --- a/app/models/configuration.rb +++ b/app/models/configuration.rb @@ -8,18 +8,24 @@ SYSTEM_MODE_CONF_KEY = 'system.mode' TEST_REQUEST_EARLY_TIMEOUT_KEY = 'contest.test_request.early_timeout' - # set @@cache = true to only reload once. - @@cache = false + cattr_accessor :cache + cattr_accessor :config_cache + cattr_accessor :task_grading_info + cattr_accessor :contest_time_str + cattr_accessor :contest_time - @@configurations = nil - @@task_grading_info = nil + # set @@cache = true to only reload once. + Configuration.cache = false + + Configuration.config_cache = nil + Configuration.task_grading_info = nil def self.get(key) - if @@cache - if @@configurations == nil + if Configuration.cache + if Configuration.config_cache == nil self.read_config end - return @@configurations[key] + return Configuration.config_cache[key] else return Configuration.read_one_key(key) end @@ -34,15 +40,15 @@ end def self.clear - @@configurations = nil + Configuration.config_cache = nil end def self.cache? - @@cache + Configuration.cache end def self.enable_caching - @@cache = true + Configuration.cache = true end # @@ -82,10 +88,10 @@ end def self.task_grading_info - if @@task_grading_info==nil + if Configuration.task_grading_info==nil read_grading_info end - return @@task_grading_info + return Configuration.task_grading_info end def self.standard_mode? @@ -112,22 +118,22 @@ def self.contest_time_limit contest_time_str = Configuration['contest.time_limit'] - if not defined? @@contest_time_str - @@contest_time_str = nil + if not defined? Configuration.contest_time_str + Configuration.contest_time_str = nil end - if @@contest_time_str != contest_time_str - @@contest_time_str = contest_time_str + if Configuration.contest_time_str != contest_time_str + Configuration.contest_time_str = contest_time_str if tmatch = /(\d+):(\d+)/.match(contest_time_str) h = tmatch[1].to_i m = tmatch[2].to_i - @@contest_time = h.hour + m.minute + Configuration.contest_time = h.hour + m.minute else - @@contest_time = nil + Configuration.contest_time = nil end end - return @@contest_time + return Configuration.contest_time end protected @@ -146,11 +152,11 @@ end def self.read_config - @@configurations = {} + Configuration.config_cache = {} Configuration.find(:all).each do |conf| key = conf.key val = conf.value - @@configurations[key] = Configuration.convert_type(val,conf.value_type) + Configuration.config_cache[key] = Configuration.convert_type(val,conf.value_type) end end @@ -165,7 +171,7 @@ def self.read_grading_info f = File.open(TASK_GRADING_INFO_FILENAME) - @@task_grading_info = YAML.load(f) + Configuration.task_grading_info = YAML.load(f) f.close end diff --git a/app/models/user.rb b/app/models/user.rb --- a/app/models/user.rb +++ b/app/models/user.rb @@ -130,6 +130,9 @@ return site.time_left elsif Configuration.indv_contest_mode? time_limit = Configuration.contest_time_limit + if time_limit == nil + return nil + end if contest_stat==nil return (Time.now.gmtime + time_limit) - Time.now.gmtime else @@ -170,6 +173,15 @@ end end + def update_start_time + stat = self.contest_stat + if stat == nil + stat = UserContestStat.new(:user => self, + :started_at => Time.now.gmtime) + stat.save + end + end + protected def encrypt_new_password return if password.blank? diff --git a/app/models/user_contest_stat.rb b/app/models/user_contest_stat.rb --- a/app/models/user_contest_stat.rb +++ b/app/models/user_contest_stat.rb @@ -2,13 +2,4 @@ belongs_to :user - def self.update_user_start_time(user) - stat = user.contest_stat - if stat == nil - stat = UserContestStat.new(:user => user, - :started_at => Time.now.gmtime) - stat.save - end - end - end diff --git a/db/migrate/020_add_graders_right_to_admin_role.rb b/db/migrate/020_add_graders_right_to_admin_role.rb --- a/db/migrate/020_add_graders_right_to_admin_role.rb +++ b/db/migrate/020_add_graders_right_to_admin_role.rb @@ -12,6 +12,6 @@ def self.down graders_right = Right.find_by_name('graders_admin') - graders_right.destroy + graders_right.destroy if graders_right end end diff --git a/db/migrate/025_add_site_to_user_and_add_default_site.rb b/db/migrate/025_add_site_to_user_and_add_default_site.rb --- a/db/migrate/025_add_site_to_user_and_add_default_site.rb +++ b/db/migrate/025_add_site_to_user_and_add_default_site.rb @@ -24,6 +24,6 @@ remove_column :users, :site_id default_site = Site.find_by_name('default') - default_site.destroy + default_site.destroy if default_site end end diff --git a/db/schema.rb b/db/schema.rb --- a/db/schema.rb +++ b/db/schema.rb @@ -101,18 +101,16 @@ end create_table "problems", :force => true do |t| - t.string "name", :limit => 30 - t.string "full_name" - t.integer "full_score" - t.date "date_added" - t.boolean "available" - t.string "url" - t.integer "description_id" - t.boolean "test_allowed" - t.boolean "output_only" - t.integer "level", :default => 0 - t.datetime "updated_at" - t.string "description_filename" + t.string "name", :limit => 30 + t.string "full_name" + t.integer "full_score" + t.date "date_added" + t.boolean "available" + t.string "url" + t.integer "description_id" + t.boolean "test_allowed" + t.boolean "output_only" + t.string "description_filename" end create_table "rights", :force => true do |t| @@ -210,7 +208,6 @@ t.text "solution", :limit => 16777215 t.datetime "created_at" t.datetime "updated_at" - t.integer "number" end create_table "test_requests", :force => true do |t| @@ -243,27 +240,17 @@ end create_table "users", :force => true do |t| - t.string "login", :limit => 50 + t.string "login", :limit => 50 t.string "full_name" t.string "hashed_password" - t.string "salt", :limit => 5 + t.string "salt", :limit => 5 t.string "alias" t.string "email" t.integer "site_id" t.integer "country_id" - t.boolean "activated", :default => false + t.boolean "activated", :default => false t.datetime "created_at" t.datetime "updated_at" - t.string "member1_full_name" - t.string "member2_full_name" - t.string "member3_full_name" - t.boolean "high_school" - t.string "member1_school_name" - t.string "member2_school_name" - t.string "member3_school_name" - t.string "school_name" - t.string "province" - t.integer "year" end add_index "users", ["login"], :name => "index_users_on_login", :unique => true diff --git a/spec/controllers/main_controller_spec.rb b/spec/controllers/main_controller_spec.rb --- a/spec/controllers/main_controller_spec.rb +++ b/spec/controllers/main_controller_spec.rb @@ -25,6 +25,7 @@ it "should let user sees her own source" do Submission.should_receive(:find).with(@submission.id.to_s).and_return(@submission) User.should_receive(:find).with(1).and_return(@user) + @user.should_receive(:update_start_time) get 'source', {:id => @submission.id}, {:user_id => 1} response.should be_success end diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -18,7 +18,7 @@ # in your config/boot.rb config.use_transactional_fixtures = true config.use_instantiated_fixtures = false - config.fixture_path = RAILS_ROOT + '/spec/fixtures/' + config.fixture_path = RAILS_ROOT + '/test/fixtures/' # == Fixtures # diff --git a/test/fixtures/users.yml b/test/fixtures/users.yml --- a/test/fixtures/users.yml +++ b/test/fixtures/users.yml @@ -3,16 +3,18 @@ <% User.public_class_method :encrypt -SALT = "abc" +salt = "abc" %> john: login: john - hashed_password: <%= User.encrypt("hello",SALT) %> - salt: <%= SALT %> + full_name: john + hashed_password: <%= User.encrypt("hello",salt) %> + salt: <%= salt %> mary: login: mary - hashed_password: <%= User.encrypt("goodbye",SALT) %> - salt: <%= SALT %> + full_name: mary + hashed_password: <%= User.encrypt("goodbye",salt) %> + salt: <%= salt %> roles: admin diff --git a/test/functional/announcements_controller_test.rb b/test/functional/announcements_controller_test.rb --- a/test/functional/announcements_controller_test.rb +++ b/test/functional/announcements_controller_test.rb @@ -1,45 +1,4 @@ require File.dirname(__FILE__) + '/../test_helper' class AnnouncementsControllerTest < ActionController::TestCase - def test_should_get_index - get :index - assert_response :success - assert_not_nil assigns(:announcements) - end - - def test_should_get_new - get :new - assert_response :success - end - - def test_should_create_announcement - assert_difference('Announcement.count') do - post :create, :announcement => { } - end - - assert_redirected_to announcement_path(assigns(:announcement)) - end - - def test_should_show_announcement - get :show, :id => announcements(:one).id - assert_response :success - end - - def test_should_get_edit - get :edit, :id => announcements(:one).id - assert_response :success - end - - def test_should_update_announcement - put :update, :id => announcements(:one).id, :announcement => { } - assert_redirected_to announcement_path(assigns(:announcement)) - end - - def test_should_destroy_announcement - assert_difference('Announcement.count', -1) do - delete :destroy, :id => announcements(:one).id - end - - assert_redirected_to announcements_path - end end diff --git a/test/functional/contests_controller_test.rb b/test/functional/contests_controller_test.rb --- a/test/functional/contests_controller_test.rb +++ b/test/functional/contests_controller_test.rb @@ -1,45 +1,4 @@ require 'test_helper' class ContestsControllerTest < ActionController::TestCase - test "should get index" do - get :index - assert_response :success - assert_not_nil assigns(:contests) - end - - test "should get new" do - get :new - assert_response :success - end - - test "should create contest" do - assert_difference('Contest.count') do - post :create, :contest => { } - end - - assert_redirected_to contest_path(assigns(:contest)) - end - - test "should show contest" do - get :show, :id => contests(:one).to_param - assert_response :success - end - - test "should get edit" do - get :edit, :id => contests(:one).to_param - assert_response :success - end - - test "should update contest" do - put :update, :id => contests(:one).to_param, :contest => { } - assert_redirected_to contest_path(assigns(:contest)) - end - - test "should destroy contest" do - assert_difference('Contest.count', -1) do - delete :destroy, :id => contests(:one).to_param - end - - assert_redirected_to contests_path - end end diff --git a/test/functional/login_controller_test.rb b/test/functional/login_controller_test.rb --- a/test/functional/login_controller_test.rb +++ b/test/functional/login_controller_test.rb @@ -4,7 +4,7 @@ # Re-raise errors caught by the controller. class LoginController; def rescue_action(e) raise e end; end -class LoginControllerTest < Test::Unit::TestCase +class LoginControllerTest < ActionController::TestCase fixtures :users diff --git a/test/functional/main_controller_test.rb b/test/functional/main_controller_test.rb --- a/test/functional/main_controller_test.rb +++ b/test/functional/main_controller_test.rb @@ -1,24 +1,12 @@ -require File.dirname(__FILE__) + '/../test_helper' -require 'main_controller' +require File.dirname(__FILE__) + '/../test_helper' -# Re-raise errors caught by the controller. -class MainController; def rescue_action(e) raise e end; end - -class MainControllerTest < Test::Unit::TestCase - +class MainControllerTest < ActionController::TestCase + fixtures :users fixtures :problems - fixtures :users - def setup - @controller = MainController.new - @request = ActionController::TestRequest.new - @response = ActionController::TestResponse.new - end - - # Replace this with your real tests. def test_should_redirect_new_user_to_login get :list - assert_redirected_to :action => 'login' + assert_redirected_to :controller => 'main', :action => 'login' end def test_should_list_available_problems_if_logged_in diff --git a/test/functional/problems_controller_test.rb b/test/functional/problems_controller_test.rb --- a/test/functional/problems_controller_test.rb +++ b/test/functional/problems_controller_test.rb @@ -4,7 +4,7 @@ # Re-raise errors caught by the controller. class ProblemsController; def rescue_action(e) raise e end; end -class ProblemsControllerTest < Test::Unit::TestCase +class ProblemsControllerTest < ActionController::TestCase fixtures :problems def setup diff --git a/test/functional/sites_controller_test.rb b/test/functional/sites_controller_test.rb --- a/test/functional/sites_controller_test.rb +++ b/test/functional/sites_controller_test.rb @@ -1,45 +1,4 @@ require File.dirname(__FILE__) + '/../test_helper' class SitesControllerTest < ActionController::TestCase - def test_should_get_index - get :index - assert_response :success - assert_not_nil assigns(:sites) - end - - def test_should_get_new - get :new - assert_response :success - end - - def test_should_create_site - assert_difference('Site.count') do - post :create, :site => { } - end - - assert_redirected_to site_path(assigns(:site)) - end - - def test_should_show_site - get :show, :id => sites(:one).id - assert_response :success - end - - def test_should_get_edit - get :edit, :id => sites(:one).id - assert_response :success - end - - def test_should_update_site - put :update, :id => sites(:one).id, :site => { } - assert_redirected_to site_path(assigns(:site)) - end - - def test_should_destroy_site - assert_difference('Site.count', -1) do - delete :destroy, :id => sites(:one).id - end - - assert_redirected_to sites_path - end end diff --git a/test/functional/user_admin_controller_test.rb b/test/functional/user_admin_controller_test.rb --- a/test/functional/user_admin_controller_test.rb +++ b/test/functional/user_admin_controller_test.rb @@ -4,7 +4,7 @@ # Re-raise errors caught by the controller. class UserAdminController; def rescue_action(e) raise e end; end -class UserAdminControllerTest < Test::Unit::TestCase +class UserAdminControllerTest < ActionController::TestCase fixtures :users fixtures :roles fixtures :rights diff --git a/test/functional/users_controller_test.rb b/test/functional/users_controller_test.rb --- a/test/functional/users_controller_test.rb +++ b/test/functional/users_controller_test.rb @@ -4,7 +4,7 @@ # Re-raise errors caught by the controller. class UsersController; def rescue_action(e) raise e end; end -class UsersControllerTest < Test::Unit::TestCase +class UsersControllerTest < ActionController::TestCase def setup @controller = UsersController.new @request = ActionController::TestRequest.new diff --git a/test/test_helper.rb b/test/test_helper.rb --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -2,7 +2,7 @@ require File.expand_path(File.dirname(__FILE__) + "/../config/environment") require 'test_help' -class Test::Unit::TestCase +class ActiveSupport::TestCase # Transactional fixtures accelerate your tests by wrapping each test method # in a transaction that's rolled back on completion. This ensures that the # test database remains unchanged so your fixtures don't have to be reloaded @@ -15,7 +15,8 @@ # in MySQL. Turn off transactional fixtures in this case; however, if you # don't care one way or the other, switching from MyISAM to InnoDB tables # is recommended. - self.use_transactional_fixtures = true + + self.use_transactional_fixtures = false # Instantiated fixtures are slow, but give you @david where otherwise you # would need people(:david). If you don't want to migrate your existing