OSDN Git Service

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