From e586fa2a510c607181a3fa9e968497bf143986bf Mon Sep 17 00:00:00 2001 From: yasushiito Date: Mon, 17 Dec 2012 14:10:30 +0900 Subject: [PATCH] t#30326:create provider_artist --- app/controllers/application_controller.rb | 4 +- app/controllers/demanders_controller.rb | 12 +- app/controllers/provider_statuses_controller.rb | 18 +- app/models/artist.rb | 12 ++ app/models/demander.rb | 5 + app/models/provider_artist.rb | 45 +++++ app/views/artists/browse.html.erb | 7 + app/views/artists/list.html.erb | 8 +- .../provider_statuses/artists_import.html.erb | 29 ++++ app/views/provider_statuses/show.html.erb | 3 + config/locales/pettanr.ja.yml | 8 + config/routes.rb | 2 + .../20121216060750_create_provider_artists.rb | 11 ++ spec/controllers/demanders_controller_spec.rb | 87 +++++++++- .../provider_statuses_controller_spec.rb | 126 +++++++++++++- spec/factories.rb | 7 + spec/models/artist_spec.rb | 17 ++ spec/models/demander_spec.rb | 49 +++++- spec/models/provider_artist_spec.rb | 187 +++++++++++++++++++++ spec/models/provider_license_spec.rb | 2 +- 20 files changed, 625 insertions(+), 14 deletions(-) create mode 100644 app/models/provider_artist.rb create mode 100644 app/views/provider_statuses/artists_import.html.erb create mode 100644 db/migrate/20121216060750_create_provider_artists.rb create mode 100644 spec/models/provider_artist_spec.rb diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d802fbf8..5bbc56b9 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -98,9 +98,9 @@ class ApplicationController < ActionController::Base date end - def export_url demander_url, token, date + def export_url demander_url, action, token, date u = demander_url + (demander_url[-1] == '/' ? '' : '/') - u = URI.join(u, 'licenses_export.json?auth_token=' + token) + u = URI.join(u, action + '.json?auth_token=' + token) u = URI.join(u, '&date=' + date) unless date.blank? u.to_s end diff --git a/app/controllers/demanders_controller.rb b/app/controllers/demanders_controller.rb index 9549dedb..22ee9800 100644 --- a/app/controllers/demanders_controller.rb +++ b/app/controllers/demanders_controller.rb @@ -1,6 +1,6 @@ class DemandersController < ApplicationController layout 'test' if MagicNumber['test_layout'] - before_filter :authenticate_demand_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy, :req, :licenses_export] + before_filter :authenticate_demand_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy, :req, :licenses_export, :artists_export] def index @demander = @demand_user.demander @@ -106,4 +106,14 @@ class DemandersController < ApplicationController end end + def artists_export + @demander = @demand_user.demander + date = ymd_to_time params[:date] + @artists = @demander.artists_export(date) + respond_to do |format| + format.html { render :text } + format.json { render :json => @artists.to_json } + end + end + end diff --git a/app/controllers/provider_statuses_controller.rb b/app/controllers/provider_statuses_controller.rb index 866fc146..9395eea5 100644 --- a/app/controllers/provider_statuses_controller.rb +++ b/app/controllers/provider_statuses_controller.rb @@ -1,6 +1,6 @@ class ProviderStatusesController < ApplicationController layout 'test' if MagicNumber['test_layout'] - before_filter :authenticate_admin!, :only => [:index, :show, :edit, :update, :destroy, :licenses_import] + before_filter :authenticate_admin!, :only => [:index, :show, :edit, :update, :destroy, :licenses_import, :artists_import] def index @page = ProviderStatus.page params[:page] @@ -63,11 +63,23 @@ class ProviderStatusesController < ApplicationController @provider_status = ProviderStatus.show(params[:id], @admin) raise ActiveRecord::Forbidden if @provider_status.status == 0 t = ymd_to_time params[:date] - url = export_url @provider_status.provider.demander_url, @provider_status.token, t + url = export_url @provider_status.provider.demander_url, 'licenses_export', @provider_status.token, t @failures = ProviderLicense.import @provider_status.provider.id, export_from_provider(url) respond_to do |format| format.html # show.html.erb - format.json { render :json => @provider_status.to_json(ProviderStatus.show_json_opt) } + format.json { render :json => @failures.to_json() } + end + end + + def artists_import + @provider_status = ProviderStatus.show(params[:id], @admin) + raise ActiveRecord::Forbidden if @provider_status.status == 0 + t = ymd_to_time params[:date] + url = export_url @provider_status.provider.demander_url, 'artists_export', @provider_status.token, t + @failures = ProviderArtist.import @provider_status.provider.id, export_from_provider(url) + respond_to do |format| + format.html # show.html.erb + format.json { render :json => @failures.to_json() } end end diff --git a/app/models/artist.rb b/app/models/artist.rb index 4dc1e50a..93c6716f 100644 --- a/app/models/artist.rb +++ b/app/models/artist.rb @@ -114,4 +114,16 @@ class Artist < ActiveRecord::Base Artist.count :conditions => ['artists.author_id is not null'] end + def self.export(dt = nil) + opt = {} + cond = if dt + ['artists.author_id is not null and artists.updated_at >= ?', dt] + else + 'artists.author_id is not null' + end + opt.merge!({:conditions => cond}) + opt.merge!({:order => 'id'}) + Artist.find(:all, opt) + end + end diff --git a/app/models/demander.rb b/app/models/demander.rb index 7a882e08..61bc4088 100644 --- a/app/models/demander.rb +++ b/app/models/demander.rb @@ -123,4 +123,9 @@ class Demander < ActiveRecord::Base License.export date_str end + def artists_export date_str = nil + raise ActiveRecord::Forbidden unless self.status == 3 + Artist.export date_str + end + end diff --git a/app/models/provider_artist.rb b/app/models/provider_artist.rb new file mode 100644 index 00000000..6f2d6d13 --- /dev/null +++ b/app/models/provider_artist.rb @@ -0,0 +1,45 @@ +class ProviderArtist < ActiveRecord::Base + belongs_to :provider + belongs_to :artist, :foreign_key => :demanders_artist_id + + validates :provider_id, :presence => true, :numericality => true, :existence => true + validates :providers_artist_id, :presence => true, :numericality => true + validates :demanders_artist_id, :presence => true, :numericality => true + + def self.get_one pid, paid + ProviderArtist.find_by_provider_id_and_providers_artist_id(pid, paid) || ProviderArtist.new + end + + def modify_artist attr + ar = if self.artist + self.artist + else + Artist.new + end + ar.attributes = attr + ar.author_id = nil + ar.save + ar + end + + def self.import pid, providers_artists + res = [] + ProviderArtist.transaction do + providers_artists.each do |providers_artist_attr| + provider_artist = ProviderArtist.get_one(pid, providers_artist_attr['id']) + demander_artist = provider_artist.modify_artist providers_artist_attr + if demander_artist.valid? + provider_artist.attributes = {:provider_id => pid, :providers_artist_id => providers_artist_attr['id'], :demanders_artist_id => demander_artist.id} + unless provider_artist.save + res << providers_artist_attr + end + else + res << providers_artist_attr + end + end + raise ActiveRecord::Rollback if res.any? + end + res + end + +end diff --git a/app/views/artists/browse.html.erb b/app/views/artists/browse.html.erb index 2602e3aa..17c920ee 100644 --- a/app/views/artists/browse.html.erb +++ b/app/views/artists/browse.html.erb @@ -12,6 +12,13 @@

