OSDN Git Service

t#29400
[pettanr/pettanr.git] / spec / models / comic_spec.rb
index b4d9f42..f83e83f 100644 (file)
@@ -18,183 +18,109 @@ describe Comic do
   
   describe '検証に於いて' do
     before do
-    end
-    
-    it 'オーソドックスなデータなら通る' do
       @comic = FactoryGirl.build :comic, :author_id => @author.id
-      @comic.should be_valid
     end
     
-    context 'titleを検証するとき' do
-      it 'nullなら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :title => nil
-        @comic.should_not be_valid
-      end
-      it '100文字以上なら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :title => 'a'*101
-        @comic.should_not be_valid
-      end
-    end
-    context 'widthを検証するとき' do
-      it 'nullなら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :width => nil
-        @comic.should_not be_valid
-      end
-      it '0なら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :width => 0
-        @comic.should_not be_valid
-      end
-      it '負でも失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :width => -1
-        @comic.should_not be_valid
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @comic.title = 'a'
+        @comic.visible = 0
+        @comic.should be_valid
       end
-      it '正なら通る' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :width => 1
+      it '上限データが通る' do
+        @comic.title = 'a'*100
+        @comic.visible = 1
         @comic.should be_valid
       end
     end
-    context 'heightを検証するとき' do
+    
+    context 'titleを検証するとき' do
       it 'nullなら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :height => nil
-        @comic.should_not be_valid
-      end
-      it '0なら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :height => 0
+        @comic.title = nil
         @comic.should_not be_valid
       end
-      it '負でも失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :height => -1
+      it '100文字以上なら失敗する' do
+        @comic.title = 'a'*101
         @comic.should_not be_valid
       end
-      it '正なら通る' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :height => 1
-        @comic.should be_valid
-      end
     end
     context 'visibleを検証するとき' do
       it 'nullなら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :visible => nil
+        @comic.visible = nil
         @comic.should_not be_valid
       end
       it '負なら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :visible => -1
+        @comic.visible = -1
         @comic.should_not be_valid
       end
-      it '4以上なら失敗する' do
-        @comic = FactoryGirl.build :comic, :author_id => @author.id, :visible => 4
+      it '2以上なら失敗する' do
+        @comic.visible = 2
         @comic.should_not be_valid
       end
     end
   end
   
-  describe 'データ補充に於いて' do
-    before do
-      @comic = Comic.new
+  describe 'デフォルト値補充に於いて' do
+    it 'visibleが0になっている' do
+      @comic = FactoryGirl.build :comic, :visible => nil
+      @comic.supply_default
+      @comic.visible.should eq 0
     end
-    
-    context '初期値を補充するとき' do
-      it '空なら0が補充される' do
-        @comic.supply_default
-        @comic.visible.should == 0
-      end
-      it 'visibleが空でないなら変化なし' do
-        @comic.visible = 1
-        lambda{@comic.supply_default}.should_not change(@comic, :visible)
-      end
+  end
+  
+  describe '上書き補充に於いて' do
+    it '作家idが設定されている' do
+      @comic = FactoryGirl.build :comic, :author_id => nil
+      @comic.overwrite @author
+      @comic.author_id.should eq @author.id
     end
   end
   
-  describe '作者判定に於いて' do
+  describe '所持判定に於いて' do
     before do
+      @comic = FactoryGirl.build :comic, :author_id => @author.id
     end
-    it '自作のコミックならyes' do
-      comic = FactoryGirl.create :comic, :author_id => @author.id
-      comic.own?(@author).should == true
+    it '自分のコミックならyes' do
+      @comic.own?(@author).should == true
     end
     it '他人のコミックならno' do
-      comic = FactoryGirl.create :comic, :author_id => 0
-      comic.own?(@author).should == false
+      @comic.own?(@other_author).should == false
     end
     it '作家が不明ならno' do
-      comic = FactoryGirl.create :comic, :author_id => @author.id
-      comic.own?(nil).should == false
+      @comic.own?(nil).should == false
     end
   end
+  
   describe '閲覧許可に於いて' do
     before do
+      @comic = FactoryGirl.build :comic, :author_id => @author.id
     end
