+# -*- encoding: utf-8 -*-
require 'spec_helper'
-
+#フキダシテンプレート
describe SpeechBalloonTemplate do
- pending "add some examples to (or delete) #{__FILE__}"
+ before do
+ SpeechBalloonTemplate.delete_all
+ #テストデータを用意してね
+ @f = Rails.root + 'spec/json/speech_balloon_template.json'
+ @t = File.open(@f, 'r').read
+ @j = JSON.parse @t
+ @fs = Rails.root + 'spec/json/speech_balloon_templates.json'
+ @ts = File.open(@fs, 'r').read
+ @js = JSON.parse @ts
+ @fes = Rails.root + 'spec/json/invalid_speech_balloon_templates.json'
+ @tes = File.open(@fes, 'r').read
+ @jes = JSON.parse @tes
+
+ @admin = FactoryGirl.create :admin
+ @user = FactoryGirl.create( :user_yas)
+ @author = FactoryGirl.create :author, :user_id => @user.id
+ @artist = FactoryGirl.create :artist_yas, :author_id => @author.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
+ end
+
+ describe '検証に於いて' do
+ before do
+ @st = FactoryGirl.build :speech_balloon_template
+ end
+
+ context 'オーソドックスなデータのとき' do
+ it '下限データが通る' do
+ @st.name = 'a'
+ @st.classname = 'a'
+ @st.caption = 'a'
+ @st.t = 0
+ @st.settings = '{}'
+ @st.should be_valid
+ end
+ it '上限データが通る' do
+ @st.name = 'a'*50
+ @st.classname = 'a'*50
+ @st.caption = 'a'*30
+ @st.t = 99999
+ @st.settings = '{' + 'a'*99999 + '}'
+ @st.should be_valid
+ end
+ end
+
+
+ context 'nameを検証するとき' do
+ it 'nullなら失敗する' do
+ @st.name = ''
+ @st.should_not be_valid
+ end
+ it '51文字以上なら失敗する' do
+ @st.name = 'a'*51
+ @st.should_not be_valid
+ end
+ end
+ context 'classnameを検証するとき' do
+ it 'nullなら失敗する' do
+ @st.classname = ''
+ @st.should_not be_valid
+ end
+ it '51文字以上なら失敗する' do
+ @st.classname = 'a'*51
+ @st.should_not be_valid
+ end
+ end
+ context 'captionを検証するとき' do
+ it 'nullなら失敗する' do
+ @st.caption = ''
+ @st.should_not be_valid
+ end
+ it '31文字以上なら失敗する' do
+ @st.caption = 'a'*31
+ @st.should_not be_valid
+ end
+ end
+ context 'tを検証するとき' do
+ it 'nullなら失敗する' do
+ @st.t = nil
+ @st.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @st.t = 'a'
+ @st.should_not be_valid
+ end
+ it '負なら失敗する' do
+ @st.t = -1
+ @st.should_not be_valid
+ end
+ end
+ context 'system_picture_idを検証するとき' do
+ it 'nullなら失敗する' do
+ @st.system_picture_id = nil
+ @st.should_not be_valid
+ end
+ it '数値でなければ失敗する' do
+ @st.system_picture_id = 'a'
+ @st.should_not be_valid
+ end
+ it '存在するシステム画像でなければ失敗する' do
+ @st.system_picture_id = 0
+ @st.should_not be_valid
+ end
+ end
+ context 'settingsを検証するとき' do
+ it 'nullなら失敗する' do
+ @st.settings = nil
+ @st.should_not be_valid
+ end
+ end
+ end
+
+ describe '文字コード検証に於いて' do
+ before do
+ @st = FactoryGirl.build :speech_balloon_template
+ end
+
+ context 'nameを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @st.name = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @st.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+
+ context 'classnameを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @st.classname = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @st.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+
+ context 'captionを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @st.caption = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @st.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+
+ context 'settingsを検証するとき' do
+ it 'Shift JISなら失敗する' do
+ @st.settings = "\x83G\x83r\x83]\x83D"
+ lambda{
+ @st.valid_encode
+ }.should raise_error(Pettanr::BadRequest)
+ end
+ end
+ end
+
+ describe 'デフォルト値補充に於いて' do
+ it 'defined' do
+ @st = FactoryGirl.build :speech_balloon_template
+ @st.supply_default
+ end
+ end
+
+ describe '上書き補充に於いて' do
+ before do
+ @st = FactoryGirl.build :speech_balloon_template
+ end
+ it 'テーブルが空のときは0を返す' do
+ @st.overwrite
+ @st.t.should eq 0
+ end
+ it 'テーブルが1行あるときは1を返す' do
+ FactoryGirl.create :speech_balloon_template
+ @st.overwrite
+ @st.t.should eq 1
+ end
+ it 'テーブルが2行あるときは2を返す' do
+ FactoryGirl.create :speech_balloon_template
+ FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
+ @st.overwrite
+ @st.t.should eq 2
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ @st = FactoryGirl.create :speech_balloon_template, :t => 0
+ end
+ it '更新のときは補充しない' do
+ @st.overwrite
+ @st.t.should eq 0
+ end
+ end
+ end
+
+ describe '閲覧許可に於いて' do
+ before do
+ @st = FactoryGirl.build :speech_balloon_template
+ end
+ it '許可する' do
+ @st.visible?(@author).should == true
+ end
+ end
+
+ describe '一覧取得に於いて' do
+ before do
+ @st = FactoryGirl.create :speech_balloon_template
+ end
+ context 'つつがなく終わるとき' do
+ it '一覧取得オプションを利用している' do
+ SpeechBalloonTemplate.stub(:list_opt).with(any_args).and_return({})
+ SpeechBalloonTemplate.should_receive(:list_opt).with(any_args).exactly(1)
+ r = SpeechBalloonTemplate.list
+ end
+ end
+ it 'リストを返す' do
+ l = SpeechBalloonTemplate.list
+ l.should eq [@st]
+ end
+ it 't順で並んでいる' do
+ @st2 = FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
+ l = SpeechBalloonTemplate.list
+ l.should eq [@st, @st2]
+ end
+ end
+ describe '一覧取得オプションに於いて' do
+ it '空のHashを返す' do
+ r = SpeechBalloonTemplate.list_opt
+ r.is_a?(Hash).should be_true
+ r.should be_empty
+ end
+ end
+ describe 'json一覧出力オプションに於いて' do
+ it '空のHashを返す' do
+ r = SpeechBalloonTemplate.list_json_opt
+ r.is_a?(Hash).should be_true
+ r.should be_empty
+ end
+ end
+
+ describe '単体取得に於いて' do
+ before do
+ @st = FactoryGirl.create :speech_balloon_template
+ end
+ context 'つつがなく終わるとき' do
+ it '単体取得オプションを利用している' do
+ SpeechBalloonTemplate.stub(:show_opt).with(any_args).and_return({})
+ SpeechBalloonTemplate.should_receive(:show_opt).with(any_args).exactly(1)
+ r = SpeechBalloonTemplate.show @st.id, @author
+ end
+ end
+ it '指定のフキダシテンプレートを返す' do
+ l = SpeechBalloonTemplate.show @st.id, @author
+ l.should eq @st
+ end
+ context '他人のフキダシテンプレートを開こうとしたとき' do
+ it '403Forbidden例外を返す' do
+ SpeechBalloonTemplate.any_instance.stub(:visible?).and_return(false)
+ lambda{
+ r = SpeechBalloonTemplate.show @st.id, @other_author
+ }.should raise_error(ActiveRecord::Forbidden)
+ end
+ end
+ context '存在しないフキダシテンプレートを開こうとしたとき' do
+ it '404RecordNotFound例外を返す' do
+ lambda{
+ SpeechBalloonTemplate.show 110, @author
+ }.should raise_error(ActiveRecord::RecordNotFound)
+ end
+ end
+ end
+ describe '単体出力オプションに於いて' do
+ it '空のHashを返す' do
+ r = SpeechBalloonTemplate.show_opt
+ r.is_a?(Hash).should be_true
+ r.should be_empty
+ end
+ end
+ describe 'json単体出力オプションに於いて' do
+ it '空のHashを返す' do
+ r = SpeechBalloonTemplate.show_json_opt
+ r.is_a?(Hash).should be_true
+ r.should be_empty
+ end
+ end
+
+ describe '更新に於いて' do
+ #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
+ #特にシステム画像データはidを直接記述して置換させないようにテストしている。
+ before do
+ @sp = FactoryGirl.create :system_picture
+ @st = FactoryGirl.create :speech_balloon_template
+ @settings = {
+ "speech_balloon" => {"default" => 1},
+ "one" => {"balloon" => {"system_picture_id" => @sp.id}}
+ }
+ @attr = {"classname" => 'store', "caption" => 'store test', "system_picture_id" => @sp.id,
+ "settings" => @settings
+ }
+ end
+ context '事前チェック' do
+ before do
+ SpeechBalloonTemplate.stub(:replace_system_picture).with(@attr).and_return(@attr)
+ SpeechBalloonTemplate.stub(:modify_object).with(any_args).and_return(@st)
+ SpeechBalloonTemplate.any_instance.stub(:overwrite).with(any_args).and_return(true)
+ SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(true)
+ end
+ it 'システム画像置換に依頼している' do
+ SpeechBalloonTemplate.should_receive(:replace_system_picture).with(@attr).exactly(1)
+ r = SpeechBalloonTemplate.store 'circle_v01', @attr
+ end
+ it 'インポート処理のデータ更新準備に依頼している' do
+ SpeechBalloonTemplate.should_receive(:modify_object).exactly(1)
+ r = SpeechBalloonTemplate.store 'circle_v01', @attr
+ end
+ it '上書き補充を依頼している' do
+ SpeechBalloonTemplate.any_instance.should_receive(:overwrite).exactly(1)
+ r = SpeechBalloonTemplate.store 'circle_v01', @attr
+ end
+ it 'オブジェクトを保存している' do
+ SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
+ r = SpeechBalloonTemplate.store 'circle_v01', @attr
+ end
+ end
+ context 'つつがなく終わるとき' do
+ before do
+ end
+ it 'カラム値のsettingsをセットしている' do
+ r = SpeechBalloonTemplate.store 'circle_v01', @attr
+ r.settings.should eq @settings.to_json.to_s
+ end
+ it 'モデルが作成されている' do
+ lambda {
+ r = SpeechBalloonTemplate.store 'circle_v01', @attr
+ }.should change SpeechBalloonTemplate, :count
+ end
+ it 'オブジェクトを返している' do
+ r = SpeechBalloonTemplate.store 'circle_v01', @attr
+ r.is_a?(SpeechBalloonTemplate).should be_true
+ end
+ end
+ end
+
end