+ <%= t_m 'Artist.author_id' -%>: + <% if @ar.author_id %> + <%= link_to @ar.author_id, browse_author_path(@ar.author) %> + <% end %> +

+ +

<%= t_m 'Artist.created_at' -%>: <%= l @ar.created_at %>

diff --git a/app/views/artists/list.html.erb b/app/views/artists/list.html.erb index abca0851..99947d0c 100644 --- a/app/views/artists/list.html.erb +++ b/app/views/artists/list.html.erb @@ -4,14 +4,20 @@ <%= t_m 'Artist.id' -%> <%= t_m 'Artist.name' -%> + <%= t_m 'Artist.author_id' -%> <%= t_m 'Artist.created_at' -%> <%= t_m 'Artist.updated_at' -%> - + <% @artists.each do |artist| %> <%= link_to artist.id, browse_artist_path(artist) %> <%= h(truncate(artist.name, :length => 12)) %> + + <% if artist.author_id %> + <%= link_to artist.author_id, browse_author_path(artist.author) %> + <% end %> + <%= l artist.created_at %> <%= l artist.updated_at %> diff --git a/app/views/provider_statuses/artists_import.html.erb b/app/views/provider_statuses/artists_import.html.erb new file mode 100644 index 00000000..0f96f7b9 --- /dev/null +++ b/app/views/provider_statuses/artists_import.html.erb @@ -0,0 +1,29 @@ +

