OSDN Git Service

t#30515:fix import op
[pettanr/pettanr.git] / spec / models / artist_spec.rb
index 815edbc..c555521 100644 (file)
@@ -4,11 +4,11 @@ require 'spec_helper'
 
 describe Artist do
   before do
-    FactoryGirl.create :admin
+    @admin = FactoryGirl.create :admin
     @user = FactoryGirl.create( :user_yas)
-    @author = @user.author
-    @other_user = FactoryGirl.create( :user_yas)\r
-    @other_author = @other_user.author\r
+    @author = FactoryGirl.create :author, :user_id => @user.id
+    @other_user = FactoryGirl.create( :user_yas)
+    @other_author = FactoryGirl.create :author, :user_id => @other_user.id
     @sp = FactoryGirl.create :system_picture
     @lg = FactoryGirl.create :license_group
     @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
@@ -68,7 +68,7 @@ describe Artist do
   describe '所持判定に於いて' do
     before do
       @artist = FactoryGirl.create :artist, :author_id => @author.id
-      @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id\r
+      @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
     end
     it '自分の絵師ならyes' do
       @artist.own?(@author).should == true
@@ -76,7 +76,7 @@ describe Artist do
     it '他人の絵師ならno' do
       @artist.own?(@other_author).should == false
     end
-    it '作家が不明ならno' do
+    it 'パラメータが作家でないならならno' do
       @artist.own?(nil).should == false
     end
   end
@@ -85,14 +85,38 @@ describe Artist do
     before do
       @artist = FactoryGirl.create :artist, :author_id => @author.id
     end
-    it '自分の絵師を見るときは許可する' do\r
-      Artist.any_instance.stub(:own?).and_return(true)\r
-      @artist.visible?(@author).should == true
-    end\r
-    it '他人の絵師でも許可する' do\r
-      Artist.any_instance.stub(:own?).and_return(false)\r
-      @artist.visible?(@author).should == true
-    end\r
+    context '検査対象がnil(ゲスト)のとき' do
+      context 'クローズドモードのとき' do
+        before do
+          MagicNumber['run_mode'] = 1
+        end
+        it '不許可を返す。' do
+          r = @artist.visible?(nil)
+          r.should be_false
+        end
+      end
+      context 'オープンモードのとき' do
+        before do
+          MagicNumber['run_mode'] = 0
+        end
+        it '許可する' do
+          r = @artist.visible?(nil)
+          r.should be_true
+        end
+      end
+    end
+    context '検査対象が作家のとき' do
+      it '許可する' do
+        r = @artist.visible?(@author)
+        r.should == true
+      end
+    end
+    context '検査対象がそれ以外のとき' do
+      it '不許可を返す。' do
+        r = @artist.visible?(@admin)
+        r.should be_false
+      end
+    end
   end
   
   describe '一覧取得に於いて' do
@@ -124,13 +148,13 @@ describe Artist do
         Artist.page_size('1000').should eq Artist.max_page_size
       end
     end
