1 # -*- encoding: utf-8 -*-
4 describe SpeechBalloonTemplate do
7 @f = Rails.root + 'spec/json/speech_balloon_template.json'
8 @t = File.open(@f, 'r').read
10 @fs = Rails.root + 'spec/json/speech_balloon_templates.json'
11 @ts = File.open(@fs, 'r').read
13 @fes = Rails.root + 'spec/json/invalid_speech_balloon_templates.json'
14 @tes = File.open(@fes, 'r').read
15 @jes = JSON.parse @tes
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
28 @st = FactoryGirl.build :speech_balloon_template
31 context 'オーソドックスなデータのとき' do
42 @st.classname = 'a'*50
45 @st.settings = '{' + 'a'*99999 + '}'
51 context 'nameを検証するとき' do
54 @st.should_not be_valid
58 @st.should_not be_valid
61 context 'classnameを検証するとき' do
64 @st.should_not be_valid
67 @st.classname = 'a'*51
68 @st.should_not be_valid
71 context 'captionを検証するとき' do
74 @st.should_not be_valid
78 @st.should_not be_valid
84 @st.should_not be_valid
88 @st.should_not be_valid
92 @st.should_not be_valid
95 context 'settingsを検証するとき' do
98 @st.should_not be_valid
103 describe 'デフォルト値補充に於いて' do
105 @st = FactoryGirl.build :speech_balloon_template
110 describe '上書き補充に於いて' do
112 @st = FactoryGirl.build :speech_balloon_template
114 it 'テーブルが空のときは0を返す' do
118 it 'テーブルが1行あるときは1を返す' do
119 FactoryGirl.create :speech_balloon_template
123 it 'テーブルが2行あるときは2を返す' do
124 FactoryGirl.create :speech_balloon_template
125 FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
129 context 'つつがなく終わるとき' do
\r
131 @st = FactoryGirl.create :speech_balloon_template, :t => 0
140 describe '閲覧許可に於いて' do
142 @st = FactoryGirl.build :speech_balloon_template
145 @st.visible?(@author).should == true
149 describe '一覧取得に於いて' do
151 @st = FactoryGirl.create :speech_balloon_template
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
161 l = SpeechBalloonTemplate.list
165 @st2 = FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
166 l = SpeechBalloonTemplate.list
167 l.should eq [@st, @st2]
170 describe '一覧取得オプションに於いて' do
172 r = SpeechBalloonTemplate.list_opt
173 r.is_a?(Hash).should be_true
177 describe 'json一覧出力オプションに於いて' do
179 r = SpeechBalloonTemplate.list_json_opt
180 r.is_a?(Hash).should be_true
185 describe '単体取得に於いて' do
187 @st = FactoryGirl.create :speech_balloon_template
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
196 it '指定のフキダシテンプレートを返す' do
197 l = SpeechBalloonTemplate.show @st.id, @author
200 context '他人のフキダシテンプレートを開こうとしたとき' do
201 it '403Forbidden例外を返す' do
202 SpeechBalloonTemplate.any_instance.stub(:visible?).and_return(false)
204 r = SpeechBalloonTemplate.show @st.id, @other_author
205 }.should raise_error(ActiveRecord::Forbidden)
208 context '存在しないフキダシテンプレートを開こうとしたとき' do
\r
209 it '404RecordNotFound例外を返す' do
\r
211 SpeechBalloonTemplate.show 110, @author
\r
212 }.should raise_error(ActiveRecord::RecordNotFound)
\r
216 describe '単体出力オプションに於いて' do
218 r = SpeechBalloonTemplate.show_opt
219 r.is_a?(Hash).should be_true
223 describe 'json単体出力オプションに於いて' do
225 r = SpeechBalloonTemplate.show_json_opt
226 r.is_a?(Hash).should be_true
231 describe '拡張データ補充に於いて' do
232 #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
233 #特にシステム画像データはidを直接記述して置換させないようにテストしている。
235 @sp = FactoryGirl.create :system_picture
236 @st = FactoryGirl.build :speech_balloon_template
238 "speech_balloon" => {"default" => 1},
239 "one" => {"balloon" => {"system_picture" => 'BASE64'}}
241 @sp_replaced_templates = {
242 "speech_balloon" => {"default" => 1},
243 "one" => {"balloon" => {"system_picture_id" => @sp.id}}
245 @attr = {"classname" => 'store', "caption" => 'store test',
246 "templates" => @templates
248 @sp_replaced_attr = {"classname" => 'store', "caption" => 'store test',
249 "templates" => @sp_replaced_templates
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
259 context 'つつがなく終わるとき' do
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
267 context 'システム画像の作成に失敗したとき' do
269 SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(false)
272 r = SpeechBalloonTemplate.templates_json_from @attr
276 context 'カラム値にtemplatesがないとき' do
278 @attr.delete "templates"
279 SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(true)
281 it '{}をjsonテキスト化して返す' do
282 r = SpeechBalloonTemplate.templates_json_from @attr
286 context 'templateにballoonがないとき' do
289 "speech_balloon" => {"default" => 1},
292 @attr = {"classname" => 'store', "caption" => 'store test',
293 "templates" => @templates
296 it 'templates以下をjsonテキスト化してを返す' do
297 r = SpeechBalloonTemplate.templates_json_from @attr
298 r.should eq @templates.to_json.to_s
304 #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
305 #特にシステム画像データはidを直接記述して置換させないようにテストしている。
307 @sp = FactoryGirl.create :system_picture
308 @st = FactoryGirl.create :speech_balloon_template
310 "speech_balloon" => {"default" => 1},
311 "one" => {"balloon" => {"system_picture_id" => @sp.id}}
313 @attr = {"classname" => 'store', "caption" => 'store test',
314 "templates" => @templates
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)
324 it 'テンプレート置換に問い合わせている' do
325 SpeechBalloonTemplate.should_receive(:templates_json_from).exactly(1)
326 r = SpeechBalloonTemplate.store 'circle_v01', @attr
328 it 'インポート処理のデータ更新準備に依頼している' do
329 SpeechBalloonTemplate.should_receive(:modify_object).exactly(1)
330 r = SpeechBalloonTemplate.store 'circle_v01', @attr
333 SpeechBalloonTemplate.any_instance.should_receive(:overwrite).exactly(1)
334 r = SpeechBalloonTemplate.store 'circle_v01', @attr
336 it 'オブジェクトを保存している' do
337 SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
338 r = SpeechBalloonTemplate.store 'circle_v01', @attr
341 context 'つつがなく終わるとき' do
344 it 'カラム値のsettingsをセットしている' do
345 r = SpeechBalloonTemplate.store 'circle_v01', @attr
346 r.settings.should eq @templates.to_json.to_s
350 r = SpeechBalloonTemplate.store 'circle_v01', @attr
351 }.should change SpeechBalloonTemplate, :count
354 r = SpeechBalloonTemplate.store 'circle_v01', @attr
355 r.is_a?(SpeechBalloonTemplate).should be_true