OSDN Git Service

t#30073:importer add source
[pettanr/pettanr.git] / vendor / plugins / pettan_importer / test / import_spec.rb
index 83052dd..7d3c1e0 100644 (file)
@@ -1,6 +1,6 @@
 # -*- encoding: utf-8 -*-
 require 'spec_helper'
-require File.expand_path(File.dirname(__FILE__) + '/import')\r
+require File.expand_path(File.dirname(__FILE__) + '/import')
 #インポート処理
 
 describe Import do
@@ -12,6 +12,7 @@ describe Import do
     @tes = '{"1": {"a": 0}, "2": {"a": 0}, "Z": {"a": 2, "b": "Z"}}'
     @jes = JSON.parse @tes
     @f = File.expand_path(File.dirname(__FILE__) + '/import.json')
+    @u = 'http://localhost:3000/profile.json'
   end
   
   describe 'システム画像インポートに於いて' do
@@ -220,64 +221,48 @@ describe Import do
     end
   end
   
-  describe 'テキスト取り込みに於いて' do
-    #成功でTrue、パース失敗でFalse、失敗は保存エラーのモデルを配列で返す
+  describe 'リストからのインポートに於いて' do
+    #インポート失敗データ(オブジェクト)のすべてを配列で返す
+    #成功したときは空の配列を返す 
     before do
     end
-    context 'つつがなく終わるとき' do
-      it 'Json解析を依頼する' do
-        JSON.should_receive(:parse_no_except).with(any_args).exactly(1)
-        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
-        Import.import_text(@t) {|name, attr| Import.new(attr)}
-      end
+    context '事前チェック' do
       it '更新を一回依頼する' do
-        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
         Import.any_instance.stub(:valid?).with(any_args).and_return(true)
         #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
         Import.should_receive(:etest).with(any_args).exactly(1)
-        Import.import_text(@t) {|name, attr| Import.etest ; Import.new(attr) }
-      end
-      it '[]を返す' do
-        JSON.stub(:parse_no_except).with(any_args).and_return(@j)
-        Import.any_instance.stub(:valid?).with(any_args).and_return(true)
-        Import.import_text(@t) {|name, attr| Import.new(attr)}.should eq []
+        Import.import_list(@j) {|name, attr| Import.etest ; Import.new(attr) }
       end
     end
     context '複数データがつつがなく終わるとき' do
-      it 'ライセンス更新を二回依頼する' do
-        JSON.stub(:parse_no_except).with(any_args).and_return(@js)
+      it '更新を二回依頼する' do
         Import.any_instance.stub(:valid?).with(any_args).and_return(true)
         #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
         Import.should_receive(:etest).with(any_args).exactly(2)
-        Import.import_text(@ts) {|name, attr| Import.etest ; Import.new(attr)}.should eq []
-      end
-      it '[]を返す' do
-        Import.import_text(@ts) {|name, attr| Import.new(attr)}.should eq []
+        Import.import_list(@js) {|name, attr| Import.etest ; Import.new(attr)}
       end
     end
-    #例外ケース
-    context 'Json解析に失敗したとき' do
-      before do
-        JSON.stub(:parse_no_except).with(any_args).and_return(false)
-      end
-      it 'Falseを返す' do
-        Import.import_text(@t){|name, attr| Import.new(attr)}.should be_false
+    context 'つつがなく終わるとき' do
+      it '[]を返す' do
+        r = Import.import_list(@js) {|name, attr| Import.new(attr)}
+        r.should eq []
       end
     end
+    #注意ケース
     context '作成に失敗したとき' do
       before do
         Import.any_instance.stub(:valid?).with(any_args).and_return(false)
       end
       it '配列を返す' do
-        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r = Import.import_list(@j){|name, attr| Import.new(attr)}
         r.is_a?(Array).should be_true
       end
       it '配列の中身は一件' do
-        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r = Import.import_list(@j){|name, attr| Import.new(attr)}
         r.should have(1).items
       end
       it 'オブジェクトが入っている' do
-        r = Import.import_text(@t){|name, attr| Import.new(attr)}
+        r = Import.import_list(@j){|name, attr| Import.new(attr)}
         r.first.is_a?(Import).should be_true
       end
     end
