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 RSpec.describe Google::APIClient::Service::SimpleFileStore do
23 FILE_NAME = 'test.cache'
25 describe 'with no cache file' do
27 File.delete(FILE_NAME) if File.exists?(FILE_NAME)
28 @cache = Google::APIClient::Service::SimpleFileStore.new(FILE_NAME)
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
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
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
46 it 'should create a cache file when asked to fetch a key with a default' do
47 expect(@cache.fetch('new_key') do
50 expect(File.exists?(FILE_NAME)).to be_truthy
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
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
64 describe 'with an existing cache' 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')
71 it 'should return true when asked if an existing key exists' do
72 expect(@cache.exist?('existing_key')).to be_truthy
75 it 'should return false when asked if a nonexistent key exists' do
76 expect(@cache.exist?('invalid')).to be_falsey
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')
83 it 'should return nil for a nonexistent key when asked to read it' do
84 expect(@cache.read('invalid')).to be_nil
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')
91 it 'should return nil for a nonexistent key when asked to fetch it' do
92 expect(@cache.fetch('invalid')).to be_nil
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
99 expect(@cache.read('new_key')).to eq('value')
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
107 it 'should return false when asked to delete a nonexistent key' do
108 expect(@cache.delete('invalid')).to be_falsey
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')
116 it 'should convert keys to strings when reading them' do
117 expect(@cache.read(:existing_key)).to eq('existing_value')
120 it 'should convert keys to strings when fetching them' do
121 expect(@cache.fetch(:existing_key)).to eq('existing_value')
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
131 File.delete(FILE_NAME) if File.exists?(FILE_NAME)