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 expect(@cache.exist?('invalid')).to be_nil
37 expect(File.exists?(FILE_NAME)).to be_falsey
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
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
50 it 'should create a cache file when asked to fetch a key with a default' do
51 expect(@cache.fetch('new_key') do
54 expect(File.exists?(FILE_NAME)).to be_truthy
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
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
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 expect(@cache.exist?('existing_key')).to be_truthy
79 it 'should return false when asked if a nonexistent key exists' do
80 expect(@cache.exist?('invalid')).to be_falsey
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')
87 it 'should return nil for a nonexistent key when asked to read it' do
88 expect(@cache.read('invalid')).to be_nil
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')
95 it 'should return nil for a nonexistent key when asked to fetch it' do
96 expect(@cache.fetch('invalid')).to 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 expect(@cache.fetch('new_key') do
103 expect(@cache.read('new_key')).to eq('value')
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
111 it 'should return false when asked to delete a nonexistent key' do
112 expect(@cache.delete('invalid')).to be_falsey
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')
120 it 'should convert keys to strings when reading them' do
121 expect(@cache.read(:existing_key)).to eq('existing_value')
124 it 'should convert keys to strings when fetching them' do
125 expect(@cache.fetch(:existing_key)).to eq('existing_value')
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
135 File.delete(FILE_NAME) if File.exists?(FILE_NAME)