OSDN Git Service

t#31486:add t and caption on ground
[pettanr/pettanr.git] / spec / models / ground_color_spec.rb
index 9ca2979..8b979b7 100644 (file)
@@ -1,6 +1,6 @@
 # -*- encoding: utf-8 -*-
 require 'spec_helper'
-#選択色地
+#色地
 
 describe GroundColor do
   before do
@@ -18,20 +18,21 @@ describe GroundColor do
     @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
     @panel = FactoryGirl.create :panel, :author_id => @author.id
-    @color = FactoryGirl.create :color
   end
   
   describe '検証に於いて' do
     before do
-      @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
     end
     
     context 'オーソドックスなデータのとき' do
       it '下限データが通る' do
+        @gc.code = 0
         @gc.z = 1
         @gc.should be_valid
       end
       it '上限データが通る' do
+        @gc.code = 99999
         @gc.z = 99999
         @gc.should be_valid
       end
@@ -44,17 +45,25 @@ describe GroundColor do
         @gc.should_not be_valid
       end
     end
-    context 'color_idを検証するとき' do
+    context 'codeを検証するとき' do
+      it 'テストデータの確認' do
+        @gc.code = 0xffffff
+        @gc.should be_valid
+      end
       it 'nullなら失敗する' do
-        @gc.color_id = nil
+        @gc.code = nil
         @gc.should_not be_valid
       end
       it '数値でなければ失敗する' do
-        @gc.color_id = 'a'
+        @gc.code = 'a'
+        @gc.should_not be_valid
+      end
+      it '負なら失敗する' do
+        @gc.code = -1
         @gc.should_not be_valid
       end
-      it '存在する色でなければ失敗する' do
-        @gc.color_id = 0
+      it '24bit colorでなければ失敗する' do
+        @gc.code = 0x1000000
         @gc.should_not be_valid
       end
     end
@@ -78,23 +87,39 @@ describe GroundColor do
     end
   end
   
+  describe '文字コード検証に於いて' do
+    before do
+      @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
+    end
+    
+    context 'captionを検証するとき' do
+      it 'Shift JISなら失敗する' do
+        @gc.caption = "\x83G\x83r\x83]\x83D"
+        lambda{
+          @gc.valid_encode
+        }.should raise_error(Pettanr::BadRequest)
+      end
+    end
+  end
+  
   describe 'デフォルト値補充に於いて' do
     it 'defined' do
-      @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
       @gc.supply_default
     end
   end
   
   describe '上書き補充に於いて' do
-    it 'defined' do
-      @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id, :color_id => @color.id
-      @gc.overwrite
+    it 'panel_idが設定されている' do
+      @gc = FactoryGirl.build :ground_color, :panel_id => @panel.id
+      @gc.overwrite  @panel.id
+      @gc.panel_id.should eq @panel.id
     end
   end
   
   describe '閲覧許可に於いて' do
     before do
-      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
     end
     context 'オープンモードのとき' do
       before do
@@ -148,19 +173,19 @@ describe GroundColor do
         MagicNumber['run_mode'] = 1
         Panel.any_instance.stub(:reader_role_check).and_return(true)
       end
-      it '自分のコマの選択色地なら許可する' do
+      it '自分のコマの色地なら許可する' do
         Panel.any_instance.stub(:own?).and_return(true)
         Panel.any_instance.stub(:visible?).and_return(false)
         r = @gc.visible?([@author])
         r.should be_true
       end
-      it '他人の非公開コマの選択色地なら許可しない' do
+      it '他人の非公開コマの色地なら許可しない' do
         Panel.any_instance.stub(:own?).and_return(false)
         Panel.any_instance.stub(:visible?).and_return(false)
         r = @gc.visible?([@author])
         r.should be_false
       end
-      it '他人のコマの選択色地でも公開なら許可する' do
+      it '他人のコマの色地でも公開なら許可する' do
         Panel.any_instance.stub(:own?).and_return(false)
         Panel.any_instance.stub(:visible?).and_return(true)
         r = @gc.visible?([@author])
@@ -171,7 +196,7 @@ describe GroundColor do
   
   describe '一覧取得に於いて' do
     before do
