OSDN Git Service

t#30322:create provider license import func
[pettanr/pettanr.git] / spec / models / license_spec.rb
index 39fda16..e0dafd4 100644 (file)
@@ -7,13 +7,40 @@ describe License do
   end
   describe '検証に於いて' do
     before do
-      @l = Factory.build :license
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     end
     
-    it 'オーソドックスなデータなら通る' do
-      @l.should be_valid
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @l.name = 'a'
+        @l.caption = 'a'
+        @l.url = 'http://test.jp/'
+        @l.should be_valid
+      end
+      it '上限データが通る' do
+        @l.name = 'a'*50
+        @l.caption = 'a'*30
+        @l.url = 'http://test.jp/aaaaa' + 'a' * 180
+        @l.should be_valid
+      end
     end
     
+    context 'license_group_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @l.license_group_id = ''
+        @l.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @l.license_group_id = 'a'
+        @l.should_not be_valid
+      end
+      it '存在するライセンスグループでなければ失敗する' do
+        @l.license_group_id = 0
+        @l.should_not be_valid
+      end
+    end
     context 'nameを検証するとき' do
       it 'nullなら失敗する' do
         @l.name = ''
@@ -23,6 +50,20 @@ describe License do
         @l.name = 'a'*51
         @l.should_not be_valid
       end
+      it '重複していたら失敗する' do
+        lc = FactoryGirl.create :license
+        @l.should_not be_valid
+      end
+    end
+    context 'captionを検証するとき' do
+      it 'nullなら失敗する' do
+        @l.caption = ''
+        @l.should_not be_valid
+      end
+      it '31文字以上なら失敗する' do
+        @l.caption = 'a'*31
+        @l.should_not be_valid
+      end
     end
     context 'urlを検証するとき' do
       it 'nullなら失敗する' do
@@ -30,203 +71,290 @@ describe License do
         @l.should_not be_valid
       end
       it '201文字以上なら失敗する' do
-        @l.url = 'a'*201
+        @l.url = 'http://test.jp/aaaaa' + 'a' * 181
         @l.should_not be_valid
       end
-      it '重複していたら失敗する' do
-        lc = Factory :license
+      it 'url形式でないなら失敗する' do
+        @l.url = 'a'*200
         @l.should_not be_valid
       end
-      it 'url形式でなら失敗する' do
-        @l.url = ''
-        pending
+    end
+    context 'system_picture_idを検証するとき' do
+      it 'nullなら失敗する' do
+        @l.system_picture_id = ''
+        @l.should_not be_valid
       end
+      it '数値でなければ失敗する' do
+        @l.system_picture_id = 'a'
+        @l.should_not be_valid
+      end
+      it '存在するシステム画像でなければ失敗する' do
+        @l.system_picture_id = 0
+        @l.should_not be_valid
+      end
+    end
+  end
+  
+  describe 'デフォルト値補充に於いて' do
+    it 'defined' do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l.supply_default
+    end
+  end
+  
+  describe '上書き補充に於いて' do
+    it 'defined' do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.build :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l.overwrite
     end
   end
   
-  describe '対象ライセンスの取得に於いて' do
+  describe '閲覧許可に於いて' do
+    #ライセンスは作家作成する前から存在するので、閲覧制限の意味がない
+  end
+  
+  describe '一覧取得に於いて' do
     before do
-      @lc = Factory :license
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @lg2 = FactoryGirl.create :license_group, :name => 'pubdm'
     end
-    context 'urlが一致するライセンスがないとき' do
-      it '新規ライセンスを準備して返す' do
-        cl = Factory.build(:common_license, :url => 'http://domain.no')
-        r = License.update_license cl
-        r.should be_a_new License
+    context 'つつがなく終わるとき' do
+      it '一覧取得オプションを利用している' do
+        License.stub(:list_opt).with(any_args).and_return({})
+        License.should_receive(:list_opt).with(any_args).exactly(1)
+        r = License.list
       end
     end
-    context 'urlが一致するライセンスがあるとき' do
-      it '該当ライセンスを返す' do
-        r = License.update_license @lc
-        r.is_a?(License).should be_true
-        r.should_not be_a_new License
-        r.url.should eq @lc.url
-        r.name.should eq @lc.name
-      end
+    it 'リストを返す' do
+      l = License.list 
+      l.should eq [@l]
     end
