OSDN Git Service

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