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 = Factory :admin
18 @license = Factory :license
25 it 'オーソドックスなデータなら通る' do
26 @st = Factory.build :speech_balloon_template
30 context 'nameを検証するとき' do
32 @st = Factory.build :speech_balloon_template
35 @st.name = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
40 @st.should_not be_valid
44 @st.should_not be_valid
47 context 'classnameを検証するとき' do
49 @st = Factory.build :speech_balloon_template
52 @st.classname = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
57 @st.should_not be_valid
60 @st.classname = 'a'*51
61 @st.should_not be_valid
64 context 'captionを検証するとき' do
66 @st = Factory.build :speech_balloon_template
69 @st.caption = 'abcdefghi0abcdefghi0abcdefghi0'
74 @st.should_not be_valid
78 @st.should_not be_valid
83 @st = Factory.build :speech_balloon_template
91 @st.should_not be_valid
95 @st.should_not be_valid
99 @st.should_not be_valid
102 context 'settingsを検証するとき' do
104 @st = Factory.build :speech_balloon_template
112 @st.should_not be_valid
117 describe 'データ補充に於いて' do
123 describe 'Json解析に於いて' do
126 context 'テキストを渡されたとき' do
128 JSON.should_receive(:parse).with(@t).exactly(1)
129 r = SpeechBalloonTemplate.parse @t
132 r = SpeechBalloonTemplate.parse @t
133 r.is_a?(Hash).should be_true
136 context 'パース失敗したとき' do
138 JSON.should_receive(:parse).with(any_args).and_raise('StandardError')
139 r = SpeechBalloonTemplate.parse @t
145 describe 'システム画像置換に於いて' do
147 @sp = Factory :system_picture
148 @sp2 = Factory :system_picture
149 @st = Factory.build :speech_balloon_template
150 @hash = {"classname" => 'sore', "caption" => 'store test',
152 "one" => {"balloon" => {"system_picture" => 'BASE64'}}
155 @hashs = {"classname" => 'sore2', "caption" => 'store test2',
157 "one" => {"balloon" => {"system_picture" => 'DUAL'}},
158 "two" => {"balloon" => {"system_picture" => 'BASE64ENC'}}
163 it 'システム画像に保存を依頼している' do
164 SystemPicture.stub(:store).with(any_args).and_return(@sp)
165 SystemPicture.should_receive(:store).exactly(1)
166 r = @st.pic_rehash @hash
169 context 'つつがなく終わるとき' do
171 SystemPicture.stub(:store).with(any_args).and_return(@sp)
173 it 'システム画像のidを作成している' do
174 r = @st.pic_rehash @hash
175 @hash["templates"]["one"]["balloon"].has_key?("system_picture_id").should be_true
176 @hash["templates"]["one"]["balloon"]["system_picture_id"].should eq @sp.id
178 it 'システム画像のテキストデータを削除している' do
179 r = @st.pic_rehash @hash
180 @hash["templates"]["one"]["balloon"].has_key?("system_picture").should_not be_true
183 r = @st.pic_rehash @hash
187 context 'システム画像の作成に失敗したとき' do
189 SystemPicture.stub(:store).with(any_args).and_return(false)
192 r = @st.pic_rehash @hash
198 SystemPicture.stub(:store).with(any_args).and_return(@sp)
200 it 'システム画像のidを作成している' do
201 r = @st.pic_rehash @hashs
202 @hashs["templates"]["one"]["balloon"].has_key?("system_picture_id").should be_true
203 @hashs["templates"]["one"]["balloon"]["system_picture_id"].should eq @sp.id
204 @hashs["templates"]["two"]["balloon"].has_key?("system_picture_id").should be_true
205 @hashs["templates"]["two"]["balloon"]["system_picture_id"].should eq @sp.id
207 it 'システム画像のテキストデータを削除している' do
208 r = @st.pic_rehash @hashs
209 @hashs["templates"]["one"]["balloon"].has_key?("system_picture").should_not be_true
210 @hashs["templates"]["two"]["balloon"].has_key?("system_picture").should_not be_true
213 context '例外データのとき' do
214 it 'templatesがなくても何ごともなく続行する' do
215 r = @st.pic_rehash({})
218 it 'templatesが空でも何ごともなく続行する' do
219 r = @st.pic_rehash( {"templates" => nil})
222 it 'balloonがなくても何ごともなく続行する' do
223 r = @st.pic_rehash({"templates" => {
228 it 'balloonが空でも何ごともなく続行する' do
229 r = @st.pic_rehash({"templates" => {
230 "one" => {"balloon" => nil}
234 it 'system_pictureがなくても何ごともなく続行する' do
235 r = @st.pic_rehash({"templates" => {
236 "one" => {"balloon" => {}}
243 describe 'フキダシテンプレート保存に於いて' do
245 @st = Factory.build :speech_balloon_template
246 @hash = {"classname" => 'sore', "caption" => 'store test'}
249 it 'システム画像置換を依頼している' do
250 SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(true)
251 SpeechBalloonTemplate.any_instance.should_receive(:pic_rehash).exactly(1)
255 context 'つつがなく終わるとき' do
257 SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(true)
259 it 'hashから取り出したclassnameをセットしている' do
261 @st.classname.should eq @hash["classname"]
263 it 'hashから取り出したcaptionをセットしている' do
265 @st.caption.should eq @hash["caption"]
267 it 'hashをテキスト化してsettingsにセットしている' do
269 @st.settings.should eq @hash.to_s
271 it 'フキダシテンプレートが作成されている' do
274 }.should change SpeechBalloonTemplate, :count
277 context 'システム画像置換が失敗したとき' do
279 SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(false)
286 context '保存が失敗したとき' do
288 SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(true)
289 SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(false)
298 describe 'フキダシテンプレート更新に於いて' do
300 @st = Factory :speech_balloon_template
303 context 'つつがなく終わるとき' do
305 SpeechBalloonTemplate.any_instance.stub(:store).with(@hash).and_return(true)
306 SpeechBalloonTemplate.should_receive(:store).exactly(1)
307 SpeechBalloonTemplate.store @st.name, @hash
310 #新規作成をチェックしたいが、楽するために(storeが保存しないことを利用して)新規オブジェクト生成かでテスト
311 it 'フキダシテンプレートは新規オブジェクトである' do
312 SpeechBalloonTemplate.any_instance.stub(:store).with(@hash).and_return(true)
313 r = SpeechBalloonTemplate.store :newsbtname, @hash
314 r.should be_a_new SpeechBalloonTemplate
318 it '名前が一致するテンプレを返す' do
319 SpeechBalloonTemplate.any_instance.stub(:store).with(@hash).and_return(true)
320 r = SpeechBalloonTemplate.store @st.name, @hash
321 r.name.should eq @st.name
322 r.should_not be_a_new SpeechBalloonTemplate
326 context 'フキダシテンプレートの作成に失敗するとき' do
328 SpeechBalloonTemplate.any_instance.stub(:store).with(any_args).and_return(false)
330 #成功失敗に関わらずオブジェクトを返すのでテストのやりようがない
335 describe 'テキスト取り込みに於いて' do
336 #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
339 context 'つつがなく終わるとき' do
341 SpeechBalloonTemplate.should_receive(:parse).with(any_args).exactly(1)
342 SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(@j)
343 SpeechBalloonTemplate.import(@t)
345 it 'フキダシテンプレート更新を一回依頼する' do
346 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
347 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(true)
348 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(1)
349 SpeechBalloonTemplate.import(@t)
351 it 'フキダシテンプレートが追加される' do
353 SpeechBalloonTemplate.import(@t)
354 }.should change SpeechBalloonTemplate, :count
357 SpeechBalloonTemplate.import(@t).should eq []
360 context '複数データがつつがなく終わるとき' do
361 it 'フキダシテンプレート更新を二回依頼する' do
362 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
363 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(true)
364 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(2)
365 SpeechBalloonTemplate.import(@ts)
367 it 'フキダシテンプレートが二個追加される' do
369 SpeechBalloonTemplate.import(@ts)
370 }.should change(SpeechBalloonTemplate, :count).by 2
373 SpeechBalloonTemplate.import(@ts).should eq []
377 context 'Json解析に失敗したとき' do
379 SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(false)
381 it 'フキダシテンプレートの数に変化がない' do
383 SpeechBalloonTemplate.import(@t)
384 }.should_not change SpeechBalloonTemplate, :count
387 SpeechBalloonTemplate.import(@t).should be_false
390 context 'フキダシテンプレート作成に失敗したとき' do
392 SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(false)
393 SpeechBalloonTemplate.any_instance.stub(:valid?).with(any_args).and_return(false)
395 it 'フキダシテンプレートの数に変化がない' do
397 SpeechBalloonTemplate.import(@t)
398 }.should_not change SpeechBalloonTemplate, :count
401 r = SpeechBalloonTemplate.import(@t)
402 r.is_a?(Array).should be_true
405 r = SpeechBalloonTemplate.import(@t)
406 r.should have(1).items
408 it 'フキダシテンプレートオブジェクトが入っている' do
409 r = SpeechBalloonTemplate.import(@t)
410 r.first.is_a?(SpeechBalloonTemplate).should be_true
413 context '複数のフキダシテンプレート作成に失敗したとき' do
414 #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
415 it 'フキダシテンプレートの数に変化がない' do
417 SpeechBalloonTemplate.import(@tes)
418 }.should_not change SpeechBalloonTemplate, :count
420 it '途中で保存に失敗しても全件更新依頼する' do
421 SpeechBalloonTemplate.stub(:store).with(any_args).and_return(SpeechBalloonTemplate.new)
422 SpeechBalloonTemplate.should_receive(:store).with(any_args).exactly(3)
423 SpeechBalloonTemplate.import(@tes)
426 r = SpeechBalloonTemplate.import(@tes)
427 r.is_a?(Array).should be_true
430 r = SpeechBalloonTemplate.import(@tes)
431 r.should have(2).items
433 it '配列の中身は失敗したフキダシテンプレートオブジェクトが入っている' do
434 r = SpeechBalloonTemplate.import(@tes)
435 r[0].is_a?(SpeechBalloonTemplate).should be_true
436 r[0]["name"].should eq 'squareR@pettan.com'
437 r[1].is_a?(SpeechBalloonTemplate).should be_true
438 r[1]["name"].should eq 'squareRR@pettan.com'
443 describe 'インポートエラーの表示に於いて' do
446 describe 'ファイル取り込みに於いて' do
448 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
450 context 'つつがなく終わるとき' do
452 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
454 it 'ファイルを開いてテキストを読む' do
455 File.should_receive(:open).with(@f, 'r').exactly(1)
456 SpeechBalloonTemplate.import_file(@f)
458 it 'テキスト取り込みを依頼する' do
459 SpeechBalloonTemplate.should_receive(:import).with(any_args).exactly(1)
460 SpeechBalloonTemplate.import_file(@f)
464 SpeechBalloonTemplate.import_file(@f).should be_true
467 context 'ファイルが開けないとき' do
469 File.stub(:open).with(any_args).and_raise('StandardError')
471 it 'ファイルエラーのメッセージを出力する' do
475 SpeechBalloonTemplate.import_file(@f).should be_false
478 #失敗したときは、失敗したフキダシテンプレートが配列で返る
479 context 'テキスト取り込みが失敗したとき' do
481 SpeechBalloonTemplate.stub(:import).with(any_args).and_return(false)
483 it '各フキダシテンプレートのエラーメッセージを出力する' do
487 SpeechBalloonTemplate.import_file(@f).should be_false