fixed unit tests

This commit is contained in:
albert
2011-01-22 03:01:15 -05:00
parent 6824e98af3
commit c5a9067e72
6 changed files with 157 additions and 100 deletions

View File

@@ -14,7 +14,8 @@ class ForumPost < ActiveRecord::Base
end end
def update_topic_updated_at def update_topic_updated_at
topic.update_attributes(:updater_id => CurrentUser.id) topic.update_attribute(:updater_id, CurrentUser.id)
topic.touch
end end
def initialize_creator def initialize_creator

View File

@@ -13,7 +13,7 @@ class Note < ActiveRecord::Base
validate :post_must_not_be_note_locked validate :post_must_not_be_note_locked
attr_accessible :x, :y, :width, :height, :body, :updater_id, :updater_ip_addr, :is_active, :post_id attr_accessible :x, :y, :width, :height, :body, :updater_id, :updater_ip_addr, :is_active, :post_id
scope :active, where("is_active = TRUE") scope :active, where("is_active = TRUE")
scope :body_matches, lambda {|query| where("text_index @@ plainto_tsquery(?)", query)} scope :body_matches, lambda {|query| where("text_index @@ plainto_tsquery(?)", query.scan(/\S+/).join(" & "))}
search_method :body_matches search_method :body_matches
def presenter def presenter
@@ -97,21 +97,4 @@ class Note < ActiveRecord::Base
end end
end end
end end
def self.build_relation(params)
relation = where("TRUE")
if !params[:query].blank?
query = params[:query].scan(/\S+/).join(" & ")
relation = relation.where(["text_index @@ plainto_tsquery(?)", query])
end
if params[:status] == "Active"
relation = relation.where("is_active = TRUE")
elsif params[:status] == "Deleted"
relation = relation.where("is_active = FALSE")
end
relation
end
end end

View File

@@ -5,10 +5,14 @@ class BanTest < ActiveSupport::TestCase
context "created by an admin" do context "created by an admin" do
setup do setup do
@banner = Factory.create(:admin_user) @banner = Factory.create(:admin_user)
CurrentUser.user = @banner
CurrentUser.ip_addr = "127.0.0.1"
end end
teardown do teardown do
@banner = nil @banner = nil
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end end
should "not be valid against another admin" do should "not be valid against another admin" do
@@ -44,10 +48,14 @@ class BanTest < ActiveSupport::TestCase
context "created by a moderator" do context "created by a moderator" do
setup do setup do
@banner = Factory.create(:moderator_user) @banner = Factory.create(:moderator_user)
CurrentUser.user = @banner
CurrentUser.ip_addr = "127.0.0.1"
end end
teardown do teardown do
@banner = nil @banner = nil
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end end
should "not be valid against an admin or moderator" do should "not be valid against an admin or moderator" do
@@ -84,10 +92,14 @@ class BanTest < ActiveSupport::TestCase
context "created by a janitor" do context "created by a janitor" do
setup do setup do
@banner = Factory.create(:janitor_user) @banner = Factory.create(:janitor_user)
CurrentUser.user = @banner
CurrentUser.ip_addr = "127.0.0.1"
end end
teardown do teardown do
@banner = nil @banner = nil
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end end
should "always be invalid" do should "always be invalid" do
@@ -126,7 +138,9 @@ class BanTest < ActiveSupport::TestCase
should "initialize the expiration date" do should "initialize the expiration date" do
user = Factory.create(:user) user = Factory.create(:user)
admin = Factory.create(:admin_user) admin = Factory.create(:admin_user)
CurrentUser.user = admin
ban = Factory.create(:ban, :user => user, :banner => admin) ban = Factory.create(:ban, :user => user, :banner => admin)
CurrentUser.user = nil
assert_not_nil(ban.expires_at) assert_not_nil(ban.expires_at)
end end
@@ -134,7 +148,9 @@ class BanTest < ActiveSupport::TestCase
user = Factory.create(:user) user = Factory.create(:user)
admin = Factory.create(:admin_user) admin = Factory.create(:admin_user)
assert(user.feedback.empty?) assert(user.feedback.empty?)
CurrentUser.user = admin
ban = Factory.create(:ban, :user => user, :banner => admin) ban = Factory.create(:ban, :user => user, :banner => admin)
CurrentUser.user = nil
assert(!user.feedback.empty?) assert(!user.feedback.empty?)
assert(!user.feedback.last.is_positive?) assert(!user.feedback.last.is_positive?)
end end
@@ -142,16 +158,36 @@ class BanTest < ActiveSupport::TestCase
context "Searching for a ban" do context "Searching for a ban" do
context "by user id" do context "by user id" do
should "not return expired bans" do setup do
admin = Factory.create(:admin_user) @admin = Factory.create(:admin_user)
CurrentUser.user = @admin
CurrentUser.ip_addr = "127.0.0.1"
@user = Factory.create(:user)
end
user = Factory.create(:user) teardown do
ban = Factory.create(:ban, :user => user, :banner => admin, :duration => -1) CurrentUser.user = nil
assert(!Ban.is_banned?(user)) CurrentUser.ip_addr = nil
end
user = Factory.create(:user) context "when only expired bans exist" do
ban = Factory.create(:ban, :user => user, :banner => admin, :duration => 1) setup do
assert(Ban.is_banned?(user)) @ban = Factory.create(:ban, :user => @user, :banner => @admin, :duration => -1)
end
should "not return expired bans" do
assert(!Ban.is_banned?(@user))
end
end
context "when active bans still exist" do
setup do
@ban = Factory.create(:ban, :user => @user, :banner => @admin, :duration => 1)
end
should "return active bans" do
assert(Ban.is_banned?(@user))
end
end end
end end
end end

