OSDN Git Service

Merge branch 'v03_test' of git.sourceforge.jp:/gitroot/pettanr/pettanr into v03_test
[pettanr/pettanr.git] / spec / models / panel_spec.rb
index ad5959d..2b07d60 100644 (file)
@@ -1,5 +1,664 @@
+# -*- encoding: utf-8 -*-\r
 require 'spec_helper'
 
 describe Panel do
-  pending "add some examples to (or delete) #{__FILE__}"
+  before do
+    Factory :admin
+    @license = Factory :license
+    @user = Factory( :user_yas)
+    @author = @user.author
+    @artist = Factory :artist_yas, :author_id => @author.id\r
+    @other_user = Factory( :user_yas)
+    @other_author = @other_user.author
+    @other_artist = Factory :artist_yas, :author_id => @other_author.id
+    
+  end\r
+  
+  describe '検証に於いて' do
+    before do\r
+      @comic = Factory :comic, :author_id => @author.id
+    end
+    
+    it 'オーソドックスなデータなら通る' do
+      @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      @panel.should be_valid
+    end
+    
+    context 'comic_idを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => nil
+      end
+      it 'テストデータの確認' do
+        @panel.comic_id = @comic.id
+        @panel.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @panel.comic_id = nil
+        @panel.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.comic_id = 'a'
+        @panel.should_not be_valid
+      end
+      it '存在するコミックでなければ失敗する' do
+        @panel.comic_id = 0
+        @panel.should_not be_valid
+      end
+      it 'コミックidとtが重複していると失敗する' do
+        @panel.save\r
+        @panel2 = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+        @panel2.should_not be_valid
+      end
+    end
+    context 'resource_picture_idを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.resource_picture_id = 1
+        @panel.should be_valid
+      end
+      it 'nullなら通る' do
+        @panel.resource_picture_id = nil
+        @panel.should be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.resource_picture_id = 'a'
+        @panel.should_not be_valid
+      end
+      it '存在する素材でなければ失敗する' do
+        @panel.resource_picture_id = 0
+        @panel.should_not be_valid
+      end
+    end
+    context 'widthを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.width = 1
+        @panel.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @panel.width = nil
+        @panel.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.width = 'a'
+        @panel.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @panel.width = '0'
+        @panel.should_not be_valid
+      end
+      it '負でも失敗する' do
+        @panel.width = -1
+        @panel.should_not be_valid
+      end
+    end
+    context 'heightを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.height = '1'
+        @panel.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @panel.height = nil
+        @panel.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.height = 'a'
+        @panel.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @panel.height = '0'
+        @panel.should_not be_valid
+      end
+      it '負でも失敗する' do
+        @panel.height = -1
+        @panel.should_not be_valid
+      end
+    end
+    context 'borderを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.border = '1'
+        @panel.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @panel.border = nil
+        @panel.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.border = 'a'
+        @panel.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @panel.border = '-1'
+        @panel.should_not be_valid
+      end
+      it '0なら通る' do
+        @panel.border = 0
+        @panel.should be_valid
+      end
+    end
+    context 'xを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.x = '1'
+        @panel.should be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.x = 'a'
+        @panel.should_not be_valid
+      end
+      it '0なら通る' do
+        @panel.x = '0'
+        @panel.should be_valid
+      end
+      it '負でも通る' do
+        @panel.x = -1
+        @panel.should be_valid
+      end
+    end
+    context 'yを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.y = '1'
+        @panel.should be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.y = 'a'
+        @panel.should_not be_valid
+      end
+      it '0なら通る' do
+        @panel.y = '0'
+        @panel.should be_valid
+      end
+      it '負でも通る' do
+        @panel.y = -1
+        @panel.should be_valid
+      end
+    end
+    context 'zを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.z = '1'
+        @panel.should be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.z = 'a'
+        @panel.should_not be_valid
+      end
+      it '0なら失敗する' do
+        @panel.z = '0'
+        @panel.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @panel.z = -1
+        @panel.should_not be_valid
+      end
+    end
+    context 'tを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.t = '1'
+        @panel.should be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.t = 'a'
+        @panel.should_not be_valid
+      end
+      it '0なら通る' do
+        @panel.t = '0'
+        @panel.should be_valid
+      end
+      it '負でも失敗する' do
+        @panel.t = -1
+        @panel.should_not be_valid
+      end
+    end
+    context 'author_idを検証するとき' do
+      before do
+        @panel = Factory.build :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+      it 'テストデータの確認' do
+        @panel.author_id = @author.id
+        @panel.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @panel.author_id = nil
+        @panel.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @panel.author_id = 'a'
+        @panel.should_not be_valid
+      end
+      it '存在する絵師でなければ失敗する' do
+        @panel.author_id = 0
+        @panel.should_not be_valid
+      end
+    end
+    context '全体を検証するとき' do
+      before do
+        @panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
+      end
+    end
+  end
+  
+  describe 'データ補充に於いて' do
+    before do\r
+      @comic = Factory :comic, :author_id => @author.id
+      @panel = Factory.build :panel, :comic_id => @comic.id
+    end
+    context 'widthを補充' do
+      it '空の時はコミックから補充する' do\r
+        @panel.width = nil
+        @panel.supply_default @author\r
+        @panel.width.should eq @comic.width
+      end
+      it '空の時でもコミックが不在なら補充しない' do\r
+        @panel.width = nil
+        @panel.comic_id = nil
+        @panel.supply_default @author\r
+        @panel.width.should be_nil
+      end
+      it '空でない時は変化しない' do
+        @panel.width = 45\r
+        lambda {
+          @panel.supply_default @author\r
+        }.should_not change @panel, :width
+      end
+    end
+    context 'heightを補充' do
+      it '空の時はコミックから補充する' do
+        @panel.height = nil
+        @panel.supply_default @author\r
+        @panel.height.should eq @comic.height
+      end
+      it '空の時でもコミックが不在なら補充しない' do\r
+        @panel.height = nil
+        @panel.comic_id = nil
+        @panel.supply_default @author\r
+        @panel.height.should be_nil
+      end
+      it '空でない時は変化しない' do
+        @panel.height = 87\r
+        lambda {
+          @panel.supply_default @author\r
+        }.should_not change @panel, :height
+      end
+    end
+    context 'borderを補充' do
+      it '空の時は0を補充する' do
+        @panel.border = nil
+        @panel.supply_default @author\r
+        @panel.border.should eq 0
+      end
+      it '空でない時は変化しない' do
+        @panel.border = 1\r
+        lambda {
+          @panel.supply_default @author\r
+        }.should_not change @panel, :border
+      end
+    end
+    context 'tを補充' do
+      it '空の時はコミック内のtの最大値+1を補充する' do
+        pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 0\r
+        @panel.t = nil
+        @panel.supply_default @author\r
+        @panel.t.should eq 1
+      end
+      it '空でコミック初のコマなら0を補充する' do
+        @panel.t = nil
+        @panel.supply_default @author\r
+        @panel.t.should eq 0
+      end
+      it '空の時でも更新ケースなら補充しない' do
+        pl = Factory :panel, :author_id => @author.id, :comic_id => @comic.id, :t => 1\r
+        pl.t = nil\r
+        lambda {
+          pl.supply_default @author\r
+        }.should_not change pl, :t
+      end
+      it '空でない時は変化しない' do
+        @panel.t = 1\r
+        lambda {
+          @panel.supply_default @author\r
+        }.should_not change @panel, :t
+      end
+    end
+    context 'author_idを補充' do
+      it 'ログイン中の作家idを補充する' do
+        @panel.author_id = nil
+        @panel.supply_default @author\r
+        @panel.author_id.should eq @author.id
+      end
+    end
+    
+  end
+  \r
+  describe '作者判定に於いて' do
+    before do
+      @comic = Factory :comic, :author_id => @author.id
+    end
+    it '自作のコマならyes' do
+      panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
+      panel.own?(@author).should == true
+    end
+    it '他人のコマならno' do
+      panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id
+      panel.own?(@author).should == false
+    end
+    it '作家が不明ならno' do
+      panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
+      panel.own?(nil).should == false
+    end
+  end
+  describe '閲覧許可に於いて' do
+    before do
+      @comic = Factory :comic, :author_id => @author.id
+    end
+    it '自作の公開コミックのコマを見るときは許可する' do
+      Comic.any_instance.stub(:visible?).and_return(true)
+      panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
+      panel.visible?(@author).should == true
+    end
+    it '自作のコマは非公開コミックでも許可する' do
+      Comic.any_instance.stub(:visible?).and_return(false)
+      panel = Factory :panel, :author_id => @author.id, :comic_id => @comic.id
+      panel.visible?(@author).should == true
+    end
+    it '他人のコマでも公開コミックなら許可する' do
+      Comic.any_instance.stub(:visible?).and_return(true)
+      panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id
+      panel.visible?(@author).should == true
+    end
+    it '他人のコマで非公開コミックなら許可しない' do
+      Comic.any_instance.stub(:visible?).and_return(false)
+      panel = Factory :panel, :author_id => @other_author.id, :comic_id => @comic.id
+      panel.visible?(@author).should == false
+    end
+  end
+  describe '単体取得に於いて' do
+    before do
+      @comic = Factory :comic, :author_id => @author.id
+      @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id
+    end
+    it '指定のコマを返す' do
+      Comic.any_instance.stub(:visible?).and_return(true)
+      pl = Panel.show @panel.id, @author
+      pl.should eq @panel
+    end
+    context '他人の非公開コミックのコマを開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        Panel.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          Panel.show @panel.id, @author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しないコマを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          Panel.show 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '関連テーブルプションに於いて' do
+    context 'オプションがないとき' do
+      it '4つの項目を含んでいる' do
+        r = Panel.show_include_opt
+        r.should have(4).items\r
+      end
+      it 'コミックを含んでいる' do
+        r = Panel.show_include_opt
+        r.has_key?(:comic).should be_true\r
+      end
+      it 'コマ絵を含んでいる' do
+        r = Panel.show_include_opt
+        r.has_key?(:panel_pictures).should be_true\r
+      end
+        it 'コマ絵は素材を含んでいる' do
+          r = Panel.show_include_opt
+          r[:panel_pictures].has_key?(:resource_picture).should be_true\r
+        end
+          it '素材は絵師を含んでいる' do
+            r = Panel.show_include_opt
+            r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
+          end
+          it '素材はライセンスを含んでいる' do
+            r = Panel.show_include_opt
+            r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
+          end
+      it 'フキダシを含んでいる' do
+        r = Panel.show_include_opt
+        r.has_key?(:balloons).should be_true\r
+      end
+        it 'フキダシはセリフを含んでいる' do
+          r = Panel.show_include_opt
+          r[:balloons].has_key?(:speeches).should be_true\r
+        end
+      it '作家を含んでいる' do
+        r = Panel.show_include_opt
+        r.has_key?(:author).should be_true\r
+      end
+    end
+    context 'オプションで原画を含ませたとき' do
+      it '5つの項目を含んでいる' do
+        r = Panel.show_include_opt(:include => {:test => {}})
+        r.should have(5).items\r
+      end
+      it 'testを含んでいる' do
+        r = Panel.show_include_opt(:include => {:test => {}})
+        r.has_key?(:test).should be_true\r
+      end
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = Panel.show_json_include_opt
+      r.has_key?(:include).should be_true
+    end
+    it '4つの項目を含んでいる' do
+      r = Panel.show_json_include_opt[:include]
+      r.should have(4).items\r
+    end
+    it 'コミックを含んでいる' do
+      r = Panel.show_json_include_opt[:include]
+      r.has_key?(:comic).should be_true\r
+    end
+    it 'コマ絵を含んでいる' do
+      r = Panel.show_json_include_opt[:include]
+      r.has_key?(:panel_pictures).should be_true\r
+    end
+      it 'コマ絵は素材を含んでいる' do
+        r = Panel.show_json_include_opt[:include]
+        r[:panel_pictures].has_key?(:resource_picture).should be_true\r
+      end
+        it '素材は絵師を含んでいる' do
+          r = Panel.show_json_include_opt[:include]
+          r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
+        end
+        it '素材はライセンスを含んでいる' do
+          r = Panel.show_json_include_opt[:include]
+          r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
+        end
+    it 'フキダシを含んでいる' do
+      r = Panel.show_json_include_opt[:include]
+      r.has_key?(:balloons).should be_true\r
+    end
+      it 'フキダシはセリフを含んでいる' do
+        r = Panel.show_json_include_opt[:include]
+        r[:balloons].has_key?(:speeches).should be_true\r
+      end
+    it '作家を含んでいる' do
+      r = Panel.show_json_include_opt[:include]
+      r.has_key?(:author).should be_true\r
+    end
+  end
+  describe '一覧取得に於いて' do
+    before do
+      @comic = Factory :comic, :author_id => @author.id
+      @panel = Factory :panel, :comic_id => @comic.id, :author_id => @author.id
+    end
+    context 'page補正について' do
+      it '文字列から数値に変換される' do
+        Panel.page('8').should eq 8
+      end
+      it 'nilの場合は1になる' do
+        Panel.page().should eq 1
+      end
+      it '0以下の場合は1になる' do
+        Panel.page('0').should eq 1
+      end
+    end
+    context 'page_size補正について' do
+      it '文字列から数値に変換される' do
+        Panel.page_size('7').should eq 7
+      end
+      it 'nilの場合はPanel.default_page_sizeになる' do
+        Panel.page_size().should eq Panel.default_page_size
+      end
+      it '0以下の場合はPanel.default_page_sizeになる' do
+        Panel.page_size('0').should eq Panel.default_page_size
+      end
+      it 'Panel.max_page_sizeを超えた場合はPanel.max_page_sizeになる' do
+        Panel.page_size('1000').should eq Panel.max_page_size
+      end
+    end
+    it 'リストを返す' do
+      pl = Panel.list
+      pl.should eq [@panel]
+    end
+    it '時系列で並んでいる' do
+      npl = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1
+      pl = Panel.list
+      pl.should eq [npl, @panel]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @npl2 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 1
+        @npl3 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 2
+        @npl4 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 3
+        @npl5 = Factory :panel, :comic_id => @comic.id, :author_id => @author.id, :t => 4
+        Panel.stub(:default_page_size).and_return(2)
+      end
+      it '通常は2件を返す' do
+        pl = Panel.list
+        pl.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        pl = Panel.list( {}, 1)
+        pl.should eq [@npl5, @npl4]
+      end
+      it 'page=2なら中間2件を返す' do
+        pl = Panel.list({}, 2)
+        pl.should eq [@npl3, @npl2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        pl = Panel.list({}, 3)
+        pl.should eq [@panel]
+      end
+    end
+  end
+  describe 'list関連テーブルプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = Panel.list_opt
+      r.has_key?(:include).should be_true
+    end
+    it '4つの項目を含んでいる' do
+      r = Panel.list_opt[:include]
+      r.should have(4).items\r
+    end
+    it 'コミックを含んでいる' do
+      r = Panel.list_opt[:include]
+      r.has_key?(:comic).should be_true\r
+    end
+    it 'コマ絵を含んでいる' do
+      r = Panel.list_opt[:include]
+      r.has_key?(:panel_pictures).should be_true\r
+    end
+      it 'コマ絵は素材を含んでいる' do
+        r = Panel.list_opt[:include]
+        r[:panel_pictures].has_key?(:resource_picture).should be_true\r
+      end
+        it '素材は絵師を含んでいる' do
+          r = Panel.list_opt[:include]
+          r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
+        end
+        it '素材はライセンスを含んでいる' do
+          r = Panel.list_opt[:include]
+          r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
+        end
+    it 'フキダシを含んでいる' do
+      r = Panel.list_opt[:include]
+      r.has_key?(:balloons).should be_true\r
+    end
+      it 'フキダシはセリフを含んでいる' do
+        r = Panel.list_opt[:include]
+        r[:balloons].has_key?(:speeches).should be_true\r
+      end
+    it '作家を含んでいる' do
+      r = Panel.list_opt[:include]
+      r.has_key?(:author).should be_true\r
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = Panel.list_json_opt
+      r.has_key?(:include).should be_true
+    end
+    it '4つの項目を含んでいる' do
+      r = Panel.list_json_opt[:include]
+      r.should have(4).items\r
+    end
+    it 'コミックを含んでいる' do
+      r = Panel.list_json_opt[:include]
+      r.has_key?(:comic).should be_true\r
+    end
+    it 'コマ絵を含んでいる' do
+      r = Panel.list_json_opt[:include]
+      r.has_key?(:panel_pictures).should be_true\r
+    end
+      it 'コマ絵は素材を含んでいる' do
+        r = Panel.list_json_opt[:include]
+        r[:panel_pictures].has_key?(:resource_picture).should be_true\r
+      end
+        it '素材は絵師を含んでいる' do
+          r = Panel.list_json_opt[:include]
+          r[:panel_pictures][:resource_picture].has_key?(:artist).should be_true\r
+        end
+        it '素材はライセンスを含んでいる' do
+          r = Panel.list_json_opt[:include]
+          r[:panel_pictures][:resource_picture].has_key?(:license).should be_true\r
+        end
+    it 'フキダシを含んでいる' do
+      r = Panel.list_json_opt[:include]
+      r.has_key?(:balloons).should be_true\r
+    end
+      it 'フキダシはセリフを含んでいる' do
+        r = Panel.list_json_opt[:include]
+        r[:balloons].has_key?(:speeches).should be_true\r
+      end
+    it '作家を含んでいる' do
+      r = Panel.list_json_opt[:include]
+      r.has_key?(:author).should be_true\r
+    end
+  end\r
+  
 end