f041c42935f1b24b75d1117473fb0f5dde240479
[arvados.git] / spec / google / api_client / simple_file_store_spec.rb
1 # encoding:utf-8
2
3 # Copyright 2013 Google Inc.
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 #      http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 require 'spec_helper'
18
19 require 'google/api_client/service/simple_file_store'
20
21 describe Google::APIClient::Service::SimpleFileStore do
22
23   FILE_NAME = 'test.cache'
24
25   before(:all) do
26     File.delete(FILE_NAME) if File.exists?(FILE_NAME)
27   end
28
29   describe 'with no cache file' do
30     before(:each) do
31       File.delete(FILE_NAME) if File.exists?(FILE_NAME)
32       @cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
33     end
34
35     it 'should return nil when asked if a key exists' do
36       expect(@cache.exist?('invalid')).to be_nil
37       expect(File.exists?(FILE_NAME)).to be_falsey
38     end
39
40     it 'should return nil when asked to read a key' do
41       expect(@cache.read('invalid')).to be_nil
42       expect(File.exists?(FILE_NAME)).to be_falsey
43     end
44
45     it 'should return nil when asked to fetch a key' do
46       expect(@cache.fetch('invalid')).to be_nil
47       expect(File.exists?(FILE_NAME)).to be_falsey
48     end
49
50     it 'should create a cache file when asked to fetch a key with a default' do
51       expect(@cache.fetch('new_key') do
52         'value'
53       end).to eq('value')
54       expect(File.exists?(FILE_NAME)).to be_truthy
55     end
56
57     it 'should create a cache file when asked to write a key' do
58       @cache.write('new_key', 'value')
59       expect(File.exists?(FILE_NAME)).to be_truthy
60     end
61
62     it 'should return nil when asked to delete a key' do
63       expect(@cache.delete('invalid')).to be_nil
64       expect(File.exists?(FILE_NAME)).to be_falsey
65     end
66   end
67
68   describe 'with an existing cache' do
69     before(:each) do
70       File.delete(FILE_NAME) if File.exists?(FILE_NAME)
71       @cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
72       @cache.write('existing_key', 'existing_value')
73     end
74
75     it 'should return true when asked if an existing key exists' do
76       expect(@cache.exist?('existing_key')).to be_truthy
77     end
78
79     it 'should return false when asked if a nonexistent key exists' do
80       expect(@cache.exist?('invalid')).to be_falsey
81     end
82
83     it 'should return the value for an existing key when asked to read it' do
84       expect(@cache.read('existing_key')).to eq('existing_value')
85     end
86
87     it 'should return nil for a nonexistent key when asked to read it' do
88       expect(@cache.read('invalid')).to be_nil
89     end
90
91     it 'should return the value for an existing key when asked to read it' do
92       expect(@cache.read('existing_key')).to eq('existing_value')
93     end
94
95     it 'should return nil for a nonexistent key when asked to fetch it' do
96       expect(@cache.fetch('invalid')).to be_nil
97     end
98
99     it 'should return and save the default value for a nonexistent key when asked to fetch it with a default' do
100       expect(@cache.fetch('new_key') do
101         'value'
102       end).to eq('value')
103       expect(@cache.read('new_key')).to eq('value')
104     end
105
106     it 'should remove an existing value and return true when asked to delete it' do
107       expect(@cache.delete('existing_key')).to be_truthy
108       expect(@cache.read('existing_key')).to be_nil
109     end
110
111     it 'should return false when asked to delete a nonexistent key' do
112       expect(@cache.delete('invalid')).to be_falsey
113     end
114
115     it 'should convert keys to strings when storing them' do
116       @cache.write(:symbol_key, 'value')
117       expect(@cache.read('symbol_key')).to eq('value')
118     end
119
120     it 'should convert keys to strings when reading them' do
121       expect(@cache.read(:existing_key)).to eq('existing_value')
122     end
123
124     it 'should convert keys to strings when fetching them' do
125       expect(@cache.fetch(:existing_key)).to eq('existing_value')
126     end
127
128     it 'should convert keys to strings when deleting them' do
129       expect(@cache.delete(:existing_key)).to be_truthy
130       expect(@cache.read('existing_key')).to be_nil
131     end
132   end
133
134   after(:all) do
135     File.delete(FILE_NAME) if File.exists?(FILE_NAME)
136   end
137 end