OSDN Git Service

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