OSDN Git Service

fix:error dlg
[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 'PictureIO::Error例外が発生する' do
59         AWS::S3::S3Object.stub(:exist?).with(any_args()).and_raise(StandardError)
60         lambda {
61           @io.exist?('test.get')
62         }.should raise_error(PictureIO::Error)
63       end
64     end
65   end
66   
67   describe 'ファイル取得に於いて' do
68     before do
69     end
70     context 'ベースディレクトリから取るとき' do
71       it 'S3に依頼している' do
72         testname = 'test.get'
73         AWS::S3::S3Object.should_receive(:stream).with(testname, @path).exactly(1)
74         @io.get(testname)
75       end
76       it 'データが返る' do
77         AWS::S3::S3Object.stub(:stream).with(any_args()).and_yield('data')
78         @io.get('test.get').should eq 'data'
79       end
80     end
81     context 'サブディレクトリvから取るとき' do
82       it 'S3に依頼している' do
83         testname = 'test.get'
84         subd = 'v'
85         AWS::S3::S3Object.should_receive(:stream).with(subd + '/' + testname, @path).exactly(1)
86         @io.get(testname, subd)
87       end
88       it 'データが返る' do
89         subd = 'v'
90         AWS::S3::S3Object.stub(:stream).with(any_args()).and_yield('data')
91         @io.get('test.get', subd).should eq 'data'
92       end
93     end
94     context '例外が発生するとき' do
95       it 'PictureIO::Error例外が発生する' do
96         AWS::S3::S3Object.stub(:get).with(any_args()).and_raise(StandardError)
97         lambda {
98           @io.get('test.get')
99         }.should raise_error(PictureIO::Error)
100       end
101     end
102   end
103   
104   describe 'ファイル作成に於いて' do
105     before do
106     end
107     context 'ベースディレクトリに作成するとき' do
108       it 'S3に依頼している' do
109         testname = 'create'
110         AWS::S3::S3Object.should_receive(:store).with(testname, 'bindata', @path).exactly(1)
111         @io.put('bindata', testname)
112       end
113     end
114     context 'サブディレクトリvに作成するとき' do
115       it 'S3に依頼している' do
116         testname = 'create'
117         subd = 'h'
118         AWS::S3::S3Object.should_receive(:store).with(subd + '/' + testname, 'bindata', @path).exactly(1)
119         @io.put('bindata', testname, subd)
120       end
121     end
122     context '例外が発生するとき' do
123       it 'PictureIO::Error例外が発生する' do
124         AWS::S3::S3Object.stub(:store).with(any_args()).and_raise(StandardError)
125         lambda {
126           @io.put('bindata', 'test.put')
127         }.should raise_error(PictureIO::Error)
128       end
129     end
130   end
131   
132   describe 'ファイル削除に於いて' do
133     before do
134     end
135     context 'ベースディレクトリから削除するとき' do
136       it 'S3に依頼している' do
137         testname = 'destroy'
138         AWS::S3::S3Object.should_receive(:delete).with(testname, @path).exactly(1)
139         @io.delete(testname)
140       end
141     end
142     context 'サブディレクトリvhから削除するとき' do
143       it 'S3に依頼している' do
144         testname = 'destroy'
145         subd = 'vh'
146         AWS::S3::S3Object.should_receive(:delete).with(subd + '/' + testname, @path).exactly(1)
147         @io.delete(testname, subd)
148       end
149     end
150     context '例外が発生するとき' do
151       it 'PictureIO::Error例外が発生する' do
152         AWS::S3::S3Object.stub(:delete).with(any_args()).and_raise(StandardError)
153         lambda {
154           @io.delete('test.del.not.exist')
155         }.should raise_error(PictureIO::Error)
156       end
157     end
158   end
159 end