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
131 describe '閲覧許可に於いて' do
133 @st = FactoryGirl.build :speech_balloon_template
136 @st.visible?(@author).should == true
140 describe '一覧取得に於いて' do
142 @st = FactoryGirl.create :speech_balloon_template
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
152 l = SpeechBalloonTemplate.list
156 @st2 = FactoryGirl.create :speech_balloon_template, :name => "5", :t => 1
157 l = SpeechBalloonTemplate.list
158 l.should eq [@st, @st2]
161 describe '一覧取得オプションに於いて' do
163 r = SpeechBalloonTemplate.list_opt
164 r.is_a?(Hash).should be_true
168 describe 'json一覧出力オプションに於いて' do
170 r = SpeechBalloonTemplate.list_json_opt
171 r.is_a?(Hash).should be_true
176 describe '単体取得に於いて' do
178 @st = FactoryGirl.create :speech_balloon_template
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
187 it '指定のフキダシテンプレートを返す' do
188 l = SpeechBalloonTemplate.show @st.id, @author
191 context '他人のフキダシテンプレートを開こうとしたとき' do
192 it '403Forbidden例外を返す' do
193 SpeechBalloonTemplate.any_instance.stub(:visible?).and_return(false)
195 r = SpeechBalloonTemplate.show @st.id, @other_author
196 }.should raise_error(ActiveRecord::Forbidden)
199 context '存在しないフキダシテンプレートを開こうとしたとき' do
\r
200 it '404RecordNotFound例外を返す' do
\r
202 SpeechBalloonTemplate.show 110, @author
\r
203 }.should raise_error(ActiveRecord::RecordNotFound)
\r
207 describe '単体出力オプションに於いて' do
209 r = SpeechBalloonTemplate.show_opt
210 r.is_a?(Hash).should be_true
214 describe 'json単体出力オプションに於いて' do
216 r = SpeechBalloonTemplate.show_json_opt
217 r.is_a?(Hash).should be_true
222 describe '拡張データ補充に於いて' do
223 #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
224 #特にシステム画像データはidを直接記述して置換させないようにテストしている。
226 @sp = FactoryGirl.create :system_picture
227 @st = FactoryGirl.build :speech_balloon_template
229 "one" => {"balloon" => {"system_picture" => 'BASE64'}}
231 @sp_replaced_templates = {
232 "one" => {"balloon" => {"system_picture_id" => @sp.id}}
234 @attr = {"classname" => 'store', "caption" => 'store test',
235 "templates" => @templates
237 @sp_replaced_attr = {"classname" => 'store', "caption" => 'store test',
238 "templates" => @sp_replaced_templates
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
248 context 'つつがなく終わるとき' do
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
256 context 'システム画像の作成に失敗したとき' do
258 SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(false)
261 r = SpeechBalloonTemplate.templates_json_from @attr
265 context 'カラム値にtemplatesがないとき' do
267 @attr.delete "templates"
268 SpeechBalloonTemplate.stub(:replace_system_picture).with(any_args).and_return(true)
270 it '{}をjsonテキスト化して返す' do
271 r = SpeechBalloonTemplate.templates_json_from @attr
275 context 'templateにballoonがないとき' do
280 @attr = {"classname" => 'store', "caption" => 'store test',
281 "templates" => @templates
284 it 'templates以下をjsonテキスト化してを返す' do
285 r = SpeechBalloonTemplate.templates_json_from @attr
286 r.should eq @templates.to_json.to_s
292 #置換まわりが破壊的でテストしにくいので、外部と連携したテストが多い。
293 #特にシステム画像データはidを直接記述して置換させないようにテストしている。
295 @sp = FactoryGirl.create :system_picture
296 @st = FactoryGirl.create :speech_balloon_template
298 "one" => {"balloon" => {"system_picture_id" => @sp.id}}
300 @attr = {"classname" => 'store', "caption" => 'store test',
301 "templates" => @templates
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)
311 it 'テンプレート拡張データ置換に問い合わせている' do
312 SpeechBalloonTemplate.should_receive(:templates_json_from).exactly(1)
313 r = SpeechBalloonTemplate.store 'circle_v01', @attr
315 it 'インポート処理のデータ更新準備に依頼している' do
316 SpeechBalloonTemplate.should_receive(:modify_object).exactly(1)
317 r = SpeechBalloonTemplate.store 'circle_v01', @attr
320 SpeechBalloonTemplate.any_instance.should_receive(:overwrite).exactly(1)
321 r = SpeechBalloonTemplate.store 'circle_v01', @attr
323 it 'オブジェクトを保存している' do
324 SpeechBalloonTemplate.any_instance.should_receive(:save).exactly(1)
325 r = SpeechBalloonTemplate.store 'circle_v01', @attr
328 context 'つつがなく終わるとき' do
331 it 'カラム値のsettingsをセットしている' do
332 r = SpeechBalloonTemplate.store 'circle_v01', @attr
333 r.settings.should eq @templates.to_json.to_s
337 r = SpeechBalloonTemplate.store 'circle_v01', @attr
338 }.should change SpeechBalloonTemplate, :count
341 r = SpeechBalloonTemplate.store 'circle_v01', @attr
342 r.is_a?(SpeechBalloonTemplate).should be_true