OSDN Git Service

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