<%= t('.title') %>

+

<%= notice %>

+ +<% if @failures.empty? %> +

+ <%= t('provider_statuses.artists_import.success') %> +

+<% else %> +

+ <%= t('provider_statuses.artists_import.fail', :size => @failures.size) %> +

+ + + + + + <% @failures.each do |artist| %> + + + + + <% end %> +
<%= t_m 'License.caption' -%><%= t_m 'License.name' -%>
+ <%= link_to h(artist['caption']), artist['url'] %> + + <%= h(artist['name']) %> +
+ <%= link_to t('provider_statuses.artists_import.to_show'), provider_status_path(@provider_status) %> +<% end %> diff --git a/app/views/provider_statuses/show.html.erb b/app/views/provider_statuses/show.html.erb index 8d772e9a..4baeac98 100644 --- a/app/views/provider_statuses/show.html.erb +++ b/app/views/provider_statuses/show.html.erb @@ -37,6 +37,9 @@ <%= link_to t('provider_statuses.show.licenses_import'), licenses_import_provider_status_path(@provider_status), :method => :post %>

+ <%= link_to t('provider_statuses.show.artists_import'), artists_import_provider_status_path(@provider_status), :method => :post %> +

+

<%= link_to t('provider_statuses.show.receive_stop'), provider_status_path(@provider_status), :method => :delete %>