@@ -285,22 +270,24 @@ describe Import do
       #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
       it '行の数に変化がない' do
         lambda {
-          Import.import_text(@tes){|name, attr| Import.create(attr)}
+          Import.import_list(@jes){|name, attr| Import.create(attr)}
         }.should_not change Import, :count
       end
       it '途中で保存に失敗しても全件更新依頼する' do
-        Import.import_text(@tes){|name, attr| Import.create(attr)}
+        #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
+        Import.should_receive(:etest).with(any_args).exactly(3)
+        Import.import_list(@jes){|name, attr| Import.etest ; Import.create(attr)}
       end
       it '配列を返す' do
-        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r = Import.import_list(@jes){|name, attr| Import.create(attr)}
         r.is_a?(Array).should be_true
       end
       it '配列の中身は2件' do
-        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r = Import.import_list(@jes){|name, attr| Import.create(attr)}
         r.should have(2).items
       end
       it '配列の中身は失敗したオブジェクトが入っている' do
-        r = Import.import_text(@tes){|name, attr| Import.create(attr)}
+        r = Import.import_list(@jes){|name, attr| Import.create(attr)}
         r[0].is_a?(Import).should be_true
         r[0]["a"].should eq 0
         r[1].is_a?(Import).should be_true
@@ -309,60 +296,157 @@ describe Import do
     end
   end
   
-  describe 'インポートエラーの表示に於いて' do
+  describe 'テキスト取り込みに於いて' do
+    #インポート失敗データ(オブジェクト)のすべてを配列で返す
+    #成功したときは空の配列を返す 
+    before do
+    end
+    context '事前チェック' do
+      it 'Json解析を依頼する' do
+        JSON.stub(:parse).with(@t).and_return(@j)
+        JSON.should_receive(:parse).with(@t).exactly(1)
+        Import.import_text(@t) {|name, attr| Import.new(attr)}
+      end
+      it 'リストからのインポートを依頼する' do
+        JSON.stub(:parse).with(@t).and_return(@j)
+        Import.any_instance.stub(:import_list).with(@j).and_return([])
+        Import.should_receive(:import_list).with(@j).exactly(1)
+        Import.import_text(@t) {|name, attr| Import.new(attr) }
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it '[]を返す' do
+        Import.import_text(@t) {|name, attr| Import.new(attr)}.should eq []
+      end
+    end
+    #例外ケース
+    context 'Json解析に失敗したとき' do
+      before do
+      end
+      it '処理を中断する' do
+        lambda {
+          Import.import_text(''){|name, attr| Import.new(attr)}
+        }.should raise_error
+      end
+    end
   end
   
   describe 'ファイル取り込みに於いて' do
     before do
-      Import.stub(:import).with(any_args).and_return(true)
     end
-    context 'つつがなく終わるとき' do
+    context '事前チェック' do
       before do
-        Import.stub(:import).with(any_args).and_return(true)
-      end
-      it 'ファイルを開いてテキストを読む' do
-        File.should_receive(:open).with(@f, 'r').exactly(1)
-        Import.import_file(@f){|name, attr| Import.new(attr) }
       end
       it 'テキスト取り込みを依頼する' do
         Import.should_receive(:import_text).with(any_args).exactly(1)
         Import.import_file(@f){|name, attr| Import.new(attr) }
       end
-      it 'ブロックがあるとき更新を一回依頼する' do
-        Import.any_instance.stub(:valid?).with(any_args).and_return(true)
-        #newでスタブを作るとインスタンス生成ができないので、テスト用スタブで
-        Import.should_receive(:etest).with(any_args).exactly(1)
-        Import.import_file(@f) {|name, attr| Import.etest ; Import.new(attr) }
-      end
-      #テキスト取り込み成功でTrueが返る
-      it 'Trueを返す' do
-        Import.import_file(@f){|name, attr| Import.new(attr) }.should be_true
+    end
+    context 'つつがなく終わるとき' do
+      it 'インポート結果を返す' do
+        Import.stub(:import_text).with(any_args).and_return([])
+        r = Import.import_file(@f){|name, attr| Import.new(attr) }
+        r.is_a?(Array).should be_true
+        r.should be_empty
       end
     end
     context 'ファイルが開けないとき' do
       before do
         File.stub(:open).with(any_args).and_raise('StandardError')
       end
