X-Git-Url: http://git.osdn.net/view?a=blobdiff_plain;f=spec%2Fmodels%2Fspeech_spec.rb;h=51af8e190fede5f317097e6488ae9c63bc7386df;hb=eb397c27a98c59c856b91a5b09d1316b1135a15c;hp=406b4aed6d4df643fdb09bcf804a3ff218c91941;hpb=5860cb44b475e1dd24c2b0f87b74b516c97c33bf;p=pettanr%2Fpettanr.git diff --git a/spec/models/speech_spec.rb b/spec/models/speech_spec.rb index 406b4aed..51af8e19 100644 --- a/spec/models/speech_spec.rb +++ b/spec/models/speech_spec.rb @@ -1,156 +1,731 @@ # -*- encoding: utf-8 -*- -require 'spec_helper' - -describe Speech do - before do - Factory :admin - @license = Factory :license - @user = Factory( :user_yas) - @author = @user.author - - @panel = Factory :panel - @speech_balloon_template = Factory :speech_balloon_template - @speech_balloon = Factory :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id +#セリフ +require 'spec_helper' + +describe Speech do + before do + SpeechBalloonTemplate.delete_all + @admin = FactoryGirl.create :admin + @user = FactoryGirl.create( :user_yas) + @author = FactoryGirl.create :author, :user_id => @user.id + @other_user = FactoryGirl.create( :user_yas) + @other_author = FactoryGirl.create :author, :user_id => @other_user.id + @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id + @sp = FactoryGirl.create :system_picture + @lg = FactoryGirl.create :license_group + @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id + + @writing_format = FactoryGirl.create :writing_format + SpeechBalloonTemplate.delete_all + @speech_balloon_template = FactoryGirl.create :speech_balloon_template, "name" => "circle@pettan.com", "classname" => "CircleSpeechBalloon", "caption" => "cc", "system_picture_id" => @sp.id, "settings" => '{}' + @panel = FactoryGirl.create :panel, :author_id => @author.id end - - describe '検証に於いて' do - it 'オーソドックスなデータなら通る' do - @speech = Factory.build :speech, :speech_balloon_id => @speech_balloon.id - @speech.should be_valid - end - - context 'speech_balloon_idを検証するとき' do - before do - @speech = Factory.build :speech, :speech_balloon_id => @speech_balloon.id - end - it 'テストデータの確認' do - @speech.speech_balloon_id = @speech_balloon.id - @speech.should be_valid - end - it 'nullなら失敗する' do - @speech.speech_balloon_id = nil - @speech.should_not be_valid - end - it '数値でなければ失敗する' do - @speech.speech_balloon_id = 'a' - @speech.should_not be_valid - end - it '存在するフキダシコミックでなければ失敗する' do - @speech.speech_balloon_id = 0 - @speech.should_not be_valid - end - end - context 'contentを検証するとき' do - end - context 'xを検証するとき' do - before do - @speech = Factory.build :speech, :speech_balloon_id => @speech_balloon.id - end - it 'テストデータの確認' do - @speech.x = '1' - @speech.should be_valid - end - it 'nullなら失敗する' do - @speech.x = nil - @speech.should_not be_valid - end - it '数値でなければ失敗する' do - @speech.x = 'a' - @speech.should_not be_valid - end - it '0なら通る' do - @speech.x = '0' - @speech.should be_valid - end - it '負でも通る' do - @speech.x = -1 - @speech.should be_valid - end - end - context 'yを検証するとき' do - before do - @speech = Factory.build :speech, :speech_balloon_id => @speech_balloon.id - end - it 'テストデータの確認' do - @speech.y = '1' - @speech.should be_valid - end - it 'nullなら失敗する' do - @speech.y = nil - @speech.should_not be_valid - end - it '数値でなければ失敗する' do - @speech.y = 'a' - @speech.should_not be_valid - end - it '0なら通る' do - @speech.y = '0' - @speech.should be_valid - end - it '負でも通る' do - @speech.y = -1 - @speech.should be_valid - end - end - context 'widthを検証するとき' do - before do - @speech = Factory.build :speech, :speech_balloon_id => @speech_balloon.id - end - it 'テストデータの確認' do - @speech.width = 1 - @speech.should be_valid - end - it 'nullなら失敗する' do - @speech.width = nil - @speech.should_not be_valid - end - it '数値でなければ失敗する' do - @speech.width = 'a' - @speech.should_not be_valid - end - it '0なら失敗する' do - @speech.width = '0' - @speech.should_not be_valid - end - it '負でも失敗する' do - @speech.width = -1 - @speech.should_not be_valid - end - end - context 'heightを検証するとき' do - before do - @speech = Factory.build :speech, :speech_balloon_id => @speech_balloon.id - end - it 'テストデータの確認' do - @speech.height = '1' - @speech.should be_valid - end - it 'nullなら失敗する' do - @speech.height = nil - @speech.should_not be_valid - end - it '数値でなければ失敗する' do - @speech.height = 'a' - @speech.should_not be_valid - end - it '0なら失敗する' do - @speech.height = '0' - @speech.should_not be_valid - end - it '負でも失敗する' do - @speech.height = -1 - @speech.should_not be_valid - end - end - context 'settingsを検証するとき' do - before do - @speech = Factory.build :speech, :speech_balloon_id => @speech_balloon.id - end - it 'テストデータの確認' do - @speech.settings = '' - @speech.should be_valid - end - end - end - -end + + describe '検証に於いて' do + before do + @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = @sb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id) + ) + @balloon = @sb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb.boost + @sb.save! + end + + context 'オーソドックスなデータのとき' do + it '下限データが通る' do + @speech.font_size = 1.0 + @speech.text_align = 0 + @speech.fore_color = 0 + @speech.x = 0 + @speech.y = 0 + @speech.width = 1 + @speech.height = 1 + @speech.quotes = '' + @speech.should be_valid + end + it '上限データが通る' do + @speech.font_size = 99999.0 + @speech.text_align = 3 + @speech.fore_color = 0xffffff + @speech.x = 99999 + @speech.y = 99999 + @speech.width = 99999 + @speech.height = 99999 + @speech.quotes = '[ ] [ ] [ ] [ ]' + @speech.should be_valid + end + end + + context 'speech_balloon_idを検証するとき' do + #ネストの保存はnilを許可しなければならないので数値チェックだけ + it 'テストデータの確認' do + @speech.speech_balloon_id = @sb.id + @speech.should be_valid + end + it '数値でなければ失敗する' do + @speech.speech_balloon_id = 'a' + @speech.should_not be_valid + end + end + context 'contentを検証するとき' do + end + context 'font_sizeを検証するとき' do + it 'nullなら失敗する' do + @speech.font_size = nil + @speech.should_not be_valid + end + it '数値でなければ失敗する' do + @speech.font_size = 'a' + @speech.should_not be_valid + end + it '0なら通る' do + @speech.font_size = '0' + @speech.should be_valid + end + it '10.5なら通る' do + @speech.font_size = '0' + @speech.should be_valid + end + it '負なら失敗する' do + @speech.font_size = 10.5 + @speech.should be_valid + end + end + context 'text_alignを検証するとき' do + it 'nullなら失敗する' do + @speech.text_align = nil + @speech.should_not be_valid + end + it '数値でなければ失敗する' do + @speech.text_align = 'a' + @speech.should_not be_valid + end + it '0なら通る' do + @speech.text_align = '0' + @speech.should be_valid + end + it '負なら失敗する' do + @speech.text_align = -1 + @speech.should_not be_valid + end + it '4なら失敗する' do + @speech.text_align = 4 + @speech.should_not be_valid + end + end + context 'fore_colorを検証するとき' do + it 'nullなら失敗する' do + @speech.fore_color = nil + @speech.should_not be_valid + end + it '数値でなければ失敗する' do + @speech.fore_color = 'a' + @speech.should_not be_valid + end + it '0なら通る' do + @speech.fore_color = '0' + @speech.should be_valid + end + it '負なら失敗する' do + @speech.fore_color = -1 + @speech.should_not be_valid + end + it '24bit colorでなければ失敗する' do + @speech.fore_color = 0x1000000 + @speech.should_not be_valid + end + end + context 'xを検証するとき' do + it 'nullなら失敗する' do + @speech.x = nil + @speech.should_not be_valid + end + it '数値でなければ失敗する' do + @speech.x = 'a' + @speech.should_not be_valid + end + it '0なら通る' do + @speech.x = '0' + @speech.should be_valid + end + it '負でも通る' do + @speech.x = -1 + @speech.should be_valid + end + end + context 'yを検証するとき' do + it 'nullなら失敗する' do + @speech.y = nil + @speech.should_not be_valid + end + it '数値でなければ失敗する' do + @speech.y = 'a' + @speech.should_not be_valid + end + it '0なら通る' do + @speech.y = '0' + @speech.should be_valid + end + it '負でも通る' do + @speech.y = -1 + @speech.should be_valid + end + end + context 'widthを検証するとき' do + it 'nullなら失敗する' do + @speech.width = nil + @speech.should_not be_valid + end + it '数値でなければ失敗する' do + @speech.width = 'a' + @speech.should_not be_valid + end + it '0なら失敗する' do + @speech.width = '0' + @speech.should_not be_valid + end + it '負でも失敗する' do + @speech.width = -1 + @speech.should_not be_valid + end + end + context 'heightを検証するとき' do + it 'nullなら失敗する' do + @speech.height = nil + @speech.should_not be_valid + end + it '数値でなければ失敗する' do + @speech.height = 'a' + @speech.should_not be_valid + end + it '0なら失敗する' do + @speech.height = '0' + @speech.should_not be_valid + end + it '負でも失敗する' do + @speech.height = -1 + @speech.should_not be_valid + end + end + context 'quotesを検証するとき' do + it '奇数なら失敗する' do + @speech.quotes = '[ ] [' + @speech.should_not be_valid + end + it '16文字以上なら失敗する' do + @speech.quotes = '[ ] [ ] [ ] [ ] [ ]' + @speech.should_not be_valid + end + end + context 'settingsを検証するとき' do + end + end + + describe '文字コード検証に於いて' do + before do + @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = @sb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id) + ) + @balloon = @sb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb.boost + @sb.save! + end + + context 'contentを検証するとき' do + it 'Shift JISなら失敗する' do + @speech.content = "\x83G\x83r\x83]\x83D" + lambda{ + @speech.valid_encode + }.should raise_error(Pettanr::BadRequest) + end + end + + context 'quotesを検証するとき' do + it 'Shift JISなら失敗する' do + @speech.quotes = "\x83G \x83r \x83 ]\x83D" + lambda{ + @speech.valid_encode + }.should raise_error(Pettanr::BadRequest) + end + end + + context 'settingsを検証するとき' do + it 'Shift JISなら失敗する' do + @speech.settings = "\x83G\x83r\x83]\x83D" + lambda{ + @speech.valid_encode + }.should raise_error(Pettanr::BadRequest) + end + end + end + + describe '閲覧許可に於いて' do + before do + @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = @sb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id) + ) + @balloon = @sb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb.boost + @sb.save! + end + context 'オープンモードのとき' do + before do + MagicNumber['run_mode'] = 0 + end + it '自身にゲスト用ロールチェックを問い合わせしている' do + Speech.any_instance.stub(:guest_role_check).and_return(true) + Speech.any_instance.should_receive(:guest_role_check).with(any_args).exactly(1) + r = @speech.visible?([@author]) + end + it 'ゲスト用ロールチェックが失敗したとき、falseを返す' do + Speech.any_instance.stub(:guest_role_check).and_return(false) + r = @speech.visible?([@author]) + r.should be_false + end + end + context 'クローズドモードのとき' do + before do + MagicNumber['run_mode'] = 1 + end + it '自身に読者用ロールチェックを問い合わせしている' do + Speech.any_instance.stub(:reader_role_check).and_return(true) + Speech.any_instance.should_receive(:reader_role_check).with(any_args).exactly(1) + r = @speech.visible?([@author]) + end + it '読者用ロールチェックが失敗したとき、falseを返す' do + Speech.any_instance.stub(:reader_role_check).and_return(false) + r = @speech.visible?([@author]) + r.should be_false + end + end + context '事前チェックする' do + before do + MagicNumber['run_mode'] = 1 + Speech.any_instance.stub(:reader_role_check).and_return(true) + end + it '自身のコマに所持判定を問い合わせしている' do + Panel.any_instance.stub(:own?).and_return(true) + Panel.any_instance.should_receive(:own?).with(any_args).exactly(1) + r = @speech.visible?([@author]) + end + it '自身のコマに閲覧許可を問い合わせしている' do + Panel.any_instance.stub(:own?).and_return(false) + Panel.any_instance.stub(:visible?).and_return(true) + Panel.any_instance.should_receive(:visible?).with(any_args).exactly(1) + r = @speech.visible?([@author]) + end + end + context 'つつがなく終わるとき' do + before do + MagicNumber['run_mode'] = 1 + Panel.any_instance.stub(:reader_role_check).and_return(true) + end + it '自分のコマのセリフなら許可する' do + Panel.any_instance.stub(:own?).and_return(true) + Panel.any_instance.stub(:visible?).and_return(false) + r = @speech.visible?([@author]) + r.should be_true + end + it '他人の非公開コマのセリフなら許可しない' do + Panel.any_instance.stub(:own?).and_return(false) + Panel.any_instance.stub(:visible?).and_return(false) + r = @speech.visible?([@author]) + r.should be_false + end + it '他人のコマのセリフでも公開なら許可する' do + Panel.any_instance.stub(:own?).and_return(false) + Panel.any_instance.stub(:visible?).and_return(true) + r = @speech.visible?([@author]) + r.should be_true + end + end + end + + describe '一覧取得に於いて' do + before do + @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = @sb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id) + ) + @balloon = @sb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb.boost + @sb.save! + end + context 'page補正について' do + it '文字列から数値に変換される' do + Speech.page('8').should eq 8 + end + it 'nilの場合は1になる' do + Speech.page().should eq 1 + end + it '0以下の場合は1になる' do + Speech.page('0').should eq 1 + end + end + context 'page_size補正について' do + it '文字列から数値に変換される' do + Speech.page_size('7').should eq 7 + end + it 'nilの場合はSpeech.default_page_sizeになる' do + Speech.page_size().should eq Speech.default_page_size + end + it '0以下の場合はSpeech.default_page_sizeになる' do + Speech.page_size('0').should eq Speech.default_page_size + end + it 'Speech.max_page_sizeを超えた場合はSpeech.max_page_sizeになる' do + Speech.page_size('1000').should eq Speech.max_page_size + end + end + context 'つつがなく終わるとき' do + it '一覧取得オプションを利用している' do + Speech.stub(:list_opt).with(any_args).and_return({:include => {:speech_balloon => {:panel => {}}}}) + Speech.should_receive(:list_opt).with(any_args).exactly(1) + r = Speech.list + end + end + it 'リストを返す' do + r = Speech.list + r.should eq [@speech] + end + it '時系列で並んでいる' do + #公開されたコマのセリフは(他人のセリフであっても)含んでいる + hc = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1 + nsb = FactoryGirl.build :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id + npl = nsb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 100) + ) + nsb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + nsb.boost + nsb.save! + r = Speech.list + r.should eq [npl, @speech] + end + it '非公開のコマのセリフは自分のセリフであっても含まない' do + hc = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0 + nsb = FactoryGirl.build :speech_balloon, :panel_id => hc.id, :speech_balloon_template_id => @speech_balloon_template.id + npl = nsb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 100) + ) + nsb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + nsb.boost + nsb.save! + pl = Speech.list + pl.should eq [@speech] + end + context 'DBに5件あって1ページの件数を2件に変えたとして' do + before do + @sb2 = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech2 = @sb2.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 100) + ) + @balloon2 = @sb2.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb2.boost + @sb2.save! + @sb3 = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech3 = @sb3.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 200) + ) + @balloon3 = @sb3.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb3.boost + @sb3.save! + @sb4 = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech4 = @sb4.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 300) + ) + @balloon4 = @sb4.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb4.boost + @sb4.save! + @sb5 = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech5 = @sb5.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 400) + ) + @balloon5 = @sb5.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb5.boost + @sb5.save! + Speech.stub(:default_page_size).and_return(2) + end + it '通常は2件を返す' do + pl = Speech.list + pl.should have(2).items + end + it 'page=1なら末尾2件を返す' do + #時系列で並んでいる + pl = Speech.list(1) + pl.should eq [@speech5, @speech4] + end + it 'page=2なら中間2件を返す' do + pl = Speech.list(2) + pl.should eq [@speech3, @speech2] + end + it 'page=3なら先頭1件を返す' do + pl = Speech.list(3) + pl.should eq [@speech] + end + end + context 'DBに5件あって1ページの件数を0件に変えたとして' do + before do + @sb2 = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech2 = @sb2.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 100) + ) + @balloon2 = @sb2.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb2.boost + @sb2.save! + @sb3 = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech3 = @sb3.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 200) + ) + @balloon3 = @sb3.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb3.boost + @sb3.save! + @sb4 = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech4 = @sb4.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 300) + ) + @balloon4 = @sb4.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb4.boost + @sb4.save! + @sb5 = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech5 = @sb5.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id, :updated_at => Time.now + 400) + ) + @balloon5 = @sb5.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb5.boost + @sb5.save! + Speech.stub(:default_page_size).and_return(2) + end + it '通常は全件(5件)を返す' do + r = Speech.list 5, 0 + r.should have(5).items + end + end + end + describe '一覧取得オプションに於いて' do + it 'includeキーを含んでいる' do + r = Speech.list_opt + r.has_key?(:include).should be_true + end + it '1つの項目を含んでいる' do + r = Speech.list_opt[:include] + r.should have(1).items + end + it 'フキダシを含んでいる' do + r = Speech.list_opt[:include] + r.has_key?(:speech_balloon).should be_true + end + it 'フキダシはコマを含んでいる' do + r = Speech.list_opt[:include] + r[:speech_balloon].has_key?(:panel).should be_true + end + it 'コマは作家を含んでいる' do + r = Speech.list_opt[:include] + r[:speech_balloon][:panel].has_key?(:author).should be_true + end + it 'フキダシはフキダシ枠を含んでいる' do + r = Speech.list_opt[:include] + r[:speech_balloon].has_key?(:balloon).should be_true + end + it 'フキダシはフキダシテンプレートを含んでいる' do + r = Speech.list_opt[:include] + r[:speech_balloon].has_key?(:speech_balloon_template).should be_true + end + end + describe 'json一覧出力オプションに於いて' do + before do + @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = @sb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id) + ) + @balloon = @sb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb.boost + @sb.save! + end + it 'フキダシを含んでいる' do + r = Speech.list.to_json Speech.list_json_opt + j = JSON.parse r + i = j.first + i.has_key?('speech_balloon').should be_true + end + it 'フキダシはコマを含んでいる' do + r = Speech.list.to_json Speech.list_json_opt + j = JSON.parse r + i = j.first + s = i['speech_balloon'] + s.has_key?('panel').should be_true + end + it 'コマは作家を含んでいる' do + r = Speech.list.to_json Speech.list_json_opt + j = JSON.parse r + i = j.first + s = i['speech_balloon']['panel'] + s.has_key?('author').should be_true + end + it 'フキダシはフキダシ枠を含んでいる' do + r = Speech.list.to_json Speech.list_json_opt + j = JSON.parse r + i = j.first + s = i['speech_balloon'] + s.has_key?('balloon').should be_true + end + it 'フキダシはフキダシテンプレートを含んでいる' do + r = Speech.list.to_json Speech.list_json_opt + j = JSON.parse r + i = j.first + s = i['speech_balloon'] + s.has_key?('speech_balloon_template').should be_true + end + end + + describe '単体取得に於いて' do + before do + @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = @sb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id) + ) + @balloon = @sb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb.boost + @sb.save! + end + context 'つつがなく終わるとき' do + it '単体取得オプションを利用している' do + Speech.stub(:show_opt).with(any_args).and_return({:include => {:speech_balloon => {:panel => {}}}}) + Speech.should_receive(:show_opt).with(any_args).exactly(1) + r = Speech.show @speech.id, @author + end + it '閲覧許可を問い合わせている' do + Speech.any_instance.stub(:visible?).with(any_args).and_return(true) + Speech.any_instance.should_receive(:visible?).with(any_args).exactly(1) + r = Speech.show @speech.id, @author + end + end + it '指定のセリフを返す' do + Speech.any_instance.stub(:visible?).and_return(true) + pl = Speech.show @speech.id, @author + pl.should eq @speech + end + context '閲覧許可が出なかったとき' do + it '403Forbidden例外を返す' do + Speech.any_instance.stub(:visible?).and_return(false) + lambda{ + Speech.show @speech.id, @author + }.should raise_error(ActiveRecord::Forbidden) + end + end + context '存在しないセリフを開こうとしたとき' do + it '404RecordNotFound例外を返す' do + lambda{ + Speech.show 110, @author + }.should raise_error(ActiveRecord::RecordNotFound) + end + end + end + describe '単体取得オプションに於いて' do + it 'includeキーを含んでいる' do + r = Speech.show_opt + r.has_key?(:include).should be_true + end + it '1つの項目を含んでいる' do + r = Speech.show_opt[:include] + r.should have(1).items + end + it 'フキダシを含んでいる' do + r = Speech.show_opt[:include] + r.has_key?(:speech_balloon).should be_true + end + it 'フキダシはコマを含んでいる' do + r = Speech.show_opt[:include] + r[:speech_balloon].has_key?(:panel).should be_true + end + it 'コマは作家を含んでいる' do + r = Speech.show_opt[:include] + r[:speech_balloon][:panel].has_key?(:author).should be_true + end + it 'フキダシはフキダシ枠を含んでいる' do + r = Speech.show_opt[:include] + r[:speech_balloon].has_key?(:balloon).should be_true + end + it 'フキダシはフキダシテンプレートを含んでいる' do + r = Speech.show_opt[:include] + r[:speech_balloon].has_key?(:speech_balloon_template).should be_true + end + end + describe 'json単体出力オプションに於いて' do + before do + @sb = FactoryGirl.build :speech_balloon, :panel_id => @panel.id, :speech_balloon_template_id => @speech_balloon_template.id + @speech = @sb.build_speech( + FactoryGirl.attributes_for(:speech, :writing_format_id => @writing_format.id) + ) + @balloon = @sb.build_balloon( + FactoryGirl.attributes_for(:balloon, :system_picture_id => @sp.id) + ) + @sb.boost + @sb.save! + end + it 'フキダシを含んでいる' do + r = Speech.show(@speech.id, @author).to_json Speech.show_json_opt + j = JSON.parse r + i = j + i.has_key?('speech_balloon').should be_true + end + it 'コマを含んでいる' do + r = Speech.show(@speech.id, @author).to_json Speech.show_json_opt + j = JSON.parse r + i = j['speech_balloon'] + i.has_key?('panel').should be_true + end + it 'コマは作家を含んでいる' do + r = Speech.show(@speech.id, @author).to_json Speech.show_json_opt + j = JSON.parse r + i = j['speech_balloon'] + s = i['panel'] + s.has_key?('author').should be_true + end + it 'フキダシ枠を含んでいる' do + r = Speech.show(@speech.id, @author).to_json Speech.show_json_opt + j = JSON.parse r + i = j['speech_balloon'] + i.has_key?('balloon').should be_true + end + it 'フキダシテンプレートを含んでいる' do + r = Speech.show(@speech.id, @author).to_json Speech.show_json_opt + j = JSON.parse r + i = j['speech_balloon'] + i.has_key?('speech_balloon_template').should be_true + end + end +end