Skip to content

Commit 0deb979

Browse files
committed
Add some tests for NamespacedEnvCache
1 parent 036ec6a commit 0deb979

2 files changed

Lines changed: 36 additions & 1 deletion

File tree

lib/namespaced_env_cache.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ def write_collection(name, value, **opts)
8585
raise TypeError, "Can't cache more than one type of object via write_collection"
8686
end
8787

88-
data = [types[0], ...value.map(&:id)]
88+
data = [types[0].to_s, ...value.map(&:id)]
8989
namespaced = construct_ns_key(name, include_community: include_community(opts))
9090
@underlying.write(namespaced, data, **opts)
9191
end
@@ -99,6 +99,7 @@ def write_collection(name, value, **opts)
9999
def read_collection(name, **opts)
100100
namespaced = construct_ns_key(name, include_community: include_community(opts))
101101
data = @underlying.read(namespaced, **opts)
102+
return nil if data.nil?
102103
type = data.slice!(0)
103104
type.constantize.where(id: data)
104105
end
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
require 'test_helper'
2+
3+
class NamespacedEnvCacheTest < ActiveSupport::TestCase
4+
setup :new_cache_store
5+
6+
test 'write_collection/read_collection' do
7+
collection = Post.where(user: users(:standard_user))
8+
assert_nothing_raised do
9+
@cache.write_collection('test_posts_cache', collection)
10+
end
11+
data = @cache.read_collection('test_posts_cache')
12+
assert_not_nil data
13+
original_ids = collection.map(&:id)
14+
cached_ids = data.map(&:id)
15+
cached_ids.each do |cached|
16+
assert_includes original_ids, cached
17+
end
18+
end
19+
20+
test 'fetch_collection' do
21+
data = assert_nothing_raised do
22+
@cache.fetch_collection('test_posts_cache') do
23+
Post.where(user: users(:standard_user))
24+
end
25+
end
26+
assert_not_nil data
27+
end
28+
29+
private
30+
31+
def new_cache_store
32+
@cache = QPixel::NamespacedEnvCache.new(ActiveSupport::Cache::MemoryStore.new)
33+
end
34+
end

0 commit comments

Comments
 (0)