-      it 'ファイルエラーのメッセージを出力する' do
-        pending
-      end
-      it 'Falseを返す' do
-        Import.import_file(@f){|name, attr| Import.new(attr) }.should be_false
+      it '処理を中断する' do
+        lambda {
+          Import.import_file(@f){|name, attr| Import.new(attr)}
+        }.should raise_error
       end
     end
-    #失敗したときは、失敗したライセンスが配列で返る
-    context 'テキスト取り込みが失敗したとき' do
+  end
+  
+  describe 'Urlからのインポートに於いて' do
+    before do
+    end
+    context '事前チェック' do
       before do
-        Import.stub(:import_text).with(any_args).and_return(false)
       end
-      it '各コモンライセンスのエラーメッセージを出力する' do
-        pending
+      it 'テキスト取り込みを依頼する' do
+        Import.should_receive(:import_text).with(any_args).exactly(1)
+        Import.import_url(@u){|name, attr| Import.new(attr) }
+      end
+    end
+    context 'つつがなく終わるとき' do
+      it 'インポート結果を返す' do
+        Import.stub(:import_text).with(any_args).and_return([])
+        r = Import.import_url(@u){|name, attr| Import.new(attr) }
+        r.is_a?(Array).should be_true
+        r.should be_empty
       end
-      it 'Falseを返す' do
-        Import.import_file(@f) {|name, attr| Import.new(attr) }.should be_false
+    end
+    context 'ファイルが開けないとき' do
+      it '処理を中断する' do
+        lambda {
+          Import.import_url('http://localhost:3000/noroute'){|name, attr| Import.new(attr)}
+        }.should raise_error
       end
     end
   end
   
+  describe '文献からのインポートに於いて' do
+    before do
+      @urls = ['urla', 'urlb']
+    end
+    context '事前チェック' do
+      before do
+      end
+      it 'Urlからのインポートを依頼する' do
+        Import.stub(:import_url).with(any_args).and_return([])
+        Import.should_receive(:import_url).with(any_args).exactly(2)
+        Import.import_urls(@urls){|name, attr| Import.new(attr) }
+      end
+    end
+    #戻り
+    #各urlのインポート結果
+    #  Hash:キー値はurl、値はHash
+    #    キー値がexceptionのとき、値はHashで、location例外発生場所とmessage例外発生理由
+    #    キー値がvalidationsのとき、値は配列で、インポート失敗データ(オブジェクト)のすべて 
+    context 'つつがなく終わるとき' do
+      it 'インポート結果を返す' do
+        Import.stub(:import_url).with(any_args).and_return([])
+        r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
+        r.is_a?(Hash).should be_true
+        r.should have(2).items
+        r['urla'].is_a?(Hash).should be_true
+        r['urla'].should have(1).items
+        r['urla'][:validations].is_a?(Array).should be_true
+        r['urla'][:validations].should be_empty
+        r['urlb'].is_a?(Hash).should be_true
+        r['urlb'].should have(1).items
+        r['urlb'][:validations].is_a?(Array).should be_true
+        r['urlb'][:validations].should be_empty
+      end
+    end
+    context 'Urlからのインポートで何らかの例外が発生したとき' do
+      before do
+        Import.stub(:import_url).with(any_args).and_raise('StandardError')
+      end
+      it 'インポート結果Hashにexceptionをキーを追加する' do
+        r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
+        r.is_a?(Hash).should be_true
+        r.should have(2).items
+        r['urla'].is_a?(Hash).should be_true
+        r['urla'].should have(1).items
+        r['urlb'].is_a?(Hash).should be_true
+        r['urlb'].should have(1).items
+      end
+      it '例外発生場所と例外発生理由を追加する' do
+        r = Import.import_urls(@urls){|name, attr| Import.new(attr) }
+        r['urla'][:exception].is_a?(Hash).should be_true
+        r['urla'][:exception][:location].should_not be_nil
+        r['urla'][:exception][:message].should_not be_nil
+        r['urlb'][:exception].is_a?(Hash).should be_true
+        r['urlb'][:exception][:location].should_not be_nil
+        r['urlb'][:exception][:message].should_not be_nil
+      end
+    end
+  end
 end