1 # -*- encoding: utf-8 -*-
4 describe SpeechBalloonTemplate do
7 @f = Rails.root + 'spec/json/speech_balloon_template.json'
8 @t = File.open(@f, 'r').read
10 @fs = Rails.root + 'spec/json/speech_balloon_templates.json'
11 @ts = File.open(@fs, 'r').read
13 @fes = Rails.root + 'spec/json/invalid_speech_balloon_templates.json'
14 @tes = File.open(@fes, 'r').read
15 @jes = JSON.parse @tes
17 @admin = FactoryGirl.create :admin
18 @user = FactoryGirl.create( :user_yas)
19 @author = FactoryGirl.create :author, :user_id => @user.id
20 @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
21 @other_user = FactoryGirl.create( :user_yas)
22 @other_author = FactoryGirl.create :author, :user_id => @other_user.id
23 @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
24 @sp = FactoryGirl.create :system_picture
29 @st = FactoryGirl.build :speech_balloon_template
32 context 'オーソドックスなデータのとき' do
43 @st.classname = 'a'*50
46 @st.settings = '{' + 'a'*99999 + '}'
52 context 'nameを検証するとき' do
55 @st.should_not be_valid
59 @st.should_not be_valid
62 context 'classnameを検証するとき' do
65 @st.should_not be_valid
68 @st.classname = 'a'*51
69 @st.should_not be_valid
72 context 'captionを検証するとき' do
75 @st.should_not be_valid
79 @st.should_not be_valid
85 @st.should_not be_valid
89 @st.should_not be_valid
93 @st.should_not be_valid
96 context 'system_picture_idを検証するとき' do
98 @st.system_picture_id = nil
99 @st.should_not be_valid
102 @st.system_picture_id = 'a'
103 @st.should_not be_valid
105 it '存在するシステム画像でなければ失敗する' do
106 @st.system_picture_id = 0
107 @st.should_not be_valid
110 context 'settingsを検証するとき' do
113 @st.should_not be_valid
118 describe '文字コード検証に於いて' do
120 @st = FactoryGirl.build :speech_balloon_template
123 context 'nameを検証するとき' do
124 it 'Shift JISなら失敗する' do
125 @st.name = "\x83G\x83r\x83]\x83D"
128 }.should raise_error(Pettanr::BadRequest)
132 context 'classnameを検証するとき' do
133 it 'Shift JISなら失敗する' do
134 @st.classname = "\x83G\x83r\x83]\x83D"
137 }.should raise_error(Pettanr::BadRequest)
141 context 'captionを検証するとき' do
142 it 'Shift JISなら失敗する' do
143 @st.caption = "\x83G\x83r\x83]\x83D"
146 }.should raise_error(Pettanr::BadRequest)
150 context 'settingsを検証するとき' do
151 it 'Shift JISなら失敗する' do
152 @st.settings = "\x83G\x83r\x83]\x83D"
155 }.should raise_error(Pettanr::BadRequest)
160 describe 'デフォルト値補充に於いて' do
162 @st = FactoryGirl.build :speech_balloon_template
167 describe '上書き補充に於いて' do
169 @st = FactoryGirl.build :speech_balloon_template
171 it 'テーブルが空のときは0を返す' do
175 it 'テーブルが1行あるときは1を返す' do
176 FactoryGirl.create :speech_balloon_template
180 it 'テーブルが2行あるときは2を返す' do
181 FactoryGirl.create :speech_balloon_template
182 FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
186 context 'つつがなく終わるとき' do
188 @st = FactoryGirl.create :speech_balloon_template, :t => 0
197 describe '閲覧許可に於いて' do
199 @st = FactoryGirl.build :speech_balloon_template
202 @st.visible?(@author).should == true
206 describe '一覧取得に於いて' do
208 @st = FactoryGirl.create :speech_balloon_template
210 context 'つつがなく終わるとき' do
211 it '一覧取得オプションを利用している' do
212 SpeechBalloonTemplate.stub(:list_opt).with(any_args).and_return({})
213 SpeechBalloonTemplate.should_receive(:list_opt).with(any_args).exactly(1)
214 r = SpeechBalloonTemplate.list
218 l = SpeechBalloonTemplate.list
222 @st2 = FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
223 l = SpeechBalloonTemplate.list
224 l.should eq [@st, @st2]
227 describe '一覧取得オプションに於いて' do
229 r = SpeechBalloonTemplate.list_opt
230 r.is_a?(Hash).should be_true
234 describe 'json一覧出力オプションに於いて' do
236 r = SpeechBalloonTemplate.list_json_opt
237 r.is_a?(Hash).should be_true
242 describe '単体取得に於いて' do
244 @st = FactoryGirl.create :speech_balloon_template
246 context 'つつがなく終わるとき' do
247 it '単体取得オプションを利用している' do
248 SpeechBalloonTemplate.stub(:show_opt).with(any_args).and_return({})
249 SpeechBalloonTemplate.should_receive(:show_opt).with(any_args).exactly(1)
250 r = SpeechBalloonTemplate.show @st.id, @author
253 it '指定のフキダシテンプレートを返す' do
254 l = SpeechBalloonTemplate.show @st.id, @author
257 context '他人のフキダシテンプレートを開こうとしたとき' do
258 it '403Forbidden例外を返す' do
259 SpeechBalloonTemplate.any_instance.stub(:visible?).and_return(false)
261 r = SpeechBalloonTemplate.show @st.id, @other_author
262 }.should raise_error(ActiveRecord::Forbidden)
265 context '存在しないフキダシテンプレートを開こうとしたとき' do
266 it '404RecordNotFound例外を返す' do
268 SpeechBalloonTemplate.show 110, @author
269 }.should raise_error(ActiveRecord::RecordNotFound)
273 describe '単体出力オプションに於いて' do
275 r = SpeechBalloonTemplate.show_opt
276 r.is_a?(Hash).should be_true
280 describe 'json単体出力オプションに於いて' do
282 r = SpeechBalloonTemplate.show_json_opt
283 r.is_a?(Hash).should be_true
289 #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
290 #特にシステム画像データはidを直接記述して置換させないようにテストしている。
292 @sp = FactoryGirl.create :system_picture
293 @st = FactoryGirl.create :speech_balloon_template
295 "speech_balloon" => {"default" => 1},
296 "one" => {"balloon" => {"system_picture_id" => @sp.id}}
298 @attr = {"classname" => 'store', "caption" => 'store test', "system_picture_id" => @sp.id,
299 "settings" => @settings
304 SpeechBalloonTemplate.stub(:replace_system_picture).with(@attr).and_return(@attr)
305 SpeechBalloonTemplate.stub(:modify_object).with(any_args).and_return(@st)
306 SpeechBalloonTemplate.any_instance.stub(:overwrite).with(any_args).and_return(true)
307 SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(true)
309 it 'システム画像置換に依頼している' do
310 SpeechBalloonTemplate.should_receive(:replace_system_picture).with(@attr).exactly(1)
311 r = SpeechBalloonTemplate.store 'circle_v01', @attr
313 it 'インポート処理のデータ更新準備に依頼している' do
314 SpeechBalloonTemplate.should_receive(:modify_object).exactly(1)
315 r = SpeechBalloonTemplate.store 'circle_v01', @attr
318 SpeechBalloonTemplate.any_instance.should_receive(:overwrite).exactly(1)
319 r = SpeechBalloonTemplate.store 'circle_v01', @attr
321 it 'オブジェクトを保存している' do
322 SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
323 r = SpeechBalloonTemplate.store 'circle_v01', @attr
326 context 'つつがなく終わるとき' do
329 it 'カラム値のsettingsをセットしている' do
330 r = SpeechBalloonTemplate.store 'circle_v01', @attr
331 r.settings.should eq @settings.to_json.to_s
335 r = SpeechBalloonTemplate.store 'circle_v01', @attr
336 }.should change SpeechBalloonTemplate, :count
339 r = SpeechBalloonTemplate.store 'circle_v01', @attr
340 r.is_a?(SpeechBalloonTemplate).should be_true