OSDN Git Service

t#30220:i18n system error message
[pettanr/pettanr.git] / spec / models / license_spec.rb
index 4e93e29..c30123c 100644 (file)
@@ -7,38 +7,41 @@ describe License do
   end
   describe '検証に於いて' do
     before do
-      @sp = Factory :system_picture
-      @lg = Factory :license_group
-      @l = Factory.build :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
+      @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 'テストデータの確認' do\r
-        @l.license_group_id = @lg.id\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.license_group_id = ''
         @l.should_not be_valid
       end
-      it '数値でなければ失敗する' do\r
-        @l.license_group_id = 'a'\r
-        @l.should_not be_valid\r
-      end\r
-      it '存在するライセンスグループでなければ失敗する' do\r
-        @l.license_group_id = 0\r
-        @l.should_not be_valid\r
-      end\r
+      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 'テストデータの確認' do\r
-        @l.name = 'a'*50\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.name = ''
         @l.should_not be_valid
@@ -48,15 +51,11 @@ describe License do
         @l.should_not be_valid
       end
       it '重複していたら失敗する' do
-        lc = Factory :license
+        lc = FactoryGirl.create :license
         @l.should_not be_valid
       end
     end
     context 'captionを検証するとき' do
-      it 'テストデータの確認' do\r
-        @l.caption = 'a'*30\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.caption = ''
         @l.should_not be_valid
@@ -67,11 +66,6 @@ describe License do
       end
     end
     context 'urlを検証するとき' do
-      it 'テストデータの確認' do\r
-        @l.url = 'http://test.jp/aaaaa' + 'a' * 180
-        @l.save!\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.url = ''
         @l.should_not be_valid
@@ -80,89 +74,75 @@ describe License do
         @l.url = 'http://test.jp/aaaaa' + 'a' * 181
         @l.should_not be_valid
       end
-      it 'url形式でなら失敗する' do
-        @l.url = 'a'*200\r
-        @l.should_not be_valid\r
+      it 'urlå½¢å¼\8fã\81§ã\81ªã\81\84ã\81ªã\82\89失æ\95\97ã\81\99ã\82\8b' do
+        @l.url = 'a'*200
+        @l.should_not be_valid
       end
     end
     context 'system_picture_idを検証するとき' do
-      it 'テストデータの確認' do\r
-        @l.system_picture_id = @sp.id\r
-        @l.should be_valid\r
-      end\r
       it 'nullなら失敗する' do
         @l.system_picture_id = ''
         @l.should_not be_valid
       end
-      it '数値でなければ失敗する' do\r
-        @l.system_picture_id = 'a'\r
-        @l.should_not be_valid\r
-      end\r
-      it '存在するシステム画像でなければ失敗する' do\r
-        @l.system_picture_id = 0\r
-        @l.should_not be_valid\r
-      end\r
+      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
-    before do
-      @sp = Factory :system_picture
-      @lg = Factory :license_group
-      @l = Factory :license, :license_group_id => @lg.id, :system_picture_id => @sp.id
-    end
-    it '指定のコマを返す' do
-      l = License.show @l.id
-      l.should eq @l
+  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
-    context 'オプションがないとき' do
-      it 'ライセンスグループを含んでいる' do
-        r = License.show_include_opt
-        r.should eq [:license_group]
-      end
-    end
-    context 'オプションで素材ライセンスを含ませたとき' do
-      it 'ライセンスグループと素材ライセンスを含んでいる' do
-        r = License.show_include_opt(:include => :resource_picture_license)
-        r.should eq [:license_group, :resource_picture_license]
-      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 'json単体出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = License.show_json_include_opt
-      r.has_key?(:include).should be_true
-    end
-    it '1つの項目を含んでいる' do
-      r = License.show_json_include_opt[:include]
-      r.should have(1).items
-    end
-    it 'ライセンスグループを含んでいる' do
-      r = License.show_json_include_opt[:include]
-      r.has_key?(:license_group).should be_true
-    end
+  
+  describe '閲覧許可に於いて' do
+    #ライセンスは作家作成する前から存在するので、閲覧制限の意味がない
   end
+  
   describe '一覧取得に於いて' do
     before do
-      @sp = Factory :system_picture
-      @lg = Factory :license_group
-      @l = Factory :license, :name => 'peta2.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
-      @lg2 = Factory :license_group, :name => 'pubdm'
+      @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 'つつがなく終わるとき' 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
     it 'リストを返す' do
-      l = License.list @lg.id
+      l = License.list 
       l.should eq [@l]
     end
-    it 'グループ順,名前順で並んでいる' do
-      @l2 = Factory :license, :name => 'peta3.0', :url => 'http://pe.ta/3.0', :license_group_id => @lg.id, :system_picture_id => @sp.id
-      @l3 = Factory :license, :name => 'pd1.0', :url => 'http://pb.dm/1.0', :license_group_id => @lg2.id, :system_picture_id => @sp.id
-      l = License.list @lg.id
-      l.should eq [@l, @l2]
+    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 'list関連テーブルプションに於いて' do
+  describe '一覧取得オプションに於いて' do
     it 'includeキーを含んでいる' do
       r = License.list_opt
       r.has_key?(:include).should be_true
@@ -177,18 +157,188 @@ describe License do
     end
   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.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
+    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
+    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 '単体取得オプションに於いて' do
     it 'includeキーを含んでいる' do
-      r = License.list_json_opt
+      r = License.show_opt
       r.has_key?(:include).should be_true
     end
     it '1つの項目を含んでいる' do
-      r = License.list_json_opt[:include]
+      r = License.show_opt[:include]
       r.should have(1).items
     end
     it 'ライセンスグループを含んでいる' do
-      r = License.list_json_opt[:include]
+      r = License.show_opt[:include]
       r.has_key?(:license_group).should be_true
     end
   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(@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
+    before do
+      @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 'つつがなく終わるとき' 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 'データ更新準備を依頼する' 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
+      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 'オブジェクトを返す' 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 'ライセンスが作成されている' do
+        lambda {
+          r = License.store(@ln, @la)
+        }.should change License, :count
+      end
+      it 'システム画像が作成されている' do
+        lambda {
+          r = License.store(@ln, @la)
+        }.should change SystemPicture, :count
+      end
+    end
+    context 'システム画像置換が失敗するとき' do
+      before do
+        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 '複数の更新に於いて' 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
+    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
+    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
+    context 'attrsがnilなどのHashでないとき' do
+      it '処理にかけず0を返す' do
+        r = License.stores(nil, @lg.id)
+        r.should eq 0
+      end
+    end
+  end
   
 end