-    it '自作の公開コミックを見るときは許可する' do
-      Comic.any_instance.stub(:own?).and_return(true)
-      comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 3
-      comic.visible?(@author).should == true
-    end
-    it '自作のコミックは非公開でも許可する' do
-      Comic.any_instance.stub(:own?).and_return(true)
-      comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 0
-      comic.visible?(@author).should == true
-    end
-    it '他人のコミックでも公開コミックなら許可する' do
-      Comic.any_instance.stub(:own?).and_return(false)
-      comic = FactoryGirl.create :comic, :author_id => 0, :visible => 3
-      comic.visible?(@author).should == true
-    end
-    it '他人のコミックで非公開コミックなら許可しない' do
-      Comic.any_instance.stub(:own?).and_return(false)
-      comic = FactoryGirl.create :comic, :author_id => 0, :visible => 0
-      comic.visible?(@author).should == false
-    end
-  end
-  describe '単体取得に於いて' do
-    before do
-      @comic = FactoryGirl.create :comic, :author_id => @author.id
-    end
-    it '指定のコミックを返す' do
-      c = Comic.show @comic.id, @author.id
-      c.should eq @comic
-    end
-  end
-  describe '関連テーブルプションに於いて' do
-    context 'オプションがないとき' do
-      it '2つの項目を含んでいる' do
-        r = Comic.show_include_opt
-        r.should have(2).items
-      end
-      it '作家を含んでいる' do
-        r = Comic.show_include_opt
-        r.has_key?(:author).should be_true
-      end
-      it 'ストーリーを含んでいる' do
-        r = Comic.show_include_opt
-        r.has_key?(:stories).should be_true
-      end
-        it 'ストーリーはコマを含んでいる' do
-          r = Comic.show_include_opt
-          r[:stories].has_key?(:panel).should be_true
-        end
-    end
-  end
-  describe 'json単体出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = Comic.show_json_include_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = Comic.show_json_include_opt[:include]
-      r.should have(2).items
-    end
-    it '作家を含んでいる' do
-      r = Comic.show_json_include_opt[:include]
-      r.has_key?(:author).should be_true
-    end
-    it 'ストーリーを含んでいる' do
-      r = Comic.show_json_include_opt[:include]
-      r.has_key?(:stories).should be_true
-    end
-      it 'ストーリーはコマを含んでいる' do
-        r = Comic.show_json_include_opt[:include]
-        r[:stories].has_key?(:panel).should be_true
-      end
+    it '自分の公開コミックを見るときは許可する' do\r
+      Comic.any_instance.stub(:own?).and_return(true)\r
+      Comic.any_instance.stub(:visible).and_return(1)\r
+      r = @comic.visible?(@author)
+      r.should == true
+    end\r
+    it '自分のコミックなら非公開でも許可する' do\r
+      Comic.any_instance.stub(:own?).and_return(true)\r
+      Comic.any_instance.stub(:visible).and_return(0)\r
+      r = @comic.visible?(@author)
+      r.should == true
+    end\r
+    it '他人のコミックでも公開なら許可する' do\r
+      Comic.any_instance.stub(:own?).and_return(false)\r
+      Comic.any_instance.stub(:visible).and_return(1)\r
+      r = @comic.visible?(@author)
+      r.should == true
+    end\r
+    it '他人のコミックで非公開なら許可しない' do\r
+      Comic.any_instance.stub(:own?).and_return(false)\r
+      Comic.any_instance.stub(:visible).and_return(0)\r
+      r = @comic.visible?(@author)
+      r.should == false
+    end\r
   end
+  
   describe '一覧取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
@@ -224,6 +150,13 @@ describe Comic do
         Comic.page_size('1000').should eq Comic.max_page_size
       end
     end
+    context 'つつがなく終わるとき' do\r
+      it '一覧取得オプションを利用している' do\r
+        Comic.stub(:list_opt).with(any_args).and_return({})\r
+        Comic.should_receive(:list_opt).with(any_args).exactly(1)\r
+        r = Comic.list
+      end\r
+    end\r
     it 'リストを返す' do
       c = Comic.list
       c.should eq [@comic]
@@ -234,7 +167,8 @@ describe Comic do
       c.should eq [@comic]
     end
     it '時系列で並んでいる' do
-      v = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
+      #公開コミックは(他人のコミックであっても)含んでいる
+      v = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
       c = Comic.list
       c.should eq [v, @comic]
     end
@@ -252,18 +186,31 @@ describe Comic do
       end
       it 'page=1なら末尾2件を返す' do
         #時系列で並んでいる
-        c = Comic.list({}, 1)
+        c = Comic.list(1)
         c.should eq [@comic5, @comic4]
       end
       it 'page=2なら中間2件を返す' do
-        c = Comic.list({}, 2)
+        c = Comic.list(2)
         c.should eq [@comic3, @comic2]
       end
       it 'page=3なら先頭1件を返す' do
-        c = Comic.list({}, 3)
+        c = Comic.list(3)
         c.should eq [@comic]
       end
     end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
+        @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
+        @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
+        @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
+        Comic.stub(:default_page_size).and_return(2)
+      end
+      it '件数0は全件(5件)を返す' do
+        r = Comic.list 5, 0
+        r.should have(5).items 
+      end
+    end
   end
   describe 'list関連テーブルプションに於いて' do
     it 'includeキーを含んでいる' do
@@ -314,6 +261,13 @@ describe Comic do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
     end
+    context 'つつがなく終わるとき' do\r
+      it '一覧取得オプションを利用している' do\r
+        Comic.stub(:list_opt).with(any_args).and_return({})\r
+        Comic.should_receive(:list_opt).with(any_args).exactly(1)\r
+        r = Comic.mylist @author
+      end\r
+    end\r
     it 'リストを返す' do
       c = Comic.mylist @author
       c.should eq [@comic]
