cc7208d6ea73d3f12ba468e575f9395f6ad5d8b3
[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   describe 'with no cache file' do
26     before(:each) do
27       File.delete(FILE_NAME) if File.exists?(FILE_NAME)
28       @cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
29     end
30
31     it 'should return nil when asked if a key exists' do
32       expect(@cache.exist?('invalid')).to be_nil
33       expect(File.exists?(FILE_NAME)).to be_falsey
34     end
35
36     it 'should return nil when asked to read a key' do
37       expect(@cache.read('invalid')).to be_nil
38       expect(File.exists?(FILE_NAME)).to be_falsey
39     end
40
41     it 'should return nil when asked to fetch a key' do
42       expect(@cache.fetch('invalid')).to be_nil
43       expect(File.exists?(FILE_NAME)).to be_falsey
44     end
45
46     it 'should create a cache file when asked to fetch a key with a default' do
47       expect(@cache.fetch('new_key') do
48         'value'
49       end).to eq('value')
50       expect(File.exists?(FILE_NAME)).to be_truthy
51     end
52
53     it 'should create a cache file when asked to write a key' do
54       @cache.write('new_key', 'value')
55       expect(File.exists?(FILE_NAME)).to be_truthy
56     end
57
58     it 'should return nil when asked to delete a key' do
59       expect(@cache.delete('invalid')).to be_nil
60       expect(File.exists?(FILE_NAME)).to be_falsey
61     end
62   end
63
64   describe 'with an existing cache' do
65     before(:each) do
66       File.delete(FILE_NAME) if File.exists?(FILE_NAME)
67       @cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
68       @cache.write('existing_key', 'existing_value')
69     end
70
71     it 'should return true when asked if an existing key exists' do
72       expect(@cache.exist?('existing_key')).to be_truthy
73     end
74
75     it 'should return false when asked if a nonexistent key exists' do
76       expect(@cache.exist?('invalid')).to be_falsey
77     end
78
79     it 'should return the value for an existing key when asked to read it' do
80       expect(@cache.read('existing_key')).to eq('existing_value')
81     end
82
83     it 'should return nil for a nonexistent key when asked to read it' do
84       expect(@cache.read('invalid')).to be_nil
85     end
86
87     it 'should return the value for an existing key when asked to read it' do
88       expect(@cache.read('existing_key')).to eq('existing_value')
89     end
90
91     it 'should return nil for a nonexistent key when asked to fetch it' do
92       expect(@cache.fetch('invalid')).to be_nil
93     end
94
95     it 'should return and save the default value for a nonexistent key when asked to fetch it with a default' do
96       expect(@cache.fetch('new_key') do
97         'value'
98       end).to eq('value')
99       expect(@cache.read('new_key')).to eq('value')
100     end
101
102     it 'should remove an existing value and return true when asked to delete it' do
103       expect(@cache.delete('existing_key')).to be_truthy
104       expect(@cache.read('existing_key')).to be_nil
105     end
106
107     it 'should return false when asked to delete a nonexistent key' do
108       expect(@cache.delete('invalid')).to be_falsey
109     end
110
111     it 'should convert keys to strings when storing them' do
112       @cache.write(:symbol_key, 'value')
113       expect(@cache.read('symbol_key')).to eq('value')
114     end
115
116     it 'should convert keys to strings when reading them' do
117       expect(@cache.read(:existing_key)).to eq('existing_value')
118     end
119
120     it 'should convert keys to strings when fetching them' do
121       expect(@cache.fetch(:existing_key)).to eq('existing_value')
122     end
123
124     it 'should convert keys to strings when deleting them' do
125       expect(@cache.delete(:existing_key)).to be_truthy
126       expect(@cache.read('existing_key')).to be_nil
127     end
128   end
129
130   after(:all) do
131     File.delete(FILE_NAME) if File.exists?(FILE_NAME)
132   end
133 end