Skip to content

Commit

Permalink
Get back to 100% code coverage in static_cache_cache plugin
Browse files Browse the repository at this point in the history
Move sorting the cache into a private method that's easier to test.
  • Loading branch information
jeremyevans committed Dec 20, 2024
1 parent 42a8b30 commit f827455
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 10 deletions.
23 changes: 13 additions & 10 deletions lib/sequel/plugins/static_cache_cache.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,18 @@ def self.configure(model, file)
module ClassMethods
# Dump the in-memory cached rows to the cache file.
def dump_static_cache_cache
static_cache_cache = {}
@static_cache_cache.sort do |a, b|
File.open(@static_cache_cache_file, 'wb'){|f| f.write(Marshal.dump(sort_static_cache_hash(@static_cache_cache)))}
nil
end

Plugins.inherited_instance_variables(self, :@static_cache_cache_file=>nil, :@static_cache_cache=>nil)

private

# Sort the given static cache hash in a deterministic way, so that
# the same static cache values will result in the same marshal file.
def sort_static_cache_hash(cache)
cache = cache.sort do |a, b|
a, = a
b, = b
if a.is_a?(Array)
Expand All @@ -47,17 +57,10 @@ def dump_static_cache_cache
else
a <=> b
end
end.each do |k, v|
static_cache_cache[k] = v
end
File.open(@static_cache_cache_file, 'wb'){|f| f.write(Marshal.dump(static_cache_cache))}
nil
Hash[cache]
end

Plugins.inherited_instance_variables(self, :@static_cache_cache_file=>nil, :@static_cache_cache=>nil)

private

# Load the rows for the model from the cache if available.
# If not available, load the rows from the database, and
# then update the cache with the raw rows.
Expand Down
4 changes: 4 additions & 0 deletions spec/extensions/static_cache_cache_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -88,5 +88,9 @@ def self.name; 'Bar' end
c = Class.new(Sequel::Model)
c.plugin :static_cache_cache, @file
c.instance_variable_get(:@static_cache_cache).keys.must_equal ['Bar', 'Foo', ['Bar', :a], ['Bar', :c]]

c.send(:sort_static_cache_hash, {"Foo"=>[], ["Bar", "baz"]=>[]}).keys.must_equal ["Foo", ["Bar", "baz"]]
c.send(:sort_static_cache_hash, {["Bar", "baz"]=>[], "Foo"=>[]}).keys.must_equal ["Foo", ["Bar", "baz"]]
c.send(:sort_static_cache_hash, {["Foo", "baz"]=>[], ["Bar", "bar"]=>[]}).keys.must_equal [["Bar", "bar"], ["Foo", "baz"]]
end
end

0 comments on commit f827455

Please sign in to comment.