@@ -333,6 +287,162 @@ describe Comic do
       cl = Comic.mylist @author
       cl.should eq [nc, @comic]
     end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
+        @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
+        @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
+        @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
+      end
+      it '通常は2件を返す' do
+        c = Comic.mylist @author, 1, 2
+        c.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        c = Comic.mylist(@author, 1, 2)
+        c.should eq [@comic5, @comic4]
+      end
+      it 'page=2なら中間2件を返す' do
+        c = Comic.mylist(@author, 2, 2)
+        c.should eq [@comic3, @comic2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        c = Comic.mylist(@author, 3, 2)
+        c.should eq [@comic]
+      end
+    end
+    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+      before do
+        @comic2 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 100
+        @comic3 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 200
+        @comic4 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 300
+        @comic5 = FactoryGirl.create :comic, :author_id => @author.id, :updated_at => Time.now + 400
+        Comic.stub(:default_page_size).and_return(2)
+      end
+      it '通常は全件(5件)を返す' do
+        r = Comic.mylist @author, 5, 0
+        r.should have(5).items 
+      end
+    end
   end
   
+  describe '単体取得に於いて' do
+    before do
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+    end
+    context 'つつがなく終わるとき' do\r
+      it '単体取得オプションを利用している' do\r
+        Comic.stub(:show_opt).with(any_args).and_return({})\r
+        Comic.should_receive(:show_opt).with(any_args).exactly(1)\r
+        r = Comic.show @comic.id, @author
+      end\r
+      it '閲覧許可を問い合わせている' do\r
+        Comic.any_instance.stub(:visible?).with(any_args).and_return(true)\r
+        Comic.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
+        r = Comic.show @comic.id, @author
+      end\r
+    end\r
+    it '指定のコミックを返す' do
+      c = Comic.show @comic.id, @author.id
+      c.should eq @comic
+    end
+    context '閲覧許可が出なかったとき' do\r
+      it '403Forbidden例外を返す' do\r
+        Comic.any_instance.stub(:visible?).and_return(false)\r
+        lambda{\r
+          Comic.show @comic.id, @author\r
+        }.should raise_error(ActiveRecord::Forbidden)\r
+      end\r
+    end\r
+    context '存在しない作家を開こうとしたとき' do\r
+      it '404RecordNotFound例外を返す' do\r
+        lambda{\r
+          Comic.show 110, @author\r
+        }.should raise_error(ActiveRecord::RecordNotFound)\r
+      end\r
+    end\r
+  end
+  
+  describe '編集取得に於いて' do
+    before do
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+    end
+    context 'つつがなく終わるとき' do\r
+      it '単体取得オプションを利用している' do\r
+        Comic.stub(:show_opt).with(any_args).and_return({})\r
+        Comic.should_receive(:show_opt).with(any_args).exactly(1)\r
+        r = Comic.edit @comic.id, @author
+      end\r
+      it '所持判定を問い合わせている' do\r
+        Comic.any_instance.stub(:own?).with(any_args).and_return(true)\r
+        Comic.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
+        r = Comic.edit @comic.id, @author
+      end\r
+    end\r
+    it '指定のコミックを返す' do
+      Comic.any_instance.stub(:own?).and_return(true)
+      c = Comic.edit @comic.id, @author.id
+      c.should eq @comic
+    end
+    context '他人のコミックを開こうとしたとき' do
+      it '403Forbidden例外を返す' do
+        Comic.any_instance.stub(:own?).and_return(false)
+        lambda{
+          Comic.edit @comic.id, @author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しないコミックを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          Comic.edit 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
+  end
+  describe '関連テーブルプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = Comic.show_opt
+      r.has_key?(:include).should be_true
+    end
+    it '2つの項目を含んでいる' do
+      r = Comic.show_opt[:include]
+      r.should have(2).items
+    end
+    it '作家を含んでいる' do
+      r = Comic.show_opt[:include]
+      r.has_key?(:author).should be_true
+    end
+    it 'ストーリーを含んでいる' do
+      r = Comic.show_opt[:include]
+      r.has_key?(:stories).should be_true
+    end
+      it 'ストーリーはコマを含んでいる' do
+        r = Comic.show_opt[:include]
+        r[:stories].has_key?(:panel).should be_true
+      end
+  end
+  describe 'json単体出力オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = Comic.show_json_opt
+      r.has_key?(:include).should be_true
+    end
+    it '2つの項目を含んでいる' do
+      r = Comic.show_json_opt[:include]
+      r.should have(2).items
+    end
+    it '作家を含んでいる' do
+      r = Comic.show_json_opt[:include]
+      r.has_key?(:author).should be_true
+    end
+    it 'ストーリーを含んでいる' do
+      r = Comic.show_json_opt[:include]
+      r.has_key?(:stories).should be_true
+    end
+      it 'ストーリーはコマを含んでいる' do
+        r = Comic.show_json_opt[:include]
+        r[:stories].has_key?(:panel).should be_true
+      end
+  end
 end