OSDN Git Service

fix peta spec
[pettanr/pettanr.git] / spec / peta / root_spec.rb
1 # -*- encoding: utf-8 -*-
2 require 'spec_helper'
3 #スクロール
4
5 describe Sheet do
6   include RootMacros
7   before do
8     @admin =FactoryGirl.create :admin
9     @user_yas = FactoryGirl.create :user_yas_with_owner
10     @user_rom = FactoryGirl.create :user_rom
11     @author_yas =  @user_yas.author
12   end
13   
14   describe '生成メソッドに於いて' do
15     before do
16       @item =  FactoryGirl.create :sheet_install_a_with_sheet_panels, author: @author_yas
17     end
18     
19   end
20   
21   describe 'copy_attributesに於いて' do
22     before do
23       @item =  FactoryGirl.create :sheet_install_a_with_sheet_panels, author: @author_yas
24     end
25     
26     it 'ブーストメッセージをリーフに伝搬する' do
27     end
28   end
29   
30 end
31
32 describe Panel do
33   include RootMacros
34   before do
35     @admin =FactoryGirl.create :admin
36     @user_yas = FactoryGirl.create :user_yas_with_owner
37     @user_rom = FactoryGirl.create :user_rom
38     @author_yas =  @user_yas.author
39   end
40   
41   describe '生成メソッドに於いて' do
42     before do
43       @item =  FactoryGirl.create :panel_hello_with_scroll_panels, author: @author_yas
44     end
45     
46   end
47   
48 end
49
50 describe ScrollPanel do
51   include LeafMacros
52   before do
53     @admin =FactoryGirl.create :admin
54     @user_yas = FactoryGirl.create :user_yas_with_owner
55     @user_rom = FactoryGirl.create :user_rom
56     @author_yas =  @user_yas.author
57     
58     @model = ScrollPanel
59   end
60   
61   describe 'new_tに於いて' do
62     before do
63       @scroll =  FactoryGirl.create :scroll_hello, author: @author_yas
64     end
65     
66     context 'オーソドックスなデータのとき' do
67       it 'cut' do
68         expect(@model.new_t(@scroll.id)).to eq 0
69       end
70       
71     end
72     
73     context 'オーソドックスなデータのとき' do
74       it 'cut' do
75         @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
76         expect(@model.new_t(@scroll.id)).to eq 1
77       end
78       
79       it 'cut' do
80         @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
81         @item =  FactoryGirl.create :scroll_panel_plain, z: 2, t: 1, scroll: @scroll, panel: @panel
82         @item =  FactoryGirl.create :scroll_panel_plain, z: 3, t: 2, scroll: @scroll, panel: @panel
83         expect(@model.new_t(@scroll.id)).to eq 3
84       end
85       
86     end
87     
88   end
89   
90   describe 'collect_tに於いて' do
91     before do
92       @dummy =  FactoryGirl.create :scroll_great_with_scroll_panels, author: @author_yas
93       @scroll =  FactoryGirl.create :scroll_hello, author: @author_yas
94     end
95     
96     context 'オーソドックスなデータのとき' do
97       it 'cut' do
98         expect(@model.collect_t(@scroll.id)).to be_empty
99       end
100       
101     end
102     
103     context 'オーソドックスなデータのとき' do
104       it 'cut' do
105         @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
106         expect(@model.collect_t(@scroll.id)).to eq [0]
107       end
108       
109       it 'cut' do
110         @item =  FactoryGirl.create :scroll_panel_plain, z: 1, t: 0, scroll: @scroll, panel: @panel
111         @item =  FactoryGirl.create :scroll_panel_plain, z: 2, t: 1, scroll: @scroll, panel: @panel
112         @item =  FactoryGirl.create :scroll_panel_plain, z: 3, t: 2, scroll: @scroll, panel: @panel
113         expect(@model.collect_t(@scroll.id)).to eq [0, 1, 2]
114       end
115       
116     end
117     
118   end
119   
120   describe 'tチェックに於いて' do
121     before do
122       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
123       @panel = FactoryGirl.create :panel, :author_id => @author.id
124       @item = FactoryGirl.build :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
125     end
126     context 'つつがなく終わるとき' do
127       it 't収集を依頼している' do
128         SheetPanel.should_receive(:collect_t).with(any_args).exactly(1)
129         SheetPanel.stub(:collect_t).with(any_args).and_return([])
130         SheetPanel.stub(:serial?).with(any_args).and_return(true)
131         r = SheetPanel.validate_t @item
132       end
133       it '収集したtをシリアライズチェック依頼している' do
134         SheetPanel.stub(:collect_t).with(any_args).and_return([])
135         SheetPanel.should_receive(:serial?).with(any_args).exactly(1)
136         SheetPanel.stub(:serial?).with(any_args).and_return(true)
137         r = SheetPanel.validate_t @item
138       end
139     end
140     #実データでチェック
141     #依頼チェックだけでは不安なので最低限のチェックを
142     context '新規のとき' do
143       it '一件だけで正常通過している' do
144         @item = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
145         r = SheetPanel.validate_t @item
146         r.should be_true 
147       end
148     end
149     context '既存のとき' do
150       it '2件目を作っても正常通過している' do
151         @item = FactoryGirl.create :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 0
152         @item2 = FactoryGirl.build :sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id, :t => 1
153         r = SheetPanel.validate_t @item2
154         r.should be_true 
155       end
156     end
157   end
158   describe '保存に於いて' do
159     before do
160       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
161       @panel = FactoryGirl.create :panel, :author_id => @author.id
162       @item = FactoryGirl.build :sheet_panel, :t => nil, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
163     end
164     context 'つつがなく終わるとき' do
165       it '編集許可チェックを依頼している' do
166         SheetPanel.any_instance.stub(:allow?).with(any_args).and_return(true)
167         SheetPanel.any_instance.should_receive(:allow?).with(any_args).exactly(1)
168         r = @item.store
169       end
170       it '順序入れ替えを依頼している' do
171         SheetPanel.any_instance.stub(:rotate).with(any_args).and_return(0)
172         SheetPanel.any_instance.should_receive(:rotate).with(any_args).exactly(1)
173         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
174         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
175         r = @item.store 
176       end
177       it '保存を依頼している' do
178         SheetPanel.stub(:new_t).with(any_args).and_return(0)
179         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
180         SheetPanel.any_instance.should_receive(:save).with(any_args).exactly(1)
181         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
182         r = @item.store
183       end
184       it 'tのシリアライズチェックを依頼している' do
185         SheetPanel.stub(:new_t).with(any_args).and_return(0)
186         SheetPanel.any_instance.stub(:save).with(any_args).and_return(true)
187         SheetPanel.stub(:validate_t).with(any_args).and_return(true)
188         SheetPanel.should_receive(:validate_t).with(any_args).exactly(1)
189         r = @item.store
190       end
191     end
192     #入れ替えテストと同じテストを実施。こちらはシフトだけでなく本尊も更新されている
193     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したとき' do
194       before do
195         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
196         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
197         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
198         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
199         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
200         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
201         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
202         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
203       end
204       it '既存のt0には変化がない' do
205         @item6.store
206         @item.reload
207         @item.t.should eq 0
208       end
209       it '既存のt1には変化がない' do
210         @item6.store
211         @item2.reload
212         @item2.t.should eq 1
213       end
214       it '既存のt2を3にシフトしている' do
215         @item6.store
216         @item3.reload
217         @item3.t.should eq 3
218       end
219       it '既存のt3を4にシフトしている' do
220         @item6.store
221         @item4.reload
222         @item4.t.should eq 4
223       end
224       it '既存のt5を5にシフトしている' do
225         @item6.store
226         @item5.reload
227         @item5.t.should eq 5
228       end
229       it '新規のt2が作成されている' do
230         @item6.store
231         @item6.reload
232         @item6.t.should eq 2
233       end
234       it '他の用紙に影響がない' do
235         @ot = @itemc2.t
236         @item6.store
237         @itemc2.reload
238         @itemc2.t.should eq @ot
239       end
240     end
241     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したとき' do
242       before do
243         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
244         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
245         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
246         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
247         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
248         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
249         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
250         @ot = @item4.t
251         @item4.t = 1
252       end
253       it '既存のt0には変化がない' do
254         @item4.store @ot
255         @item.reload
256         @item.t.should eq 0
257       end
258       it '既存のt4には変化がない' do
259         @item4.store @ot
260         @item5.reload
261         @item5.t.should eq 4
262       end
263       it '既存のt1を2にシフトしている' do
264         @item4.store @ot
265         @item2.reload
266         @item2.t.should eq 2
267       end
268       it '既存のt2を3にシフトしている' do
269         @item4.store @ot
270         @item3.reload
271         @item3.t.should eq 3
272       end
273       it '既存のt3を1にシフトしている' do
274         @item4.store @ot
275         @item4.reload
276         @item4.t.should eq 1
277       end
278       it '他の用紙に影響がない' do
279         @item4.store @ot
280         @itemc2.reload
281         @itemc2.t.should eq 0
282       end
283     end
284     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で1を3に移動したとき' do
285       before do
286         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
287         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
288         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
289         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
290         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
291         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
292         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
293         @ot = @item2.t
294         @item2.t = 3
295       end
296       it '既存のt0には変化がない' do
297         @item2.store @ot
298         @item.reload
299         @item.t.should eq 0
300       end
301       it '既存のt4には変化がない' do
302         @item2.store @ot
303         @item5.reload
304         @item5.t.should eq 4
305       end
306       it '既存のt1を3にシフトしている' do
307         @item2.store @ot
308         @item2.reload
309         @item2.t.should eq 3
310       end
311       it '既存のt2を1にシフトしている' do
312         @item2.store @ot
313         @item3.reload
314         @item3.t.should eq 1
315       end
316       it '既存のt3を2にシフトしている' do
317         @item2.store @ot
318         @item4.reload
319         @item4.t.should eq 2
320       end
321       it '他の用紙に影響がない' do
322         @item2.store @ot
323         @itemc2.reload
324         @itemc2.t.should eq 0
325       end
326     end
327     #ロールバックテスト。入れ替えが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
328     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で2に挿入したが保存に失敗したとき' do
329       before do
330         SheetPanel.any_instance.stub(:save).with(any_args).and_return(false)
331         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
332         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
333         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
334         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
335         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
336         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
337         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
338         @item6 = FactoryGirl.build :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
339       end
340       it '既存のtに変化がない' do
341         @item6.store
342         @item.reload
343         @item.t.should eq 0
344         @item2.reload
345         @item2.t.should eq 1
346         @item3.reload
347         @item3.t.should eq 2
348         @item4.reload
349         @item4.t.should eq 3
350         @item5.reload
351         @item5.t.should eq 4
352         @itemc2.reload
353         @itemc2.t.should eq 0
354       end
355       it 'falseを返す' do
356         r = @item6.store
357         r.should be_false
358       end
359     end
360     context 'テーブルに5件(t:0,1,2,3,4)+他の用紙1件で3を1に移動したがシリアルチェックに失敗したとき' do
361       before do
362         SheetPanel.stub(:validate_t).with(any_args).and_return(false)
363         @sheet2 = FactoryGirl.create :sheet, :author_id => @author.id
364         @itemc2 = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet2.id, :panel_id => @panel.id, :author_id => @author.id
365         @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
366         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
367         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
368         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
369         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
370         @ot = @item4.t
371         @item4.t = 1
372       end
373       it '既存のtに変化がない' do
374         @item4.store @ot
375         @item.reload
376         @item.t.should eq 0
377         @item2.reload
378         @item2.t.should eq 1
379         @item3.reload
380         @item3.t.should eq 2
381         @item4.reload
382         @item4.t.should eq 3
383         @item5.reload
384         @item5.t.should eq 4
385         @itemc2.reload
386         @itemc2.t.should eq 0
387       end
388       it 'falseを返す' do
389         r = @item4.store @ot
390         r.should be_false
391       end
392       it 'tにエラーメッセージが入っている' do
393         @item4.store @ot
394         @item4.errors[:t].should_not be_empty
395         @item4.valid?.should be_true
396       end
397     end
398     context '編集不可だったとき' do
399       before do
400         @item = FactoryGirl.build :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
401         SheetPanel.any_instance.stub(:allow?).and_return(false)
402       end
403       it '403Forbidden例外を返す' do
404         lambda{
405           @item.store
406         }.should raise_error(ActiveRecord::Forbidden)
407       end
408     end
409   end
410   describe '切り詰め処理つき削除に於いて' do
411     before do
412       @sheet = FactoryGirl.create :sheet, :author_id => @author.id
413       @panel = FactoryGirl.create :panel, :author_id => @author.id
414       @item = FactoryGirl.create :sheet_panel, :t => 0, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
415     end
416     context 'つつがなく終わるとき' do
417       it '削除される' do
418         lambda{
419           @item.destroy_and_shorten
420         }.should change(SheetPanel, :count ).by(-1)
421       end
422       it 'Trueを返す' do
423         r = @item.destroy_and_shorten
424         r.should be_true 
425       end
426     end
427     context '削除に失敗したとき' do
428       before do
429         SheetPanel.any_instance.stub(:destroy).and_return(false)
430       end
431       it 'ロールバックされる' do
432         lambda{
433           @item.destroy_and_shorten
434         }.should_not change(SheetPanel, :count )
435       end
436       it 'Falseを返す' do
437         r = @item.destroy_and_shorten
438         r.should be_false
439       end
440     end
441     #連携テスト。切り詰めが直接DBをいじる
442     context '2件で先頭を削除したとき' do
443       before do
444         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
445       end
446       it '行が削除される' do
447         lambda{
448           @item.destroy_and_shorten
449         }.should change(SheetPanel, :count ).by(-1)
450       end
451       it '先頭は削除される' do
452         @item.destroy_and_shorten
453         lambda{
454           SheetPanel.find @item.id
455         }.should raise_error(ActiveRecord::RecordNotFound)
456       end
457       it '2件目は前に詰められる' do
458         @item.destroy_and_shorten
459         @item2.reload
460         @item2.t.should eq 0
461       end
462     end
463     context '3件で先頭を削除したとき' do
464       before do
465         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
466         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
467       end
468       it '行が削除される' do
469         lambda{
470           @item.destroy_and_shorten
471         }.should change(SheetPanel, :count ).by(-1)
472       end
473       it '先頭は削除される' do
474         @item.destroy_and_shorten
475         lambda{
476           SheetPanel.find @item.id
477         }.should raise_error(ActiveRecord::RecordNotFound)
478       end
479       it '2件目は前に詰められる' do
480         @item.destroy_and_shorten
481         @item2.reload
482         @item2.t.should eq 0
483       end
484       it '3件目は前に詰められる' do
485         @item.destroy_and_shorten
486         @item3.reload
487         @item3.t.should eq 1
488       end
489     end
490     context '5件で3件目を削除したとき' do
491       before do
492         @item2 = FactoryGirl.create :sheet_panel, :t => 1, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
493         @item3 = FactoryGirl.create :sheet_panel, :t => 2, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
494         @item4 = FactoryGirl.create :sheet_panel, :t => 3, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
495         @item5 = FactoryGirl.create :sheet_panel, :t => 4, :sheet_id => @sheet.id, :panel_id => @panel.id, :author_id => @author.id
496       end
497       it '行が削除される' do
498         lambda{
499           @item3.destroy_and_shorten
500         }.should change(SheetPanel, :count ).by(-1)
501       end
502       it '1件目は変化がない' do
503         @item3.destroy_and_shorten
504         @item.reload
505         @item.t.should eq 0
506       end
507       it '2件目は変化がない' do
508         @item3.destroy_and_shorten
509         @item2.reload
510         @item2.t.should eq 1
511       end
512       it '3件目は削除される' do
513         @item3.destroy_and_shorten
514         lambda{
515           SheetPanel.find @item3.id
516         }.should raise_error(ActiveRecord::RecordNotFound)
517       end
518       it '4件目は前に詰められる' do
519         @item3.destroy_and_shorten
520         @item4.reload
521         @item4.t.should eq 2
522       end
523       it '5件目は前に詰められる' do
524         @item3.destroy_and_shorten
525         @item5.reload
526         @item5.t.should eq 3
527       end
528     end
529     #ロールバックテスト。切り詰めが直接DBをいじるので、すべてのケースで確実にロールバックを確認する
530   end
531 end
532