<% end %> diff --git a/config/locales/pettanr.ja.yml b/config/locales/pettanr.ja.yml index 9ab78885..98283864 100644 --- a/config/locales/pettanr.ja.yml +++ b/config/locales/pettanr.ja.yml @@ -263,6 +263,9 @@ ja: created_at: 作成 updated_at: 更新 provider_artist: + provider_id: 貸手 + providers_artist_id: 貸手側絵師 + demanders_artist_id: 借手側絵師 created_at: 作成 updated_at: 更新 provider_original_picture: @@ -635,6 +638,11 @@ ja: success: インポートが完了しました fail: 次のライセンス(%{size}件)でエラーがありました。ライセンスのインストール状況を確認してください。 to_show: 借受状況を表示する + artists_import: + title: 絵師インポート + success: インポートが完了しました + fail: 次の絵師(%{size}件)でエラーがありました。 + to_show: 借受状況を表示する status: 状態 open: 開く to_provider: 貸手に切り替える diff --git a/config/routes.rb b/config/routes.rb index f0b5e89f..d00e5231 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -299,6 +299,7 @@ Pettanr::Application.routes.draw do put :update delete :destroy post :licenses_import + post :artists_import end end resources :demanders do @@ -313,6 +314,7 @@ Pettanr::Application.routes.draw do get :browse post :req get :licenses_export + get :artists_export end member do get :edit diff --git a/db/migrate/20121216060750_create_provider_artists.rb b/db/migrate/20121216060750_create_provider_artists.rb new file mode 100644 index 00000000..0bf3525c --- /dev/null +++ b/db/migrate/20121216060750_create_provider_artists.rb @@ -0,0 +1,11 @@ +class CreateProviderArtists < ActiveRecord::Migration + def change + create_table :provider_artists do |t| + t.integer :provider_id, :null => false, :default => 0 + t.integer :providers_artist_id, :null => false, :default => 0 + t.integer :demanders_artist_id, :null => false, :default => 0 + + t.timestamps + end + end +end diff --git a/spec/controllers/demanders_controller_spec.rb b/spec/controllers/demanders_controller_spec.rb index ed7b6f01..08af954f 100644 --- a/spec/controllers/demanders_controller_spec.rb +++ b/spec/controllers/demanders_controller_spec.rb @@ -9,7 +9,7 @@ describe DemandersController do @lg = FactoryGirl.create :license_group @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id @user = FactoryGirl.create :user_yas - @author = @user.author #ユーザ作成時に連動して作成される + @author = FactoryGirl.create :author, :user_id => @user.id @demand_user = FactoryGirl.create :demand_user end @@ -685,4 +685,89 @@ describe DemandersController do end end end + + describe '絵師エクスポートに於いて' do + before do + @ds = FactoryGirl.create :demander_status + @demander = FactoryGirl.create :demander, :demander_status_id => @ds.id, :demand_user_id => @demand_user.id + @artist = FactoryGirl.create :artist, :author_id => @author.id + sign_in @demand_user + end + context 'つつがなく終わるとき' do + before do + Demander.any_instance.stub(:artists_export).with(any_args).and_return([@artist, @artist, @artist]) + end + it 'ステータスコード200 OKを返す' do + get :artists_export, :format => :json + response.should be_success + end + it '日付文字列変換を依頼している' do + DemandersController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1) + get :artists_export, :date => '20111010', :format => :json + end + it '借手モデルに絵師エクスポートを問い合わせている' do + Demander.any_instance.should_receive(:artists_export).with(Time.parse('2011/10/10')).exactly(1) + get :artists_export, :date => '20111010', :format => :json + end + it '@artistsにリストを取得している' do + get :artists_export, :format => :json + assigns(:artists).should have_at_least(3).items + end + context 'html形式' do + end + context 'json形式' do + it 'jsonデータを返す' do + get :artists_export, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがリスト構造になっている' do + get :artists_export, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + it 'リストの先頭くらいは絵師っぽいものであって欲しい' do + get :artists_export, :format => :json + json = JSON.parse response.body + json.first.has_key?("name").should be_true + json.first.has_key?("author_id").should be_true + end + end + end + context '借手権限がないとき' do + before do + sign_out @demand_user + end + it 'ステータスコード302 Foundを返す' do + get :artists_export + response.status.should eq 302 + end + context 'html形式' do + it 'サインインページへ遷移する' do + get :artists_export + response.body.should redirect_to '/demand_users/sign_in' + end + end + context 'json形式' do + it '応答メッセージにUnauthorizedを返す' do + get :artists_export, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '借受権限がないとき' do + before do + Demander.any_instance.stub(:status).and_return(1) + end + context 'html形式' do + end + context 'json形式' do + it 'ステータスコード403 forbiddenを返す' do + lambda{ + get :artists_export, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + end + end diff --git a/spec/controllers/provider_statuses_controller_spec.rb b/spec/controllers/provider_statuses_controller_spec.rb index ed08f0ef..db2c7b3e 100644 --- a/spec/controllers/provider_statuses_controller_spec.rb +++ b/spec/controllers/provider_statuses_controller_spec.rb @@ -9,7 +9,8 @@ describe ProviderStatusesController do @lg = FactoryGirl.create :license_group @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id @user = FactoryGirl.create :user_yas - @author = @user.author #ユーザ作成時に連動して作成される + @author = FactoryGirl.create :author, :user_id => @user.id + @artist = FactoryGirl.create :artist, :author_id => @author.id end describe '一覧表示に於いて' do @@ -369,7 +370,7 @@ describe ProviderStatusesController do before do ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1) ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil) - ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps) + ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/licenses_export/1.json') ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes]) ProviderLicense.stub(:import).with(any_args).and_return([]) end @@ -383,7 +384,7 @@ describe ProviderStatusesController do end it 'エクスポートurl取得を依頼している' do ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10')) - ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, @ps.token, Time.parse('2011/10/10')).exactly(1) + ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, 'licenses_export', @ps.token, Time.parse('2011/10/10')).exactly(1) get :licenses_import, :id => @ps.id, :date => '20111010' end it '貸手からのエクスポートを依頼している' do @@ -400,7 +401,7 @@ describe ProviderStatusesController do ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1) ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10')) ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil) - ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return(@ps) + ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/licenses_export/1.json') ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@license.attributes]) ProviderLicense.stub(:import).with(any_args).and_return([]) end @@ -475,4 +476,121 @@ describe ProviderStatusesController do end end + describe '絵師インポートに於いて' do + before do + @ps = FactoryGirl.create :provider_status, :token => 'aaaaaaaaaaaaaaaaa' + @provider = FactoryGirl.create :provider, :provider_status_id => @ps.id + ProviderStatus.stub(:show).and_return(@ps) + sign_in @admin + end + context '事前チェックしておく' do + before do + ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1) + ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil) + ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/artists_export/1.json') + ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@artist.attributes]) + ProviderArtist.stub(:import).with(any_args).and_return([]) + end + it '借受状況モデルに単体取得を問い合わせている' do + ProviderStatus.should_receive(:show).exactly(1) + get :artists_import, :id => @ps.id + end + it '日付文字列変換を依頼している' do + ProviderStatusesController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1) + get :licenses_import, :id => @ps.id, :date => '20111010' + end + it 'エクスポートurl取得を依頼している' do + ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10')) + ProviderStatusesController.any_instance.should_receive(:export_url).with(@provider.demander_url, 'artists_export', @ps.token, Time.parse('2011/10/10')).exactly(1) + get :artists_import, :id => @ps.id, :date => '20111010' + end + it '貸手からのエクスポートを依頼している' do + ProviderStatusesController.any_instance.should_receive(:export_from_provider).exactly(1) + get :artists_import, :id => @ps.id + end + it '絵師対照表モデルにインポートを依頼している' do + ProviderArtist.should_receive(:import).exactly(1) + get :artists_import, :id => @ps.id + end + end + context 'つつがなく終わるとき' do + before do + ProviderStatus.any_instance.stub(:status).with(any_args).and_return(1) + ProviderStatusesController.any_instance.stub(:ymd_to_time).with('20111010').and_return(Time.parse('2011/10/10')) + ProviderStatusesController.any_instance.stub(:ymd_to_time).with(any_args).and_return(nil) + ProviderStatusesController.any_instance.stub(:export_url).with(any_args).and_return('http://localhost:3000/demanders/artists_export/1.json') + ProviderStatusesController.any_instance.stub(:export_from_provider).with(any_args).and_return([@artist.attributes]) + ProviderLicense.stub(:import).with(any_args).and_return([]) + end + it 'ステータスコード200 OKを返す' do + get :artists_import, :id => @ps.id + response.should be_success + end + it '@provider_statusに借受状況を取得している' do + get :artists_import, :id => @ps.id + assigns(:provider_status).should eq(@ps) + end + it '@failuresにインポート失敗リストを取得している' do + get :artists_import, :id => @ps.id + assigns(:failures).should eq([]) + end + context 'html形式' do + it 'artists_importテンプレートを描画する' do + get :artists_import, :id => @ps.id + response.should render_template("artists_import") + end + end + context 'json形式' do + it 'jsonデータを返す' do + get :artists_import, :id => @ps.id, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + end + end + context '管理者権限がないとき' do + before do + sign_out @admin + end + context 'html形式' do + it 'ステータスコード302 Foundを返す' do + get :artists_import, :id => @ps.id + response.status.should eq 302 + end + it 'サインインページへ遷移する' do + get :artists_import, :id => @ps.id + response.body.should redirect_to '/admins/sign_in' + end + end + context 'json形式' do + it 'ステータスコード401 Unauthorizedを返す' do + get :artists_import, :id => @ps.id, :format => :json + response.status.should eq 401 + end + it '応答メッセージにUnauthorizedを返す' do + get :artists_import, :id => @ps.id, :format => :json + response.message.should match(/Unauthorized/) + end + end + end + context '借受権限がないとき' do + before do + ProviderStatus.any_instance.stub(:status).and_return(0) + end + context 'html形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :artists_import, :id => @ps.id + }.should raise_error(ActiveRecord::Forbidden) + end + end + context 'json形式' do + it '例外403 forbiddenを返す' do + lambda{ + get :artists_import, :id => @ps.id, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + end + end diff --git a/spec/factories.rb b/spec/factories.rb index 2fe602a5..254b3c9f 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -249,4 +249,11 @@ FactoryGirl.define do provider_license.providers_license_id nil provider_license.demanders_license_id nil end + + factory :provider_artist, :class => ProviderArtist do |provider_artist| + provider_artist.provider_id nil + provider_artist.providers_artist_id nil + provider_artist.demanders_artist_id nil + end + end diff --git a/spec/models/artist_spec.rb b/spec/models/artist_spec.rb index 3ea2d45a..c5555215 100644 --- a/spec/models/artist_spec.rb +++ b/spec/models/artist_spec.rb @@ -341,5 +341,22 @@ describe Artist do r.should eq 2 end end + + describe 'エクスポートに於いて' do + before do + @artist = FactoryGirl.create :artist, :author_id => @author.id + @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :updated_at => Time.now - 3000 + @artist3 = FactoryGirl.create :artist, :author_id => nil + end + it '開始日時が省略された場合はすべての内絵師を返す' do + r = Artist.export + r.should eq [@artist, @artist2] + end + it '開始日時以降に更新された内絵師を返す' do + r = Artist.export @artist.updated_at - 100 + r.should eq [@artist] + end + end + end diff --git a/spec/models/demander_spec.rb b/spec/models/demander_spec.rb index 525e7175..638a40a5 100644 --- a/spec/models/demander_spec.rb +++ b/spec/models/demander_spec.rb @@ -9,7 +9,7 @@ describe Demander do @lg = FactoryGirl.create :license_group @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id @user = FactoryGirl.create :user_yas - @author = @user.author #ユーザ作成時に連動して作成される + @author = FactoryGirl.create :author, :user_id => @user.id @demand_user = FactoryGirl.create :demand_user end describe '検証に於いて' do @@ -544,4 +544,51 @@ describe Demander do end end + describe '絵師エクスポートに於いて' do + before do + @ds = FactoryGirl.create :demander_status, :requested_at => Time.now, :approved_at => Time.now, :rejected_at => Time.now, :receive_hour1 => 0, :receive_hour2 => 0 + @demander = FactoryGirl.create :demander, :demander_status_id => @ds.id + @artist = FactoryGirl.create :artist, :author_id => @author.id + end + context '事前チェックする' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it '絵師モデルにエクスポートを依頼している' do + Artist.stub(:export).with(nil).and_return([@artist]) + Artist.should_receive(:export).with(nil).exactly(1) + r = @demander.artists_export + end + end + context 'つつがなく終わるとき' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it '絵師リストを返す' do + Artist.stub(:export).with(Time.parse('2011/10/10')).and_return([@artist]) + r = @demander.artists_export Time.parse('2011/10/10') + r.should eq [@artist] + end + end + context '日時指定しないとき' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it 'パラメータなしでリストを取得する' do + Artist.stub(:export).with(nil).and_return([@artist]) + Artist.should_receive(:export).with(nil).exactly(1) + r = @demander.artists_export nil + end + end + #例外ケース + context '状態が貸与中でないとき' do + it '例外403 ActiveRecord::Forbiddenを発生させる' do + Demander.any_instance.stub(:status).and_return(2) + lambda{ + r = @demander.artists_export + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + end diff --git a/spec/models/provider_artist_spec.rb b/spec/models/provider_artist_spec.rb new file mode 100644 index 00000000..1f4e2752 --- /dev/null +++ b/spec/models/provider_artist_spec.rb @@ -0,0 +1,187 @@ +# -*- encoding: utf-8 -*- +#絵師対照表 +require 'spec_helper' + +describe ProviderArtist do + before do + @admin = FactoryGirl.create :admin + @sp = FactoryGirl.create :system_picture + @lg = FactoryGirl.create :license_group + @license = FactoryGirl.create :license, :license_group_id => @lg.id, :system_picture_id => @sp.id + @user = FactoryGirl.create :user_yas + @author = FactoryGirl.create :author, :user_id => @user.id + @artist = FactoryGirl.create :artist, :author_id => @author.id + @other_user = FactoryGirl.create :user_yas + @other_author = FactoryGirl.create :author, :user_id => @other_user.id + @provider_status = FactoryGirl.create :provider_status + @provider = FactoryGirl.create :provider, :provider_status_id => @provider_status.id + end + describe '検証に於いて' do + before do + @pa = FactoryGirl.build :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id + end + + context 'オーソドックスなデータのとき' do + it '下限データが通る' do + @pa.should be_valid + end + it '上限データが通る' do + @pa.should be_valid + end + end + + context 'provider_idを検証するとき' do + it 'nullなら失敗する' do + @pa.provider_id = nil + @pa.should_not be_valid + end + it '数値でなければ失敗する' do + @pa.provider_id = 'a' + @pa.should_not be_valid + end + it '存在する貸手でなければ失敗する' do + @pa.provider_id = 0 + @pa.should_not be_valid + end + end + context 'providers_artist_idを検証するとき' do + it 'nullなら失敗する' do + @pa.providers_artist_id = nil + @pa.should_not be_valid + end + it '数値でなければ失敗する' do + @pa.providers_artist_id = 'a' + @pa.should_not be_valid + end + end + context 'demanders_artist_idを検証するとき' do + it 'nullなら失敗する' do + @pa.demanders_artist_id = nil + @pa.should_not be_valid + end + it '数値でなければ失敗する' do + @pa.demanders_artist_id = 'a' + @pa.should_not be_valid + end + end + end + + describe '対照表取得に於いて' do + before do + @pa = FactoryGirl.create :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id + end + it '取得した対照表を返す' do + r = ProviderArtist.get_one @provider.id, 2 + r.should be_true + end + it '該当する対照表が存在しなかったら、新規に興した対照表オブジェクトを返す' do + r = ProviderArtist.get_one @provider.id, 1 + r.should be_a_new ProviderArtist + r = ProviderArtist.get_one 0, 2 + r.should be_a_new ProviderArtist + end + end + + describe '貸手側絵師同期に於いて' do + before do + @pa = FactoryGirl.create :provider_artist, :provider_id => @provider.id, :providers_artist_id => 2, :demanders_artist_id => @artist.id + @newpa = ProviderArtist.new + end + context '自身に絵師がリンクしてないとき' do + before do + end + it '絵師オブジェクトを新規に興す' do + lambda { + r = @newpa.modify_artist @artist.attributes + }.should change Artist, :count + end + end + context '自身に絵師がリンクしているとき' do + it '自身から絵師を取得する' do + lambda { + r = @pa.modify_artist @artist.attributes + }.should_not change Artist, :count + end + end + it '貸手側絵師カラム値から絵師名をセットする 絵師オブジェクトを保存する' do + r = @pa.modify_artist :name => 'artist2' + @artist.reload + @artist.name.should eq 'artist2' + end + it '作家idをクリアする。(外絵師の証明)' do + r = @pa.modify_artist :name => 'artist2' + @artist.reload + @artist.author_id.should be_blank + end + it '絵師オブジェクトを返す' do + r = @pa.modify_artist @artist.attributes + r.should eq @artist + end + end + + describe 'インポートに於いて' do + before do + @artist2 = FactoryGirl.create :artist, :author_id => @other_author.id, :name => 'artist2' + end + context '事前チェックする' do + before do + ProviderArtist.stub(:get_one).with(any_args).and_return(ProviderArtist.new) + ProviderArtist.any_instance.stub(:modify_artist).with(any_args).and_return(@artist) + ProviderArtist.any_instance.stub(:save).with(any_args).and_return(true) + end + it '対照表取得を問い合わせている' do + ProviderArtist.should_receive(:get_one).with(any_args).exactly(1) + r = ProviderArtist.import @provider.id, [@artist.attributes] + end + it '対照表に絵師同期を依頼してしている' do + ProviderArtist.any_instance.should_receive(:modify_artist).with(any_args).exactly(1) + r = ProviderArtist.import @provider.id, [@artist.attributes] + end + it '対照表オブジェクトを保存している' do + ProviderArtist.any_instance.should_receive(:save).with(any_args).exactly(1) + r = ProviderArtist.import @provider.id, [@artist.attributes] + end + end + context 'つつがなく終わるとき' do + it '空っぽの配列を返す' do + r = ProviderArtist.import @provider.id, [@artist.attributes] + r.should be_empty + end + it '対照表が追加される' do + lambda { + r = ProviderArtist.import @provider.id, [@artist.attributes] + }.should change ProviderArtist, :count + end + end + context '複数インポートのとき' do + it '空っぽの配列を返す' do + r = ProviderArtist.import @provider.id, [@artist.attributes, @artist2.attributes] + r.should be_empty + end + it '対照表が追加される' do + lambda { + r = ProviderArtist.import @provider.id, [@artist.attributes, @artist2.attributes] + }.should change(ProviderArtist, :count).by(2) + end + end + #警告ケース + context '対照表オブジェクトの保存に失敗したとき' do + before do + ProviderArtist.any_instance.stub(:save).with(any_args).and_return(false) + end + it '結果に貸手側絵師のカラム値を追加している' do + r = ProviderArtist.import @provider.id, [@artist.attributes] + r.should_not be_empty + end + end + context '絵師オブジェクトの保存に失敗したとき' do + before do + Artist.any_instance.stub(:save).with(any_args).and_return(false) + end + it '結果に貸手側絵師のカラム値を追加している' do + r = ProviderArtist.import @provider.id, [@artist.attributes] + r.should_not be_empty + end + end + end +end diff --git a/spec/models/provider_license_spec.rb b/spec/models/provider_license_spec.rb index 50501fcf..3983a3f9 100644 --- a/spec/models/provider_license_spec.rb +++ b/spec/models/provider_license_spec.rb @@ -36,7 +36,7 @@ describe ProviderLicense do @pl.provider_id = 'a' @pl.should_not be_valid end - it '存在する貸手ユーザでなければ失敗する' do + it '存在する貸手でなければ失敗する' do @pl.provider_id = 0 @pl.should_not be_valid end -- 2.11.0