-    context 'つつがなく終わるとき' do\r
-      it '一覧取得オプションを利用している' do\r
-        Artist.stub(:list_opt).with(any_args).and_return({})\r
-        Artist.should_receive(:list_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        Artist.stub(:list_opt).with(any_args).and_return({})
+        Artist.should_receive(:list_opt).with(any_args).exactly(1)
         r = Artist.list
-      end\r
-    end\r
+      end
+    end
     it 'リストを返す' do
       r = Artist.list
       r.should eq [@artist]
@@ -140,6 +164,11 @@ describe Artist do
       l = Artist.list
       l.should eq [n, @artist]
     end
+    it 'ただし、内絵師(作家idが空)だけとする' do
+      n = FactoryGirl.create :artist, :author_id => nil
+      l = Artist.list
+      l.should eq [@artist]
+    end
     context 'DBに5件あって1ページの件数を2件に変えたとして' do
       before do
         @artist2 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist2', :created_at => Time.now + 100
@@ -172,10 +201,10 @@ describe Artist do
         @artist3 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist3', :created_at => Time.now + 200
         @artist4 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist4', :created_at => Time.now + 300
         @artist5 = FactoryGirl.create :artist, :author_id => @author.id, :name => 'artist5', :created_at => Time.now + 400
-        Artist.stub(:default_page_size).and_return(0)
+        Artist.stub(:default_page_size).and_return(2)
       end
       it '通常は全件(5件)を返す' do
-        r = Artist.list
+        r = Artist.list 5, 0
         r.should have(5).items 
       end
     end
@@ -185,51 +214,24 @@ describe Artist do
       r = Artist.list_opt
       r.has_key?(:include).should be_true
     end
-    it '4つの項目を含んでいる' do
+    it '1つの項目を含んでいる' do
       r = Artist.list_opt[:include]
-      r.should have(4).items
+      r.should have(1).items
     end
     it '作家を含んでいる' do
       r = Artist.list_opt[:include]
       r.has_key?(:author).should be_true
     end
-    it '原画を含んでいる' do
-      r = Artist.list_opt[:include]
-      r.has_key?(:original_pictures).should be_true
-    end
-    it '実素材を含んでいる' do
-      r = Artist.list_opt[:include]
-      r.has_key?(:pictures).should be_true
-    end
-    it '素材を含んでいる' do
-      r = Artist.list_opt[:include]
-      r.has_key?(:resource_pictures).should be_true
-    end
   end
   describe 'json一覧出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = Artist.list_json_opt
-      r.has_key?(:include).should be_true
-    end
-    it '4つの項目を含んでいる' do
-      r = Artist.list_json_opt[:include]
-      r.should have(4).items
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
     end
     it '作家を含んでいる' do
-      r = Artist.list_json_opt[:include]
-      r.has_key?(:author).should be_true
-    end
-    it '原画を含んでいる' do
-      r = Artist.list_json_opt[:include]
-      r.has_key?(:original_pictures).should be_true
-    end
-    it '実素材を含んでいる' do
-      r = Artist.list_json_opt[:include]
-      r.has_key?(:pictures).should be_true
-    end
-    it '素材を含んでいる' do
-      r = Artist.list_json_opt[:include]
-      r.has_key?(:resource_pictures).should be_true
+      r = Artist.list.to_json Artist.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('author').should be_true
     end
   end
   
@@ -237,54 +239,54 @@ describe Artist do
     before do
       @artist = FactoryGirl.create :artist, :author_id => @author.id
     end
-    context 'つつがなく終わるとき' do\r
-      it '単体取得オプションを利用している' do\r
-        Artist.stub(:show_opt).with(any_args).and_return({})\r
-        Artist.should_receive(:show_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        Artist.stub(:show_opt).with(any_args).and_return({})
+        Artist.should_receive(:show_opt).with(any_args).exactly(1)
         r = Artist.show @artist.id, @author
-      end\r
-      it '閲覧許可を問い合わせている' do\r
-        Artist.any_instance.stub(:visible?).with(any_args).and_return(true)\r
-        Artist.any_instance.should_receive(:visible?).with(any_args).exactly(1)\r
+      end
+      it '閲覧許可を問い合わせている' do
+        Artist.any_instance.stub(:visible?).with(any_args).and_return(true)
+        Artist.any_instance.should_receive(:visible?).with(any_args).exactly(1)
         r = Artist.show @artist.id, @author
-      end\r
-    end\r
+      end
+    end
     it '指定の絵師を返す' do
       a = Artist.show @artist.id, @author
       a.should eq @artist
     end
-    context '閲覧許可が出なかったとき' do\r
-      it '403Forbidden例外を返す' do\r
-        Artist.any_instance.stub(:visible?).and_return(false)\r
-        lambda{\r
-          Artist.show @artist.id, @author\r
-        }.should raise_error(ActiveRecord::Forbidden)\r
-      end\r
-    end\r
-    context '存在しない絵師を開こうとしたとき' do\r
-      it '404RecordNotFound例外を返す' do\r
-        lambda{\r
-          Artist.show 110, @author\r
-        }.should raise_error(ActiveRecord::RecordNotFound)\r
-      end\r
-    end\r
+    context '閲覧許可が出なかったとき' do
+      it '403Forbidden例外を返す' do
+        Artist.any_instance.stub(:visible?).and_return(false)
+        lambda{
+          Artist.show @artist.id, @author
+        }.should raise_error(ActiveRecord::Forbidden)
+      end
+    end
+    context '存在しない絵師を開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          Artist.show 110, @author
+        }.should raise_error(ActiveRecord::RecordNotFound)
+      end
+    end
   end
   describe '編集取得に於いて' do
     before do
       @artist = FactoryGirl.create :artist, :author_id => @author.id
     end
-    context 'つつがなく終わるとき' do\r
-      it '単体取得オプションを利用している' do\r
-        Artist.stub(:show_opt).with(any_args).and_return({})\r
-        Artist.should_receive(:show_opt).with(any_args).exactly(1)\r
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        Artist.stub(:show_opt).with(any_args).and_return({})
+        Artist.should_receive(:show_opt).with(any_args).exactly(1)
         r = Artist.edit @artist.id, @author
-      end\r
-      it '所持判定を問い合わせている' do\r
-        Artist.any_instance.stub(:own?).with(any_args).and_return(true)\r
-        Artist.any_instance.should_receive(:own?).with(any_args).exactly(1)\r
+      end
+      it '所持判定を問い合わせている' do
+        Artist.any_instance.stub(:own?).with(any_args).and_return(true)
+        Artist.any_instance.should_receive(:own?).with(any_args).exactly(1)
         r = Artist.edit @artist.id, @author
-      end\r
-    end\r
+      end
+    end
     it '指定の絵師を返す' do
       Artist.any_instance.stub(:own?).and_return(true)
       r = Artist.edit @artist.id, @author.id
@@ -311,51 +313,50 @@ describe Artist do
       r = Artist.show_opt
       r.has_key?(:include).should be_true
     end
-    it '4つの項目を含んでいる' do
+    it '1つの項目を含んでいる' do
       r = Artist.show_opt[:include]
-      r.should have(4).items
-    end
-    it '作家を含んでいる' do
-      r = Artist.show_opt[:include]
-      r.has_key?(:author).should be_true
-    end
-    it '原画を含んでいる' do
-      r = Artist.show_opt[:include]
-      r.has_key?(:original_pictures).should be_true
-    end
-    it '実素材を含んでいる' do
-      r = Artist.show_opt[:include]
-      r.has_key?(:pictures).should be_true
-    end
-    it '素材を含んでいる' do
-      r = Artist.show_opt[:include]
-      r.has_key?(:resource_pictures).should be_true
+      r.should have(1).items
     end
   end
   describe 'json単体出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = Artist.show_json_opt
-      r.has_key?(:include).should be_true
-    end
-    it '4つの項目を含んでいる' do
-      r = Artist.show_json_opt[:include]
-      r.should have(4).items
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
     end
     it '作家を含んでいる' do
-      r = Artist.show_json_opt[:include]
-      r.has_key?(:author).should be_true
+      r = Artist.show(@artist.id, @author).to_json Artist.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('author').should be_true
+    end
+  end
+  
+  describe '有効絵師数に於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @artist = FactoryGirl.create :artist, :author_id => @other_author.id
+      @artist = FactoryGirl.create :artist, :author_id => nil
     end
-    it 'å\8e\9fç\94»ã\82\92å\90«ã\82\93ã\81§ã\81\84ã\82\8b' do
-      r = Artist.show_json_opt[:include]
-      r.has_key?(:original_pictures).should be_true
+    it 'å\86\85絵師æ\95°ã\82\92è¿\94ã\81\99' do
+      r = Artist.visible_count
+      r.should eq 2
     end
-    it '実素材を含んでいる' do
-      r = Artist.show_json_opt[:include]
-      r.has_key?(:pictures).should be_true
+  end
+  
+  describe 'エクスポートに於いて' do
+    before do
+      @artist = FactoryGirl.create :artist, :author_id => @author.id
+      @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :updated_at => Time.now - 3000
+      @artist3 = FactoryGirl.create :artist, :author_id => nil
     end
-    it '素材を含んでいる' do
-      r = Artist.show_json_opt[:include]
-      r.has_key?(:resource_pictures).should be_true
+    it '開始日時が省略された場合はすべての内絵師を返す' do
+      r = Artist.export 
+      r.should eq [@artist, @artist2]
+    end
+    it '開始日時以降に更新された内絵師を返す' do
+      r = Artist.export @artist.updated_at - 100
+      r.should eq [@artist]
     end
   end
+  
+
 end