OSDN Git Service

pass test: Picture.store
[pettanr/pettanr.git] / lib / test / local_picture_spec.rb
1 # -*- encoding: utf-8 -*-
2 #ファイル保存
3 require 'local_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::LocalPicture do
18   before do
19     @path = Rails.root + 'lib/test/temp/'
20     @io = PictureIO::LocalPicture.new @path
21   end
22
23   describe 'ファイル存在確認に於いて' do
24     before do
25     end
26     context 'ファイルがあるとき' do
27       it 'trueが返る' do
28         File.open @path + 'exist', 'w' do |f|
29           f.puts 'test data'
30         end
31         @io.exist?('exist').should be_true
32       end
33     end
34     context 'ファイルがないとき' do
35       it 'falseが返る' do
36         @io.exist?('not.exist').should be_false
37       end
38     end
39   end
40   
41   describe 'ファイル取得に於いて' do
42     before do
43       File.open @path + 'test.get', 'w' do |f|
44         f.puts 'basedata'
45       end
46       File.open @path + 'v/testsub.get', 'w' do |f|
47         f.puts 'subdata'
48       end
49     end
50     context 'ベースディレクトリから取るとき' do
51       it 'basedataが返る' do
52         @io.get('test.get').should match /basedata/
53       end
54     end
55     context 'サブディレクトリvから取るとき' do
56       it 'subdataが返る' do
57         @io.get('testsub.get', 'v').should match /subdata/
58       end
59     end
60     context '例外が発生するとき' do
61       it 'falseが返る' do
62         File.stub(:open).with(any_args()).and_raise(StandardError)
63         @io.get('test.get').should be_false
64       end
65     end
66   end
67   
68   describe 'ファイル作成に於いて' do
69     before do
70       File.open @path + 'test.put', 'w' do |f|
71         f.puts 'already'
72       end
73     end
74     context 'ベースディレクトリに作成するとき' do
75       before do
76         @io.put('created', 'create.put')
77       end
78       it 'ファイルが作成されている' do
79         File.exist?(@path + 'create.put').should be_true
80       end
81       it 'createdが書きこまれている' do
82         d = File.open(@path + 'create.put').read
83         d.should match /create/
84       end
85     end
86     context '既存ファイルを更新するとき' do
87       before do
88         @io.put('overwrited', 'test.put')
89       end
90       it 'overwritedが書きこまれている' do
91         d = File.open(@path + 'test.put').read
92         d.should match /overwrited/
93       end
94       it '上書きされている' do
95         d = File.open(@path + 'test.put').read
96         d.should_not match /already/
97       end
98     end
99     context 'サブディレクトリhに作成するとき' do
100       before do
101         @io.put('subdirtest', 'testsub.put', 'h')
102       end
103       it 'ファイルが作成されている' do
104         File.exist?(@path + 'h/testsub.put').should be_true
105       end
106       it 'subdirtestが書きこまれている' do
107         d = File.open(@path + 'h/testsub.put').read
108         d.should match /subdirtest/
109       end
110     end
111     context '例外が発生するとき' do
112       it 'falseが返る' do
113         File.stub(:open).with(any_args()).and_raise(StandardError)
114         @io.put('bindata', 'test.put').should be_false
115       end
116     end
117   end
118   
119   describe 'ファイル削除に於いて' do
120     before do
121       File.open @path + 'test.del', 'w' do |f|
122         f.puts 'basedata'
123       end
124       File.open @path + 'thumbnail/testsub.del', 'w' do |f|
125         f.puts 'subdata'
126       end
127     end
128     context 'ベースディレクトリから削除するとき' do
129       it 'ファイルが削除されている' do
130         @io.delete('test.del')
131         File.exist?(@path + 'test.del').should be_false
132       end
133     end
134     context 'サブディレクトリthumbnailから削除するとき' do
135       it 'ファイルが削除されている' do
136         @io.delete('testsub.del', 'thumbnail')
137         File.exist?(@path + 'thumbnail/testsub.del').should be_false
138       end
139     end
140     context '例外が発生するとき' do
141       it 'falseが返る' do
142         File.stub(:delete).with(any_args()).and_raise(StandardError)
143         @io.delete('test.del.not.exist').should be_false
144       end
145     end
146   end
147 end