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
24 it 'オーソドックスなデータなら通る' do
25 @st = FactoryGirl.build :speech_balloon_template
29 context 'nameを検証するとき' do
31 @st = FactoryGirl.build :speech_balloon_template
34 @st.name = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
39 @st.should_not be_valid
43 @st.should_not be_valid
46 context 'classnameを検証するとき' do
48 @st = FactoryGirl.build :speech_balloon_template
51 @st.classname = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
56 @st.should_not be_valid
59 @st.classname = 'a'*51
60 @st.should_not be_valid
63 context 'captionを検証するとき' do
65 @st = FactoryGirl.build :speech_balloon_template
68 @st.caption = 'abcdefghi0abcdefghi0abcdefghi0'
73 @st.should_not be_valid
77 @st.should_not be_valid
82 @st = FactoryGirl.build :speech_balloon_template
90 @st.should_not be_valid
94 @st.should_not be_valid
98 @st.should_not be_valid
101 context 'settingsを検証するとき' do
103 @st = FactoryGirl.build :speech_balloon_template
111 @st.should_not be_valid
116 describe 'データ補充に於いて' do
122 describe 'Json解析に於いて' do
125 context 'テキストを渡されたとき' do
127 JSON.should_receive(:parse).with(@t).exactly(1)
128 r = SpeechBalloonTemplate.parse @t
131 r = SpeechBalloonTemplate.parse @t
132 r.is_a?(Hash).should be_true
135 context 'パース失敗したとき' do
137 JSON.should_receive(:parse).with(any_args).and_raise('StandardError')
138 r = SpeechBalloonTemplate.parse @t
144 describe 'システム画像置換に於いて' do
146 @sp = FactoryGirl.create :system_picture
147 @sp2 = FactoryGirl.create :system_picture
148 @st = FactoryGirl.build :speech_balloon_template
149 @hash = {"classname" => 'sore', "caption" => 'store test',
151 "one" => {"balloon" => {"system_picture" => 'BASE64'}}
154 @hashs = {"classname" => 'sore2', "caption" => 'store test2',
156 "one" => {"balloon" => {"system_picture" => 'DUAL'}},
157 "two" => {"balloon" => {"system_picture" => 'BASE64ENC'}}
162 it 'システム画像に保存を依頼している' do
163 SystemPicture.stub(:store).with(any_args).and_return(@sp)
164 SystemPicture.should_receive(:store).exactly(1)
165 r = @st.pic_rehash @hash
168 context 'つつがなく終わるとき' do
170 SystemPicture.stub(:store).with(any_args).and_return(@sp)
172 it 'システム画像のidを作成している' do
173 r = @st.pic_rehash @hash
174 @hash["templates"]["one"]["balloon"].has_key?("system_picture_id").should be_true
175 @hash["templates"]["one"]["balloon"]["system_picture_id"].should eq @sp.id
177 it 'システム画像のテキストデータを削除している' do
178 r = @st.pic_rehash @hash
179 @hash["templates"]["one"]["balloon"].has_key?("system_picture").should_not be_true
182 r = @st.pic_rehash @hash
186 context 'システム画像の作成に失敗したとき' do
188 SystemPicture.stub(:store).with(any_args).and_return(false)
191 r = @st.pic_rehash @hash
197 SystemPicture.stub(:store).with(any_args).and_return(@sp)
199 it 'システム画像のidを作成している' do
200 r = @st.pic_rehash @hashs
201 @hashs["templates"]["one"]["balloon"].has_key?("system_picture_id").should be_true
202 @hashs["templates"]["one"]["balloon"]["system_picture_id"].should eq @sp.id
203 @hashs["templates"]["two"]["balloon"].has_key?("system_picture_id").should be_true
204 @hashs["templates"]["two"]["balloon"]["system_picture_id"].should eq @sp.id
206 it 'システム画像のテキストデータを削除している' do
207 r = @st.pic_rehash @hashs
208 @hashs["templates"]["one"]["balloon"].has_key?("system_picture").should_not be_true
209 @hashs["templates"]["two"]["balloon"].has_key?("system_picture").should_not be_true
212 context '例外データのとき' do
213 it 'templatesがなくても何ごともなく続行する' do
214 r = @st.pic_rehash({})
217 it 'templatesが空でも何ごともなく続行する' do
218 r = @st.pic_rehash( {"templates" => nil})
221 it 'balloonがなくても何ごともなく続行する' do
222 r = @st.pic_rehash({"templates" => {
227 it 'balloonが空でも何ごともなく続行する' do
228 r = @st.pic_rehash({"templates" => {
229 "one" => {"balloon" => nil}
233 it 'system_pictureがなくても何ごともなく続行する' do
234 r = @st.pic_rehash({"templates" => {
235 "one" => {"balloon" => {}}
242 describe 'フキダシテンプレート保存に於いて' do
244 @st = FactoryGirl.build :speech_balloon_template
245 @hash = {"classname" => 'sore', "caption" => 'store test'}
248 it 'システム画像置換を依頼している' do
249 SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(true)
250 SpeechBalloonTemplate.any_instance.should_receive(:pic_rehash).exactly(1)
254 context 'つつがなく終わるとき' do
256 SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(true)
258 it 'hashから取り出したclassnameをセットしている' do
260 @st.classname.should eq @hash["classname"]
262 it 'hashから取り出したcaptionをセットしている' do
264 @st.caption.should eq @hash["caption"]
266 it 'hashをテキスト化してsettingsにセットしている' do
268 @st.settings.should eq @hash.to_s
270 it 'フキダシテンプレートが作成されている' do
273 }.should change SpeechBalloonTemplate, :count
276 context 'システム画像置換が失敗したとき' do
278 SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(false)
285 context '保存が失敗したとき' do
287 SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(true)
288 SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(false)
297 describe 'フキダシテンプレート更新に於いて' do
299 @st = FactoryGirl.create :speech_balloon_template
302 context 'つつがなく終わるとき' do
304 SpeechBalloonTemplate.any_instance.stub(:store).with(@hash).and_return(true)
305 SpeechBalloonTemplate.should_receive(:store).exactly(1)
306 SpeechBalloonTemplate.store @st.name, @hash
309 #新規作成をチェックしたいが、楽するために(storeが保存しないことを利用して)新規オブジェクト生成かでテスト
310 it 'フキダシテンプレートは新規オブジェクトである' do
311 SpeechBalloonTemplate.any_instance.stub(:store).with(@hash).and_return(true)
312 r = SpeechBalloonTemplate.store :newsbtname, @hash
313 r.should be_a_new SpeechBalloonTemplate
317 it '名前が一致するテンプレを返す' do
318 SpeechBalloonTemplate.any_instance.stub(:store).with(@hash).and_return(true)
319 r = SpeechBalloonTemplate.store @st.name, @hash
320 r.name.should eq @st.name
321 r.should_not be_a_new SpeechBalloonTemplate
325 context 'フキダシテンプレートの作成に失敗するとき' do
327 SpeechBalloonTemplate.any_instance.stub(:store).with(any_args).and_return(false)
329 #成功失敗に関わらずオブジェクトを返すのでテストのやりようがない
334 describe 'テキスト取り込みに於いて' do
335 #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
338 context 'つつがなく終わるとき' do
340 SpeechBalloonTemplate.should_receive(:parse).with(any_args).exactly(1)
341 SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(@j)
342 SpeechBalloonTemplate.import(@t)
344 it 'フキダシテンプレート更新を一回依頼する' do
345 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
346 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(true)
347 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(1)
348 SpeechBalloonTemplate.import(@t)
350 it 'フキダシテンプレートが追加される' do
352 SpeechBalloonTemplate.import(@t)
353 }.should change SpeechBalloonTemplate, :count
356 SpeechBalloonTemplate.import(@t).should eq []
359 context '複数データがつつがなく終わるとき' do
360 it 'フキダシテンプレート更新を二回依頼する' do
361 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
362 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(true)
363 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(2)
364 SpeechBalloonTemplate.import(@ts)
366 it 'フキダシテンプレートが二個追加される' do
368 SpeechBalloonTemplate.import(@ts)
369 }.should change(SpeechBalloonTemplate, :count).by 2
372 SpeechBalloonTemplate.import(@ts).should eq []
376 context 'Json解析に失敗したとき' do
378 SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(false)
380 it 'フキダシテンプレートの数に変化がない' do
382 SpeechBalloonTemplate.import(@t)
383 }.should_not change SpeechBalloonTemplate, :count
386 SpeechBalloonTemplate.import(@t).should be_false
389 context 'フキダシテンプレート作成に失敗したとき' do
391 SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(false)
392 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(false)
394 it 'フキダシテンプレートの数に変化がない' do
396 SpeechBalloonTemplate.import(@t)
397 }.should_not change SpeechBalloonTemplate, :count
400 r = SpeechBalloonTemplate.import(@t)
401 r.is_a?(Array).should be_true
404 r = SpeechBalloonTemplate.import(@t)
405 r.should have(1).items
407 it 'フキダシテンプレートオブジェクトが入っている' do
408 r = SpeechBalloonTemplate.import(@t)
409 r.first.is_a?(SpeechBalloonTemplate).should be_true
412 context '複数のフキダシテンプレート作成に失敗したとき' do
413 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
414 it 'フキダシテンプレートの数に変化がない' do
416 SpeechBalloonTemplate.import(@tes)
417 }.should_not change SpeechBalloonTemplate, :count
419 it '途中で保存に失敗しても全件更新依頼する' do
420 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
421 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(3)
422 SpeechBalloonTemplate.import(@tes)
425 r = SpeechBalloonTemplate.import(@tes)
426 r.is_a?(Array).should be_true
429 r = SpeechBalloonTemplate.import(@tes)
430 r.should have(2).items
432 it '配列の中身は失敗したフキダシテンプレートオブジェクトが入っている' do
433 r = SpeechBalloonTemplate.import(@tes)
434 r[0].is_a?(SpeechBalloonTemplate).should be_true
435 r[0]["name"].should eq 'squareR@pettan.com'
436 r[1].is_a?(SpeechBalloonTemplate).should be_true
437 r[1]["name"].should eq 'squareRR@pettan.com'
442 describe 'インポートエラーの表示に於いて' do
445 describe 'ファイル取り込みに於いて' do
447 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
449 context 'つつがなく終わるとき' do
451 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
453 it 'ファイルを開いてテキストを読む' do
454 File.should_receive(:open).with(@f, 'r').exactly(1)
455 SpeechBalloonTemplate.import_file(@f)
457 it 'テキスト取り込みを依頼する' do
458 SpeechBalloonTemplate.should_receive(:import).with(any_args).exactly(1)
459 SpeechBalloonTemplate.import_file(@f)
463 SpeechBalloonTemplate.import_file(@f).should be_true
466 context 'ファイルが開けないとき' do
468 File.stub(:open).with(any_args).and_raise('StandardError')
470 it 'ファイルエラーのメッセージを出力する' do
474 SpeechBalloonTemplate.import_file(@f).should be_false
477 #失敗したときは、失敗したフキダシテンプレートが配列で返る
478 context 'テキスト取り込みが失敗したとき' do
480 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(false)
482 it '各フキダシテンプレートのエラーメッセージを出力する' do
486 SpeechBalloonTemplate.import_file(@f).should be_false