OSDN Git Service

add test
[pettanr/pettanr.git] / lib / test / s3_picture_spec.rb
diff --git a/lib/test/s3_picture_spec.rb b/lib/test/s3_picture_spec.rb
new file mode 100644 (file)
index 0000000..5d8eee7
--- /dev/null
@@ -0,0 +1,151 @@
+# -*- encoding: utf-8 -*-
+#ファイル保存
+require 's3_picture'
+require 'spec_helper'
+
+#テスト用ファイルをクリアしておく
+@path = Rails.root + 'lib/test/temp/'
+Dir.glob @path + '*' do |filename|
+  if File.directory?(filename)
+    Dir.glob filename do |filename2|
+      File.delete(filename2) unless File.directory?(filename2)
+    end
+  else
+    File.delete(filename)
+  end
+end
+describe PictureIO::S3Picture do
+  before do
+    @path = 'test'
+    @io = PictureIO::S3Picture.new @path
+  end
+
+  describe 'サブディレクトリに於いて' do
+    it '配列が返る' do
+      PictureIO::S3Picture.subdirs.is_a?(Array).should be_true 
+    end
+
+    it 'カレント、サムネ、水平反転、垂直反転、水平垂直反転が返る' do
+      PictureIO::S3Picture.subdirs.size.should eq 5
+      PictureIO::S3Picture.subdirs.include?('').should eq true
+      PictureIO::S3Picture.subdirs.include?('v').should eq true
+      PictureIO::S3Picture.subdirs.include?('h').should eq true
+      PictureIO::S3Picture.subdirs.include?('vh').should eq true
+      PictureIO::S3Picture.subdirs.include?('thumbnail').should eq true
+    end
+  end
+  
+  describe 'ファイル存在確認に於いて' do
+    before do
+      AWS::S3::S3Object.stub(:exist?).with(any_args).and_return(true)
+    end
+    context 'ベースディレクトリから取るとき' do
+      it 'S3に依頼している' do
+        testname = 'exist'
+        AWS::S3::S3Object.should_receive(:exist?).with(testname, @path).exactly(1)
+        @io.exist?(testname)
+      end
+    end
+    context 'サブディレクトリvから取るとき' do
+      it 'S3に依頼している' do
+        testname = 'exist'
+        subd = 'v'
+        AWS::S3::S3Object.should_receive(:exist?).with(subd + '/' + testname, @path).exactly(1)
+        @io.exist?(testname, subd)
+      end
+    end
+    context '例外が発生するとき' do
+      it 'falseが返る' do
+        AWS::S3::S3Object.stub(:exist?).with(any_args()).and_raise(StandardError)
+        @io.exist?('test.get').should be_false
+      end
+    end
+  end
+  
+  describe 'ファイル取得に於いて' do
+    before do
+    end
+    context 'ベースディレクトリから取るとき' do
+      it 'S3に依頼している' do
+        testname = 'test.get'
+        AWS::S3::S3Object.should_receive(:stream).with(testname, @path).exactly(1)
+        @io.get(testname)
+      end
+      it 'データが返る' do
+        AWS::S3::S3Object.stub(:stream).with(any_args()).and_yield('data')
+        @io.get('test.get').should eq 'data'
+      end
+    end
+    context 'サブディレクトリvから取るとき' do
+      it 'S3に依頼している' do
+        testname = 'test.get'
+        subd = 'v'
+        AWS::S3::S3Object.should_receive(:stream).with(subd + '/' + testname, @path).exactly(1)
+        @io.get(testname, subd)
+      end
+      it 'データが返る' do
+        subd = 'v'
+        AWS::S3::S3Object.stub(:stream).with(any_args()).and_yield('data')
+        @io.get('test.get', subd).should eq 'data'
+      end
+    end
+    context '例外が発生するとき' do
+      it 'falseが返る' do
+        AWS::S3::S3Object.stub(:get).with(any_args()).and_raise(StandardError)
+        @io.get('test.get').should be_false
+      end
+    end
+  end
+  
+  describe 'ファイル作成に於いて' do
+    before do
+    end
+    context 'ベースディレクトリに作成するとき' do
+      it 'S3に依頼している' do
+        testname = 'create'
+        AWS::S3::S3Object.should_receive(:store).with(testname, 'bindata', @path).exactly(1)
+        @io.put('bindata', testname)
+      end
+    end
+    context 'サブディレクトリvに作成するとき' do
+      it 'S3に依頼している' do
+        testname = 'create'
+        subd = 'h'
+        AWS::S3::S3Object.should_receive(:store).with(subd + '/' + testname, 'bindata', @path).exactly(1)
+        @io.put('bindata', testname, subd)
+      end
+    end
+    context '例外が発生するとき' do
+      it 'falseが返る' do
+        AWS::S3::S3Object.stub(:store).with(any_args()).and_raise(StandardError)
+        @io.put('bindata', 'test.put').should be_false
+      end
+    end
+  end
+  
+  describe 'ファイル削除に於いて' do
+    before do
+    end
+    context 'ベースディレクトリから削除するとき' do
+      it 'S3に依頼している' do
+        testname = 'destroy'
+        AWS::S3::S3Object.should_receive(:delete).with(testname, @path).exactly(1)
+        @io.delete(testname)
+      end
+    end
+    context 'サブディレクトリvhから削除するとき' do
+      it 'S3に依頼している' do
+        testname = 'destroy'
+        subd = 'vh'
+        AWS::S3::S3Object.should_receive(:delete).with(subd + '/' + testname, @path).exactly(1)
+        @io.delete(testname, subd)
+      end
+    end
+    context '例外が発生するとき' do
+      it 'falseが返る' do
+        AWS::S3::S3Object.stub(:delete).with(any_args()).and_raise(StandardError)
+        @io.delete('test.del.not.exist').should be_false
+      end
+    end
+  end
+end