OSDN Git Service

t#29400:itr3?
[pettanr/pettanr.git] / spec / models / license_group_spec.rb
index 00a4df0..97cc5d2 100644 (file)
@@ -13,21 +13,27 @@ describe LicenseGroup do
   end
   describe '検証に於いて' do
     before do
+      @lg = FactoryGirl.build :license_group
     end
     
-    it 'オーソドックスなデータなら通る' do
-      @lg = Factory.build :license_group
-      @lg.should be_valid
-    end
-    
-    context 'nameを検証するとき' do
-      before do
-        @lg = Factory.build :license_group
+    context 'オーソドックスなデータのとき' do
+      it '下限データが通る' do
+        @lg.name = 'a'
+        @lg.classname = 'a'
+        @lg.caption = 'a'
+        @lg.url = 'http://test.jp/'
+        @lg.should be_valid
       end
-      it 'テストデータの確認' do
+      it '上限データが通る' do
         @lg.name = 'a'*50
+        @lg.classname = 'a'*50
+        @lg.caption = 'a'*30
+        @lg.url = 'http://test.jp/aaaaa' + 'a' * 180
         @lg.should be_valid
       end
+    end
+    
+    context 'nameを検証するとき' do
       it 'nullなら失敗する' do
         @lg.name = ''
         @lg.should_not be_valid
@@ -37,18 +43,11 @@ describe LicenseGroup do
         @lg.should_not be_valid
       end
       it '重複していたら失敗する' do
-        l = Factory :license_group
+        l = FactoryGirl.create :license_group
         @lg.should_not be_valid
       end
     end
     context 'classnameを検証するとき' do
-      before do
-        @lg = Factory.build :license_group
-      end
-      it 'テストデータの確認' do
-        @lg.classname = 'a'*50
-        @lg.should be_valid
-      end
       it 'nullなら失敗する' do
         @lg.classname = ''
         @lg.should_not be_valid
@@ -59,13 +58,6 @@ describe LicenseGroup do
       end
     end
     context 'captionを検証するとき' do
-      before do
-        @lg = Factory.build :license_group
-      end
-      it 'テストデータの確認' do
-        @lg.caption = 'a'*30
-        @lg.should be_valid
-      end
       it 'nullなら失敗する' do
         @lg.caption = ''
         @lg.should_not be_valid
@@ -76,13 +68,6 @@ describe LicenseGroup do
       end
     end
     context 'urlを検証するとき' do
-      before do
-        @lg = Factory.build :license_group
-      end
-      it 'テストデータの確認' do
-        @lg.url = 'http://test.com/'
-        @lg.should be_valid
-      end
       it 'nullなら失敗する' do
         @lg.url = ''
         @lg.should_not be_valid
@@ -98,20 +83,151 @@ describe LicenseGroup do
     end
   end
   