View File

@@ -16,9 +16,10 @@ class ForumPostTest < ActiveSupport::TestCase
should "update its parent when saved" do should "update its parent when saved" do
sleep 1 sleep 1
original_topic_updated_at = @topic.updated_at
post = Factory.create(:forum_post, :topic_id => @topic.id) post = Factory.create(:forum_post, :topic_id => @topic.id)
@topic.reload @topic.reload
assert(@topic.updated_at > 1.second.ago) assert_not_equal(original_topic_updated_at, @topic.updated_at)
end end
should "be searchable by body content" do should "be searchable by body content" do

View File

@@ -1,84 +1,123 @@
require_relative '../test_helper' require_relative '../test_helper'
class NoteTest < ActiveSupport::TestCase class NoteTest < ActiveSupport::TestCase
setup do context "In all cases" do
user = Factory.create(:user)
CurrentUser.user = user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all
end
teardown do
CurrentUser.user = nil
CurrentUser.ip_addr = nil
end
context "A note" do
setup do setup do
@user = Factory.create(:user)
CurrentUser.user = @user
CurrentUser.ip_addr = "127.0.0.1"
MEMCACHE.flush_all MEMCACHE.flush_all
end end
should "create versions" do teardown do
note = nil CurrentUser.user = nil
assert_difference("NoteVersion.count") do CurrentUser.ip_addr = nil
note = Factory.create(:note) end
context "creating a note" do
setup do
@post = Factory.create(:post)
end end
version = NoteVersion.last
assert_equal(note.body, version.body) should "create a version" do
assert_difference("NoteVersion.count") do assert_difference("NoteVersion.count", 1) do
note.update_attributes(:updater_id => note.creator_id, :updater_ip_addr => "127.0.0.1", :body => "fafafa") @note = Factory.create(:note, :post => @post)
end
assert_equal(1, @note.versions.count)
assert_equal(@note.body, @note.versions.first.body)
end end
version = NoteVersion.last
assert_equal("fafafa", version.body)
end
should "allow undoing any change from a user" do should "update the post's last_noted_at field" do
vandal = Factory.create(:user) assert_nil(@post.last_noted_at)
reverter = Factory.create(:user) @note = Factory.create(:note, :post => @post)
note = Factory.create(:note, :x => 100, :y => 100) @post.reload
note.update_attributes(:x => 2000, :y => 2000, :updater_id => vandal.id, :updater_ip_addr => "127.0.0.1") assert_not_nil(@post.last_noted_at)
note.reload end
assert_equal(2000, note.x)
assert_equal(2000, note.y) context "for a note-locked post" do
Note.undo_changes_by_user(vandal.id, reverter.id, "127.0.0.1") setup do
note.reload @post.update_attribute(:is_note_locked, true)
assert_equal(100, note.x) end
assert_equal(100, note.y)
end should "fail" do
assert_difference("Note.count", 0) do
should "not validate if the post is note locked" do @note = Factory.build(:note, :post => @post)
post = Factory.create(:post, :is_note_locked => true) @note.save
note = Factory.build(:note, :post => post) end
assert_difference("Note.count", 0) do assert_equal(["Post is note locked"], @note.errors.full_messages)
note.save end
end end
assert(note.errors.any?)
end end
should "update the post when saved" do context "updating a note" do
post = Factory.create(:post) setup do
assert_nil(post.last_noted_at) @post = Factory.create(:post)
note = Factory.create(:note, :post => post) @note = Factory.create(:note, :post => @post)
post.reload end
assert_not_nil(post.last_noted_at)
should "update the post's last_noted_at field" do
assert_nil(@post.last_noted_at)
@note.update_attributes(:x => 1000)
@post.reload
assert_equal(@post.last_noted_at, @note.updated_at)
end
should "create a version" do
assert_difference("NoteVersion.count", 1) do
@note.update_attributes(:body => "fafafa")
end
assert_equal(2, @note.versions.count)
assert_equal("fafafa", @note.versions.last.body)
end
context "for a note-locked post" do
setup do
@post.update_attribute(:is_note_locked, true)
end
should "fail" do
@note.update_attributes(:x => 1000)
assert_equal(["Post is note locked"], @note.errors.full_messages)
end
end
end end
should "know when the post is note locked" do context "when notes have been vandalized by one user" do
post = Factory.create(:post, :is_note_locked => true) setup do
note = Factory.build(:note, :post => post) @vandal = Factory.create(:user)
assert(note.is_locked?) @note = Factory.create(:note, :x => 100, :y => 100)
CurrentUser.scoped(@vandal, "127.0.0.1") do
@note.update_attributes(:x => 2000, :y => 2000)
end
end
context "the act of undoing all changes by that user" do
should "revert any affected notes" do
Note.undo_changes_by_user(@vandal.id)
@note.reload
assert_equal(100, @note.x)
assert_equal(100, @note.y)
end
end
end end
should "return hits when searched" do context "searching for a note" do
notes = [] setup do
notes << Factory.create(:note, :body => "aaa bbb ccc") @note = Factory.create(:note, :body => "aaa")
notes << Factory.create(:note, :body => "bbb ccc ddd", :is_active => false) end
notes << Factory.create(:note, :body => "eee")
results = Note.build_relation(:query => "bbb").all context "where the body contains the string 'aaa'" do
assert_equal(2, results.size) should "return a hit" do
results = Note.build_relation(:query => "bbb", :status => "Active").all assert_equal(1, Note.body_matches("aaa").count)
assert_equal(1, results.size) end
assert_equal(notes[0].id, results[0].id) end
context "where the body contains the string 'bbb'" do
should "return no hits" do
assert_equal(0, Note.body_matches("bbb").count)
end
end
end end
end end
end end

View File

@@ -23,13 +23,10 @@ class PoolTest < ActiveSupport::TestCase
user = Factory.create(:user) user = Factory.create(:user)
assert_equal(1, pool.versions(true).size) assert_equal(1, pool.versions(true).size)
pool.post_ids = "1" pool.post_ids = "1"
pool.updater_id = user.id CurrentUser.ip_addr = "1.2.3.4"
pool.updater_ip_addr = "128.0.0.1"
pool.save pool.save
assert_equal(2, pool.versions(true).size) assert_equal(2, pool.versions(true).size)
pool.post_ids = "1 2" pool.post_ids = "1 2"
pool.updater_id = user.id
pool.updater_ip_addr = "128.0.0.1"
pool.save pool.save
assert_equal(2, pool.versions(true).size) assert_equal(2, pool.versions(true).size)
pool.revert_to!(PoolVersion.first) pool.revert_to!(PoolVersion.first)