OSDN Git Service

fix: any
[pettanr/pettanr.git] / spec / models / comic_spec.rb
index f9978aa..755c7d0 100644 (file)
@@ -4,6 +4,7 @@ require 'spec_helper'
 
 describe Comic do
   before do
+    SpeechBalloonTemplate.delete_all
     @admin = FactoryGirl.create :admin
     @demand_user = FactoryGirl.create :demand_user
     @sp = FactoryGirl.create :system_picture
@@ -45,6 +46,7 @@ describe Comic do
         @comic.should_not be_valid
       end
     end
+    
     context 'visibleを検証するとき' do
       it 'nullなら失敗する' do
         @comic.visible = nil
@@ -59,6 +61,22 @@ describe Comic do
         @comic.should_not be_valid
       end
     end
+    
+    context 'author_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @comic.author_id = nil
+        @comic.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @comic.author_id = 'a'
+        @comic.should_not be_valid
+      end
+      it '存在する作家でなければ失敗する' do
+        @comic.author_id = 0
+        @comic.should_not be_valid
+      end
+    end
+    
   end
   
   describe '文字コード検証に於いて' do
@@ -441,71 +459,6 @@ describe Comic do
         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 '一覧取得オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = Comic.list_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = Comic.list_opt[:include]
-      r.should have(2).items
-    end
-    it 'ストーリーを含んでいる' do
-      r = Comic.list_opt[:include]
-      r.has_key?(:stories).should be_true
-    end
-      it 'ストーリーはコマを含んでいる' do
-        r = Comic.list_opt[:include]
-        r[:stories].has_key?(:panel).should be_true
-      end
-    it '作家を含んでいる' do
-      r = Comic.list_opt[:include]
-      r.has_key?(:author).should be_true
-    end
-  end
-  describe 'json一覧出力オプションに於いて' do
-    before do
-      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
-      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
-      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
-      @sbt = FactoryGirl.create :speech_balloon_template
-      @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
-      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
-      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
-    end
-    it 'ストーリーを含んでいる' do
-      r = Comic.list.to_json Comic.list_json_opt
-      j = JSON.parse r
-      i = j.first
-      i.has_key?('stories').should be_true
-    end
-      it 'ストーリーはコマを含んでいる' do
-        r = Comic.list.to_json Comic.list_json_opt
-        j = JSON.parse r
-        i = j.first
-        s = i['stories'].first
-        s.has_key?('panel').should be_true
-      end
-    it '作家を含んでいる' do
-      r = Comic.list.to_json Comic.list_json_opt
-      j = JSON.parse r
-      i = j.first
-      i.has_key?('author').should be_true
-    end
   end
   
   describe '自分のコミック一覧取得に於いて' do
@@ -563,21 +516,158 @@ describe Comic do
         c.should eq [@comic]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
