3 # Copyright 2013 Google Inc.
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
19 require 'google/api_client/service/simple_file_store'
21 describe Google::APIClient::Service::SimpleFileStore do
23 FILE_NAME = 'test.cache'
26 File.delete(FILE_NAME) if File.exists?(FILE_NAME)
29 describe 'with no cache file' do
31 File.delete(FILE_NAME) if File.exists?(FILE_NAME)
32 @cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
35 it 'should return nil when asked if a key exists' do
36 @cache.exist?('invalid').should be_nil
37 File.exists?(FILE_NAME).should be_false
40 it 'should return nil when asked to read a key' do
41 @cache.read('invalid').should be_nil
42 File.exists?(FILE_NAME).should be_false
45 it 'should return nil when asked to fetch a key' do
46 @cache.fetch('invalid').should be_nil
47 File.exists?(FILE_NAME).should be_false
50 it 'should create a cache file when asked to fetch a key with a default' do
51 @cache.fetch('new_key') do
54 File.exists?(FILE_NAME).should be_true
57 it 'should create a cache file when asked to write a key' do
58 @cache.write('new_key', 'value')
59 File.exists?(FILE_NAME).should be_true
62 it 'should return nil when asked to delete a key' do
63 @cache.delete('invalid').should be_nil
64 File.exists?(FILE_NAME).should be_false
68 describe 'with an existing cache' 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')
75 it 'should return true when asked if an existing key exists' do
76 @cache.exist?('existing_key').should be_true
79 it 'should return false when asked if a nonexistent key exists' do
80 @cache.exist?('invalid').should be_false
83 it 'should return the value for an existing key when asked to read it' do
84 @cache.read('existing_key').should == 'existing_value'
87 it 'should return nil for a nonexistent key when asked to read it' do
88 @cache.read('invalid').should be_nil
91 it 'should return the value for an existing key when asked to read it' do
92 @cache.read('existing_key').should == 'existing_value'
95 it 'should return nil for a nonexistent key when asked to fetch it' do
96 @cache.fetch('invalid').should be_nil
99 it 'should return and save the default value for a nonexistent key when asked to fetch it with a default' do
100 @cache.fetch('new_key') do
102 end.should == 'value'
103 @cache.read('new_key').should == 'value'
106 it 'should remove an existing value and return true when asked to delete it' do
107 @cache.delete('existing_key').should be_true
108 @cache.read('existing_key').should be_nil
111 it 'should return false when asked to delete a nonexistent key' do
112 @cache.delete('invalid').should be_false
115 it 'should convert keys to strings when storing them' do
116 @cache.write(:symbol_key, 'value')
117 @cache.read('symbol_key').should == 'value'
120 it 'should convert keys to strings when reading them' do
121 @cache.read(:existing_key).should == 'existing_value'
124 it 'should convert keys to strings when fetching them' do
125 @cache.fetch(:existing_key).should == 'existing_value'
128 it 'should convert keys to strings when deleting them' do
129 @cache.delete(:existing_key).should be_true
130 @cache.read('existing_key').should be_nil
135 File.delete(FILE_NAME) if File.exists?(FILE_NAME)