+  describe 'デフォルト値補充に於いて' do
+    it 'defined' do
+      @lg = FactoryGirl.build :license_group, :name => "1"
+      @lg.supply_default
+    end
+  end
+  
+  describe '上書き補充に於いて' do
+    it 'defined' do
+      @lg = FactoryGirl.build :license_group, :name => "1"
+      @lg.overwrite
+    end
+  end
+  
+  describe '閲覧許可に於いて' do
+    #ライセンスグループは作家作成する前から存在するので、閲覧制限の意味がない
+  end
+  
+  describe '一覧取得に於いて' do
+    before do
+      @lg = FactoryGirl.create :license_group, :name => "1"
+    end
+    context 'つつがなく終わるとき' do\r
+      it '一覧取得オプションを利用している' do\r
+        LicenseGroup.stub(:list_opt).with(any_args).and_return({})\r
+        LicenseGroup.should_receive(:list_opt).with(any_args).exactly(1)\r
+        r = LicenseGroup.list
+      end\r
+    end\r
+    it 'リストを返す' do
+      l = LicenseGroup.list
+      l.should eq [@lg]
+    end
+    it '名前順で並んでいる' do
+      @lg2 = FactoryGirl.create :license_group, :name => "5", :url => 'http://test.ptn/10'
+      l = LicenseGroup.list
+      l.should eq [@lg, @lg2]
+    end
+  end
+  describe 'list関連テーブルプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = LicenseGroup.list_opt
+      r.has_key?(:include).should be_true
+    end
+    it '1つの項目を含んでいる' do
+      r = LicenseGroup.list_opt[:include]
+      r.should have(1).items
+    end
+    it 'ライセンスを含んでいる' do
+      r = LicenseGroup.list_opt[:include]
+      r.has_key?(:licenses).should be_true
+    end
+  end
+  describe 'json一覧出力オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = LicenseGroup.list_json_opt
+      r.has_key?(:include).should be_true
+    end
+    it '1つの項目を含んでいる' do
+      r = LicenseGroup.list_json_opt[:include]
+      r.should have(1).items
+    end
+    it 'ライセンスを含んでいる' do
+      r = LicenseGroup.list_json_opt[:include]
+      r.has_key?(:licenses).should be_true
+    end
+  end
+  
+  describe '単体取得に於いて' do
+    before do
+      @lg = FactoryGirl.create :license_group
+    end
+    context 'つつがなく終わるとき' do\r
+      it '単体取得オプションを利用している' do\r
+        LicenseGroup.stub(:show_opt).with(any_args).and_return({})\r
+        LicenseGroup.should_receive(:show_opt).with(any_args).exactly(1)\r
+        r = LicenseGroup.show @lg.id
+      end\r
+    end\r
+    it '指定のコマを返す' do
+      l = LicenseGroup.show @lg.id
+      l.should eq @lg
+    end
+    context '存在しないライセンスグループを開こうとしたとき' do\r
+      it '404RecordNotFound例外を返す' do\r
+        lambda{\r
+          LicenseGroup.show 110\r
+        }.should raise_error(ActiveRecord::RecordNotFound)\r
+      end\r
+    end\r
+  end
+  describe '単体出力オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = LicenseGroup.show_opt
+      r.has_key?(:include).should be_true
+    end
+    it '1つの項目を含んでいる' do
+      r = LicenseGroup.show_opt[:include]
+      r.should have(1).items
+    end
+    it 'ライセンスを含んでいる' do
+      r = LicenseGroup.show_opt[:include]
+      r.has_key?(:licenses).should be_true
+    end
+  end
+  describe 'json単体出力オプションに於いて' do
+    it 'includeキーを含んでいる' do
+      r = LicenseGroup.show_json_opt
+      r.has_key?(:include).should be_true
+    end
+    it '1つの項目を含んでいる' do
+      r = LicenseGroup.show_json_opt[:include]
+      r.should have(1).items
+    end
+    it 'ライセンスを含んでいる' do
+      r = LicenseGroup.show_json_opt[:include]
+      r.has_key?(:licenses).should be_true
+    end
+  end
+  
   describe '更新に於いて' do
     before do
       @n = @j.keys.first
       @a = @j.values.first
+      @imager = ImagerTest.load("abc\ndef\nghi")
+      PettanImager.stub(:load).with(any_args).and_return(@imager)
     end
     context 'つつがなく終わるとき' do
       it 'データ更新準備を依頼する' do
         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
         LicenseGroup.should_receive(:modify_object).with(any_args).exactly(1)
+        License.stub(:stores).with(any_args).and_return(0)
+        LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+        r = LicenseGroup.store(@n, @a)
+      end
+      it 'ライセンスに複数の更新を依頼する' do
+        LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+        License.stub(:stores).with(any_args).and_return(0)
+        License.should_receive(:stores).with(any_args).exactly(1)
         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
         r = LicenseGroup.store(@n, @a)
       end
       it '保存を依頼する' do
         LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+        License.stub(:stores).with(any_args).and_return(0)
         LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
         LicenseGroup.any_instance.should_receive(:save).with(any_args).exactly(1)
         r = LicenseGroup.store(@n, @a)
@@ -122,6 +238,32 @@ describe LicenseGroup do
         r.name.should eq @n
         r.url.should eq @a["url"]
       end