-    #コモンライセンスとオリジナルライセンスをまたいだUrl重複チェックはここでやるよりない
-    #コモンライセンスが新規オブジェクトなのにライセンスが取得できるのは、
-    #そのUrlがオリジナルライセンスから登録されているということ
-    context '新規でユニークチェックするとき' do
-      it 'ライセンスの全体エラーに重複メッセージを入れて返す' do
-        cl = Factory.build(:common_license, :url => 'http://domain.no')
-        License.stub(:find_by_url).with(any_args).and_return(@lc)
-        r = License.update_license cl
-        r.errors[:base].should_not be_empty
-      end
+    it '名前順で並んでいる' do
+      @l2 = FactoryGirl.create :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l3 = FactoryGirl.create :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
+      l = License.list
+      l.should eq [@l3, @l, @l2]
     end
   end
-  
-  #作成が
-  describe '単体取得に於いて' do
+  describe '一覧取得オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = License.list_opt
+      r.has_key?(:include).should be_true
+    end
+    it '1つの項目を含んでいる' do
+      r = License.list_opt[:include]
+      r.should have(1).items
+    end
+    it 'ライセンスグループを含んでいる' do
+      r = License.list_opt[:include]
+      r.has_key?(:license_group).should be_true
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
     before do
-      @lcl = Factory :license
-      @cl = Factory :common_license, :license_id => @lcl.id
-      @lol = Factory :license, :url => 'http://test.ptn/10'
-      @ol = Factory :original_license, :license_id => @lol.id, :url => 'http://test.ptn/10'
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     end
-    it '指定のコマを返す' do
-      l = License.show @lcl.id
-      l.should eq @lcl
+    it 'ライセンスグループを含んでいる' do
+      r = License.list.to_json License.list_json_opt
+      j = JSON.parse r
+      i = j.first
+      i.has_key?('license_group').should be_true
     end
   end
-  describe '関連テーブルプションに於いて' do
-    context 'オプションがないとき' do
-      it 'コモンライセンスとオリジナルライセンスを含んでいる' do
-        r = License.show_include_opt
-        r.should eq [:common_license, :original_license]
+  
+  describe '単体取得に於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+    end
+    context 'つつがなく終わるとき' do
+      it '単体取得オプションを利用している' do
+        License.stub(:show_opt).with(any_args).and_return({})
+        License.should_receive(:show_opt).with(any_args).exactly(1)
+        r = License.show @l.id
       end
     end
-    context 'オプションで原画を含ませたとき' do
-      it 'コモンライセンスとオリジナルライセンスと原画データを含んでいる' do
-        r = License.show_include_opt(:include => :original_picture)
-        r.should eq [:common_license, :original_license, :original_picture]
+    it '指定のライセンスを返す' do
+      l = License.show @l.id
+      l.should eq @l
+    end
+    context '存在しないライセンスを開こうとしたとき' do
+      it '404RecordNotFound例外を返す' do
+        lambda{
+          License.show 110
+        }.should raise_error(ActiveRecord::RecordNotFound)
       end
     end
   end
-  describe 'json単体出力オプションに於いて' do
+  describe '単体取得オプションに於いて' do
     it 'includeキーを含んでいる' do
-      r = License.show_json_include_opt
+      r = License.show_opt
       r.has_key?(:include).should be_true
     end
-    it '2つの項目を含んでいる' do
-      r = License.show_json_include_opt[:include]
-      r.should have(2).items
+    it '1つの項目を含んでいる' do
+      r = License.show_opt[:include]
+      r.should have(1).items
+    end
+    it 'ライセンスグループを含んでいる' do
+      r = License.show_opt[:include]
+      r.has_key?(:license_group).should be_true
     end
-    it 'コモンライセンスを含んでいる' do
-      r = License.show_json_include_opt[:include]
-      r.has_key?(:common_license).should be_true
+  end
+  describe 'json単体出力オプションに於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
     end
-    it 'オリジナルライセンスを含んでいる' do
-      r = License.show_json_include_opt[:include]
-      r.has_key?(:original_license).should be_true
+    it 'ライセンスグループを含んでいる' do
+      r = License.show(@l.id).to_json License.show_json_opt
+      j = JSON.parse r
+      i = j
+      i.has_key?('license_group').should be_true
     end
   end
-  describe '一覧取得に於いて' do
+  
+  describe '更新に於いて' do
     before do
-      @lcl = Factory :license, :name => 'peta2.0'
-      @cl = Factory :common_license, :license_id => @lcl.id
+      @lg = FactoryGirl.create :license_group
+      @f = Rails.root + 'spec/json/license_group.json'
+      @t = File.open(@f, 'r').read
+      @j = JSON.parse @t
+      @n = @j.keys.first
+      @a = @j.values.first
+      @attr = @a["licenses_attributes"]
+      @ln = @attr.keys.first
+      @la = @attr.values.first
+      @la["license_group_id"] = @lg.id
     end
