OSDN Git Service

t#30328:add export
[pettanr/pettanr.git] / spec / models / picture_spec.rb
index eb6e962..7d7105b 100644 (file)
@@ -4,12 +4,12 @@ require 'spec_helper'
 
 describe Picture do
   before do
-    FactoryGirl.create :admin
+    @admin = FactoryGirl.create :admin
     @user = FactoryGirl.create( :user_yas)
-    @author = @user.author
+    @author = FactoryGirl.create :author, :user_id => @user.id
     @artist = FactoryGirl.create :artist_yas, :author_id => @author.id
     @other_user = FactoryGirl.create( :user_yas)
-    @other_author = @other_user.author
+    @other_author = FactoryGirl.create :author, :user_id => @other_user.id
     @other_artist = FactoryGirl.create :artist_yas, :author_id => @other_author.id
     @sp = FactoryGirl.create :system_picture
     @lg = FactoryGirl.create :license_group
@@ -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