-      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
     end
     context 'page補正について' do
       it '文字列から数値に変換される' do
@@ -212,22 +237,22 @@ describe GroundColor do
     it '時系列で並んでいる' do
       #公開コマなら(他人のコマであっても)含んでいる
       opl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
-      npl = FactoryGirl.create :ground_color, :panel_id => opl.id, :color_id => @color.id, :updated_at => Time.now + 100
+      npl = FactoryGirl.create :ground_color, :panel_id => opl.id, :updated_at => Time.now + 100
       pl = GroundColor.list
       pl.should eq [npl, @gc]
     end
-    it '非公開のコマの選択色地は含まない' do
+    it '非公開のコマの色地は含まない' do
       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
-      npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :color_id => @color.id, :updated_at => Time.now + 100
+      npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :updated_at => Time.now + 100
       pl = GroundColor.list
       pl.should eq [@gc]
     end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
-        @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :color_id => @color.id, :updated_at => Time.now + 100
-        @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :color_id => @color.id, :updated_at => Time.now + 200
-        @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :color_id => @color.id, :updated_at => Time.now + 300
-        @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :color_id => @color.id, :updated_at => Time.now + 400
+        @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
+        @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
+        @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
+        @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
         GroundColor.stub(:default_page_size).and_return(2)
       end
       it '通常は2件を返す' do
@@ -248,24 +273,11 @@ describe GroundColor do
         pl.should eq [@gc]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
-      before do
-        @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :color_id => @color.id, :updated_at => Time.now + 100
-        @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :color_id => @color.id, :updated_at => Time.now + 200
-        @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :color_id => @color.id, :updated_at => Time.now + 300
-        @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :color_id => @color.id, :updated_at => Time.now + 400
-        GroundColor.stub(:default_page_size).and_return(2)
-      end
-      it '通常は全件(5件)を返す' do
-        r = GroundColor.list 5, 0
-        r.should have(5).items 
-      end
-    end
   end
   
-  describe '自分のコマで使った選択色地一覧取得に於いて' do
+  describe '自分のコマで使った色地一覧取得に於いて' do
     before do
-      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
     end
     context 'つつがなく終わるとき' do
       it '一覧取得オプションを利用している' do
@@ -279,28 +291,28 @@ describe GroundColor do
       pl.should eq [@gc]
     end
     it '時系列で並んでいる' do
-      npl = FactoryGirl.create :ground_color, :panel_id => @panel.id, :color_id => @color.id, :z => 2, :updated_at => Time.now + 100
+      npl = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 100
       pl = GroundColor.mylist @author
       pl.should eq [npl, @gc]
     end
-    it '他人のコマの選択色地は公開でも含まない' do
+    it '他人のコマの色地は公開でも含まない' do
       hpl = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
-      npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :color_id => @color.id
+      npl = FactoryGirl.create :ground_color, :panel_id => hpl.id
       pl = GroundColor.mylist @author
       pl.should eq [@gc]
     end
-    it '自分のコマの選択色地は非公開でも含んでいる' do
+    it '自分のコマの色地は非公開でも含んでいる' do
       hpl = FactoryGirl.create :panel, :author_id => @author.id, :publish => 0
-      npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :color_id => @color.id, :z => 2, :updated_at => Time.now + 100
+      npl = FactoryGirl.create :ground_color, :panel_id => hpl.id, :z => 2, :updated_at => Time.now + 100
       pl = GroundColor.mylist @author
       pl.should eq [npl, @gc]
     end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
-        @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :color_id => @color.id, :updated_at => Time.now + 100
-        @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :color_id => @color.id, :updated_at => Time.now + 200
-        @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :color_id => @color.id, :updated_at => Time.now + 300
-        @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :color_id => @color.id, :updated_at => Time.now + 400
+        @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :updated_at => Time.now + 100
+        @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :updated_at => Time.now + 200
+        @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :updated_at => Time.now + 300
+        @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :updated_at => Time.now + 400
       end
       it '通常は2件を返す' do
         c = GroundColor.mylist @author, 1, 2
@@ -320,26 +332,13 @@ describe GroundColor do
         c.should eq [@gc]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
