OSDN Git Service

try autotest
[pettanr/pettanr.git] / spec / models / artist_spec.rb
index c26f27f..a3a167f 100644 (file)
@@ -1,5 +1,5 @@
 # -*- encoding: utf-8 -*-
-#
+#絵師
 require 'spec_helper'
 
 describe Artist do
@@ -7,13 +7,122 @@ describe Artist do
     Factory :admin
     @user = Factory( :user_yas)
     @author = @user.author
+    @license = Factory :license
   end
 
+  describe '検証に於いて' do
+    before do
+    end
+    
+    it 'オーソドックスなデータなら通る' do
+      @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id
+      @artist.should be_valid
+    end
+    
+    context 'nameを検証するとき' do
+      it 'nullなら失敗する' do
+        @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id, :name => nil
+        @artist.should_not be_valid
+      end
+      it '30文字以上なら失敗する' do
+        @artist = Factory.build :artist, :author_id => @author.id, :default_license_id => @license.id, :name => 'a'*31
+        @artist.should_not be_valid
+      end
+    end
+  end
+  
   describe '自動補充に於いて' do
     it '名前がno nameになっている' do
-      @artist = Factory :artist
+      @artist = Factory.build :artist, :name => nil
+      @artist.save
       @artist.name.should eq 'no name'
     end
   end
   
+  describe '単体取得に於いて' do
+    before do
+      @artist = Factory :artist, :author_id => @author.id, :default_license_id => @license.id
+    end
+    it '指定の絵師を返す' do
+      a = Artist.show @artist.id
+      a.should eq @artist
+    end
+    context '関連テーブルオプションがないとき' do
+      it '作家データだけを含んでいる' do
+        r = Artist.show_include_opt
+        r.should eq [:author]
+      end
+    end
+    context '関連テーブルオプションで素材を含ませたとき' do
+      it '作家データと素材データを含んでいる' do
+        r = Artist.show_include_opt(:include => :resource_pictures)
+        r.should eq [:author, :resource_pictures]
+      end
+    end
+  end
+  describe '一覧取得に於いて' do
+    before do
+      @artist = Factory :artist, :author_id => @author.id, :default_license_id => @license.id
+    end
+    context 'page補正について' do
+      it '文字列から数値に変換される' do
+        Artist.page('8').should eq 8
+      end
+      it 'nilの場合は1になる' do
+        Artist.page().should eq 1
+      end
+      it '0以下の場合は1になる' do
+        Artist.page('0').should eq 1
+      end
+    end
+    context 'page_size補正について' do
+      it '文字列から数値に変換される' do
+        Artist.page_size('7').should eq 7
+      end
+      it 'nilの場合はArtist.default_page_sizeになる' do
+        Artist.page_size().should eq Artist.default_page_size
+      end
+      it '0以下の場合はArtist.default_page_sizeになる' do
+        Artist.page_size('0').should eq Artist.default_page_size
+      end
+      it 'Artist.max_page_sizeを超えた場合はArtist.max_page_sizeになる' do
+        Artist.page_size('1000').should eq Artist.max_page_size
+      end
+    end
+    it 'リストを返す' do
+      c = Artist.list
+      c.should eq [@artist]
+    end
+    it '時系列で並んでいる' do
+      n = Factory :artist, :author_id => @author.id, :name => 'artist'
+      l = Artist.list
+      l.should eq [n, @artist]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @artist2 = Factory :artist, :author_id => @author.id, :name => 'artist2'
+        @artist3 = Factory :artist, :author_id => @author.id, :name => 'artist3'
+        @artist4 = Factory :artist, :author_id => @author.id, :name => 'artist4'
+        @artist5 = Factory :artist, :author_id => @author.id, :name => 'artist5'
+        Artist.stub(:default_page_size).and_return(2)
+      end
+      it '通常は2件を返す' do
+        c = Artist.list
+        c.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        c = Artist.list({}, 1)
+        c.should eq [@artist5, @artist4]
+      end
+      it 'page=2なら中間2件を返す' do
+        c = Artist.list({}, 2)
+        c.should eq [@artist3, @artist2]
+      end
+      it 'page=3なら先頭1件を返す' do
+        c = Artist.list({}, 3)
+        c.should eq [@artist]
+      end
+    end
+  end
 end