OSDN Git Service

t#29400:itr3?
[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   end
130   
131   describe '閲覧許可に於いて' do
132     before do
133       @st = FactoryGirl.build :speech_balloon_template
134     end
135     it '許可する' do\r
136       @st.visible?(@author).should == true
137     end\r
138   end
139   
140   describe '一覧取得に於いて' do
141     before do
142       @st = FactoryGirl.create :speech_balloon_template
143     end
144     context 'つつがなく終わるとき' do\r
145       it '一覧取得オプションを利用している' do\r
146         SpeechBalloonTemplate.stub(:list_opt).with(any_args).and_return({})\r
147         SpeechBalloonTemplate.should_receive(:list_opt).with(any_args).exactly(1)\r
148         r = SpeechBalloonTemplate.list
149       end\r
150     end\r
151     it 'リストを返す' do
152       l = SpeechBalloonTemplate.list
153       l.should eq [@st]
154     end
155     it 't順で並んでいる' do
156       @st2 = FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
157       l = SpeechBalloonTemplate.list
158       l.should eq [@st, @st2]
159     end
160   end
161   describe '一覧取得オプションに於いて' do
162     it '空のHashを返す' do
163       r = SpeechBalloonTemplate.list_opt
164       r.is_a?(Hash).should be_true
165       r.should be_empty
166     end
167   end
168   describe 'json一覧出力オプションに於いて' do
169     it '空のHashを返す' do
170       r = SpeechBalloonTemplate.list_json_opt
171       r.is_a?(Hash).should be_true
172       r.should be_empty
173     end
174   end
175   
176   describe '単体取得に於いて' do
177     before do
178       @st = FactoryGirl.create :speech_balloon_template
179     end
180     context 'つつがなく終わるとき' do\r
181       it '単体取得オプションを利用している' do\r
182         SpeechBalloonTemplate.stub(:show_opt).with(any_args).and_return({})\r
183         SpeechBalloonTemplate.should_receive(:show_opt).with(any_args).exactly(1)\r
184         r = SpeechBalloonTemplate.show @st.id, @author
185       end\r
186     end\r
187     it '指定のフキダシテンプレートを返す' do
188       l = SpeechBalloonTemplate.show @st.id, @author
189       l.should eq @st
190     end
191     context '他人のフキダシテンプレートを開こうとしたとき' do
192       it '403Forbidden例外を返す' do
193         SpeechBalloonTemplate.any_instance.stub(:visible?).and_return(false)
194         lambda{
195           r = SpeechBalloonTemplate.show @st.id, @other_author
196         }.should raise_error(ActiveRecord::Forbidden)
197       end
198     end
199     context '存在しないフキダシテンプレートを開こうとしたとき' do\r
200       it '404RecordNotFound例外を返す' do\r
201         lambda{\r
202           SpeechBalloonTemplate.show 110, @author\r
203         }.should raise_error(ActiveRecord::RecordNotFound)\r
204       end\r
205     end\r
206   end
207   describe '単体出力オプションに於いて' do
208     it '空のHashを返す' do
209       r = SpeechBalloonTemplate.show_opt
210       r.is_a?(Hash).should be_true
211       r.should be_empty
212     end
213   end
214   describe 'json単体出力オプションに於いて' do
215     it '空のHashを返す' do
216       r = SpeechBalloonTemplate.show_json_opt
217       r.is_a?(Hash).should be_true
218       r.should be_empty
219     end
220   end
221   
222   describe '拡張データ補充に於いて' do
223     #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
224     #特にシステム画像データはidを直接記述して置換させないようにテストしている。
225     before do
226       @sp = FactoryGirl.create :system_picture
227       @st = FactoryGirl.build :speech_balloon_template
228       @templates = {
229         "one" => {"balloon" => {"system_picture" => 'BASE64'}}
230       }
231       @sp_replaced_templates = {
232         "one" => {"balloon" => {"system_picture_id" => @sp.id}}
233       }
234       @attr = {"classname" => 'store', "caption" => 'store test',
235         "templates" => @templates
236       }
237       @sp_replaced_attr = {"classname" => 'store', "caption" => 'store test',
238         "templates" => @sp_replaced_templates
239       }
240     end
241     context '事前チェック' do
242       it 'システム画像置換に依頼して、テキストの画像データをシステム画像idに置換している' do
243         SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(true)
244         SpeechBalloonTemplate.should_receive(:replace_system_picture).exactly(1)
245         r = SpeechBalloonTemplate.templates_json_from @attr
246       end
247     end
248     context 'つつがなく終わるとき' do
249       before do
250       end
251       it 'templates以下をjsonテキスト化して返す' do
252         r = SpeechBalloonTemplate.templates_json_from @sp_replaced_attr
253         r.should eq @sp_replaced_templates.to_json.to_s
254       end
255     end
256     context 'システム画像の作成に失敗したとき' do
257       before do
258         SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(false)
259       end
260       it 'nilを返す' do
261         r = SpeechBalloonTemplate.templates_json_from @attr
262         r.should be_nil
263       end
264     end
265     context 'カラム値にtemplatesがないとき' do
266       before do
267         @attr.delete "templates"
268         SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(true)
269       end
270       it '{}をjsonテキスト化して返す' do
271         r = SpeechBalloonTemplate.templates_json_from @attr
272         r.should eq '{}'
273       end
274     end
275     context 'templateにballoonがないとき' do
276       before do
277         @templates = {
278           "one" => {}
279         }
280         @attr = {"classname" => 'store', "caption" => 'store test',
281           "templates" => @templates
282         }
283       end
284       it 'templates以下をjsonテキスト化してを返す' do
285         r = SpeechBalloonTemplate.templates_json_from @attr
286         r.should eq @templates.to_json.to_s
287       end
288     end
289   end
290   
291   describe '更新に於いて' do
292     #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
293     #特にシステム画像データはidを直接記述して置換させないようにテストしている。
294     before do
295       @sp = FactoryGirl.create :system_picture
296       @st = FactoryGirl.create :speech_balloon_template
297       @templates = {
298         "one" => {"balloon" => {"system_picture_id" => @sp.id}}
299       }
300       @attr = {"classname" => 'store', "caption" => 'store test',
301         "templates" => @templates
302       }
303     end
304     context '事前チェック' do
305       before do
306         SpeechBalloonTemplate.stub(:templates_json_from).with(any_args).and_return('{}')
307         SpeechBalloonTemplate.stub(:modify_object).with(any_args).and_return(@st)
308         SpeechBalloonTemplate.any_instance.stub(:overwrite).with(any_args).and_return(true)
309         SpeechBalloonTemplate.any_instance.stub(:save).with(any_args).and_return(true)
310       end
311       it 'テンプレート拡張データ置換に問い合わせている' do
312         SpeechBalloonTemplate.should_receive(:templates_json_from).exactly(1)
313         r = SpeechBalloonTemplate.store 'circle_v01', @attr
314       end
315       it 'インポート処理のデータ更新準備に依頼している' do
316         SpeechBalloonTemplate.should_receive(:modify_object).exactly(1)
317         r = SpeechBalloonTemplate.store 'circle_v01', @attr
318       end
319       it '上書き補充を依頼している' do
320         SpeechBalloonTemplate.any_instance.should_receive(:overwrite).exactly(1)
321         r = SpeechBalloonTemplate.store 'circle_v01', @attr
322       end
323       it 'オブジェクトを保存している' do
324         SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
325         r = SpeechBalloonTemplate.store 'circle_v01', @attr
326       end
327     end
328     context 'つつがなく終わるとき' do
329       before do
330       end
331       it 'カラム値のsettingsをセットしている' do
332         r = SpeechBalloonTemplate.store 'circle_v01', @attr
333         r.settings.should eq @templates.to_json.to_s
334       end
335       it 'モデルが作成されている' do
336         lambda {
337           r = SpeechBalloonTemplate.store 'circle_v01', @attr
338         }.should change SpeechBalloonTemplate, :count
339       end
340       it 'オブジェクトを返している' do
341         r = SpeechBalloonTemplate.store 'circle_v01', @attr
342         r.is_a?(SpeechBalloonTemplate).should be_true
343       end
344     end
345   end
346   
347 end