-      before do
-        @gc2 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 1, :color_id => @color.id, :updated_at => Time.now + 100
-        @gc3 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 2, :color_id => @color.id, :updated_at => Time.now + 200
-        @gc4 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 3, :color_id => @color.id, :updated_at => Time.now + 300
-        @gc5 = FactoryGirl.create :ground_color, :panel_id => @panel.id, :z => 4, :color_id => @color.id, :updated_at => Time.now + 400
-        Author.stub(:default_ground_color_page_size).and_return(2)
-      end
-      it '通常は全件(5件)を返す' do
-        r = GroundColor.mylist @author, 5, 0
-        r.should have(5).items 
-      end
-    end
   end
   
-  describe '他作家の選択色地一覧取得に於いて' do
+  describe '他作家の色地一覧取得に於いて' do
     before do
-      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
       @other_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 1
-      @other_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id
+      @other_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id
     end
     it 'リストを返す' do
       r = GroundColor.himlist @other_author
@@ -347,22 +346,22 @@ describe GroundColor do
     end
     it '時系列で並んでいる' do
       new_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :updated_at => Time.now + 100
-      new_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 100
+      new_gc = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
       r = GroundColor.himlist @other_author
       r.should eq [new_gc, @other_gc]
     end
     it '公開コマに限る' do
       hidden_panel = FactoryGirl.create :panel, :author_id => @other_author.id, :publish => 0
-      hidden_gc = FactoryGirl.create :ground_color, :panel_id => hidden_panel.id, :color_id => @color.id
+      hidden_gc = FactoryGirl.create :ground_color, :panel_id => hidden_panel.id
       r = GroundColor.himlist @other_author
       r.should eq [@other_gc]
     end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
-        @other_gc2 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 100
-        @other_gc3 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 200
-        @other_gc4 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 300
-        @other_gc5 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 400
+        @other_gc2 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 100
+        @other_gc3 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 200
+        @other_gc4 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 300
+        @other_gc5 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :updated_at => Time.now + 400
       end
       it '通常は2件を返す' do
         pl = GroundColor.himlist @other_author, 1, 2
@@ -382,42 +381,81 @@ describe GroundColor do
         pl.should eq [@other_gc]
       end
     end
-    context 'DBに5件あって1ページの件数を0件に変えたとして' do
-      before do
-        @other_gc2 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 100
-        @other_gc3 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 200
-        @other_gc4 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 300
-        @other_gc5 = FactoryGirl.create :ground_color, :panel_id => @other_panel.id, :color_id => @color.id, :updated_at => Time.now + 400
-        Author.stub(:default_ground_color_page_size).and_return(2)
-      end
-      it '通常は全件(5件)を返す' do
-        r = GroundColor.himlist @other_author, 5, 0
-        r.should have(5).items 
-      end
+  end
+  
+  describe '色地一覧ページ制御に於いて' do
+    before do
+      GroundColor.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = GroundColor.list_paginate 
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '色地一覧の取得条件を利用している' do
+      GroundColor.stub(:list_where).with(any_args).and_return('')
+      GroundColor.should_receive(:list_where).with(any_args).exactly(1)
+      r = GroundColor.list_paginate 
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = GroundColor.list_paginate 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '自分の色地一覧ページ制御に於いて' do
+    before do
+      GroundColor.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = GroundColor.mylist_paginate @author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '自分の色地一覧の取得条件を利用している' do
+      GroundColor.stub(:mylist_where).with(any_args).and_return('')
+      GroundColor.should_receive(:mylist_where).with(any_args).exactly(1)
+      r = GroundColor.mylist_paginate @author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = GroundColor.mylist_paginate @author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
+    end
+  end
+  
+  describe '他作家の色地一覧ページ制御に於いて' do
+    before do
+      GroundColor.stub(:count).with(any_args).and_return(100)
+    end
+    it 'ページ制御を返す' do
+      r = GroundColor.himlist_paginate @other_author
+      r.is_a?(Kaminari::PaginatableArray).should be_true
+    end
+    it '他作家の色地一覧の取得条件を利用している' do
+      GroundColor.stub(:himlist_where).with(any_args).and_return('')
+      GroundColor.should_receive(:himlist_where).with(any_args).exactly(1)
+      r = GroundColor.himlist_paginate @other_author
+    end
+    it 'ページ件数10のとき、3ページ目のオフセットは20から始まる' do
+      r = GroundColor.himlist_paginate @other_author, 3, 10
+      r.limit_value.should eq 10
+      r.offset_value.should eq 20
     end
   end
   
   describe '一覧取得オプションに於いて' do