+      it 'カラム値からlicenses_attributesが削除されている' do
+        @a["licenses_attributes"].should_not be_nil
+        r = LicenseGroup.store(@n, @a)
+        @a["licenses_attributes"].should be_nil
+      end
+    end
+    context 'ライセンス複数更新が失敗するとき' do
+      before do
+        LicenseGroup.stub(:modify_object).with(any_args).and_return(LicenseGroup.new)
+        License.stub(:stores).with(any_args).and_return(1)
+        LicenseGroup.any_instance.stub(:save).with(any_args).and_return(true)
+      end
+      it '全体エラーメッセージがセットされている' do
+        r = LicenseGroup.store(@n, @a)
+        r.errors[:base].should_not be_blank
+      end
+      it 'ライセンスが作成されていない' do
+        lambda {
+          r = LicenseGroup.store(@n, @a)
+        }.should_not change License, :count
+      end
+      it 'ライセンスグループが作成されていない' do
+        lambda {
+          r = LicenseGroup.store(@n, @a)
+        }.should_not change LicenseGroup, :count
+      end
     end
   end
   
@@ -144,6 +286,11 @@ describe LicenseGroup do
           LicenseGroup.import(@f)
         }.should change LicenseGroup, :count
       end
+      it 'ライセンスが追加される' do
+        lambda {
+          LicenseGroup.import(@f)
+        }.should change License, :count
+      end
       it '[]を返す' do
         r = LicenseGroup.import(@f)
         r.should eq []
@@ -160,6 +307,11 @@ describe LicenseGroup do
           r = LicenseGroup.import(@fs)
         }.should change(LicenseGroup, :count).by 2
       end
+      it 'ライセンスが追加される' do
+        lambda {
+          r = LicenseGroup.import(@fs)
+        }.should change(License, :count)
+      end
       it '[]を返す' do
         r = LicenseGroup.import(@fs)
         r.should eq []
@@ -218,78 +370,4 @@ describe LicenseGroup do
     end
   end
   
-  describe '単体取得に於いて' do
-    before do
-      @lg = Factory :license_group
-    end
-    it '指定のコマを返す' do
-      l = LicenseGroup.show @lg.id
-      l.should eq @lg
-    end
-  end
-  describe '関連テーブルプションに於いて' do
-    context 'オプションがないとき' do
-      it 'ライセンスを含んでいる' do
-        r = LicenseGroup.show_include_opt
-        r.has_key?(:licenses).should be_true
-      end
-    end
-  end
-  describe 'json単体出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = LicenseGroup.show_json_include_opt
-      r.has_key?(:include).should be_true
-    end
-    it '1つの項目を含んでいる' do
-      r = LicenseGroup.show_json_include_opt[:include]
-      r.should have(1).items
-    end
-    it 'ライセンスを含んでいる' do
-      r = LicenseGroup.show_json_include_opt[:include]
-      r.has_key?(:licenses).should be_true
-    end
-  end
-  describe '一覧取得に於いて' do
-    before do
-      @lg = Factory :license_group, :name => "1"
-    end
-    it 'リストを返す' do
-      l = LicenseGroup.list
-      l.should eq [@lg]
-    end
-    it '名前順で並んでいる' do
-      @lg2 = Factory :license_group, :name => "5", :url => 'http://test.ptn/10'
-      l = LicenseGroup.list
-      l.should eq [@lg, @lg2]
-    end
-  end
-  describe 'list関連テーブルプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = LicenseGroup.list_opt
-      r.has_key?(:include).should be_true
-    end
-    it '1つの項目を含んでいる' do
-      r = LicenseGroup.list_opt[:include]
-      r.should have(1).items
-    end
-    it 'ライセンスを含んでいる' do
-      r = LicenseGroup.list_opt[:include]
-      r.has_key?(:licenses).should be_true
-    end
-  end
-  describe 'json一覧出力オプションに於いて' do
-    it 'includeキーを含んでいる' do
-      r = LicenseGroup.list_json_opt
-      r.has_key?(:include).should be_true
-    end
-    it '1つの項目を含んでいる' do
-      r = LicenseGroup.list_json_opt[:include]
-      r.should have(1).items
-    end
-    it 'ライセンスを含んでいる' do
-      r = LicenseGroup.list_json_opt[:include]
-      r.has_key?(:licenses).should be_true
-    end
-  end
-  
 end