OSDN Git Service

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