-    it 'includeキーを含んでいる' do
+    it '1つの項目を含んでいる' do
       r = GroundColor.list_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = GroundColor.list_opt[:include]
-      r.should have(2).items
+      r.should have(1).items
     end
     it 'コマを含んでいる' do
-      r = GroundColor.list_opt[:include]
+      r = GroundColor.list_opt
       r.has_key?(:panel).should be_true
     end
       it 'コマは作家を含んでいる' do
-        r = GroundColor.list_opt[:include]
+        r = GroundColor.list_opt
         r[:panel].has_key?(:author).should be_true
       end
-    it '色を含んでいる' do
-      r = GroundColor.list_opt[:include]
-      r.has_key?(:color).should be_true
-    end
   end
   describe 'json一覧出力オプションに於いて' do
     before do
@@ -428,7 +466,7 @@ describe GroundColor do
       @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
-      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
     end
     it 'コマを含んでいる' do
       r = GroundColor.list.to_json GroundColor.list_json_opt
@@ -443,17 +481,11 @@ describe GroundColor do
         s = i['panel']
         s.has_key?('author').should be_true
       end
-    it '色を含んでいる' do
-      r = GroundColor.list.to_json GroundColor.list_json_opt
-      j = JSON.parse r
-      i = j.first
-      i.has_key?('color').should be_true
-    end
   end
   
   describe '単体取得に於いて' do
     before do
-      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
     end
     context 'つつがなく終わるとき' do
       it '単体取得オプションを利用している' do
@@ -467,7 +499,7 @@ describe GroundColor do
         r = GroundColor.show @gc.id, @author
       end
     end
-    it '指定の選択色地を返す' do
+    it '指定の色地を返す' do
       GroundColor.any_instance.stub(:visible?).and_return(true)
       pl = GroundColor.show @gc.id, @author
       pl.should eq @gc
@@ -480,7 +512,7 @@ describe GroundColor do
         }.should raise_error(ActiveRecord::Forbidden)
       end
     end
-    context '存在しない選択色地を開こうとしたとき' do
+    context '存在しない色地を開こうとしたとき' do
       it '404RecordNotFound例外を返す' do
         lambda{
           GroundColor.show 110, @author
@@ -493,9 +525,9 @@ describe GroundColor do
       r = GroundColor.show_opt
       r.has_key?(:include).should be_true
     end
-    it '2つの項目を含んでいる' do
+    it '1つの項目を含んでいる' do
       r = GroundColor.show_opt[:include]
-      r.should have(2).items
+      r.should have(1).items
     end
     it 'コマを含んでいる' do
       r = GroundColor.show_opt[:include]
@@ -505,14 +537,10 @@ describe GroundColor do
         r = GroundColor.show_opt[:include]
         r[:panel].has_key?(:author).should be_true
       end
-    it '色を含んでいる' do
-      r = GroundColor.show_opt[:include]
-      r.has_key?(:color).should be_true
-    end
   end
   describe 'json単体出力オプションに於いて' do
     before do
-      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id, :color_id => @color.id
+      @gc = FactoryGirl.create :ground_color, :panel_id => @panel.id
     end
     it 'コマを含んでいる' do
       r = GroundColor.show(@gc.id, @author).to_json GroundColor.show_json_opt
@@ -527,12 +555,6 @@ describe GroundColor do
         s = i['panel']
         s.has_key?('author').should be_true
       end
-    it '色を含んでいる' do
-      r = GroundColor.show(@gc.id, @author).to_json GroundColor.show_json_opt
-      j = JSON.parse r
-      i = j
-      i.has_key?('color').should be_true
-    end
   end
   
 end