+  end
+  
+  describe '他作家のコミック一覧取得に於いて' do
+    before do
+      @comic = FactoryGirl.create :comic, :author_id => @author.id
+      @other_comic = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 1
+    end
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        Comic.stub(:list_opt).with(any_args).and_return({})
+        Comic.should_receive(:list_opt).with(any_args).exactly(1)
+        r = Comic.himlist @other_author
+      end
+    end
+    it '指定した作家のリストを返す' do
+      r = Comic.himlist @other_author
+      r.should eq [@other_comic]
+    end
+    it '時系列で並んでいる' do
+      nc = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
+      r = Comic.himlist @other_author
+      r.should eq [nc, @other_comic]
+    end
+    it '公開コミックに限る ' do
+      hidden = FactoryGirl.create :comic, :author_id => @other_author.id, :visible => 0
+      r = Comic.himlist @other_author
+      r.should eq [@other_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
-        Author.stub(:default_comic_page_size).and_return(2)
+        @other_comic2 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 100
+        @other_comic3 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 200
+        @other_comic4 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 300
+        @other_comic5 = FactoryGirl.create :comic, :author_id => @other_author.id, :updated_at => Time.now + 400
       end
-      it '通常は全件(5件)を返す' do
-        r = Comic.mylist @author, 5, 0
-        r.should have(5).items 
+      it '通常は2件を返す' do
+        c = Comic.himlist @other_author, 1, 2
+        c.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        c = Comic.himlist(@other_author, 1, 2)
+        c.should eq [@other_comic5, @other_comic4]
+      end
+      it 'page=2なら中間2件を返す' do
+        c = Comic.himlist(@other_author, 2, 2)
+        c.should eq [@other_comic3, @other_comic2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        c = Comic.himlist(@other_author, 3, 2)
+        c.should eq [@other_comic]
       end
     end
   end
   
+  describe 'コミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.list_paginate 
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it 'コミック一覧の取得条件を利用している' do
+      Comic.stub(:list_where).with(any_args).and_return('')
+      Comic.should_receive(:list_where).with(any_args).exactly(1)
+      r = Comic.list_paginate 
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.list_paginate 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '自分のコミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.mylist_paginate @author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '自分のコミック一覧の取得条件を利用している' do
+      Comic.stub(:mylist_where).with(any_args).and_return('')
+      Comic.should_receive(:mylist_where).with(any_args).exactly(1)
+      r = Comic.mylist_paginate @author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.mylist_paginate @author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '他作家のコミック一覧ページ制御に於いて' do
+    before do
+      Comic.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = Comic.himlist_paginate @other_author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '他作家のコミック一覧の取得条件を利用している' do
+      Comic.stub(:himlist_where).with(any_args).and_return('')
+      Comic.should_receive(:himlist_where).with(any_args).exactly(1)
+      r = Comic.himlist_paginate @other_author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = Comic.himlist_paginate @other_author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '一覧取得オプションに於いて' do
+    it '2つの項目を含んでいる' do
+      r = Comic.list_opt
+      r.should have(2).items
+    end
+    it 'ストーリーを含んでいる' do
+      r = Comic.list_opt
+      r.has_key?(:stories).should be_true
+    end
+    it '作家を含んでいる' do
+      r = Comic.list_opt
+      r.has_key?(:author).should be_true
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    before do
+      @op = FactoryGirl.create :original_picture, :artist_id => @artist.id
+      @p = FactoryGirl.create :picture, :original_picture_id => @op.id, :license_id => @license.id, :artist_id => @artist.id
+      @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
+      @sbt = FactoryGirl.create :speech_balloon_template
+      @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
+      @story = FactoryGirl.create :story, :comic_id => @comic.id
+    end
+    it 'ストーリーを含んでいる' do
+      r = Comic.list.to_json Comic.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('stories').should be_true
+    end
+    it '作家を含んでいる' do
+      r = Comic.list.to_json Comic.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('author').should be_true
+    end
+  end
+  
   describe '単体取得に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
@@ -669,10 +759,6 @@ describe Comic 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
     before do
@@ -681,8 +767,7 @@ describe Comic do
       @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :license_id => @license.id, :original_picture_id => @op.id, :picture_id => @p.id
       @sbt = FactoryGirl.create :speech_balloon_template
       @comic = FactoryGirl.create :comic, :author_id => @author.id, :visible => 1
-      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
-      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
+      @story = FactoryGirl.create :story, :comic_id => @comic.id
     end
     it 'ストーリーを含んでいる' do
       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
@@ -690,13 +775,6 @@ describe Comic do
       i = j
       i.has_key?('stories').should be_true
     end
-      it 'ストーリーはコマを含んでいる' do
-        r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
-        j = JSON.parse r
-        i = j
-        s = i['stories'].first
-        s.has_key?('panel').should be_true
-      end
     it '作家を含んでいる' do
       r = Comic.show(@comic.id, @author).to_json Comic.show_json_opt
       j = JSON.parse r
@@ -708,10 +786,9 @@ describe Comic do
   describe '削除に於いて' do
     before do
       @comic = FactoryGirl.create :comic, :author_id => @author.id
-      @panel = FactoryGirl.create :panel, :author_id => @author.id, :publish => 1
-      @story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @comic.id, :panel_id => @panel.id
+      @story = FactoryGirl.create :story, :comic_id => @comic.id
       @other_comic = FactoryGirl.create :comic, :author_id => @author.id
-      @other_story = FactoryGirl.create :story, :author_id => @author.id, :comic_id => @other_comic.id, :panel_id => @panel.id
+      @other_story = FactoryGirl.create :story, :comic_id => @other_comic.id
     end
     context 'つつがなく終わるとき' do
       it '自身を削除する' do