From f8ab7366775a941960d941d6fea2c7194fa988ed Mon Sep 17 00:00:00 2001 From: albert Date: Fri, 19 Nov 2010 17:20:13 -0500 Subject: [PATCH] fixed tests --- app/controllers/favorites_controller.rb | 8 ++++++-- app/models/dmail.rb | 6 +++--- app/models/forum_topic.rb | 2 +- app/models/ip_ban.rb | 2 -- app/models/janitor_trial.rb | 8 +------- app/models/user.rb | 21 +++++++-------------- test/unit/dmail_test.rb | 15 ++++++++++----- test/unit/favorite_test.rb | 6 +++--- test/unit/tag_alias_test.rb | 2 +- test/unit/tag_implication_test.rb | 2 +- 10 files changed, 33 insertions(+), 39 deletions(-) diff --git a/app/controllers/favorites_controller.rb b/app/controllers/favorites_controller.rb index 83f9506fe..9bd261e12 100644 --- a/app/controllers/favorites_controller.rb +++ b/app/controllers/favorites_controller.rb @@ -1,14 +1,18 @@ class FavoritesController < ApplicationController + def index + @posts = CurrentUser.favorite_posts(params) + end + def create @favorite = Favorite.create( - :user_id => CurrentUser.user.id, + :user_id => CurrentUser.id, :post_id => params[:favorite][:post_id] ) end def destroy Favorite.destroy( - :user_id => CurrentUser.user.id, + :user_id => CurrentUser.id, :post_id => params[:favorite][:post_id] ) end diff --git a/app/models/dmail.rb b/app/models/dmail.rb index b35e003b6..7fc34776a 100644 --- a/app/models/dmail.rb +++ b/app/models/dmail.rb @@ -9,7 +9,7 @@ class Dmail < ActiveRecord::Base belongs_to :from, :class_name => "User" after_create :update_recipient after_create :send_dmail - attr_accessible :title, :body, :is_deleted + attr_accessible :title, :body, :is_deleted, :to_id scope :for, lambda {|user| where(["owner_id = ?", user])} scope :inbox, where("to_id = owner_id") scope :sent, where("from_id = owner_id") @@ -45,11 +45,11 @@ class Dmail < ActiveRecord::Base Dmail.transaction do copy = Dmail.new(params) copy.owner_id = copy.to_id - copy.save + copy.save! copy = Dmail.new(params) copy.owner_id = CurrentUser.id - copy.save + copy.save! end end diff --git a/app/models/forum_topic.rb b/app/models/forum_topic.rb index 53dea54a0..81a1844fa 100644 --- a/app/models/forum_topic.rb +++ b/app/models/forum_topic.rb @@ -4,5 +4,5 @@ class ForumTopic < ActiveRecord::Base has_many :posts, :class_name => "ForumPost", :order => "forum_posts.id asc" validates_presence_of :title, :creator_id scope :search_title, lambda {|title| where(["text_index @@ plainto_tsquery(?)", title])} - accepts_nested_attributes_for :forum_posts + accepts_nested_attributes_for :posts end diff --git a/app/models/ip_ban.rb b/app/models/ip_ban.rb index a7965f28c..c42018ae1 100644 --- a/app/models/ip_ban.rb +++ b/app/models/ip_ban.rb @@ -9,14 +9,12 @@ class IpBan < ActiveRecord::Base def self.search(user_ids) comments = count_by_ip_addr("comments", user_ids, "creator_id", "ip_addr") - posts = count_by_ip_addr("post_versions", user_ids, "updater_id", "updater_ip_addr") notes = count_by_ip_addr("note_versions", user_ids, "updater_id", "updater_ip_addr") pools = count_by_ip_addr("pool_versions", user_ids, "updater_id", "updater_ip_addr") wiki_pages = count_by_ip_addr("wiki_page_versions", user_ids, "updater_id", "updater_ip_addr") return { "comments" => comments, - "posts" => posts, "notes" => notes, "pools" => pools, "wiki_pages" => wiki_pages diff --git a/app/models/janitor_trial.rb b/app/models/janitor_trial.rb index 04f0cae6d..02d7ddef8 100644 --- a/app/models/janitor_trial.rb +++ b/app/models/janitor_trial.rb @@ -5,12 +5,6 @@ class JanitorTrial < ActiveRecord::Base def send_dmail body = "You have been selected as a test janitor. You can now approve pending posts and have access to the moderation interface.\n\nOver the next several weeks your approvals will be monitored. If the majority of them are quality uploads, then you will be promoted to full janitor status which grants you the ability to delete and undelete posts, ban users, and revert tag changes from vandals. If you fail the trial period, you will be demoted back to your original level and you'll receive a negative user record indicating you previously attempted and failed a test janitor trial.\n\nThere is a minimum quota of 5 approvals a week to indicate that you are being active. Remember, the goal isn't to approve as much as possible. It's to filter out borderline-quality art.\n\nIf you have any questions please respond to this message." - dmail = Dmail.new( - :title => "Test Janitor Trial Period", - :body => body - ) - dmail.from_id = User.admins.first.id - dmail.to_id = user_id - Dmail.create_new(dmail) + Dmail.create_split(:title => "Test Janitor Trial Period", :body => body, :to_id => user_id) end end diff --git a/app/models/user.rb b/app/models/user.rb index 260fc7470..ff27fc971 100644 --- a/app/models/user.rb +++ b/app/models/user.rb @@ -112,21 +112,14 @@ class User < ActiveRecord::Base module FavoriteMethods def favorite_posts(options = {}) favorites_table = Favorite.table_name_for(id) - before_id = options[:before] - before_id_sql_fragment = "AND favorites.id < #{before_id.to_i}" if before_id + if options[:before_id] + before_id_sql_fragment = ["favorites.id < ?", options[:before_id]] + else + before_id_sql_fragment = "TRUE" + end limit = options[:limit] || 20 - - sql = <<-EOS - SELECT posts.*, favorites.id AS favorite_id - FROM posts - JOIN #{favorites_table} AS favorites ON favorites.post_id = posts.id - WHERE - favorites.user_id = #{id} - #{before_id_sql_fragment} - ORDER BY favorite_id DESC - LIMIT #{limit.to_i} - EOS - Post.find_by_sql(sql) + + Post.joins("JOIN #{favorites_table} AS favorites ON favorites.post_id = posts.id").where("favorites.user_id = ?", id).where(before_id_sql_fragment).order("favorite_id DESC").limit(limit).select("posts.*, favorites.id AS favorite_id") end end diff --git a/test/unit/dmail_test.rb b/test/unit/dmail_test.rb index fab01ed07..7fc5af314 100644 --- a/test/unit/dmail_test.rb +++ b/test/unit/dmail_test.rb @@ -4,11 +4,18 @@ class DmailTest < ActiveSupport::TestCase context "A dmail" do setup do MEMCACHE.flush_all + @user = Factory.create(:user) + CurrentUser.user = @user + CurrentUser.ip_addr = "127.0.0.1" ActionMailer::Base.delivery_method = :test ActionMailer::Base.perform_deliveries = true ActionMailer::Base.deliveries = [] end + teardown do + CurrentUser.user = nil + end + context "search" do should "return results based on title contents" do dmail = Factory.create(:dmail, :title => "xxx") @@ -28,11 +35,10 @@ class DmailTest < ActiveSupport::TestCase end should "should parse user names" do - user = Factory.create(:user) dmail = Factory.build(:dmail) dmail.to_id = nil - dmail.to_name = user.name - assert(dmail.to_id == user.id) + dmail.to_name = @user.name + assert(dmail.to_id == @user.id) end should "construct a response" do @@ -45,9 +51,8 @@ class DmailTest < ActiveSupport::TestCase end should "create a copy for each user" do - dmail = Factory.build(:dmail) assert_difference("Dmail.count", 2) do - Dmail.create_split(dmail.attributes) + Dmail.create_split(:to_id => @user.id, :title => "foo", :body => "foo") end end diff --git a/test/unit/favorite_test.rb b/test/unit/favorite_test.rb index 36b81eaf0..139e01031 100644 --- a/test/unit/favorite_test.rb +++ b/test/unit/favorite_test.rb @@ -42,9 +42,9 @@ class FavoriteTest < ActiveSupport::TestCase p1.add_favorite(user1) p2.add_favorite(user1) p3.add_favorite(user1) - favorites = user1.favorite_posts(:limit => 1) - favorites = user1.favorite_posts(:before => favorites.first.favorite_id) - assert_equal(2, favorites.size) + favorites = user1.favorite_posts + favorites = user1.favorite_posts(:before_id => favorites.first.favorite_id) + assert_equal(2, favorites.count) assert_equal(p2.id, favorites[0].id) assert_equal(p1.id, favorites[1].id) end diff --git a/test/unit/tag_alias_test.rb b/test/unit/tag_alias_test.rb index d4a148e29..b30cb715a 100644 --- a/test/unit/tag_alias_test.rb +++ b/test/unit/tag_alias_test.rb @@ -68,7 +68,7 @@ class TagAliasTest < ActiveSupport::TestCase ta1 = Factory.create(:tag_alias, :antecedent_name => "aaa", :consequent_name => "xxx") p1.reload assert_not_equal("uploader:#{ta1.creator_id}", p1.uploader_string) - assert_equal(ta1.creator_id, p1.versions.last.updater_id) + assert_equal(ta1.creator_id, p1.history.revisions.last["user_id"]) end end end diff --git a/test/unit/tag_implication_test.rb b/test/unit/tag_implication_test.rb index 37153fa22..3a2520d41 100644 --- a/test/unit/tag_implication_test.rb +++ b/test/unit/tag_implication_test.rb @@ -114,7 +114,7 @@ class TagImplicationTest < ActiveSupport::TestCase ti1 = Factory.create(:tag_implication, :antecedent_name => "aaa", :consequent_name => "xxx") p1.reload assert_not_equal("uploader:#{ti1.creator_id}", p1.uploader_string) - assert_equal(ti1.creator_id, p1.versions.last.updater_id) + assert_equal(ti1.creator_id, p1.history.revisions.last["user_id"]) end end end