From fb0b22756b8a05bec524267aaec99c5935dc6803 Mon Sep 17 00:00:00 2001 From: yasushiito Date: Wed, 9 Jan 2013 14:14:28 +0900 Subject: [PATCH] t#30328:add export --- app/controllers/demanders_controller.rb | 22 +++- app/models/demander.rb | 10 ++ app/models/original_picture.rb | 22 +++- app/models/picture.rb | 24 ++++ app/models/resource_picture.rb | 4 + config/routes.rb | 2 + spec/controllers/demanders_controller_spec.rb | 183 ++++++++++++++++++++++++++ spec/models/demander_spec.rb | 97 ++++++++++++++ spec/models/original_picture_spec.rb | 151 +++++++-------------- spec/models/picture_spec.rb | 63 +++++++++ 10 files changed, 473 insertions(+), 105 deletions(-) diff --git a/app/controllers/demanders_controller.rb b/app/controllers/demanders_controller.rb index 22ee9800..92ac27ad 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, :artists_export] + before_filter :authenticate_demand_user!, :only => [:index, :show, :new, :create, :edit, :update, :destroy, :req, :licenses_export, :artists_export, :original_pictures_export, :pictures_export] def index @demander = @demand_user.demander @@ -116,4 +116,24 @@ class DemandersController < ApplicationController end end + def original_pictures_export + @demander = @demand_user.demander + date = ymd_to_time params[:date] + @original_pictures = @demander.original_pictures_export(date) + respond_to do |format| + format.html { render :text } + format.json { render :json => OriginalPicture.list_as_json_text(@original_pictures) } + end + end + + def pictures_export + @demander = @demand_user.demander + date = ymd_to_time params[:date] + @pictures = @demander.pictures_export(date) + respond_to do |format| + format.html { render :text } + format.json { render :json => Picture.list_as_json_text(@pictures) } + end + end + end diff --git a/app/models/demander.rb b/app/models/demander.rb index 61bc4088..f343470b 100644 --- a/app/models/demander.rb +++ b/app/models/demander.rb @@ -128,4 +128,14 @@ class Demander < ActiveRecord::Base Artist.export date_str end + def original_pictures_export date_str = nil + raise ActiveRecord::Forbidden unless self.status == 3 + OriginalPicture.export date_str + end + + def pictures_export date_str = nil + raise ActiveRecord::Forbidden unless self.status == 3 + Picture.export date_str + end + end diff --git a/app/models/original_picture.rb b/app/models/original_picture.rb index 9a435ee3..110987a1 100644 --- a/app/models/original_picture.rb +++ b/app/models/original_picture.rb @@ -150,10 +150,24 @@ class OriginalPicture < ActiveRecord::Base PictureIO.original_picture_io.get self.filename, subdir end - def self.export ar - l = LicenseGroup.list - op = OriginalPicture.list ar.id - {:license_groups => l, :original_pictures => op} + def self.export(dt = nil) + opt = {} + cond = if dt + ['artists.author_id is not null and original_pictures.updated_at >= ?', dt] + else + 'artists.author_id is not null' + end + opt.merge!({:conditions => cond}) + opt.merge!({:include => {:resource_picture => {}, :artist => {}}, :order => 'original_pictures.id'}) + OriginalPicture.find(:all, opt) + end + + def list_as_json_with_resource_picture + self.to_json({:include => {:resource_picture => {:methods => :picture_data}}}) + end + + def self.list_as_json_text ary + '[' + ary.map {|i| i.list_as_json_with_resource_picture }.join(',') + ']' end end diff --git a/app/models/picture.rb b/app/models/picture.rb index 31e6ee61..a64ce6e6 100644 --- a/app/models/picture.rb +++ b/app/models/picture.rb @@ -148,6 +148,30 @@ class Picture < ActiveRecord::Base PictureIO.picture_io.get self.filename, subdir end + def self.export(dt = nil) + opt = {} + cond = if dt + ['artists.author_id is not null and pictures.updated_at >= ?', dt] + else + 'artists.author_id is not null' + end + opt.merge!({:conditions => cond}) + opt.merge!({:include => {:artist => {}}, :order => 'pictures.updated_at desc'}) + Picture.find(:all, opt) + end + + def self.list_as_json_text ary + '[' + ary.map {|i| i.to_json_with_picture_data }.join(',') + ']' + end + + def picture_data + Base64.encode64(self.restore) + end + + def to_json_with_picture_data + self.to_json({:methods => :picture_data}) + end + def credit_template "#{self.classname.tableize}/attributes/credit" end diff --git a/app/models/resource_picture.rb b/app/models/resource_picture.rb index d22162e5..a7054f31 100644 --- a/app/models/resource_picture.rb +++ b/app/models/resource_picture.rb @@ -210,6 +210,10 @@ class ResourcePicture < ActiveRecord::Base ResourcePicture.count end + def picture_data + Base64.encode64(self.restore 'full') + end + def credit_template "#{self.classname.tableize}/attributes/credit" end diff --git a/config/routes.rb b/config/routes.rb index d00e5231..63e76c07 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -315,6 +315,8 @@ Pettanr::Application.routes.draw do post :req get :licenses_export get :artists_export + get :original_pictures_export + get :pictures_export end member do get :edit diff --git a/spec/controllers/demanders_controller_spec.rb b/spec/controllers/demanders_controller_spec.rb index 08af954f..300eb521 100644 --- a/spec/controllers/demanders_controller_spec.rb +++ b/spec/controllers/demanders_controller_spec.rb @@ -770,4 +770,187 @@ describe DemandersController do 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 + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + sign_in @demand_user + end + context 'つつがなく終わるとき' do + before do + Demander.any_instance.stub(:original_pictures_export).with(any_args).and_return([@op, @op, @op]) + end + it 'ステータスコード200 OKを返す' do + get :original_pictures_export, :format => :json + response.should be_success + end + it '日付文字列変換を依頼している' do + DemandersController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1) + get :original_pictures_export, :date => '20111010', :format => :json + end + it '借手モデルに原画エクスポートを問い合わせている' do + Demander.any_instance.should_receive(:original_pictures_export).with(Time.parse('2011/10/10')).exactly(1) + get :original_pictures_export, :date => '20111010', :format => :json + end + it '@original_picturesにリストを取得している' do + get :original_pictures_export, :format => :json + assigns(:original_pictures).should have_at_least(3).items + end + context 'html形式' do + end + context 'json形式' do + it '原画モデルにリストのjson化を依頼している' do + OriginalPicture.should_receive(:list_as_json_text).with(any_args).exactly(1) + OriginalPicture.stub(:list_as_json_text).with(any_args).and_return('[]') + get :original_pictures_export, :format => :json + end + it 'jsonデータを返す' do + get :original_pictures_export, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがリスト構造になっている' do + get :original_pictures_export, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + it 'リストの先頭くらいは原画っぽいものであって欲しい' do + get :original_pictures_export, :format => :json + json = JSON.parse response.body + json.first.has_key?("ext").should be_true + json.first.has_key?("md5").should be_true + end + end + end + context '借手権限がないとき' do + before do + sign_out @demand_user + end + it 'ステータスコード302 Foundを返す' do + get :original_pictures_export + response.status.should eq 302 + end + context 'html形式' do + it 'サインインページへ遷移する' do + get :original_pictures_export + response.body.should redirect_to '/demand_users/sign_in' + end + end + context 'json形式' do + it '応答メッセージにUnauthorizedを返す' do + get :original_pictures_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 :original_pictures_export, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + 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 + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0 + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id + sign_in @demand_user + Picture.any_instance.stub(:restore).with(any_args).and_return('picture binary data') + end + context 'つつがなく終わるとき' do + before do + Demander.any_instance.stub(:pictures_export).with(any_args).and_return([@p, @p, @p]) + end + it 'ステータスコード200 OKを返す' do + get :pictures_export, :format => :json + response.should be_success + end + it '日付文字列変換を依頼している' do + DemandersController.any_instance.should_receive(:ymd_to_time).with('20111010').exactly(1) + get :pictures_export, :date => '20111010', :format => :json + end + it '借手モデルに実素材エクスポートを問い合わせている' do + Demander.any_instance.should_receive(:pictures_export).with(Time.parse('2011/10/10')).exactly(1) + get :pictures_export, :date => '20111010', :format => :json + end + it '@picturesにリストを取得している' do + get :pictures_export, :format => :json + assigns(:pictures).should have_at_least(3).items + end + context 'html形式' do + end + context 'json形式' do + it '実素材モデルにリストのjson化を依頼している' do + Picture.should_receive(:list_as_json_text).with(any_args).exactly(1) + Picture.stub(:list_as_json_text).with(any_args).and_return('[]') + get :pictures_export, :format => :json + end + it 'jsonデータを返す' do + get :pictures_export, :format => :json + lambda{JSON.parse(response.body)}.should_not raise_error(JSON::ParserError) + end + it 'データがリスト構造になっている' do + get :pictures_export, :format => :json + json = JSON.parse response.body + json.should have_at_least(3).items + end + it 'リストの先頭くらいは実素材っぽいものであって欲しい' do + get :pictures_export, :format => :json + json = JSON.parse response.body + json.first.has_key?("ext").should be_true + json.first.has_key?("revision").should be_true + end + end + end + context '借手権限がないとき' do + before do + sign_out @demand_user + end + it 'ステータスコード302 Foundを返す' do + get :pictures_export + response.status.should eq 302 + end + context 'html形式' do + it 'サインインページへ遷移する' do + get :pictures_export + response.body.should redirect_to '/demand_users/sign_in' + end + end + context 'json形式' do + it '応答メッセージにUnauthorizedを返す' do + get :pictures_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 :pictures_export, :format => :json + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + end + end diff --git a/spec/models/demander_spec.rb b/spec/models/demander_spec.rb index 638a40a5..b4165441 100644 --- a/spec/models/demander_spec.rb +++ b/spec/models/demander_spec.rb @@ -591,4 +591,101 @@ 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 + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + end + context '事前チェックする' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it '原画モデルに内原画エクスポートを依頼している' do + OriginalPicture.stub(:export).with(nil).and_return([@op]) + OriginalPicture.should_receive(:export).with(nil).exactly(1) + r = @demander.original_pictures_export + end + end + context 'つつがなく終わるとき' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it '内原画リストを返す' do + OriginalPicture.stub(:export).with(Time.parse('2011/10/10')).and_return([@op]) + r = @demander.original_pictures_export Time.parse('2011/10/10') + r.should eq [@op] + end + end + context '日時指定しないとき' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it 'パラメータなしでリストを取得する' do + OriginalPicture.stub(:export).with(nil).and_return([@op]) + OriginalPicture.should_receive(:export).with(nil).exactly(1) + r = @demander.original_pictures_export nil + end + end + #例外ケース + context '状態が貸与中でないとき' do + it '例外403 ActiveRecord::Forbiddenを発生させる' do + Demander.any_instance.stub(:status).and_return(2) + lambda{ + r = @demander.original_pictures_export + }.should raise_error(ActiveRecord::Forbidden) + end + 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 + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0 + end + context '事前チェックする' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it '実素材モデルにエクスポートを依頼している' do + Picture.stub(:export).with(nil).and_return([@p]) + Picture.should_receive(:export).with(nil).exactly(1) + r = @demander.pictures_export + end + end + context 'つつがなく終わるとき' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it '実素材リストを返す' do + Picture.stub(:export).with(Time.parse('2011/10/10')).and_return([@p]) + r = @demander.pictures_export Time.parse('2011/10/10') + r.should eq [@p] + end + end + context '日時指定しないとき' do + before do + Demander.any_instance.stub(:status).and_return(3) + end + it 'パラメータなしでリストを取得する' do + Picture.stub(:export).with(nil).and_return([@p]) + Picture.should_receive(:export).with(nil).exactly(1) + r = @demander.pictures_export nil + end + end + #例外ケース + context '状態が貸与中でないとき' do + it '例外403 ActiveRecord::Forbiddenを発生させる' do + Demander.any_instance.stub(:status).and_return(2) + lambda{ + r = @demander.pictures_export + }.should raise_error(ActiveRecord::Forbidden) + end + end + end + end diff --git a/spec/models/original_picture_spec.rb b/spec/models/original_picture_spec.rb index 18ed1c44..7dd3f6d8 100644 --- a/spec/models/original_picture_spec.rb +++ b/spec/models/original_picture_spec.rb @@ -672,126 +672,78 @@ describe OriginalPicture do end end -=begin describe 'エクスポートに於いて' do before do @op = FactoryGirl.create :original_picture, :artist_id => @artist.id @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0 @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id - #他人の原画排除 + #開始日時以前の原画 + @old_op = FactoryGirl.create :original_picture, :artist_id => @artist.id, :updated_at => Time.now - 1000 + #素材がライセンスされていない原画 + @stopped_op = FactoryGirl.create :original_picture, :artist_id => @artist.id + #他人の原画 @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + #貸手からの原画排除 + @outer_artist = FactoryGirl.create :artist_yas, :author_id => nil + @outer_op = FactoryGirl.create :original_picture, :artist_id => @outer_artist.id end context 'つつがなく終わるとき' do - it 'ライセンスグループに依頼してリストを取得している' do - LicenseGroup.stub(:list).with(any_args).and_return([@lg]) - LicenseGroup.should_receive(:list).with(any_args).exactly(1) - r = OriginalPicture.export(@artist) + it '開始日時が省略された場合はすべての内原画を返す' do + r = OriginalPicture.export + r.should eq [@op, @old_op, @stopped_op, @other_op] end - it '原画に依頼してリストを取得している' do - OriginalPicture.stub(:list).with(any_args).and_return([@op]) - OriginalPicture.should_receive(:list).with(any_args).exactly(1) - r = OriginalPicture.export(@artist) + it '開始日時以降に更新された内原画を返す' do + r = OriginalPicture.export @op.updated_at - 100 + r.should eq [@op, @stopped_op, @other_op] end - it 'Hashを返す' do - r = OriginalPicture.export(@artist) - r.is_a?(Hash).should be_true - end - it 'Hashはライセンスグループを含んでいる' do - r = OriginalPicture.export(@artist) - r.include?(:license_groups).should be_true - end - it 'Hashは原画を含んでいる' do - r = OriginalPicture.export(@artist) - r.include?(:original_pictures).should be_true + it '素材を含んでいる' do + r = OriginalPicture.export + r.first.resource_picture.should_not be_nil end #素材がライセンスされていないケースもある - it 'Hashの原画は素材を含んでいる' do - r = OriginalPicture.export(@artist) - r[:original_pictures].first.resource_picture.should_not be_nil - end - it 'Hashの原画は実素材を含んでいる' do - r = OriginalPicture.export(@artist) - r[:original_pictures].first.pictures.should be_nil + it '停止中の原画は素材を含んでいない' do + r = OriginalPicture.export + r[2].resource_picture.should be_nil end end - context '実データ単体のとき' do - it 'ライセンスは配列構造になっている' do - r = OriginalPicture.export(@artist) - r[:license_groups].is_a?(Array).should be_true - end - it 'ライセンスが全件出ている' do - r = OriginalPicture.export(@artist) - r[:license_groups].size.should eq 1 - r[:license_groups].first.should eq @lg - end - it '原画は配列構造になっている' do - r = OriginalPicture.export(@artist) - r[:original_pictures].is_a?(Array).should be_true - end - it '原画が全件出ている' do - r = OriginalPicture.export(@artist) - r[:original_pictures].size.should eq 1 - r[:original_pictures].first.should eq @op + end + + describe 'リストのjson化に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0 + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id + @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + @other_p = FactoryGirl.create :picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now + 100 + @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :picture_id => @other_p.id + ResourcePicture.any_instance.stub(:restore).with(any_args).and_return('picture binary data') + end + context 'つつがなく終わるとき' do + it 'json文字列を返す' do + r = OriginalPicture.list_as_json_text OriginalPicture.all + lambda { + j = JSON.parse r + }.should_not raise_error(JSON::ParserError) end - it '原画に素材が関連付いている' do - r = OriginalPicture.export(@artist) - i = r[:original_pictures].first - i.resource_picture.should eq @rp + it '原画リストを返す' do + r = OriginalPicture.list_as_json_text OriginalPicture.all + j = JSON.parse r + j.size.should eq 2 end - it '原画に実素材が関連付いている' do - r = OriginalPicture.export(@artist) - i = r[:original_pictures].first - i.picture.should eq @p + it '素材を含んでいる' do + r = OriginalPicture.list_as_json_text OriginalPicture.all + j = JSON.parse r + j.first['resource_picture'].should_not be_nil end - end - context '実データ複数のとき' do - before do - @lg2 = FactoryGirl.create :license_group, :name => 'export test', :url => 'http://export.test/' - @license2 = FactoryGirl.create :license, :license_group_id => @lg2.id, :system_picture_id => @sp.id, :name => 'export test license', :url => 'http://export.test/license' - @op2 = FactoryGirl.create :original_picture, :artist_id => @artist.id - @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :revision => 0 - @rp2 = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op2.id, :license_id => @license2.id, :picture_id => @p2.id - end - it 'ライセンスは配列構造になっている' do - r = OriginalPicture.export(@artist) - r[:license_groups].is_a?(Array).should be_true - end - it 'ライセンスが全件出ている' do - r = OriginalPicture.export(@artist) - r[:license_groups].size.should eq 2 - r[:license_groups].first.should eq @lg - r[:license_groups].last.should eq @lg2 - end - it '原画は配列構造になっている' do - r = OriginalPicture.export(@artist) - r[:original_pictures].is_a?(Array).should be_true - end - it '原画が全件出ている' do - r = OriginalPicture.export(@artist) - r[:original_pictures].size.should eq 2 - r[:original_pictures].first.should eq @op - r[:original_pictures].last.should eq @op2 - end - it '原画に素材が関連付いている' do - r = OriginalPicture.export(@artist) - i = r[:original_pictures].first - i.resource_picture.should eq @rp - i2 = r[:original_pictures].last - i2.resource_picture.should eq @rp2 - end - it '原画に実素材が関連付いている' do - r = OriginalPicture.export(@artist) - i = r[:original_pictures].first - i.picture.should eq @p - i2 = r[:original_pictures].last - i2.picture.should eq @p2 + it '各素材に画像データを添えて返す' do + r = OriginalPicture.list_as_json_text OriginalPicture.all + j = JSON.parse r + j.first['resource_picture']['picture_data'].should_not be_nil + j.last['resource_picture']['picture_data'].should_not be_nil end end end - describe 'エクスポートオプションに於いて' do - end - describe 'インポートに於いて' do before do @imports = {:licenses => {}, :artist_id => @artist.id} @@ -800,5 +752,4 @@ describe OriginalPicture do end end -=end end diff --git a/spec/models/picture_spec.rb b/spec/models/picture_spec.rb index a4e372e5..7d7105b4 100644 --- a/spec/models/picture_spec.rb +++ b/spec/models/picture_spec.rb @@ -1032,4 +1032,67 @@ describe Picture do end end + describe 'エクスポートに於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0 + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id + @p2 = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 1, :updated_at => Time.now + 1000 + #開始日時以前の実素材 + @old_op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @old_p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @old_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now - 1000 + @old_rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @old_op.id, :license_id => @license.id, :picture_id => @old_p.id + #他人の実素材 + @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + @other_p = FactoryGirl.create :picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now + 100 + @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :picture_id => @other_p.id + #貸手からの実素材排除 + @outer_artist = FactoryGirl.create :artist_yas, :author_id => nil + @outer_op = FactoryGirl.create :original_picture, :artist_id => @outer_artist.id + @outer_p = FactoryGirl.create :picture, :artist_id => @outer_artist.id, :original_picture_id => @outer_op.id, :license_id => @license.id, :revision => 0 + @outer_rp = FactoryGirl.create :resource_picture, :artist_id => @outer_artist.id, :original_picture_id => @outer_op.id, :license_id => @license.id, :picture_id => @outer_p.id + end + context 'つつがなく終わるとき' do + it '開始日時が省略された場合はすべての内実素材を返す' do + r = Picture.export + r.should eq [@p2, @other_p, @p, @old_p] + end + it '開始日時以降に更新された内実素材を返す' do + r = Picture.export @p.updated_at - 100 + r.should eq [@p2, @other_p, @p] + end + end + end + + describe 'リストのjson化に於いて' do + before do + @op = FactoryGirl.create :original_picture, :artist_id => @artist.id + @p = FactoryGirl.create :picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :revision => 0 + @rp = FactoryGirl.create :resource_picture, :artist_id => @artist.id, :original_picture_id => @op.id, :license_id => @license.id, :picture_id => @p.id + @other_op = FactoryGirl.create :original_picture, :artist_id => @other_artist.id + @other_p = FactoryGirl.create :picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :revision => 0, :updated_at => Time.now + 100 + @other_rp = FactoryGirl.create :resource_picture, :artist_id => @other_artist.id, :original_picture_id => @other_op.id, :license_id => @license.id, :picture_id => @other_p.id + Picture.any_instance.stub(:restore).with(any_args).and_return('picture binary data') + end + context 'つつがなく終わるとき' do + it 'json文字列を返す' do + r = Picture.list_as_json_text Picture.all + lambda { + j = JSON.parse r + }.should_not raise_error(JSON::ParserError) + end + it '実素材リストを返す' do + r = Picture.list_as_json_text Picture.all + j = JSON.parse r + j.size.should eq 2 + end + it '各実素材に画像データを添えて返す' do + r = Picture.list_as_json_text Picture.all + j = JSON.parse r + j.first['picture_data'].should_not be_nil + j.last['picture_data'].should_not be_nil + end + end + end + end -- 2.11.0