OSDN Git Service

t#29672:fix sbt for properties
[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     @user = FactoryGirl.create( :user_yas)\r
19     @author = @user.author\r
20     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id\r
21     @other_user = FactoryGirl.create( :user_yas)\r
22     @other_author = @other_user.author\r
23     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id\r
24   end
25   
26   describe '検証に於いて' do
27     before do
28       @st = FactoryGirl.build :speech_balloon_template
29     end
30     
31     context 'オーソドックスなデータのとき' do
32       it '下限データが通る' do
33         @st.name = 'a'
34         @st.classname = 'a'
35         @st.caption = 'a'
36         @st.t = 0
37         @st.settings = '{}'
38         @st.should be_valid
39       end
40       it '上限データが通る' do
41         @st.name = 'a'*50
42         @st.classname = 'a'*50
43         @st.caption = 'a'*30
44         @st.t = 99999
45         @st.settings = '{' + 'a'*99999 + '}'
46         @st.should be_valid
47       end
48     end
49     
50     
51     context 'nameを検証するとき' do
52       it 'nullなら失敗する' do
53         @st.name = ''
54         @st.should_not be_valid
55       end
56       it '51文字以上なら失敗する' do
57         @st.name = 'a'*51
58         @st.should_not be_valid
59       end
60     end
61     context 'classnameを検証するとき' do
62       it 'nullなら失敗する' do
63         @st.classname = ''
64         @st.should_not be_valid
65       end
66       it '51文字以上なら失敗する' do
67         @st.classname = 'a'*51
68         @st.should_not be_valid
69       end
70     end
71     context 'captionを検証するとき' do
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       it 'nullなら失敗する' do
83         @st.t = nil
84         @st.should_not be_valid
85       end
86       it '数値でなければ失敗する' do
87         @st.t = 'a'
88         @st.should_not be_valid
89       end
90       it '負なら失敗する' do
91         @st.t = -1
92         @st.should_not be_valid
93       end
94     end
95     context 'settingsを検証するとき' do
96       it 'nullなら失敗する' do
97         @st.settings = nil
98         @st.should_not be_valid
99       end
100     end
101   end
102   
103   describe 'デフォルト値補充に於いて' do
104     it 'defined' do
105       @st = FactoryGirl.build :speech_balloon_template
106       @st.supply_default
107     end
108   end
109   
110   describe '上書き補充に於いて' do
111     before do
112       @st = FactoryGirl.build :speech_balloon_template
113     end
114     it 'テーブルが空のときは0を返す' do
115       @st.overwrite
116       @st.t.should eq 0
117     end
118     it 'テーブルが1行あるときは1を返す' do
119       FactoryGirl.create :speech_balloon_template
120       @st.overwrite
121       @st.t.should eq 1
122     end
123     it 'テーブルが2行あるときは2を返す' do
124       FactoryGirl.create :speech_balloon_template
125       FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
126       @st.overwrite
127       @st.t.should eq 2
128     end
129     context 'つつがなく終わるとき' do\r
130       before do
131         @st = FactoryGirl.create :speech_balloon_template, :t => 0
132       end
133       it '更新のときは補充しない' do
134         @st.overwrite
135         @st.t.should eq 0
136       end
137     end
138   end
139   
140   describe '閲覧許可に於いて' do
141     before do
142       @st = FactoryGirl.build :speech_balloon_template
143     end
144     it '許可する' do\r
145       @st.visible?(@author).should == true
146     end\r
147   end
148   
149   describe '一覧取得に於いて' do
150     before do
151       @st = FactoryGirl.create :speech_balloon_template
152     end
153     context 'つつがなく終わるとき' do\r
154       it '一覧取得オプションを利用している' do\r
155         SpeechBalloonTemplate.stub(:list_opt).with(any_args).and_return({})\r
156         SpeechBalloonTemplate.should_receive(:list_opt).with(any_args).exactly(1)\r
157         r = SpeechBalloonTemplate.list
158       end\r
159     end\r
160     it 'リストを返す' do
161       l = SpeechBalloonTemplate.list
162       l.should eq [@st]
163     end
164     it 't順で並んでいる' do
165       @st2 = FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
166       l = SpeechBalloonTemplate.list
167       l.should eq [@st, @st2]
168     end
169   end
170   describe '一覧取得オプションに於いて' do
171     it '空のHashを返す' do
172       r = SpeechBalloonTemplate.list_opt
173       r.is_a?(Hash).should be_true
174       r.should be_empty
175     end
176   end
177   describe 'json一覧出力オプションに於いて' do
178     it '空のHashを返す' do
179       r = SpeechBalloonTemplate.list_json_opt
180       r.is_a?(Hash).should be_true
181       r.should be_empty
182     end
183   end
184   
185   describe '単体取得に於いて' do
186     before do
187       @st = FactoryGirl.create :speech_balloon_template
188     end
189     context 'つつがなく終わるとき' do\r
190       it '単体取得オプションを利用している' do\r
191         SpeechBalloonTemplate.stub(:show_opt).with(any_args).and_return({})\r
192         SpeechBalloonTemplate.should_receive(:show_opt).with(any_args).exactly(1)\r
193         r = SpeechBalloonTemplate.show @st.id, @author
194       end\r
195     end\r
196     it '指定のフキダシテンプレートを返す' do
197       l = SpeechBalloonTemplate.show @st.id, @author
198       l.should eq @st
199     end
200     context '他人のフキダシテンプレートを開こうとしたとき' do
201       it '403Forbidden例外を返す' do
202         SpeechBalloonTemplate.any_instance.stub(:visible?).and_return(false)
203         lambda{
204           r = SpeechBalloonTemplate.show @st.id, @other_author
205         }.should raise_error(ActiveRecord::Forbidden)
206       end
207     end
208     context '存在しないフキダシテンプレートを開こうとしたとき' do\r
209       it '404RecordNotFound例外を返す' do\r
210         lambda{\r
211           SpeechBalloonTemplate.show 110, @author\r
212         }.should raise_error(ActiveRecord::RecordNotFound)\r
213       end\r
214     end\r
215   end
216   describe '単体出力オプションに於いて' do
217     it '空のHashを返す' do
218       r = SpeechBalloonTemplate.show_opt
219       r.is_a?(Hash).should be_true
220       r.should be_empty
221     end
222   end
223   describe 'json単体出力オプションに於いて' do
224     it '空のHashを返す' do
225       r = SpeechBalloonTemplate.show_json_opt
226       r.is_a?(Hash).should be_true
227       r.should be_empty
228     end
229   end
230   
231   describe '拡張データ補充に於いて' do
232     #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
233     #特にシステム画像データはidを直接記述して置換させないようにテストしている。
234     before do
235       @sp = FactoryGirl.create :system_picture
236       @st = FactoryGirl.build :speech_balloon_template
237       @templates = {
238         "speech_balloon" => {"default" => 1}, 
239         "one" => {"balloon" => {"system_picture" => 'BASE64'}}
240       }
241       @sp_replaced_templates = {
242         "speech_balloon" => {"default" => 1}, 
243         "one" => {"balloon" => {"system_picture_id" => @sp.id}}
244       }
245       @attr = {"classname" => 'store', "caption" => 'store test',
246         "templates" => @templates
247       }
248       @sp_replaced_attr = {"classname" => 'store', "caption" => 'store test',
249         "templates" => @sp_replaced_templates
250       }
251     end
252     context '事前チェック' do
253       it 'システム画像置換に依頼して、テキストの画像データをシステム画像idに置換している' do
254         SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(true)
255         SpeechBalloonTemplate.should_receive(:replace_system_picture).exactly(1)
256         r = SpeechBalloonTemplate.templates_json_from @attr
257       end
258     end
259     context 'つつがなく終わるとき' do
260       before do
261       end
262       it 'templates以下をjsonテキスト化して返す' do
263         r = SpeechBalloonTemplate.templates_json_from @sp_replaced_attr
264         r.should eq @sp_replaced_templates.to_json.to_s
265       end
266     end
267     context 'システム画像の作成に失敗したとき' do
268       before do
269         SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(false)
270       end
271       it 'nilを返す' do
272         r = SpeechBalloonTemplate.templates_json_from @attr
273         r.should be_nil
274       end
275     end
276     context 'カラム値にtemplatesがないとき' do
277       before do
278         @attr.delete "templates"
279         SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(true)
280       end
281       it '{}をjsonテキスト化して返す' do
282         r = SpeechBalloonTemplate.templates_json_from @attr
283         r.should eq '{}'
284       end
285     end
286     context 'templateにballoonがないとき' do
287       before do
288         @templates = {
289           "speech_balloon" => {"default" => 1}, 
290           "one" => {}
291         }
292         @attr = {"classname" => 'store', "caption" => 'store test',
293           "templates" => @templates
294         }
295       end
296       it 'templates以下をjsonテキスト化してを返す' do
297         r = SpeechBalloonTemplate.templates_json_from @attr
298         r.should eq @templates.to_json.to_s
299       end
300     end
301   end
302   
303   describe '更新に於いて' do
304     #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
305     #特にシステム画像データはidを直接記述して置換させないようにテストしている。
306     before do
307       @sp = FactoryGirl.create :system_picture
308       @st = FactoryGirl.create :speech_balloon_template
309       @templates = {
310         "speech_balloon" => {"default" => 1}, 
311         "one" => {"balloon" => {"system_picture_id" => @sp.id}}
312       }
313       @attr = {"classname" => 'store', "caption" => 'store test',
314         "templates" => @templates
315       }
316     end
317     context '事前チェック' do
318       before do
319         SpeechBalloonTemplate.stub(:templates_json_from).with(any_args).and_return('{}')
320         SpeechBalloonTemplate.stub(:modify_object).with(any_args).and_return(@st)
321         SpeechBalloonTemplate.any_instance.stub(:overwrite).with(any_args).and_return(true)
322         SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(true)
323       end
324       it 'テンプレート置換に問い合わせている' do
325         SpeechBalloonTemplate.should_receive(:templates_json_from).exactly(1)
326         r = SpeechBalloonTemplate.store 'circle_v01', @attr
327       end
328       it 'インポート処理のデータ更新準備に依頼している' do
329         SpeechBalloonTemplate.should_receive(:modify_object).exactly(1)
330         r = SpeechBalloonTemplate.store 'circle_v01', @attr
331       end
332       it '上書き補充を依頼している' do
333         SpeechBalloonTemplate.any_instance.should_receive(:overwrite).exactly(1)
334         r = SpeechBalloonTemplate.store 'circle_v01', @attr
335       end
336       it 'オブジェクトを保存している' do
337         SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
338         r = SpeechBalloonTemplate.store 'circle_v01', @attr
339       end
340     end
341     context 'つつがなく終わるとき' do
342       before do
343       end
344       it 'カラム値のsettingsをセットしている' do
345         r = SpeechBalloonTemplate.store 'circle_v01', @attr
346         r.settings.should eq @templates.to_json.to_s
347       end
348       it 'モデルが作成されている' do
349         lambda {
350           r = SpeechBalloonTemplate.store 'circle_v01', @attr
351         }.should change SpeechBalloonTemplate, :count
352       end
353       it 'オブジェクトを返している' do
354         r = SpeechBalloonTemplate.store 'circle_v01', @attr
355         r.is_a?(SpeechBalloonTemplate).should be_true
356       end
357     end
358   end
359   
360 end