+++ /dev/null
-# -*- encoding: utf-8 -*-
-require 'spec_helper'
-require File.expand_path(File.dirname(__FILE__) + '/import')
-#インポート処理
-
-describe Import do
- before do
- @t = '{"Z": {"a": 1, "b": "Z"}}'
- @j = JSON.parse @t
- @ts = '{"Z": {"a": 1, "b": "Z"}, "X": {"a": 2, "b": "X"}}'
- @js = JSON.parse @ts
- @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
- before do
- @attr = {"text" => 'PictureData'}
- @sp = FactoryGirl.create :system_picture
- @imager = ImagerTest.load "abc\ndef\nghi"
- end
- context '事前チェック' do
- it 'カラム値の"text"キーの値をBase64展開している' do
- Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
- Base64.should_receive(:decode64).with('PictureData').exactly(1)
- r = Import.import_system_picture(@attr)
- end
- it '画像データで画像ライブラリをロードを依頼している' do
- Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
- PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
- PettanImager.should_receive(:load).with('DecodedPictureData').exactly(1)
- r = Import.import_system_picture(@attr)
- end
- it 'システム画像に保存を依頼している' do
- Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
- PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
- SystemPicture.stub(:store).with(@imager).and_return(@sp)
- SystemPicture.should_receive(:store).with(@imager).exactly(1)
- r = Import.import_system_picture(@attr)
- end
- end
- context 'つつがなく終わるとき' do
- before do
- Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
- PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
- SystemPicture.stub(:store).with(@imager).and_return(@sp)
- end
- it 'システム画像オブジェクトのidを返す' do
- r = Import.import_system_picture(@attr)
- r.should eq @sp.id
- end
- end
- context '例外ケース' do
- it 'カラム値に画像データがなかったならFalseを返す' do
- @attr = {}
- Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
- r = Import.import_system_picture(@attr)
- r.should be_false
- end
- it '画像ライブラリのロードが失敗ならFalseを返す' do
- @attr = {"system_picture" => ''}
- Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
- PettanImager.stub(:load).with('DecodedPictureData').and_return(nil)
- r = Import.import_system_picture(@attr)
- r.should be_false
- end
- it 'システム画像の保存が失敗(False)ならFalseを返す' do
- Base64.stub(:decode64).with('PictureData').and_return('DecodedPictureData')
- PettanImager.stub(:load).with('DecodedPictureData').and_return(@imager)
- SystemPicture.stub(:store).with(@imager).and_return(false)
- r = Import.import_system_picture(@attr)
- r.should be_false
- end
- end
- end
-
- describe 'システム画像置換に於いて' do
- before do
- @sd = {"text" => 'PictureData'}
- @attr = {"hoge" => {"system_picture_id" => @sd, "other" => 'no replace'}}
- @sp = FactoryGirl.create :system_picture
- @imager = ImagerTest.load "abc\ndef\nghi"
- PettanImager.stub(:load).with(any_args).and_return(@imager)
- end
- context '事前チェック' do
- before do
- end
- it 'システム画像インポートを依頼している' do
- Import.stub(:import_system_picture).with(@sd).and_return(@sp.id)
- Import.should_receive(:import_system_picture).with(@sd).exactly(1)
- r = Import.replace_system_picture(@attr)
- end
- it 'システム画像置換を依頼している' do
-#再帰だからできない?
-# Import.stub(:replace_system_picture).with(@attr['hoge']).and_return({"system_picture_id" => @sp.id})
-# Import.should_receive(:replace_system_picture).with(@attr['hoge']).exactly(1)
-# r = Import.replace_system_picture(@attr)
- end
- end
- context 'つつがなく終わるとき' do
- before do
- Import.stub(:import_system_picture).with(@sd).and_return(@sp.id)
- end
- it 'カラム値を置換済カラム値として返す' do
- r = Import.replace_system_picture(@attr)
- r['hoge']['system_picture_id'].should eq @sp.id
- end
- end
- context '例外ケース' do
- it 'システム画像のインポートが失敗(False)ならFalseを返す' do
- Import.stub(:import_system_picture).with(any_args).and_return(false)
- r = Import.replace_system_picture(@attr)
- r.should be_false
- end
- it 'システム画像置換が失敗(False)ならFalseを返す' do
- Import.stub(:import_system_picture).with(any_args).and_return(@sp.id)
- Import.stub(:replace_system_picture).with(any_args).and_return(false)
- r = Import.replace_system_picture(@attr)
- r.should be_false
- end
- end
- end
- describe '対象オブジェクトの更新準備に於いて' do
- context 'つつがなく終わるとき' do
- before do
- @n = @j.keys.first
- @a = @j.values.first
- end
- it 'キーカラムをnameで補充している' do
- Import.stub(:find_by_name).with(any_args).and_return(nil)
- Import.should_receive(:__send__).with('find_by_name', @n).exactly(1)
- r = Import.modify_object(@n, @a)
- end
- it 'オブジェクト取得を依頼している' do
- Import.stub(:find_by_b).with(any_args).and_return(nil)
- Import.should_receive(:__send__).with('find_by_b', @n).exactly(1)
- r = Import.modify_object(@n, @a, :b)
- end
- it 'カラム書き換えを依頼している' do
- Import.create! @a
- Import.any_instance.should_receive(:attributes=).with(any_args).exactly(1)
- r = Import.modify_object(@n, @a, :b)
- end
- it 'オブジェクトを返す' do
- Import.create! @a
- r = Import.modify_object(@n, @a, :b)
- r.is_a?(Import).should be_true
- end
- end
- context 'キー値が一致する行がないとき' do
- before do
- @n = @j.keys.first
- @a = @j.values.first
- end
- it '新規オブジェクトを準備して返す' do
- r = Import.modify_object(@n, @a, :b)
- r.new_record?.should be_true
- end
- it 'オブジェクトが渡したデータで書き換えられている' do
- r = Import.modify_object(@n, @a, :b)
- r["a"].should eq 1
- r["b"].should eq "Z"
- end
- end
- context 'キー値が一致する行があるとき' do
- before do
- @n = @j.keys.first
- @a = @j.values.first
- Import.create!(@a)
- end
- it '該当行を返す' do
- r = Import.modify_object(@n, {:a => 3}, :b)
- r.is_a?(Import).should be_true
- r.new_record?.should be_false
- r[:b].should eq @n
- end
- it 'オブジェクトが渡したデータで書き換えられている' do
- r = Import.modify_object(@n, {:a => 3}, :b)
- r[:a].should eq 3
- end
- end
- end
-
- describe '繰り返し処理に於いて' do
- before do
- end
- context '単体データを渡されたとき' do
- it '一回処理' do
- r = []
- Import.each_import @j do |n, i|
- r << i
- end
- r.size.should eq 1
- end
- it '一回処理' do
- r = []
- Import.each_import @j do |n, i|
- r << n
- end
- r.first.should eq "Z"
- end
- it '一回処理' do
- r = []
- Import.each_import @j do |n, i|
- r << i
- end
- r.first["a"].should eq 1
- r.first["b"].should eq "Z"
- end
- end
- context '複数を渡されたとき' do
- it '二回処理' do
- r = []
- Import.each_import @js do |n, i|
- r << i
- end
- r.size.should eq 2
- end
- end
- end
-
- describe 'リストからのインポートに於いて' do
- #インポート失敗データ(オブジェクト)のすべてを配列で返す
- #成功したときは空の配列を返す
- before do
- end
- context '事前チェック' do
- 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_list(@j) {|name, attr| Import.etest ; Import.new(attr) }
- end
- end
- context '複数データがつつがなく終わるとき' do
- 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_list(@js) {|name, attr| Import.etest ; Import.new(attr)}
- end
- end
- 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_list(@j){|name, attr| Import.new(attr)}
- r.is_a?(Array).should be_true
- end
- it '配列の中身は一件' do
- r = Import.import_list(@j){|name, attr| Import.new(attr)}
- r.should have(1).items
- end
- it 'オブジェクトが入っている' do
- r = Import.import_list(@j){|name, attr| Import.new(attr)}
- r.first.is_a?(Import).should be_true
- end
- end
- context '複数の作成に失敗したとき' do
- #三件中、二件の失敗、一件を成功させ、成功データは戻り値に含まないことを確認する
- it '行の数に変化がない' do
- lambda {
- Import.import_list(@jes){|name, attr| Import.create(attr)}
- }.should_not change Import, :count
- end
- it '途中で保存に失敗しても全件更新依頼する' do
- #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_list(@jes){|name, attr| Import.create(attr)}
- r.is_a?(Array).should be_true
- end
- it '配列の中身は2件' do
- r = Import.import_list(@jes){|name, attr| Import.create(attr)}
- r.should have(2).items
- end
- it '配列の中身は失敗したオブジェクトが入っている' do
- 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
- r[1]["a"].should eq 0
- end
- end
- end
-
- 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
- end
- context '事前チェック' do
- before do
- end
- it 'テキスト取り込みを依頼する' do
- Import.should_receive(:import_text).with(any_args).exactly(1)
- Import.import_file(@f){|name, attr| Import.new(attr) }
- end
- 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
- lambda {
- Import.import_file(@f){|name, attr| Import.new(attr)}
- }.should raise_error
- end
- end
- end
-
- describe 'Urlからのインポートに於いて' do
- before do
- end
- context '事前チェック' do
- before do
- end
- 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
- 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
-