OSDN Git Service

pass test
[pettanr/pettanr.git] / spec / models / speech_balloon_template_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #フキダシテンプレート
4 describe SpeechBalloonTemplate do
5   before do
6     #テストデータを用意してね
7     @f = Rails.root + 'spec/json/speech_balloon_template.json'
8     @t = File.open(@f, 'r').read
9     @j = JSON.parse @t
10     @fs = Rails.root + 'spec/json/speech_balloon_templates.json'
11     @ts = File.open(@fs, 'r').read
12     @js = JSON.parse @ts
13     @fes = Rails.root + 'spec/json/invalid_speech_balloon_templates.json'
14     @tes = File.open(@fes, 'r').read
15     @jes = JSON.parse @tes
16     
17     @admin = Factory :admin
18     @license = Factory :license
19   end
20   
21   describe '検証に於いて' do
22     before do
23     end
24     
25     it 'オーソドックスなデータなら通る' do
26       @st = Factory.build :speech_balloon_template
27       @st.should be_valid
28     end
29     
30     context 'nameを検証するとき' do
31       before do
32         @st = Factory.build :speech_balloon_template
33       end
34       it 'テストデータの確認' do
35         @st.name = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
36         @st.should be_valid
37       end
38       it 'nullなら失敗する' do
39         @st.name = ''
40         @st.should_not be_valid
41       end
42       it '51文字以上なら失敗する' do
43         @st.name = 'a'*51
44         @st.should_not be_valid
45       end
46     end
47     context 'classnameを検証するとき' do
48       before do
49         @st = Factory.build :speech_balloon_template
50       end
51       it 'テストデータの確認' do
52         @st.classname = 'abcdefghi0abcdefghi0abcdefghi0abcdefghi0abcdefghi0'
53         @st.should be_valid
54       end
55       it 'nullなら失敗する' do
56         @st.classname = ''
57         @st.should_not be_valid
58       end
59       it '51文字以上なら失敗する' do
60         @st.classname = 'a'*51
61         @st.should_not be_valid
62       end
63     end
64     context 'captionを検証するとき' do
65       before do
66         @st = Factory.build :speech_balloon_template
67       end
68       it 'テストデータの確認' do
69         @st.caption = 'abcdefghi0abcdefghi0abcdefghi0'
70         @st.should be_valid
71       end
72       it 'nullなら失敗する' do
73         @st.caption = ''
74         @st.should_not be_valid
75       end
76       it '31文字以上なら失敗する' do
77         @st.caption = 'a'*31
78         @st.should_not be_valid
79       end
80     end
81     context 'tを検証するとき' do
82       before do
83         @st = Factory.build :speech_balloon_template
84       end
85       it 'テストデータの確認' do
86         @st.t = 0
87         @st.should be_valid
88       end
89       it 'nullなら失敗する' do
90         @st.t = nil
91         @st.should_not be_valid
92       end
93       it '数値でなければ失敗する' do
94         @st.t = 'a'
95         @st.should_not be_valid
96       end
97       it '負なら失敗する' do
98         @st.t = -1
99         @st.should_not be_valid
100       end
101     end
102     context 'settingsを検証するとき' do
103       before do
104         @st = Factory.build :speech_balloon_template
105       end
106       it 'テストデータの確認' do
107         @st.settings = '{}'
108         @st.should be_valid
109       end
110       it 'nullなら失敗する' do
111         @st.settings = nil
112         @st.should_not be_valid
113       end
114     end
115   end
116   
117   describe 'データ補充に於いて' do
118     before do
119     end
120     
121   end
122   
123   describe 'Json解析に於いて' do
124     before do
125     end
126     context 'テキストを渡されたとき' do
127       it 'Json解析している' do
128         JSON.should_receive(:parse).with(@t).exactly(1)
129         r = SpeechBalloonTemplate.parse @t
130       end
131       it 'Hashで返す' do
132         r = SpeechBalloonTemplate.parse @t
133         r.is_a?(Hash).should be_true
134       end
135     end
136     context 'パース失敗したとき' do
137       it 'Falseを返す' do
138         JSON.should_receive(:parse).with(any_args).and_raise('StandardError')
139         r = SpeechBalloonTemplate.parse @t
140         r.should be_false
141       end
142     end
143   end
144   
145   describe 'システム画像置換に於いて' do
146     before 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',
151         "templates" => {
152           "one" => {"balloon" => {"system_picture" => 'BASE64'}}
153         }
154       }
155       @hashs = {"classname" => 'sore2', "caption" => 'store test2',
156         "templates" => {
157           "one" => {"balloon" => {"system_picture" => 'DUAL'}},
158           "two" => {"balloon" => {"system_picture" => 'BASE64ENC'}}
159         }
160       }
161     end
162     context '事前チェック' do
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
167       end
168     end
169     context 'つつがなく終わるとき' do
170       before do
171         SystemPicture.stub(:store).with(any_args).and_return(@sp)
172       end
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
177       end
178       it 'システム画像のテキストデータを削除している' do
179         r = @st.pic_rehash @hash
180         @hash["templates"]["one"]["balloon"].has_key?("system_picture").should_not be_true
181       end
182       it 'trueを返す' do
183         r = @st.pic_rehash @hash
184         r.should be_true
185       end
186     end
187     context 'システム画像の作成に失敗したとき' do
188       before do
189         SystemPicture.stub(:store).with(any_args).and_return(false)
190       end
191       it 'Falseを返す' do
192         r = @st.pic_rehash @hash
193         r.should be_false
194       end
195     end
196     context '複数のとき' do
197       before do
198         SystemPicture.stub(:store).with(any_args).and_return(@sp)
199       end
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
206       end
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
211       end
212     end
213     context '例外データのとき' do
214       it 'templatesがなくても何ごともなく続行する' do
215         r = @st.pic_rehash({})
216         r.should be_true
217       end
218       it 'templatesが空でも何ごともなく続行する' do
219         r = @st.pic_rehash( {"templates" => nil})
220         r.should be_true
221       end
222       it 'balloonがなくても何ごともなく続行する' do
223         r = @st.pic_rehash({"templates" => {
224           "one" => {}
225         }})
226         r.should be_true
227       end
228       it 'balloonが空でも何ごともなく続行する' do
229         r = @st.pic_rehash({"templates" => {
230           "one" => {"balloon" => nil}
231         }})
232         r.should be_true
233       end
234       it 'system_pictureがなくても何ごともなく続行する' do
235         r = @st.pic_rehash({"templates" => {
236           "one" => {"balloon" => {}}
237         }})
238         r.should be_true
239       end
240     end
241   end
242   
243   describe 'フキダシテンプレート保存に於いて' do
244     before do
245       @st = Factory.build :speech_balloon_template
246       @hash = {"classname" => 'sore', "caption" => 'store test'}
247     end
248     context '事前チェック' do
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)
252         r = @st.store @hash
253       end
254     end
255     context 'つつがなく終わるとき' do
256       before do
257         SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(true)
258       end
259       it 'hashから取り出したclassnameをセットしている' do
260         @st.store @hash
261         @st.classname.should eq @hash["classname"]
262       end
263       it 'hashから取り出したcaptionをセットしている' do
264         @st.store @hash
265         @st.caption.should eq @hash["caption"]
266       end
267       it 'hashをテキスト化してsettingsにセットしている' do
268         @st.store @hash
269         @st.settings.should eq @hash.to_s
270       end
271       it 'フキダシテンプレートが作成されている' do
272         lambda {
273           @st.store @hash
274         }.should change SpeechBalloonTemplate, :count
275       end
276     end
277     context 'システム画像置換が失敗したとき' do
278       before do
279         SpeechBalloonTemplate.any_instance.stub(:pic_rehash).with(@hash).and_return(false)
280       end
281       it 'Falseを返す' do
282         r = @st.store @hash
283         r.should be_false
284       end
285     end
286     context '保存が失敗したとき' do
287       before 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)
290       end
291       it 'Falseを返す' do
292         r = @st.store @hash
293         r.should be_false
294       end
295     end
296   end
297   
298   describe 'フキダシテンプレート更新に於いて' do
299     before do
300       @st = Factory :speech_balloon_template
301       @hash = {}
302     end
303     context 'つつがなく終わるとき' do
304       it '保存依頼している' 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
308       end
309       context '新規のとき' do
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
315         end
316       end
317       context '更新のとき' do
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
323         end
324       end
325     end
326     context 'フキダシテンプレートの作成に失敗するとき' do
327       before do
328         SpeechBalloonTemplate.any_instance.stub(:store).with(any_args).and_return(false)
329       end
330       #成功失敗に関わらずオブジェクトを返すのでテストのやりようがない
331       
332     end
333   end
334   
335   describe 'テキスト取り込みに於いて' do
336     #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
337     before do
338     end
339     context 'つつがなく終わるとき' do
340       it 'Json解析を依頼する' 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)
344       end
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)
350       end
351       it 'フキダシテンプレートが追加される' do
352         lambda {
353           SpeechBalloonTemplate.import(@t)
354         }.should change SpeechBalloonTemplate, :count
355       end
356       it '[]を返す' do
357         SpeechBalloonTemplate.import(@t).should eq []
358       end
359     end
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)
366       end
367       it 'フキダシテンプレートが二個追加される' do
368         lambda {
369           SpeechBalloonTemplate.import(@ts)
370         }.should change(SpeechBalloonTemplate, :count).by 2
371       end
372       it '[]を返す' do
373         SpeechBalloonTemplate.import(@ts).should eq []
374       end
375     end
376     #例外ケース
377     context 'Json解析に失敗したとき' do
378       before do
379         SpeechBalloonTemplate.stub(:parse).with(any_args).and_return(false)
380       end
381       it 'フキダシテンプレートの数に変化がない' do
382         lambda {
383           SpeechBalloonTemplate.import(@t)
384         }.should_not change SpeechBalloonTemplate, :count
385       end
386       it 'Falseを返す' do
387         SpeechBalloonTemplate.import(@t).should be_false
388       end
389     end
390     context 'フキダシテンプレート作成に失敗したとき' do
391       before 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)
394       end
395       it 'フキダシテンプレートの数に変化がない' do
396         lambda {
397           SpeechBalloonTemplate.import(@t)
398         }.should_not change SpeechBalloonTemplate, :count
399       end
400       it '配列を返す' do
401         r = SpeechBalloonTemplate.import(@t)
402         r.is_a?(Array).should be_true
403       end
404       it '配列の中身は一件' do
405         r = SpeechBalloonTemplate.import(@t)
406         r.should have(1).items
407       end
408       it 'フキダシテンプレートオブジェクトが入っている' do
409         r = SpeechBalloonTemplate.import(@t)
410         r.first.is_a?(SpeechBalloonTemplate).should be_true
411       end
412     end
413     context '複数のフキダシテンプレート作成に失敗したとき' do
414       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
415       it 'フキダシテンプレートの数に変化がない' do
416         lambda {
417           SpeechBalloonTemplate.import(@tes)
418         }.should_not change SpeechBalloonTemplate, :count
419       end
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)
424       end
425       it '配列を返す' do
426         r = SpeechBalloonTemplate.import(@tes)
427         r.is_a?(Array).should be_true
428       end
429       it '配列の中身は2件' do
430         r = SpeechBalloonTemplate.import(@tes)
431         r.should have(2).items
432       end
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'
439       end
440     end
441   end
442   
443   describe 'インポートエラーの表示に於いて' do
444   end
445   
446   describe 'ファイル取り込みに於いて' do
447     before do
448       SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
449     end
450     context 'つつがなく終わるとき' do
451       before do
452         SpeechBalloonTemplate.stub(:import).with(any_args).and_return(true)
453       end
454       it 'ファイルを開いてテキストを読む' do
455         File.should_receive(:open).with(@f, 'r').exactly(1)
456         SpeechBalloonTemplate.import_file(@f)
457       end
458       it 'テキスト取り込みを依頼する' do
459         SpeechBalloonTemplate.should_receive(:import).with(any_args).exactly(1)
460         SpeechBalloonTemplate.import_file(@f)
461       end
462       #テキスト取り込み成功でTrueが返る
463       it 'Trueを返す' do
464         SpeechBalloonTemplate.import_file(@f).should be_true
465       end
466     end
467     context 'ファイルが開けないとき' do
468       before do
469         File.stub(:open).with(any_args).and_raise('StandardError')
470       end
471       it 'ファイルエラーのメッセージを出力する' do
472         pending
473       end
474       it 'Falseを返す' do
475         SpeechBalloonTemplate.import_file(@f).should be_false
476       end
477     end
478     #失敗したときは、失敗したフキダシテンプレートが配列で返る
479     context 'テキスト取り込みが失敗したとき' do
480       before do
481         SpeechBalloonTemplate.stub(:import).with(any_args).and_return(false)
482       end
483       it '各フキダシテンプレートのエラーメッセージを出力する' do
484         pending
485       end
486       it 'Falseを返す' do
487         SpeechBalloonTemplate.import_file(@f).should be_false
488       end
489     end
490   end
491   
492 end