-    context 'page補正について' do
-      it '文字列から数値に変換される' do
-        License.page('8').should eq 8
-      end
-      it 'nilの場合は1になる' do
-        License.page().should eq 1
+    context 'つつがなく終わるとき' do
+      it 'システム画像置換を依頼する' do
+        License.stub(:replace_system_picture).with(any_args).and_return(true)
+        License.should_receive(:replace_system_picture).with(any_args).exactly(1)
+        License.stub(:modify_object).with(any_args).and_return(License.new)
+        License.any_instance.stub(:save).with(any_args).and_return(true)
+        r = License.store(@ln, @la)
       end
-      it '0以下の場合は1になる' do
-        License.page('0').should eq 1
+      it 'データ更新準備を依頼する' do
+        License.stub(:replace_system_picture).with(any_args).and_return(true)
+        License.stub(:modify_object).with(any_args).and_return(License.new)
+        License.should_receive(:modify_object).with(any_args).exactly(1)
+        License.any_instance.stub(:save).with(any_args).and_return(true)
+        r = License.store(@ln, @la)
       end
-    end
-    context 'page_size補正について' do
-      it '文字列から数値に変換される' do
-        License.page_size('7').should eq 7
+      it '保存を依頼する' do
+        License.stub(:replace_system_picture).with(any_args).and_return(true)
+        License.stub(:modify_object).with(any_args).and_return(License.new)
+        License.any_instance.stub(:save).with(any_args).and_return(true)
+        License.any_instance.should_receive(:save).with(any_args).exactly(1)
+        r = License.store(@ln, @la)
       end
-      it 'nilの場合はLicense.default_page_sizeになる' do
-        License.page_size().should eq License.default_page_size
+      it 'オブジェクトを返す' do
+        r = License.store(@ln, @la)
+        r.is_a?(License).should be_true
+        r.name.should eq @ln
+        r.url.should eq @la["url"]
+        cp = JSON.parse r.credit_pictures
+        cp['a_picture_id'].should_not be_nil
+        cp['b_picture_id'].should_not be_nil
       end
-      it '0以下の場合はLicense.default_page_sizeになる' do
-        License.page_size('0').should eq License.default_page_size
+      it 'ライセンスが作成されている' do
+        lambda {
+          r = License.store(@ln, @la)
+        }.should change License, :count
       end
-      it 'License.max_page_sizeを超えた場合はLicense.max_page_sizeになる' do
-        License.page_size('1000').should eq License.max_page_size
+      it 'システム画像が作成されている' do
+        lambda {
+          r = License.store(@ln, @la)
+        }.should change SystemPicture, :count
       end
     end
-    it 'リストを返す' do
-      pl = License.list
-      pl.should eq [@lcl]
-    end
-    it '名前順で並んでいる' do
-      @lol = Factory :license, :name => 'peta1.0', :url => 'http://test.ptn/10'
-      @ol = Factory :original_license, :license_id => @lol.id, :name => 'peta1.0', :url => 'http://test.ptn/10'
-      l = License.list
-      l.should eq [@lol, @lcl]
-    end
-    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+    context 'システム画像置換が失敗するとき' do
       before do
-        @lol2 = Factory :license, :name => 'peta2.1', :url => 'http://test.ptn/21'
-        @ol2 = Factory :original_license, :license_id => @lol2.id, :name => 'peta2.1', :url => 'http://test.ptn/21'
-        @lol3 = Factory :license, :name => 'peta2.2', :url => 'http://test.ptn/22'
-        @ol3 = Factory :original_license, :license_id => @lol3.id, :name => 'peta2.2', :url => 'http://test.ptn/22'
-        @lol4 = Factory :license, :name => 'peta2.3', :url => 'http://test.ptn/23'
-        @ol4 = Factory :original_license, :license_id => @lol4.id, :name => 'peta2.3', :url => 'http://test.ptn/23'
-        @lol5 = Factory :license, :name => 'peta2.4', :url => 'http://test.ptn/24'
-        @ol5 = Factory :original_license, :license_id => @lol5.id, :name => 'peta2.4', :url => 'http://test.ptn/24'
-        License.stub(:default_page_size).and_return(2)
-      end
-      it '通常は2件を返す' do
-        l = License.list
-        l.should have(2).items 
-      end
-      it 'page=1なら末尾2件を返す' do
-        #名前順で並んでいる
-        l = License.list( {}, 1)
-        l.should eq [@lcl, @lol2]
-      end
-      it 'page=2なら中間2件を返す' do
-        l = License.list({}, 2)
-        l.should eq [@lol3, @lol4]
-      end
-      it 'page=3なら先頭1件を返す' do
-        l = License.list({}, 3)
-        l.should eq [@lol5]
+        License.stub(:replace_system_picture).with(any_args).and_return(false)
+        License.stub(:modify_object).with(any_args).and_return(License.new)
+      end
+      it '全体エラーメッセージがセットされている' do
+        r = License.store(@ln, @la)
+        r.errors[:base].should_not be_blank
+      end
+      it 'ライセンスが作成されていない' do
+        lambda {
+          r = License.store(@ln, @la)
+        }.should_not change License, :count
       end
     end
   end
