OSDN Git Service

add test
[pettanr/pettanr.git] / lib / test / s3_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ファイル保存
3 require 's3_picture'
4 require 'spec_helper'
5
6 #テスト用ファイルをクリアしておく
7 @path = Rails.root + 'lib/test/temp/'
8 Dir.glob @path + '*' do |filename|
9   if File.directory?(filename)
10     Dir.glob filename do |filename2|
11       File.delete(filename2) unless File.directory?(filename2)
12     end
13   else
14     File.delete(filename)
15   end
16 end
17 describe PictureIO::S3Picture do
18   before do
19     @path = 'test'
20     @io = PictureIO::S3Picture.new @path
21   end
22
23   describe 'サブディレクトリに於いて' do
24     it '配列が返る' do
25       PictureIO::S3Picture.subdirs.is_a?(Array).should be_true 
26     end
27
28     it 'カレント、サムネ、水平反転、垂直反転、水平垂直反転が返る' do
29       PictureIO::S3Picture.subdirs.size.should eq 5
30       PictureIO::S3Picture.subdirs.include?('').should eq true
31       PictureIO::S3Picture.subdirs.include?('v').should eq true
32       PictureIO::S3Picture.subdirs.include?('h').should eq true
33       PictureIO::S3Picture.subdirs.include?('vh').should eq true
34       PictureIO::S3Picture.subdirs.include?('thumbnail').should eq true
35     end
36   end
37   
38   describe 'ファイル存在確認に於いて' do
39     before do
40       AWS::S3::S3Object.stub(:exist?).with(any_args).and_return(true)
41     end
42     context 'ベースディレクトリから取るとき' do
43       it 'S3に依頼している' do
44         testname = 'exist'
45         AWS::S3::S3Object.should_receive(:exist?).with(testname, @path).exactly(1)
46         @io.exist?(testname)
47       end
48     end
49     context 'サブディレクトリvから取るとき' do
50       it 'S3に依頼している' do
51         testname = 'exist'
52         subd = 'v'
53         AWS::S3::S3Object.should_receive(:exist?).with(subd + '/' + testname, @path).exactly(1)
54         @io.exist?(testname, subd)
55       end
56     end
57     context '例外が発生するとき' do
58       it 'falseが返る' do
59         AWS::S3::S3Object.stub(:exist?).with(any_args()).and_raise(StandardError)
60         @io.exist?('test.get').should be_false
61       end
62     end
63   end
64   
65   describe 'ファイル取得に於いて' do
66     before do
67     end
68     context 'ベースディレクトリから取るとき' do
69       it 'S3に依頼している' do
70         testname = 'test.get'
71         AWS::S3::S3Object.should_receive(:stream).with(testname, @path).exactly(1)
72         @io.get(testname)
73       end
74       it 'データが返る' do
75         AWS::S3::S3Object.stub(:stream).with(any_args()).and_yield('data')
76         @io.get('test.get').should eq 'data'
77       end
78     end
79     context 'サブディレクトリvから取るとき' do
80       it 'S3に依頼している' do
81         testname = 'test.get'
82         subd = 'v'
83         AWS::S3::S3Object.should_receive(:stream).with(subd + '/' + testname, @path).exactly(1)
84         @io.get(testname, subd)
85       end
86       it 'データが返る' do
87         subd = 'v'
88         AWS::S3::S3Object.stub(:stream).with(any_args()).and_yield('data')
89         @io.get('test.get', subd).should eq 'data'
90       end
91     end
92     context '例外が発生するとき' do
93       it 'falseが返る' do
94         AWS::S3::S3Object.stub(:get).with(any_args()).and_raise(StandardError)
95         @io.get('test.get').should be_false
96       end
97     end
98   end
99   
100   describe 'ファイル作成に於いて' do
101     before do
102     end
103     context 'ベースディレクトリに作成するとき' do
104       it 'S3に依頼している' do
105         testname = 'create'
106         AWS::S3::S3Object.should_receive(:store).with(testname, 'bindata', @path).exactly(1)
107         @io.put('bindata', testname)
108       end
109     end
110     context 'サブディレクトリvに作成するとき' do
111       it 'S3に依頼している' do
112         testname = 'create'
113         subd = 'h'
114         AWS::S3::S3Object.should_receive(:store).with(subd + '/' + testname, 'bindata', @path).exactly(1)
115         @io.put('bindata', testname, subd)
116       end
117     end
118     context '例外が発生するとき' do
119       it 'falseが返る' do
120         AWS::S3::S3Object.stub(:store).with(any_args()).and_raise(StandardError)
121         @io.put('bindata', 'test.put').should be_false
122       end
123     end
124   end
125   
126   describe 'ファイル削除に於いて' do
127     before do
128     end
129     context 'ベースディレクトリから削除するとき' do
130       it 'S3に依頼している' do
131         testname = 'destroy'
132         AWS::S3::S3Object.should_receive(:delete).with(testname, @path).exactly(1)
133         @io.delete(testname)
134       end
135     end
136     context 'サブディレクトリvhから削除するとき' do
137       it 'S3に依頼している' do
138         testname = 'destroy'
139         subd = 'vh'
140         AWS::S3::S3Object.should_receive(:delete).with(subd + '/' + testname, @path).exactly(1)
141         @io.delete(testname, subd)
142       end
143     end
144     context '例外が発生するとき' do
145       it 'falseが返る' do
146         AWS::S3::S3Object.stub(:delete).with(any_args()).and_raise(StandardError)
147         @io.delete('test.del.not.exist').should be_false
148       end
149     end
150   end
151 end