OSDN Git Service

Merge branch 'v06' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v06
[pettanr/pettanr.git] / spec / models / sheet_spec.rb
index 4cec7ed..a78a75e 100644 (file)
@@ -170,7 +170,7 @@ describe Sheet do
   
   describe '閲覧許可に於いて' do
     before do
-      @sheet = FactoryGirl.build :sheet, :author_id => @author.id
+      @sheet = FactoryGirl.create :sheet, :author_id => @author.id
     end
     context 'オープンモードのとき' do
       before do
@@ -664,6 +664,193 @@ describe Sheet do
     end
   end
   
+  describe '検証値収集に於いて' do
+    context 'つつがなく終わるとき' do
+      it '第一パラメータで指定された配列中から第二引数のカラム値を収集している' do
+        elements = [[{:sheet_id => 1, :a => 'a'}, {:sheet_id => 2, :a => 'a'}], 
+          [{:sheet_id => 3, :a => 'a'}, {:sheet_id => 4, :a => 'a'}]]
+        r = Sheet.collect_element_value elements, :sheet_id
+        r.should eq [1, 2, 3, 4]
+      end
+      it '第一パラメータで指定された配列中から第二引数のカラム値を収集している' do
+        elements = [[{:t => 1, :a => 'a'}, {:t => 2, :a => 'a'}], 
+          [{:t => 3, :a => 'a'}, {:t => 0, :a => 'a'}]]
+        r = Sheet.collect_element_value elements, :t
+        r.should eq [1, 2, 3, 0]
+      end
+    end
+  end
+  describe 'シリアライズチェックに於いて' do
+    context 'つつがなく終わるとき' do
+      it '0からシリアライズされているならTrueを返す' do
+        r = Sheet.validate_serial [0, 1, 2]
+        r.should be_true
+      end
+      it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
+        r = Sheet.validate_serial [0, 2, 1]
+        r.should be_true
+      end
+      it '見た目はシリアライズされてなくてもソート結果が無事ならtrueを返す' do
+        r = Sheet.validate_serial [ 2, 1, 4, 3, 0]
+        r.should be_true
+      end
+    end
+    context 'オフセットが1のとき' do
+      it '0からシリアライズされているならFalseを返す' do
+        r = Sheet.validate_serial [0, 1, 2], 1
+        r.should be_false
+      end
+      it '1からシリアライズされているならTrueを返す' do
+        r = Sheet.validate_serial [1, 2, 3], 1
+        r.should be_true
+      end
+    end
+    context '異常なとき' do
+      it '0から始まらないならFalseを返す' do
+        r = Sheet.validate_serial [1, 2, 3]
+        r.should be_false
+      end
+      it '連続していないならFalseを返す' do
+        r = Sheet.validate_serial [0, 1, 2, 4]
+        r.should be_false
+      end
+      it '連続していないならFalseを返す' do
+        r = Sheet.validate_serial [0, 1, 2, 4, 5]
+        r.should be_false
+      end
+    end
+  end
+  describe 'シリアライズチェック単体に於いて' do
+    before do
+    end
+    context 'つつがなく終わるとき' do
+      it '検証値収集を依頼している' do
+        Sheet.should_receive(:collect_element_value).with(any_args).exactly(1)
+        Sheet.stub(:collect_element_value).with(any_args).and_return([])
+        Sheet.stub(:validate_serial).with(any_args).and_return(true)
+        r = Sheet.validate_element_serial [], :t
+      end
+      it 'シリアライズチェック依頼している' do
+        Sheet.stub(:collect_element_value).with(any_args).and_return([])
+        Sheet.should_receive(:validate_serial).with(any_args).exactly(1)
+        Sheet.stub(:validate_serial).with(any_args).and_return(true)
+        r = Sheet.validate_element_serial [], :t
+      end
+    end
+  end
+  describe '従属データの検証に於いて' do
+    context 'つつがなく終わるとき' do
+      it 'trueを返している' do
+        @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+        @panel = FactoryGirl.create :panel, :author_id => @author.id
+        @sheet.sheet_panels.create(
+          FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0, :z => 0+1)
+        )
+        @sheet.sheet_panels.create(
+          FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
+        )
+        r = @sheet.validate_child
+        r.should be_true
+      end
+    end
+    context 'tシリアライズされていないとき' do
+      it 'falseを返している' do
+        @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+        @panel = FactoryGirl.create :panel, :author_id => @author.id
+        @sheet.sheet_panels.create(
+          FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 2, :z => 0+1)
+        )
+        @sheet.sheet_panels.create(
+          FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
+        )
+        r = @sheet.validate_child
+        r.should be_false
+      end
+    end
+    context 'zシリアライズされていないとき' do
+      it 'falseを返している' do
+        @sheet = FactoryGirl.create :sheet, :author_id => @author.id
+        @panel = FactoryGirl.create :panel, :author_id => @author.id
+        @sheet.sheet_panels.create(
+          FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 0, :z => 0+2)
+        )
+        @sheet.sheet_panels.create(
+          FactoryGirl.attributes_for(:sheet_panel, :sheet_id => @sheet.id, :panel_id => @panel.id, :t => 1, :z => 1+1)
+        )
+        r = @sheet.validate_child
+        r.should be_false
+      end
+    end
+  end
+  describe '保存に於いて' do
+    before do
+      @attr = FactoryGirl.attributes_for :sheet
+      @sheet = Sheet.new
+      @sheet.supply_default
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
+        Sheet.any_instance.stub(:save).with(any_args).and_return(true)
+      end
+      it 'コマモデルに上書き補充を依頼している' do
+        Sheet.any_instance.should_receive(:overwrite).exactly(1)
+        r = @sheet.store @attr, @author
+      end
+      it '保存を依頼している' do
+        Sheet.any_instance.should_receive(:save).with(any_args).exactly(1)
+        @sheet = FactoryGirl.build :sheet, :author_id => @author.id
+        r = @sheet.store @attr, @author
+      end
+      it '従属データの検証を依頼している' do
+        Sheet.any_instance.should_receive(:validate_child).with(any_args).exactly(1)
+        r = @sheet.store @attr, @author
+      end
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
+      end
+      it 'Trueを返す' do
+        r = @sheet.store @attr, @author
+        r.should be_true
+      end
+      it '行が追加されている' do
+        Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
+        lambda {
+          r = @sheet.store @attr, @author
+        }.should change(Sheet, :count)
+      end
+    end
+    context '不正なjsonデータのとき' do
+      before do
+      end
+      it 'エラーメッセージがセットされている' do
+        r = @sheet.store false, @author
+        @sheet.errors[:base].should_not be_blank
+      end
+    end
+    context '従属データの検証に失敗したとき' do
+      before do
+        Sheet.any_instance.stub(:save).with(any_args).and_return(true)
+        Sheet.any_instance.stub(:validate_child).with(any_args).and_return(false)
+      end
+      it 'エラーメッセージがセットされている' do
+        r = @sheet.store @attr, @author
+        @sheet.errors[:base].should_not be_blank
+      end
+    end
+    context 'カラム値がFalseしたとき' do
+      before do
+        Sheet.any_instance.stub(:validate_child).with(any_args).and_return(true)
+      end
+      it 'エラーメッセージがセットされている' do
+        r = @sheet.store false, @author
+        @sheet.errors.should_not be_empty
+      end
+    end
+  end
+  
   describe '削除に於いて' do
     before do
       @sheet = FactoryGirl.create :sheet, :author_id => @author.id