-  describe 'list関連テーブルプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = License.list_opt
-      r.has_key?(:include).should be_true
+  
+  describe '複数の更新に於いて' do
+    before do
+      @lg = FactoryGirl.create :license_group
+      @fs = Rails.root + 'spec/json/license_groups.json'
+      @ts = File.open(@fs, 'r').read
+      @js = JSON.parse @ts
+      @n = @js.keys.last
+      @a = @js.values.last
+      @attr = @a["licenses_attributes"]
     end
-    it '2つの項目を含んでいる' do
-      r = License.list_opt[:include]
-      r.should have(2).items
+    context '2件データでつつがなく終わるとき' do
+      it '更新を2回依頼する' do
+        License.stub(:store).with(any_args).and_return(License.new)
+        License.should_receive(:store).with(any_args).exactly(2)
+        License.any_instance.stub(:valid?).with(any_args).and_return(true)
+        r = License.stores(@attr, @lg.id)
+      end
+      it '失敗件数0を返す' do
+        License.stub(:store).with(any_args).and_return(License.new)
+        License.any_instance.stub(:valid?).with(any_args).and_return(true)
+        r = License.stores(@attr, @lg.id)
+        r.should eq 0
+      end
     end
-    it 'コモンライセンスを含んでいる' do
-      r = License.list_opt[:include]
-      r.has_key?(:common_license).should be_true
+    context '2件データで失敗するとき' do
+      it '更新を2回依頼する' do
+        License.stub(:store).with(any_args).and_return(License.new)
+        License.should_receive(:store).with(any_args).exactly(2)
+        License.any_instance.stub(:valid?).with(any_args).and_return(false)
+        r = License.stores(@attr, @lg.id)
+      end
+      it '失敗件数2を返す' do
+        License.stub(:store).with(any_args).and_return(License.new)
+        License.any_instance.stub(:valid?).with(any_args).and_return(false)
+        r = License.stores(@attr, @lg.id)
+        r.should eq 2
+      end
     end
-    it 'オリジナルライセンスを含んでいる' do
-      r = License.list_opt[:include]
-      r.has_key?(:original_license).should be_true
+    context 'attrsがnilなどのHashでないとき' do
+      it '処理にかけず0を返す' do
+        r = License.stores(nil, @lg.id)
+        r.should eq 0
+      end
     end
   end
-  describe 'json一覧出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = License.list_json_opt
-      r.has_key?(:include).should be_true
-    end
-    it '2つの項目を含んでいる' do
-      r = License.list_json_opt[:include]
-      r.should have(2).items
+  
+  describe 'エクスポートに於いて' do
+    before do
+      @sp = FactoryGirl.create :system_picture
+      @lg = FactoryGirl.create :license_group
+      @l = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @l2 = FactoryGirl.create :license, :name => 'l2[old ls]', :url => @l.url+'2', :license_group_id => @lg.id, :system_picture_id => @sp.id, :updated_at => Time.now - 3000
     end
-    it 'コモンライセンスを含んでいる' do
-      r = License.list_json_opt[:include]
-      r.has_key?(:common_license).should be_true
+    it '開始日時が省略された場合はすべてのライセンスを返す' do
+      r = License.export 
+      r.should eq [@l, @l2]
     end
-    it 'オリジナルライセンスを含んでいる' do
-      r = License.list_json_opt[:include]
-      r.has_key?(:original_license).should be_true
+    it '開始日時以降に更新されたライセンスを返す' do
+      r = License.export @l.updated_at - 100
+      r.should eq [@l]
     end
   end