Permalink
Browse files

Add cache tests

  • Loading branch information...
1 parent b72b5c6 commit f317ad3fbde0e8755fd0d0e0738ba35f0c8b455b @mckramer committed Nov 3, 2012
Showing with 55 additions and 6 deletions.
  1. +6 −6 lib/earth_tools/cache.rb
  2. +49 −0 test/cache_test.rb
View
@@ -22,13 +22,13 @@ def initialize(store, prefix)
# Read from the cache.
# @return the object saved in the cache
def [](url)
- store[key_for(url)]
+ @store[key_for(url)]
end
##
# Write to the cache.
def []=(url, value)
- store[key_for(url)] = value
+ @store[key_for(url)] = value
end
##
@@ -38,7 +38,7 @@ def expire(url)
if url == :all
urls.each{ |u| expire(u) }
else
- store.del(key_for(url))
+ @store.send(@store.respond_to?(:del) ? :del : :delete, key_for(url))
end
end
@@ -51,22 +51,22 @@ def expire(url)
# Cache key for a given URL.
# @return [String] the cache key
def key_for(url)
- [prefix, url].join
+ [@prefix, url].join
end
##
# Array of keys with the currently configured prefix
# that have non-nil values.
# @return [Array] the list of keys
def keys
- store.keys.select{ |k| k.match /^#{prefix}/ and cleave(store[k]) }
+ @store.keys.select{ |k| k.match /^#{@prefix}/ and cleave(@store[k]) }
end
##
# Array of cached URLs.
# @return [Array] the list of cached URLs
def urls
- keys.map{ |k| k[/^#{prefix}(.*)/, 1] }
+ keys.map{ |k| k[/^#{@prefix}(.*)/, 1] }
end
##
View
@@ -0,0 +1,49 @@
+# encoding: utf-8
+
+require 'test_helper'
+require 'earth_tools'
+require 'earth_tools/cache'
+
+##
+# Tests for the Cache
+#
+class CacheTest < MiniTest::Unit::TestCase
+
+ def setup
+ @raw_cache = {}
+ @cache = EarthTools::Cache.new(@raw_cache, 'earth_tools:')
+ end
+
+ def test_store_and_retrieve_from_cache
+ @cache['test/1'] = :data
+
+ assert_equal :data, @cache['test/1'], 'The cache was not stored properly.'
+ end
+
+ def test_remove_single_url
+ @cache['test/1'] = :data
+ @cache['test/2'] = :data
+
+ @cache.expire('test/1')
+
+ assert_nil @cache['test/1'], 'The cache should have deleted entry.'
+ assert_equal :data, @cache['test/2'], 'The cache should not have deleted entry.'
+
+ end
+
+ def test_remove_all_prefixed_urls
+ @raw_cache['test'] = :data
+ @raw_cache['other:test'] = :data
+ @cache['test/1'] = :data
+ @cache['test/2'] = :data
+ @cache['test/3'] = :data
+
+ @cache.expire(:all)
+ assert_equal :data, @raw_cache['test'], 'The cache should not affect non-prefixed entries.'
+ assert_equal :data, @raw_cache['other:test'], 'The cache should not affect non-prefixed entries.'
+ assert_nil @cache['test/1'], 'The cache should be empty of prefixed entries.'
+ assert_nil @cache['test/2'], 'The cache should be empty of prefixed entries.'
+ assert_nil @cache['test/3'], 'The cache should be empty of prefixed entries.'
+ end
+
+end

0 comments on commit f317ad3

Please sign in to comment.