diff --git a/Gemfile b/Gemfile index 2d300d3c..b6eb8a9e 100644 --- a/Gemfile +++ b/Gemfile @@ -96,8 +96,6 @@ group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug' - gem 'minitest-reporters' - gem 'guard' gem 'guard-minitest' @@ -111,6 +109,8 @@ end group :test do gem 'test-unit-rails' + gem 'database_cleaner-active_record' + gem 'rake' gem 'rubocop' gem 'rubocop-rails' diff --git a/Gemfile.lock b/Gemfile.lock index 83ecad70..e3c61f9c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -71,7 +71,6 @@ GEM zeitwerk (~> 2.3) addressable (2.8.0) public_suffix (>= 2.0.2, < 5.0) - ansi (1.5.0) ast (2.4.2) autoprefixer-rails (10.2.5.1) execjs (> 0) @@ -98,6 +97,10 @@ GEM concurrent-ruby (1.1.9) connection_pool (2.2.5) crass (1.0.6) + database_cleaner-active_record (2.0.1) + activerecord (>= 5.a) + database_cleaner-core (~> 2.0.0) + database_cleaner-core (2.0.1) docile (1.4.0) erubi (1.10.0) execjs (2.8.1) @@ -184,11 +187,6 @@ GEM method_source (1.0.0) mini_mime (1.1.0) minitest (5.14.4) - minitest-reporters (1.4.3) - ansi - builder - minitest (>= 5.0) - ruby-progressbar msgpack (1.4.2) multi_json (1.15.0) multi_xml (0.6.0) @@ -196,6 +194,8 @@ GEM mysql2 (0.5.3) nenv (0.3.0) nio4r (2.5.7) + nokogiri (1.11.7-x86_64-darwin) + racc (~> 1.4) nokogiri (1.11.7-x86_64-linux) racc (~> 1.4) notiffany (0.1.3) @@ -383,6 +383,7 @@ GEM zeitwerk (2.4.2) PLATFORMS + x86_64-darwin-20 x86_64-linux DEPENDENCIES @@ -392,6 +393,7 @@ DEPENDENCIES byebug capybara (>= 3.26) cfnv + database_cleaner-active_record factory_bot_rails friendly_id guard @@ -403,7 +405,6 @@ DEPENDENCIES lumberjack mcinch meta-tags - minitest-reporters mysql2 (~> 0.5) pry-rails puma diff --git a/app/controllers/admin/archived_conversation_messages_controller.rb b/app/controllers/admin/archived_conversation_messages_controller.rb index 226c02dc..8ead7786 100644 --- a/app/controllers/admin/archived_conversation_messages_controller.rb +++ b/app/controllers/admin/archived_conversation_messages_controller.rb @@ -21,7 +21,8 @@ def show def create archiver = ConversationMessageArchiver.new - if am = archiver.archive!(archived_conversation_message_params_for_create) + am = archiver.archive!(archived_conversation_message_params_for_create) + if am flash[:success] = t('views.flash.added_archived_conversation_message') redirect_to(admin_archived_conversation_message_path(am.id)) else @@ -49,7 +50,8 @@ def update def destroy archiver = ConversationMessageArchiver.new - if cm = archiver.reconstitute!(params[:id]) + cm = archiver.reconstitute!(params[:id].to_i) + if cm flash[:success] = t('views.flash.deleted_archived_conversation_message') redirect_to(admin_conversation_message_path(cm)) else diff --git a/app/models/channel.rb b/app/models/channel.rb index baaca3f2..ce6e11cb 100644 --- a/app/models/channel.rb +++ b/app/models/channel.rb @@ -118,10 +118,9 @@ def lowercase_name_with_prefix # 現在の(キャッシュされていない)最終発言を求める # @return [ConversationMessage] def current_last_speech - conversation_messages. - order(timestamp: :desc). - limit(1). - first + conversation_messages + .order(timestamp: :desc) + .first end # canonical 属性の URL テンプレートを、日付を入れた URL にして返す diff --git a/app/models/channel_last_speech.rb b/app/models/channel_last_speech.rb index 3f2a75b2..353fd79a 100644 --- a/app/models/channel_last_speech.rb +++ b/app/models/channel_last_speech.rb @@ -11,17 +11,19 @@ class ChannelLastSpeech < ApplicationRecord # チャンネルの最終発言を更新する # @param [Channel] channel 更新対象のチャンネル - # @return [self] + # @return [ChannelLastSpeech, nil] 更新後の最終発言データ def self.refresh!(channel) - last_speech = channel.channel_last_speech || new(channel: channel) + current_last_speech = channel.current_last_speech + unless current_last_speech + where(channel: channel) + .delete_all - self.transaction do - last_speech.conversation_message = nil - - yield if block_given? + return nil + end - last_speech.conversation_message = channel.current_last_speech - last_speech.save! + find_or_initialize_by(channel: channel).tap do |s| + s.conversation_message = current_last_speech + s.save! end end end diff --git a/app/models/conversation_message_archiver.rb b/app/models/conversation_message_archiver.rb index e3429d9f..ed5bac17 100644 --- a/app/models/conversation_message_archiver.rb +++ b/app/models/conversation_message_archiver.rb @@ -12,36 +12,37 @@ class ConversationMessageArchiver # @option [String] :archive_reason 非表示理由 # @return [ArchivedConversationMessage] def archive!(params) - old_id = params[:old_id].to_i - unless cm = ConversationMessage.find(old_id) + cm = ConversationMessage.find(params[:old_id].to_i) + unless cm raise(ArgumentError, 'old_id is required.') end - archive_reason = params[:archive_reason_id].to_i - unless reason = ArchiveReason.find(archive_reason) - raise(ArgumentError, 'archive_reason is required.') + + archive_reason = ArchiveReason.find(params[:archive_reason_id].to_i) + unless archive_reason + raise(ArgumentError, 'archive_reason_id is required.') end am = ArchivedConversationMessage.from_conversation_message(cm) - am.archive_reason = reason + am.archive_reason = archive_reason # Mroonga ストレージモードはトランザクション非対応 ApplicationRecord.transaction do - ChannelLastSpeech.refresh!(cm.channel) do - am.save! - cm.destroy! - end + am.save! + cm.destroy! + + ChannelLastSpeech.refresh!(cm.channel) end am end # Archived -> ConversationMessage - # @param [Integer/String] old_id ArchivedConversationMessage.id - # @option [String] :id 元の ArchivedConversationMessage.old_id + # @param [Integer] archived_conversation_message_id # @return [ConversationMessage] - def reconstitute!(old_id) - unless am = ArchivedConversationMessage.find(old_id.to_i) - raise(ArgumentError, 'old_id is required.') + def reconstitute!(archived_conversation_message_id) + am = ArchivedConversationMessage.find(archived_conversation_message_id) + unless am + raise(ArgumentError, 'archived_conversation_message_id is required.') end cm = ConversationMessage.new diff --git a/lib/ircs/plugins/base.rb b/lib/ircs/plugins/base.rb index 39a5389c..93dcdd62 100644 --- a/lib/ircs/plugins/base.rb +++ b/lib/ircs/plugins/base.rb @@ -105,15 +105,6 @@ def record_message_to_channels(message, cinch_channels) end end end - - # [DEPRECATED] チャンネルごとの最終発言を更新する - # @param [::Channel] チャンネル - # @param [::ConversationMessage] 発言のメッセージ - # @return [::ChannelLastSpeech] - def update_last_speech!(channel, message) - ChannelLastSpeech.refresh!(channel) - @logger.warn('このメソッドは廃止予定です。ChannelLastSpeech.refresh! を使ってください') - end end end end diff --git a/test/controllers/channels/today_controller_test.rb b/test/controllers/channels/today_controller_test.rb index 558734de..8b9245d3 100644 --- a/test/controllers/channels/today_controller_test.rb +++ b/test/controllers/channels/today_controller_test.rb @@ -2,14 +2,9 @@ class Channels::TodayControllerTest < ActionController::TestCase setup do - Channel.delete_all @channel = create(:channel) end - teardown do - Channel.delete_all - end - test '今日のページにリダイレクトされる' do today = Time.zone.local(2017, 5, 1) diff --git a/test/controllers/channels/yesterday_controller_test.rb b/test/controllers/channels/yesterday_controller_test.rb index de1975f5..ceaf2883 100644 --- a/test/controllers/channels/yesterday_controller_test.rb +++ b/test/controllers/channels/yesterday_controller_test.rb @@ -2,14 +2,9 @@ class Channels::YesterdayControllerTest < ActionController::TestCase setup do - Channel.delete_all @channel = create(:channel) end - teardown do - Channel.delete_all - end - test '昨日のページにリダイレクトされる' do today = Time.zone.local(2017, 5, 1) diff --git a/test/factories/archive_reasons.rb b/test/factories/archive_reasons.rb new file mode 100644 index 00000000..328c4a76 --- /dev/null +++ b/test/factories/archive_reasons.rb @@ -0,0 +1,11 @@ +FactoryBot.define do + factory :archive_reason do + reason { '利用規約に違反しているため' } + + initialize_with { + ArchiveReason.find_or_initialize_by(reason: reason) do |new_reason| + new_reason.attributes = attributes + end + } + end +end diff --git a/test/integration/channels_create_test.rb b/test/integration/channels_create_test.rb index 4f284b11..cc9a0371 100644 --- a/test/integration/channels_create_test.rb +++ b/test/integration/channels_create_test.rb @@ -8,15 +8,9 @@ class ChannelsCreateTest < ActionDispatch::IntegrationTest @setting = create(:setting) @user = create(:user) - Channel.delete_all - @login_helper = UserLoginTestHelper.new(self, @user, channels_path) end - teardown do - Channel.delete_all - end - test 'ログインしていない場合、ログインページにリダイレクトされる' do @login_helper.assert_redirected_to_login_on_logged_out do post( diff --git a/test/integration/channels_days_show_linking_to_keyword_test.rb b/test/integration/channels_days_show_linking_to_keyword_test.rb index a88f31b4..aab574c9 100644 --- a/test/integration/channels_days_show_linking_to_keyword_test.rb +++ b/test/integration/channels_days_show_linking_to_keyword_test.rb @@ -7,12 +7,6 @@ class ChannelsDaysShowLinkingToKeywordTest < ActionDispatch::IntegrationTest create(:setting) create(:user) @channel = create(:channel) - - PrivmsgKeywordRelationship.delete_all - Message.delete_all - ConversationMessage.delete_all - Keyword.delete_all - @keyword = create(:keyword) # メッセージの準備 @@ -26,13 +20,6 @@ class ChannelsDaysShowLinkingToKeywordTest < ActionDispatch::IntegrationTest @privmsgs = privmsg_factory_ids.map { |id| create(id) } end - teardown do - PrivmsgKeywordRelationship.delete_all - Message.delete_all - ConversationMessage.delete_all - Keyword.delete_all - end - test 'キーワードのリンク先および表記が正しい' do # メッセージとキーワードを関連付ける extract_keyword = LogArchiver::ExtractKeyword.new diff --git a/test/integration/channels_days_show_test.rb b/test/integration/channels_days_show_test.rb index 78237c32..83299aa8 100644 --- a/test/integration/channels_days_show_test.rb +++ b/test/integration/channels_days_show_test.rb @@ -4,11 +4,6 @@ class ChannelsDaysShowTest < ActionDispatch::IntegrationTest setup do create(:setting) create(:user) - - MessageDate.delete_all - Message.delete_all - ConversationMessage.delete_all - @channel = create(:channel) # NICK 1件 @@ -45,12 +40,6 @@ class ChannelsDaysShowTest < ActionDispatch::IntegrationTest style: :normal) end - teardown do - MessageDate.delete_all - Message.delete_all - ConversationMessage.delete_all - end - test 'メッセージの一覧が正しく表示される' do get(@browse.path) assert_response(:success) diff --git a/test/integration/channels_edit_test.rb b/test/integration/channels_edit_test.rb index 7b282f7c..aba0b997 100644 --- a/test/integration/channels_edit_test.rb +++ b/test/integration/channels_edit_test.rb @@ -9,17 +9,12 @@ class ChannelsEditTest < ActionDispatch::IntegrationTest @setting = create(:setting) @user = create(:user) - Channel.delete_all @channel = create(:channel) @path = edit_channel_path(@channel) @login_helper = UserLoginTestHelper.new(self, @user, @path) end - teardown do - Channel.delete_all - end - test 'ログインしている場合、表示される' do @login_helper.assert_successful_login_and_get end diff --git a/test/integration/channels_index_test.rb b/test/integration/channels_index_test.rb index 4c7a7c15..4e24e2ac 100644 --- a/test/integration/channels_index_test.rb +++ b/test/integration/channels_index_test.rb @@ -4,15 +4,9 @@ class ChannelsIndexTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) @user = create(:user) - - Channel.delete_all @channel = create(:channel) end - teardown do - Channel.delete_all - end - test '表示される' do get(channels_path) assert_response(:success) diff --git a/test/integration/channels_show_test.rb b/test/integration/channels_show_test.rb index 56d8a8cc..45185208 100644 --- a/test/integration/channels_show_test.rb +++ b/test/integration/channels_show_test.rb @@ -4,15 +4,9 @@ class ChannelsShowTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) @user = create(:user) - - Channel.delete_all @channel = create(:channel) end - teardown do - Channel.delete_all - end - test '表示される' do get(channel_path(@channel)) assert_response(:success) diff --git a/test/integration/channels_update_test.rb b/test/integration/channels_update_test.rb index 00ff9647..45e8b795 100644 --- a/test/integration/channels_update_test.rb +++ b/test/integration/channels_update_test.rb @@ -7,18 +7,12 @@ class ChannelsUpdateTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) @user = create(:user) - - Channel.delete_all @channel = create(:channel) @path = channel_path(@channel) @login_helper = UserLoginTestHelper.new(self, @user, @path) end - teardown do - Channel.delete_all - end - test 'ログインしていない場合、ログインページにリダイレクトされる' do @login_helper.assert_redirected_to_login_on_logged_out do patch( diff --git a/test/integration/keywords_show_test.rb b/test/integration/keywords_show_test.rb index 3bac2d51..2de62c3a 100644 --- a/test/integration/keywords_show_test.rb +++ b/test/integration/keywords_show_test.rb @@ -4,12 +4,6 @@ class KeywordsShowTest < ActionDispatch::IntegrationTest setup do create(:setting) create(:user) - - PrivmsgKeywordRelationship.delete_all - Message.delete_all - ConversationMessage.delete_all - Keyword.delete_all - @keyword = create(:keyword) # メッセージの準備 @@ -23,13 +17,6 @@ class KeywordsShowTest < ActionDispatch::IntegrationTest @privmsgs = privmsg_factory_ids.map { |id| create(id) } end - teardown do - PrivmsgKeywordRelationship.delete_all - Message.delete_all - ConversationMessage.delete_all - Keyword.delete_all - end - test 'キーワードを含む日が1つにまとめられる' do # メッセージとキーワードを関連付ける @privmsgs.each do |privmsg| diff --git a/test/integration/users_create_test.rb b/test/integration/users_create_test.rb index c046047b..4931059d 100644 --- a/test/integration/users_create_test.rb +++ b/test/integration/users_create_test.rb @@ -6,17 +6,11 @@ class UsersCreateTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) - - User.delete_all @user = create(:user) @login_helper = UserLoginTestHelper.new(self, @user, users_path) end - teardown do - User.delete_all - end - test 'ログインしていない場合、ログインページにリダイレクトされる' do @login_helper.assert_redirected_to_login_on_logged_out do post( diff --git a/test/integration/users_edit_test.rb b/test/integration/users_edit_test.rb index f0d433d4..9902106d 100644 --- a/test/integration/users_edit_test.rb +++ b/test/integration/users_edit_test.rb @@ -7,18 +7,12 @@ class UsersEditTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) - - User.delete_all @user = create(:user) @path = edit_user_path(@user) @login_helper = UserLoginTestHelper.new(self, @user, @path) end - teardown do - User.delete_all - end - test 'ログインしている場合、表示される' do @login_helper.assert_successful_login_and_get end diff --git a/test/integration/users_index_test.rb b/test/integration/users_index_test.rb index 893c6645..3ce06ec1 100644 --- a/test/integration/users_index_test.rb +++ b/test/integration/users_index_test.rb @@ -7,17 +7,11 @@ class UsersIndexTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) - - User.delete_all @user = create(:user) @login_helper = UserLoginTestHelper.new(self, @user, users_path) end - teardown do - User.delete_all - end - test 'ログインしている場合、表示される' do @login_helper.assert_successful_login_and_get end diff --git a/test/integration/users_new_test.rb b/test/integration/users_new_test.rb index 27a8e9a7..2dff2fcc 100644 --- a/test/integration/users_new_test.rb +++ b/test/integration/users_new_test.rb @@ -7,17 +7,11 @@ class UsersNewTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) - - User.delete_all @user = create(:user) @login_helper = UserLoginTestHelper.new(self, @user, new_user_path) end - teardown do - User.delete_all - end - test 'ログインしている場合、表示される' do @login_helper.assert_successful_login_and_get end diff --git a/test/integration/users_show_test.rb b/test/integration/users_show_test.rb index 2098e0d7..b322b697 100644 --- a/test/integration/users_show_test.rb +++ b/test/integration/users_show_test.rb @@ -7,18 +7,12 @@ class UsersShowTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) - - User.delete_all @user = create(:user) @path = user_path(@user) @login_helper = UserLoginTestHelper.new(self, @user, @path) end - teardown do - User.delete_all - end - test 'ログインしている場合、表示される' do @login_helper.assert_successful_login_and_get end diff --git a/test/integration/users_update_test.rb b/test/integration/users_update_test.rb index 741bf1cd..4d1605c1 100644 --- a/test/integration/users_update_test.rb +++ b/test/integration/users_update_test.rb @@ -6,18 +6,12 @@ class UsersUpdateTest < ActionDispatch::IntegrationTest setup do @setting = create(:setting) - - User.delete_all @user = create(:user) @path = user_path(@user) @login_helper = UserLoginTestHelper.new(self, @user, @path) end - teardown do - User.delete_all - end - test 'ログインしていない場合、ログインページにリダイレクトされる' do @login_helper.assert_redirected_to_login_on_logged_out do patch( diff --git a/test/models/channel_browse/day_test.rb b/test/models/channel_browse/day_test.rb index c39a0933..5a4a22a8 100644 --- a/test/models/channel_browse/day_test.rb +++ b/test/models/channel_browse/day_test.rb @@ -137,8 +137,6 @@ class ChannelBrowse::DayTest < ActiveSupport::TestCase end def prepare_message_dates - MessageDate.delete_all - @message_date_20160816 = create(:message_date) @message_date_20161231 = create(:message_date_20161231) @message_date_20170401 = create(:message_date_20170401) diff --git a/test/models/channel_browse/month_test.rb b/test/models/channel_browse/month_test.rb index 996b5226..25a23eef 100644 --- a/test/models/channel_browse/month_test.rb +++ b/test/models/channel_browse/month_test.rb @@ -2,6 +2,7 @@ class ChannelBrowse::MonthTest < ActiveSupport::TestCase setup do + create(:channel) @month = build(:channel_browse_month) end @@ -82,8 +83,6 @@ class ChannelBrowse::MonthTest < ActiveSupport::TestCase end def prepare_message_dates - MessageDate.delete_all - @message_date_20160816 = create(:message_date) @message_date_20161231 = create(:message_date_20161231) @message_date_20170401 = create(:message_date_20170401) @@ -97,7 +96,9 @@ def prepare_message_dates @month.month = date.month prev_date = @message_date_20160816.date + prev_month = @month.prev_month + refute_nil(prev_month) assert_equal(@month.channel, prev_month.channel, 'チャンネルが正しい') assert_equal(prev_date.year, prev_month.year, '年が正しい') @@ -123,7 +124,9 @@ def prepare_message_dates @month.month = date.month next_date = @message_date_20170401.date + next_month = @month.next_month + refute_nil(next_month) assert_equal(@month.channel, next_month.channel, 'チャンネルが正しい') assert_equal(next_date.year, next_month.year, '年が正しい') diff --git a/test/models/channel_browse/year_test.rb b/test/models/channel_browse/year_test.rb index 0000e7c9..465caa83 100644 --- a/test/models/channel_browse/year_test.rb +++ b/test/models/channel_browse/year_test.rb @@ -2,6 +2,7 @@ class ChannelBrowse::YearTest < ActiveSupport::TestCase setup do + create(:channel) @year = build(:channel_browse_year) end @@ -55,8 +56,6 @@ class ChannelBrowse::YearTest < ActiveSupport::TestCase end def prepare_message_dates - MessageDate.delete_all - @message_date_20150123 = create(:message_date_20150123) @message_date_20161231 = create(:message_date_20161231) @message_date_20170401 = create(:message_date_20170401) @@ -69,7 +68,9 @@ def prepare_message_dates @year.year = date.year prev_date = @message_date_20150123.date + prev_year = @year.prev_year + refute_nil(prev_year) assert_equal(@year.channel, prev_year.channel, 'チャンネルが正しい') assert_equal(prev_date.year, prev_year.year, '年が正しい') @@ -92,7 +93,9 @@ def prepare_message_dates @year.year = date.year next_date = @message_date_20170401.date + next_year = @year.next_year + refute_nil(next_year) assert_equal(@year.channel, next_year.channel, 'チャンネルが正しい') assert_equal(next_date.year, next_year.year, '年が正しい') diff --git a/test/models/channel_for_channels_index_test.rb b/test/models/channel_for_channels_index_test.rb new file mode 100644 index 00000000..750ed77f --- /dev/null +++ b/test/models/channel_for_channels_index_test.rb @@ -0,0 +1,18 @@ +require 'test_helper' + +class ChannelForChannelsIndexTest < ActiveSupport::TestCase + setup do + [100, 200, 300, 400, 500].each do |id| + create("channel_#{id}".to_sym) + end + + [100, 200, 400].each do |id| + create("channel_#{id}_last_speech".to_sym) + end + end + + test 'for_channels_index の順序が正しい' do + expected = [100, 400, 200, 300, 500].map { |id| "channel_#{id}" } + assert_equal(expected, Channel.for_channels_index.map(&:identifier)) + end +end diff --git a/test/models/channel_last_speech_test.rb b/test/models/channel_last_speech_test.rb index 2c10c926..35d3047b 100644 --- a/test/models/channel_last_speech_test.rb +++ b/test/models/channel_last_speech_test.rb @@ -28,4 +28,26 @@ class ChannelLastSpeechTest < ActiveSupport::TestCase @channel_last_speech.conversation_message = nil refute(@channel_last_speech.valid?) end + + sub_test_case 'refresh!' do + test 'チャンネル所属メッセージが複数ある場合、最新のメッセージが選ばれる' do + create(:privmsg) + privmsg_a = create(:privmsg_keyword_sw_a) + create(:privmsg_keyword_sw_k) + + channel_last_speech = ChannelLastSpeech.refresh!(@channel) + + assert_equal(privmsg_a, @channel.last_speech) + refute_nil(channel_last_speech) + end + + test 'チャンネル所属メッセージが存在しない場合、削除される' do + ConversationMessage.destroy_all + + channel_last_speech = ChannelLastSpeech.refresh!(@channel) + + assert_nil(@channel.last_speech) + assert_nil(channel_last_speech) + end + end end diff --git a/test/models/channel_test.rb b/test/models/channel_test.rb index 8e3d7429..3539d793 100644 --- a/test/models/channel_test.rb +++ b/test/models/channel_test.rb @@ -81,21 +81,6 @@ class ChannelTest < ActiveSupport::TestCase assert_equal('#camelcasechannel', channel.lowercase_name_with_prefix) end - test 'for_channels_index の順序が正しい' do - Channel.delete_all - - [100, 200, 300, 400, 500].each do |id| - create("channel_#{id}".to_sym) - end - - [100, 200, 400].each do |id| - create("channel_#{id}_last_speech".to_sym) - end - - expected = [100, 400, 200, 300, 500].map { |id| "channel_#{id}" } - assert_equal(expected, Channel.for_channels_index.map(&:identifier)) - end - test 'from_cinch_message: 該当するチャンネルが返る' do cinch_channel = Object.new cinch_message = Object.new diff --git a/test/models/conversation_message_archiver_test.rb b/test/models/conversation_message_archiver_test.rb new file mode 100644 index 00000000..23ef55f3 --- /dev/null +++ b/test/models/conversation_message_archiver_test.rb @@ -0,0 +1,103 @@ +require 'test_helper' + +class ConversationMessageArchiverTest < ActiveSupport::TestCase + sub_test_case 'archive!' do + test '発言をアーカイブする(複数発言存在時)' do + privmsg1 = create(:privmsg_rgrb_20140320012345) + privmsg2 = create(:privmsg_role_20140320023456) + reason = create(:archive_reason) + + channel_last_speech1 = ChannelLastSpeech.refresh!(privmsg1.channel) + assert_equal(privmsg2, channel_last_speech1.conversation_message) + + ConversationMessageArchiver.new.archive!({ + old_id: privmsg2.id, + archive_reason_id: reason.id, + }) + + archived_privmsg2 = ArchivedConversationMessage.find_by(old_id: privmsg2.id) + refute_nil(archived_privmsg2) + + keys = [ + :old_id, + :channel_id, + :timestamp, + :nick, + :message, + :type, + :irc_user_id, + :digest, + ] + expected_attrs = privmsg2.attributes.slice(keys) + actual_attrs = archived_privmsg2.attributes.slice(keys) + assert_equal(expected_attrs, actual_attrs) + + assert_nil(ConversationMessage.find_by(id: privmsg2.id)) + + channel_last_speech2 = ChannelLastSpeech.find_by(channel: privmsg1.channel) + assert_equal(privmsg1, channel_last_speech2.conversation_message) + end + + test '発言をアーカイブする(発言1つのみ)' do + privmsg = create(:privmsg) + reason = create(:archive_reason) + + channel_last_speech1 = ChannelLastSpeech.refresh!(privmsg.channel) + assert_equal(privmsg, channel_last_speech1.conversation_message) + + ConversationMessageArchiver.new.archive!({ + old_id: privmsg.id, + archive_reason_id: reason.id, + }) + + archived_privmsg = ArchivedConversationMessage.find_by(old_id: privmsg.id) + refute_nil(archived_privmsg) + + keys = [ + :channel_id, + :timestamp, + :nick, + :message, + :type, + :irc_user_id, + :digest, + ] + expected_attrs = privmsg.attributes.slice(keys) + actual_attrs = archived_privmsg.attributes.slice(keys) + assert_equal(expected_attrs, actual_attrs) + + assert_nil(ConversationMessage.find_by(id: privmsg.id)) + + channel_last_speech2 = ChannelLastSpeech.find_by(channel: privmsg.channel) + assert_nil(channel_last_speech2) + end + end + + test 'reconstitute! はアーカイブされた発言を元に戻す' do + privmsg1 = create(:privmsg_rgrb_20140320012345) + privmsg2 = create(:privmsg_role_20140320023456) + reason = create(:archive_reason) + + ConversationMessageArchiver.new.archive!({ + old_id: privmsg2.id, + archive_reason_id: reason.id, + }) + + archived_privmsg2 = ArchivedConversationMessage.find_by(old_id: privmsg2.id) + refute_nil(archived_privmsg2) + + channel_last_speech1 = ChannelLastSpeech.find_by(channel: privmsg1.channel) + assert_equal(privmsg1, channel_last_speech1.conversation_message) + + ConversationMessageArchiver.new.reconstitute!(archived_privmsg2.id) + + archived_privmsg2_2 = ArchivedConversationMessage.find_by(old_id: privmsg2.id) + assert_nil(archived_privmsg2_2) + + restored_privmsg2 = ConversationMessage.find(privmsg2.id) + assert_equal(privmsg2, restored_privmsg2) + + channel_last_speech2 = ChannelLastSpeech.find_by(channel: privmsg1.channel) + assert_equal(restored_privmsg2, channel_last_speech2.conversation_message) + end +end diff --git a/test/models/message_date_test.rb b/test/models/message_date_test.rb index 94062efb..2713838c 100644 --- a/test/models/message_date_test.rb +++ b/test/models/message_date_test.rb @@ -20,8 +20,6 @@ class MessageDateTest < ActiveSupport::TestCase end def prepare_message_dates - MessageDate.delete_all - @message_date_20150123 = create(:message_date_20150123) @message_date_20160816 = create(:message_date) @message_date_20161231 = create(:message_date_20161231) diff --git a/test/models/message_period_test.rb b/test/models/message_period_test.rb index 8f3d7da0..ad205dde 100644 --- a/test/models/message_period_test.rb +++ b/test/models/message_period_test.rb @@ -3,14 +3,6 @@ class MessagePeriodTest < ActiveSupport::TestCase setup do @period = build(:message_period) - - Message.delete_all - ConversationMessage.delete_all - end - - teardown do - Message.delete_all - ConversationMessage.delete_all end test '有効である' do diff --git a/test/models/message_search_test.rb b/test/models/message_search_test.rb index dabe1d7c..2ff521d6 100644 --- a/test/models/message_search_test.rb +++ b/test/models/message_search_test.rb @@ -3,12 +3,6 @@ class MessageSearchTest < ActiveSupport::TestCase setup do @search = build(:message_search) - - ConversationMessage.delete_all - end - - teardown do - ConversationMessage.delete_all end test '有効である' do diff --git a/test/system/channel_days_index_chart_test.rb b/test/system/channel_days_index_chart_test.rb index b55471ce..4ac40487 100644 --- a/test/system/channel_days_index_chart_test.rb +++ b/test/system/channel_days_index_chart_test.rb @@ -13,9 +13,6 @@ class ChannelDaysIndexChartTest < ApplicationSystemTestCase @channel = create(:channel) - MessageDate.delete_all - ConversationMessage.delete_all - create(:privmsg_rgrb_20140320012345) create(:message_date_20140320) @@ -28,11 +25,6 @@ class ChannelDaysIndexChartTest < ApplicationSystemTestCase ) end - teardown do - MessageDate.delete_all - ConversationMessage.delete_all - end - test '月のページにおいてグラフが描画されている' do visit(@browse_month.path) diff --git a/test/system/message_filter_test.rb b/test/system/message_filter_test.rb index a9bb5a0e..8baee015 100644 --- a/test/system/message_filter_test.rb +++ b/test/system/message_filter_test.rb @@ -17,9 +17,6 @@ class MessageFilterTest < ApplicationSystemTestCase create(:setting) create(:user) - Message.delete_all - ConversationMessage.delete_all - @channel = create(:channel) create_speeches @@ -36,11 +33,6 @@ class MessageFilterTest < ApplicationSystemTestCase ) end - teardown do - Message.delete_all - ConversationMessage.delete_all - end - data( '日付ページ' => [->_ { @browse.path }, true], '日付ページ生ログ' => [->_ { @browse_raw.path }, false], diff --git a/test/system/message_list_style_test.rb b/test/system/message_list_style_test.rb index b364af3c..e2220f67 100644 --- a/test/system/message_list_style_test.rb +++ b/test/system/message_list_style_test.rb @@ -13,9 +13,6 @@ class MessageListStyleTest < ApplicationSystemTestCase @channel = create(:channel) - MessageDate.delete_all - ConversationMessage.delete_all - create(:privmsg_rgrb_20140320012345) create(:message_date_20140320) @@ -24,11 +21,6 @@ class MessageListStyleTest < ApplicationSystemTestCase @browse_raw = ChannelBrowse::Day.new(channel: @channel, date: date, style: :raw) end - teardown do - MessageDate.delete_all - ConversationMessage.delete_all - end - test '通常表示のとき「生ログ」を押すとCookieが正しく設定される' do visit(@browse.path) diff --git a/test/system/messages_period_test.rb b/test/system/messages_period_test.rb index 46c19a1a..43e414fc 100644 --- a/test/system/messages_period_test.rb +++ b/test/system/messages_period_test.rb @@ -7,15 +7,9 @@ class MessagesPeriodTest < ApplicationSystemTestCase setup do create(:setting) - - ConversationMessage.delete_all @privmsg = create(:privmsg) end - teardown do - ConversationMessage.delete_all - end - test '検索結果が正しく表示される' do visit( messages_period_url(channels: 'irc_test', diff --git a/test/system/messages_search_test.rb b/test/system/messages_search_test.rb index 37b3d0ae..e3b7b471 100644 --- a/test/system/messages_search_test.rb +++ b/test/system/messages_search_test.rb @@ -7,15 +7,9 @@ class MessagesSearchTest < ApplicationSystemTestCase setup do create(:setting) - - ConversationMessage.delete_all @privmsg = create(:privmsg) end - teardown do - ConversationMessage.delete_all - end - test '検索結果が正しく表示される' do visit(messages_search_url(q: 'メッセージ')) diff --git a/test/test_helper.rb b/test/test_helper.rb index 687b9a67..61e242ce 100644 --- a/test/test_helper.rb +++ b/test/test_helper.rb @@ -13,15 +13,26 @@ require 'factory_bot_rails' -class ActiveSupport::TestCase - # Setup all fixtures in test/fixtures/*.yml for all tests in alphabetical order. - fixtures :all +require 'database_cleaner/active_record' + +DatabaseCleaner.strategy = :truncation +class ActiveSupport::TestCase # Add more helper methods to be used by all tests here... include FactoryBot::Syntax::Methods include ActiveSupport::Testing::TimeHelpers + self.use_transactional_tests = false + + setup do + DatabaseCleaner.start + end + + teardown do + DatabaseCleaner.clean + end + # ファクトリーからオブジェクトを作成する # @param [Array] ids ファクトリーIDの配列 # @return [Array] 作成されたオブジェクトの配列