OSDN Git Service

update license test
[pettanr/pettanr.git] / spec / models / common_license_spec.rb
index 352eb92..389fd69 100644 (file)
@@ -1,5 +1,584 @@
+# -*- encoding: utf-8 -*-
+#コモンライセンス
 require 'spec_helper'
 
 describe CommonLicense do
-  pending "add some examples to (or delete) #{__FILE__}"
+  before do
+    #テストデータを用意してね
+    @f = Rails.root + 'spec/json/common_license.json'
+    @t = File.open(@f, 'r').read
+    @j = JSON.parse @t
+    @fs = Rails.root + 'spec/json/common_licenses.json'
+    @ts = File.open(@fs, 'r').read
+    @js = JSON.parse @ts
+    @fes = Rails.root + 'spec/json/invalid_common_licenses.json'
+    @tes = File.open(@fes, 'r').read
+    @jes = JSON.parse @tes
+  end
+  describe '検証に於いて' do
+    before do
+      @l = Factory :license
+    end
+    
+    it 'オーソドックスなデータなら通る' do
+      @cl = Factory.build :common_license, :license_id => @l.id
+      @cl.should be_valid
+    end
+    
+    context 'nameを検証するとき' do
+      before do
+        @cl = Factory.build :common_license, :license_id => @l.id
+      end
+      it 'テストデータの確認' do
+        @cl.name = 'CC by'
+        @cl.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @cl.name = ''
+        @cl.should_not be_valid
+      end
+      it '51文字以上なら失敗する' do
+        @cl.name = 'a'*51
+        @cl.should_not be_valid
+      end
+    end
+    context 'urlを検証するとき' do
+      before do
+        @cl = Factory.build :common_license, :license_id => @l.id
+      end
+      it 'テストデータの確認' do
+        @cl.url = 'CC by'
+        @cl.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @cl.url = ''
+        @cl.should_not be_valid
+      end
+      it '201文字以上なら失敗する' do
+        @cl.url = 'a'*201
+        @cl.should_not be_valid
+      end
+      it '重複していたら失敗する' do
+        cl = Factory :common_license, :license_id => @l.id
+        @cl.should_not be_valid
+      end
+      it 'url形式でなら失敗する' do
+        @cl.url = ''
+        pending
+      end
+    end
+    context 'license_idを検証するとき' do
+      before do
+        @cl = Factory.build :common_license
+      end
+      it 'テストデータの確認' do
+        @cl.license_id = @l.id
+        @cl.should be_valid
+      end
+      it 'nullなら失敗する' do
+        @cl.license_id = nil
+        @cl.should_not be_valid
+      end
+      it '数値でなければ失敗する' do
+        @cl.license_id = 'a'
+        @cl.should_not be_valid
+      end
+      it '存在するlicenseでなければ失敗する' do
+        @cl.license_id = 0
+        @cl.should_not be_valid
+      end
+    end
+  end
+  
+  describe '対象コモンライセンスの取得に於いて' do
+    before do
+      @lc = Factory :license
+      @cl = Factory :common_license, :license_id => @lc.id
+    end
+    context 'urlが一致するコモンライセンスがないとき' do
+      it '新規コモンライセンスを準備して返す' do
+        r = CommonLicense.update_common_license Factory.attributes_for(:common_license, :url => 'http://domain.no')
+        r.should be_a_new CommonLicense
+      end
+    end
+    context 'urlが一致するコモンライセンスがあるとき' do
+      it '該当コモンライセンスを返す' do
+        prm = Factory.attributes_for(:common_license)
+        r = CommonLicense.update_common_license prm
+        r.is_a?(CommonLicense).should be_true
+        r.should_not be_a_new CommonLicense
+        r[:url].should eq prm[:url]
+      end
+    end
+  end
+  
+  describe 'コモンライセンス更新に於いて' do
+    before do
+      @lc = Factory :license
+      @cl = Factory :common_license, :license_id => @lc.id
+      @attr = Factory.attributes_for(:common_license, :name => 'exist case')
+      @newattr = Factory.attributes_for(:common_license, :url => 'http://domain.no')
+    end
+    context 'つつがなく終わるとき' do
+      it '対象コモンライセンスを問い合わせている' do
+        CommonLicense.stub(:update_common_license).with(any_args).and_return(CommonLicense.new(@attr))
+        CommonLicense.should_receive(:update_common_license).exactly(1)
+        CommonLicense.store @attr
+      end
+      context '新規のとき' do
+        it 'コモンライセンスを保存しようとしている' do
+          CommonLicense.any_instance.should_receive(:save).exactly(1)
+          CommonLicense.store @newattr
+        end
+        it 'コモンライセンスが作成されている' do
+          lambda {
+            CommonLicense.store @newattr
+          }.should change CommonLicense, :count
+        end
+        context 'ライセンスとの連動' do
+          it 'ライセンスが作成されている' do
+            lambda {
+              CommonLicense.store @newattr
+            }.should change License, :count
+          end
+          it '両者がリンクされている' do
+            r = CommonLicense.store @newattr
+            l = License.find(r.license_id)
+            l.should be_true
+          end
+          it '属性が一致している' do
+            r = CommonLicense.store @newattr
+            l = License.find(r.license_id)
+            l.url.should eq r.url
+            l.name.should eq r.name
+          end
+        end
+      end
+      context '更新のとき' do
+        it 'コモンライセンスを保存しようとしている' do
+          CommonLicense.any_instance.should_receive(:save).exactly(1)
+          CommonLicense.store @attr
+        end
+        it 'コモンライセンスの数に変化がない' do
+          lambda {
+            CommonLicense.store @attr
+          }.should_not change CommonLicense, :count
+        end
+        context 'ライセンスとの連動' do
+          it 'ライセンスの数に変化がない' do
+            lambda {
+              CommonLicense.store @attr
+            }.should_not change License, :count
+          end
+          it '両者がリンクされている' do
+            r = CommonLicense.store @attr
+            l = License.find(r.license_id)
+            l.should be_true
+          end
+          it '属性が一致している' do
+            r = CommonLicense.store @attr
+            l = License.find(r.license_id)
+            l.url.should eq r.url
+            l.name.should eq r.name
+          end
+        end
+      end
+      it '属性が一致している' do
+        r = CommonLicense.store @newattr
+        r.url.should eq @newattr[:url]
+        r.name.should eq @newattr[:name]
+      end
+      it '保存されたCommonLicenseオブジェクトを返す' do
+        r = CommonLicense.store @newattr
+        r.should_not be_a_new CommonLicense
+      end
+    end
+    context 'ライセンスの作成に失敗するとき' do
+      before do
+        License.any_instance.stub(:save).with(any_args).and_return(false)
+      end
+      context '新規のとき' do
+        it 'ライセンスに変化がない' do
+          lambda {
+            r = CommonLicense.store @newattr
+          }.should_not change License, :count
+        end
+        it 'コモンライセンスに変化がない' do
+          lambda {
+            r = CommonLicense.store @newattr
+          }.should_not change CommonLicense, :count
+        end
+      end
+      context '更新のとき' do
+        it 'コモンライセンス属性に変化がない' do
+          lambda {
+            r = CommonLicense.store @attr
+          }.should_not change License.find(@cl.id), :name
+        end
+        it 'ライセンス属性に変化がない' do
+          lambda {
+            r = CommonLicense.store @attr
+          }.should_not change License.find(@lc.id), :name
+        end
+      end
+    end
+    context 'コモンライセンスの作成に失敗するとき' do
+      before do
+        CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
+      end
+      context '新規のとき' do
+        it 'ライセンスに変化がない' do
+          lambda {
+            r = CommonLicense.store @newattr
+          }.should_not change License, :count
+        end
+        it 'コモンライセンスに変化がない' do
+          lambda {
+            r = CommonLicense.store @newattr
+          }.should_not change CommonLicense, :count
+        end
+      end
+      context '更新のとき' do
+        it 'コモンライセンス属性に変化がない' do
+          lambda {
+            r = CommonLicense.store @attr
+          }.should_not change License.find(@cl.id), :name
+        end
+        it 'ライセンス属性に変化がない' do
+          lambda {
+            r = CommonLicense.store @attr
+          }.should_not change License.find(@lc.id), :name
+        end
+      end
+    end
+  end
+=begin  
+  describe '単体取得に於いて' do
+    before do
+      @cl = Factory.build :common_license
+      @cl.store
+    end
+    it '指定のライセンスを返す' do
+      l = CommonLicense.show @cl.id
+      l.should eq @cl
+    end
+    context '関連テーブルオプションがないとき' do
+      it 'ライセンスだけを含んでいる' do
+        r = CommonLicense.show_include_opt
+        r.should eq [:license]
+      end
+    end
+    context '関連テーブルオプションでコマを含ませたとき' do
+      it 'ライセンスと作家を含んでいる' do
+        r = CommonLicense.show_include_opt(:include => :author)
+        r.should eq [:license, :author]
+      end
+    end
+  end
+  describe '一覧取得に於いて' do
+    before do
+      @cl = Factory.build :common_license
+      @cl.store
+    end
+    context 'page補正について' do
+      it '文字列から数値に変換される' do
+        CommonLicense.page('8').should eq 8
+      end
+      it 'nilの場合は1になる' do
+        CommonLicense.page().should eq 1
+      end
+      it '0以下の場合は1になる' do
+        CommonLicense.page('0').should eq 1
+      end
+    end
+    context 'page_size補正について' do
+      it '文字列から数値に変換される' do
+        CommonLicense.page_size('7').should eq 7
+      end
+      it 'nilの場合はCommonLicense.default_page_sizeになる' do
+        CommonLicense.page_size().should eq CommonLicense.default_page_size
+      end
+      it '0以下の場合はCommonLicense.default_page_sizeになる' do
+        CommonLicense.page_size('0').should eq CommonLicense.default_page_size
+      end
+      it 'CommonLicense.max_page_sizeを超えた場合はCommonLicense.max_page_sizeになる' do
+        CommonLicense.page_size('1000').should eq CommonLicense.max_page_size
+      end
+    end
+    it 'リストを返す' do
+      l = CommonLicense.list
+      l.should eq [@cl]
+    end
+    it '名前順で並んでいる' do
+      n = Factory.build :common_license, :url => 'tes.to', :name => 'peta2.2'
+      n.store
+      l = CommonLicense.list
+      l.should eq [@cl, n]
+    end
+    context 'DBに5件あって1ページの件数を2件に変えたとして' do
+      before do
+        @license2 = Factory.build :common_license, :url => 'tes.to2', :name => 'peta2.2'
+        @license2.store
+        @license3 = Factory.build :common_license, :url => 'tes.to3', :name => 'peta2.3'
+        @license3.store
+        @license4 = Factory.build :common_license, :url => 'tes.to4', :name => 'peta2.4'
+        @license4.store
+        @license5 = Factory.build :common_license, :url => 'tes.to5', :name => 'peta2.5'
+        @license5.store
+        CommonLicense.stub(:default_page_size).and_return(2)
+      end
+      it '通常は2件を返す' do
+        l = CommonLicense.list
+        l.should have(2).items 
+      end
+      it 'page=1なら末尾2件を返す' do
+        #時系列で並んでいる
+        l = CommonLicense.list({}, 1)
+        l.should eq [@cl, @license2]
+      end
+      it 'page=2なら中間2件を返す' do
+        l = CommonLicense.list({}, 2)
+        l.should eq [@license3, @license4]
+      end
+      it 'page=3なら先頭1件を返す' do
+        l = CommonLicense.list({}, 3)
+        l.should eq [@license5]
+      end
+    end
+  end
+=end
+  
+  describe 'Json解析に於いて' do
+    before do
+    end
+    context 'テキストを渡されたとき' do
+      it 'Json解析している' do
+        JSON.should_receive(:parse).with(@t).exactly(1)
+        r = CommonLicense.parse @t
+      end
+      it '単数データならHashで返す' do
+        r = CommonLicense.parse @t
+        r.is_a?(Hash).should be_true
+      end
+      it '複数データならArrayで返す' do
+        r = CommonLicense.parse @ts
+        r.is_a?(Array).should be_true
+      end
+    end
+    context 'パース失敗したとき' do
+      it 'Falseを返す' do
+        JSON.should_receive(:parse).with(any_args).and_raise('StandardError')
+        r = CommonLicense.parse @t
+        r.should be_false
+      end
+    end
+  end
+  
+  describe 'Jsonの繰り返し処理に於いて' do
+    before do
+    end
+    context '単体データを渡されたとき' do
+      it '一回処理' do
+        r = []
+        CommonLicense.each_license @j do |i|
+          r << i
+        end
+        r.size.should eq 1
+      end
+    end
+    context '複数を渡されたとき' do
+      it '二回処理' do
+        r = []
+        CommonLicense.each_license @js do |i|
+          r << i
+        end
+        r.size.should eq 2
+      end
+    end
+  end
+  
+  describe 'テキスト取り込みに於いて' do
+    #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
+    #Licenseとの連動が完成していること
+    before do
+    end
+    context 'つつがなく終わるとき' do
+      it 'Json解析を依頼する' do
+        CommonLicense.should_receive(:parse).with(any_args).exactly(1)
+        CommonLicense.stub(:parse).with(any_args).and_return(@j)
+        CommonLicense.import(@t)
+      end
+      it '繰り返し処理を依頼する' do
+        CommonLicense.should_receive(:each_license).with(any_args).exactly(1)
+        CommonLicense.import(@t)
+      end
+      it 'ライセンス更新を一回依頼する' do
+        CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
+        CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
+        CommonLicense.should_receive(:store).with(any_args).exactly(1)
+        CommonLicense.import(@t)
+      end
+      it 'コモンライセンスが追加される' do
+        lambda {
+          CommonLicense.import(@t)
+        }.should change CommonLicense, :count
+      end
+      it '[]を返す' do
+        CommonLicense.import(@t).should eq []
+      end
+    end
+    context '複数データがつつがなく終わるとき' do
+      it 'ライセンス更新を二回依頼する' do
+        CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
+        CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(true)
+        CommonLicense.should_receive(:store).with(any_args).exactly(2)
+        CommonLicense.import(@ts)
+      end
+      it 'コモンライセンスが二個追加される' do
+        lambda {
+          CommonLicense.import(@ts)
+        }.should change(CommonLicense, :count).by 2
+      end
+      it '[]を返す' do
+        CommonLicense.import(@ts).should eq []
+      end
+    end
+    #例外ケース
+    context 'Json解析に失敗したとき' do
+      before do
+        CommonLicense.stub(:parse).with(any_args).and_return(false)
+      end
+      it 'コモンライセンスの数に変化がない' do
+        lambda {
+          CommonLicense.import(@t)
+        }.should_not change CommonLicense, :count
+      end
+      it 'Falseを返す' do
+        CommonLicense.import(@t).should be_false
+      end
+    end
+    context 'コモンライセンス作成に失敗したとき' do
+      before do
+        CommonLicense.any_instance.stub(:save).with(any_args).and_return(false)
+        CommonLicense.any_instance.stub(:valid?).with(any_args).and_return(false)
+      end
+      it 'コモンライセンスの数に変化がない' do
+        lambda {
+          CommonLicense.import(@t)
+        }.should_not change CommonLicense, :count
+      end
+      it '配列を返す' do
+        r = CommonLicense.import(@t)
+        r.is_a?(Array).should be_true
+      end
+      it '配列の中身は一件' do
+        r = CommonLicense.import(@t)
+        r.should have(1).items
+      end
+      it 'コモンライセンスオブジェクトが入っている' do
+        r = CommonLicense.import(@t)
+        r.first.is_a?(CommonLicense).should be_true
+      end
+    end
+    context '複数のコモンライセンス作成に失敗したとき' do
+      #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
+      it 'コモンライセンスの数に変化がない' do
+        lambda {
+          CommonLicense.import(@tes)
+        }.should_not change CommonLicense, :count
+      end
+      it '途中で保存に失敗しても全件更新依頼する' do
+        CommonLicense.stub(:store).with(any_args).and_return(CommonLicense.new)
+        CommonLicense.should_receive(:store).with(any_args).exactly(3)
+        CommonLicense.import(@tes)
+      end
+      it '配列を返す' do
+        r = CommonLicense.import(@tes)
+        r.is_a?(Array).should be_true
+      end
+      it '配列の中身は2件' do
+        r = CommonLicense.import(@tes)
+        r.should have(2).items
+      end
+      it '配列の中身は失敗したコモンライセンスオブジェクトが入っている' do
+        r = CommonLicense.import(@tes)
+        r[0].is_a?(CommonLicense).should be_true
+        r[0]["name"].should eq 'fail1'
+        r[1].is_a?(CommonLicense).should be_true
+        r[1]["name"].should eq 'fail2'
+      end
+    end
+  end
+  
+  describe 'インポートエラーの表示に於いて' do
+    before do
+      @l = Factory :license
+      @cl = Factory.build :common_license, :license_id => @l.id
+    end
+    it '全体エラーだけなら、そのまま返す' do
+      @cl.errors.add :base, 'base error'
+      @cl.import_error_message.should eq 'base error'
+    end
+    context '複数でエラーのとき' do
+      it '各エラーを改行で区切って結合して返す' do
+        @cl.errors.add :name, 'name error'
+        @cl.errors.add :url, 'url error'
+        @cl.import_error_message.should eq 'name error\nurl error'
+      end
+    end
+    context '区切り指定が<br>で複数でエラーのとき' do
+      it '各エラーを改行で区切って結合して返す' do
+        @cl.errors.add :name, 'name error'
+        @cl.errors.add :url, 'url error'
+        @cl.import_error_message('<br>').should eq 'name error<br>url error'
+      end
+    end
+  end
+  
+  describe 'ファイル取り込みに於いて' do
+    before do
+      CommonLicense.stub(:import).with(any_args).and_return(true)
+    end
+    context 'つつがなく終わるとき' do
+      before do
+        CommonLicense.stub(:import).with(any_args).and_return(true)
+      end
+      it 'ファイルを開いてテキストを読む' do
+        File.should_receive(:open).with(@f, 'r').exactly(1)
+        CommonLicense.import_file(@f)
+      end
+      it 'テキスト取り込みを依頼する' do
+        CommonLicense.should_receive(:import).with(any_args).exactly(1)
+        CommonLicense.import_file(@f)
+      end
+      #テキスト取り込み成功でTrueが返る
+      it 'Trueを返す' do
+        CommonLicense.import_file(@f).should be_true
+      end
+    end
+    context 'ファイルが開けないとき' do
+      before do
+        File.stub(:open).with(any_args).and_raise('StandardError')
+      end
+      it 'ファイルエラーのメッセージを出力する' do
+        pending
+      end
+      it 'Falseを返す' do
+        CommonLicense.import_file(@f).should be_false
+      end
+    end
+    #失敗したときは、失敗したライセンスが配列で返る
+    context 'テキスト取り込みが失敗したとき' do
+      before do
+        CommonLicense.stub(:import).with(any_args).and_return(false)
+      end
+      it '各コモンライセンスのエラーメッセージを出力する' do
+        pending
+      end
+      it 'Falseを返す' do
+        CommonLicense.import_file(@f).should be_false
+      end
+    end
+  end
+  
 end