Merge branch 'post-archive'
This commit is contained in:
@@ -53,7 +53,7 @@ module Moderator
|
||||
end
|
||||
|
||||
should "render" do
|
||||
assert_equal(1, PostVersion.count)
|
||||
assert_equal(1, PostArchive.count)
|
||||
get :show, {}, {:user_id => @admin.id}
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
@@ -144,12 +144,12 @@ class PostsControllerTest < ActionController::TestCase
|
||||
|
||||
context "revert action" do
|
||||
setup do
|
||||
@post.stubs(:merge_version?).returns(false)
|
||||
PostArchive.sqs_service.stubs(:merge?).returns(false)
|
||||
@post.update_attributes(:tag_string => "zzz")
|
||||
end
|
||||
|
||||
should "work" do
|
||||
@version = @post.versions(true).first
|
||||
@version = @post.versions.first
|
||||
assert_equal("aaaa", @version.tags)
|
||||
post :revert, {:id => @post.id, :version_id => @version.id}, {:user_id => @user.id}
|
||||
assert_redirected_to post_path(@post)
|
||||
|
||||
45
test/helpers/post_archive_test_helper.rb
Normal file
45
test/helpers/post_archive_test_helper.rb
Normal file
@@ -0,0 +1,45 @@
|
||||
module PostArchiveTestHelper
|
||||
def setup
|
||||
super
|
||||
|
||||
mock_post_archive_service!
|
||||
start_post_archive_transaction
|
||||
end
|
||||
|
||||
def teardown
|
||||
super
|
||||
|
||||
rollback_post_archive_transaction
|
||||
end
|
||||
|
||||
def mock_post_archive_service!
|
||||
mock_sqs_service = Class.new do
|
||||
def send_message(msg)
|
||||
_, json = msg.split(/\n/)
|
||||
json = JSON.parse(json)
|
||||
json.delete("created_at")
|
||||
json["version"] = 1 + PostArchive.where(post_id: json["post_id"]).count
|
||||
prev = PostArchive.where(post_id: json["post_id"]).order("id desc").first
|
||||
if merge?(prev, json)
|
||||
prev.update_columns(json)
|
||||
else
|
||||
PostArchive.create(json)
|
||||
end
|
||||
end
|
||||
|
||||
def merge?(prev, json)
|
||||
prev && (prev.updater_id == json["updater_id"]) && (prev.updated_at >= 1.hour.ago)
|
||||
end
|
||||
end
|
||||
|
||||
PostArchive.stubs(:sqs_service).returns(mock_sqs_service.new)
|
||||
end
|
||||
|
||||
def start_post_archive_transaction
|
||||
PostArchive.connection.begin_transaction joinable: false
|
||||
end
|
||||
|
||||
def rollback_post_archive_transaction
|
||||
PostArchive.connection.rollback_transaction
|
||||
end
|
||||
end
|
||||
@@ -11,6 +11,7 @@ end
|
||||
require File.expand_path('../../config/environment', __FILE__)
|
||||
require 'rails/test_help'
|
||||
require 'cache'
|
||||
require 'helpers/post_archive_test_helper'
|
||||
|
||||
Dir[File.expand_path(File.dirname(__FILE__) + "/factories/*.rb")].each {|file| require file}
|
||||
|
||||
@@ -24,6 +25,24 @@ if defined?(MEMCACHE)
|
||||
Object.send(:remove_const, :MEMCACHE)
|
||||
end
|
||||
|
||||
class ActiveSupport::TestCase
|
||||
include PostArchiveTestHelper
|
||||
end
|
||||
|
||||
class ActionController::TestCase
|
||||
include PostArchiveTestHelper
|
||||
|
||||
def assert_authentication_passes(action, http_method, role, params, session)
|
||||
__send__(http_method, action, params, session.merge(:user_id => @users[role].id))
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def assert_authentication_fails(action, http_method, role)
|
||||
__send__(http_method, action, params, session.merge(:user_id => @users[role].id))
|
||||
assert_redirected_to(new_sessions_path)
|
||||
end
|
||||
end
|
||||
|
||||
MEMCACHE = MemcacheMock.new
|
||||
Delayed::Worker.delay_jobs = false
|
||||
|
||||
|
||||
@@ -107,11 +107,14 @@ class NoteTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
@post = FactoryGirl.create(:post, :image_width => 1000, :image_height => 1000)
|
||||
@note = FactoryGirl.create(:note, :post => @post)
|
||||
@note.stubs(:merge_version?).returns(false)
|
||||
end
|
||||
|
||||
should "increment the updater's note_update_count" do
|
||||
assert_difference("CurrentUser.note_update_count", 1) do
|
||||
@user.reload
|
||||
assert_difference("@user.note_update_count", 1) do
|
||||
@note.update_attributes(:body => "zzz")
|
||||
@user.reload
|
||||
end
|
||||
end
|
||||
|
||||
|
||||
@@ -10,7 +10,9 @@ class PostTest < ActiveSupport::TestCase
|
||||
assert_equal(posts.map(&:id), Post.tag_match(query).pluck(:id))
|
||||
end
|
||||
|
||||
setup do
|
||||
def setup
|
||||
super
|
||||
|
||||
Timecop.travel(2.weeks.ago) do
|
||||
@user = FactoryGirl.create(:user)
|
||||
end
|
||||
@@ -20,7 +22,9 @@ class PostTest < ActiveSupport::TestCase
|
||||
mock_saved_search_service!
|
||||
end
|
||||
|
||||
teardown do
|
||||
def teardown
|
||||
super
|
||||
|
||||
CurrentUser.user = nil
|
||||
CurrentUser.ip_addr = nil
|
||||
end
|
||||
@@ -1055,7 +1059,7 @@ class PostTest < ActiveSupport::TestCase
|
||||
|
||||
context "that has been updated" do
|
||||
should "create a new version if it's the first version" do
|
||||
assert_difference("PostVersion.count", 1) do
|
||||
assert_difference("PostArchive.count", 1) do
|
||||
post = FactoryGirl.create(:post)
|
||||
end
|
||||
end
|
||||
@@ -1063,7 +1067,7 @@ class PostTest < ActiveSupport::TestCase
|
||||
should "create a new version if it's been over an hour since the last update" do
|
||||
post = FactoryGirl.create(:post)
|
||||
Timecop.travel(6.hours.from_now) do
|
||||
assert_difference("PostVersion.count", 1) do
|
||||
assert_difference("PostArchive.count", 1) do
|
||||
post.update_attributes(:tag_string => "zzz")
|
||||
end
|
||||
end
|
||||
@@ -1071,15 +1075,16 @@ class PostTest < ActiveSupport::TestCase
|
||||
|
||||
should "merge with the previous version if the updater is the same user and it's been less than an hour" do
|
||||
post = FactoryGirl.create(:post)
|
||||
assert_difference("PostVersion.count", 0) do
|
||||
assert_difference("PostArchive.count", 0) do
|
||||
post.update_attributes(:tag_string => "zzz")
|
||||
end
|
||||
assert_equal("zzz", post.versions.last.tags)
|
||||
end
|
||||
|
||||
should "increment the updater's post_update_count" do
|
||||
PostArchive.sqs_service.stubs(:merge?).returns(false)
|
||||
post = FactoryGirl.create(:post, :tag_string => "aaa bbb ccc")
|
||||
post.stubs(:merge_version?).returns(false)
|
||||
CurrentUser.reload
|
||||
|
||||
assert_difference("CurrentUser.post_update_count", 1) do
|
||||
post.update_attributes(:tag_string => "zzz")
|
||||
@@ -2289,8 +2294,8 @@ class PostTest < ActiveSupport::TestCase
|
||||
|
||||
context "a post that has been updated" do
|
||||
setup do
|
||||
PostArchive.sqs_service.stubs(:merge?).returns(false)
|
||||
@post = FactoryGirl.create(:post, :rating => "q", :tag_string => "aaa", :source => nil)
|
||||
@post.stubs(:merge_version?).returns(false)
|
||||
@post.update_attributes(:tag_string => "aaa bbb ccc ddd")
|
||||
@post.update_attributes(:tag_string => "bbb xxx yyy", :source => "xyz")
|
||||
@post.update_attributes(:tag_string => "bbb mmm yyy", :source => "abc")
|
||||
|
||||
@@ -18,28 +18,21 @@ class PostVersionTest < ActiveSupport::TestCase
|
||||
|
||||
context "that has multiple versions: " do
|
||||
setup do
|
||||
PostArchive.sqs_service.stubs(:merge?).returns(false)
|
||||
@post = FactoryGirl.create(:post, :tag_string => "1")
|
||||
@post.stubs(:merge_version?).returns(false)
|
||||
@post.stubs(:tag_string_changed?).returns(true)
|
||||
@post.update_attributes(:tag_string => "1 2")
|
||||
@post.update_attributes(:tag_string => "2 3")
|
||||
end
|
||||
|
||||
context "a version record" do
|
||||
setup do
|
||||
@version = PostVersion.last
|
||||
@version = PostArchive.last
|
||||
end
|
||||
|
||||
should "know its previous version" do
|
||||
assert_not_nil(@version.previous)
|
||||
assert_equal("1 2", @version.previous.tags)
|
||||
end
|
||||
|
||||
should "know the seuqence of all versions for the post" do
|
||||
assert_equal(2, @version.sequence_for_post.size)
|
||||
assert_equal(%w(3), @version.sequence_for_post[0][:added_tags])
|
||||
assert_equal(%w(2), @version.sequence_for_post[1][:added_tags])
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -75,9 +68,8 @@ class PostVersionTest < ActiveSupport::TestCase
|
||||
|
||||
context "that has been updated" do
|
||||
setup do
|
||||
@parent = FactoryGirl.create(:post)
|
||||
PostArchive.sqs_service.stubs(:merge?).returns(false)
|
||||
@post = FactoryGirl.create(:post, :tag_string => "aaa bbb ccc", :rating => "q", :source => "xyz")
|
||||
@post.stubs(:merge_version?).returns(false)
|
||||
@post.update_attributes(:tag_string => "bbb ccc xxx", :source => "")
|
||||
end
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
require 'test_helper'
|
||||
|
||||
class PostVoteTest < ActiveSupport::TestCase
|
||||
setup do
|
||||
def setup
|
||||
super
|
||||
|
||||
user = FactoryGirl.create(:user)
|
||||
CurrentUser.user = user
|
||||
CurrentUser.ip_addr = "127.0.0.1"
|
||||
|
||||
Reference in New Issue
Block a user