Skip to content

Commit

Permalink
test(cache): add specs for hash based cache
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Apr 10, 2023
1 parent 89e6a18 commit 42f9a37
Show file tree
Hide file tree
Showing 5 changed files with 225 additions and 32 deletions.
16 changes: 1 addition & 15 deletions lib/convenient_service/support/cache/entities/caches/array.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,28 +81,14 @@ def clear
self
end

##
# @param other [Object] Can be any type.
# @return [Boolean]
#
def ==(other)
return unless other.instance_of?(self.class)

return false if array != other.array

true
end

protected
private

##
# @!attribute [r] array
# @return [Array]
#
attr_reader :array

private

##
# @param key [Object] Can be any type.
# @return [Integer, nil]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
# frozen_string_literal: true

module ConvenientService
module Support
class Cache
module Entities
module Caches
class Array < Caches::Base
module Entities
class NullPair < Entities::Pair
##
# @return [void]
#
def initialize
@key = nil
@value = nil
end

##
# @param other_value [Object] Can be any type.
# @return [Object] Can be any type.
#
def value=(other_value)
other_value
end

##
# @return [Boolean]
#
def null_pair?
true
end
end
end
end
end
end
end
end
end
14 changes: 12 additions & 2 deletions lib/convenient_service/support/cache/entities/caches/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,20 @@ def []=(key, value)
# @return [ConvenientService::Support::Cache::Entities::Caches::Base]
#
def scope(key)
# byebug/

fetch(key) { self.class.new }
end

##
# @param other [Object] Can be any type.
# @return [Boolean, nil]
#
def ==(other)
return unless other.instance_of?(self.class)

return false if store != other.store

true
end
end
end
end
Expand Down
16 changes: 2 additions & 14 deletions lib/convenient_service/support/cache/entities/caches/hash.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(hash = {})
end

##
# @return [Hash{ConvenientService::Support::Cache::Entities::Key => Object}]
# @return [Hash{Object => Object}]
#
def store
hash
Expand Down Expand Up @@ -84,19 +84,7 @@ def clear
self
end

##
# @param other [Object] Can be any type.
# @return [Boolean]
#
def ==(other)
return unless other.instance_of?(self.class)

return false if hash != other.hash

true
end

protected
private

##
# @!attribute [r] hash
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,30 @@

# rubocop:disable RSpec/NestedGroups
RSpec.describe ConvenientService::Support::Cache::Entities::Caches::Hash do
example_group "class methods" do
describe ".new" do
context "when `hash` is NOT passed" do
let(:cache) { described_class.new }

it "defaults to empty hash" do
expect(cache.store).to eq({})
end
end
end
end

example_group "instance methods" do
let(:cache) { described_class.new }

describe "#store" do
let(:cache) { described_class.new(hash) }
let(:hash) { {foo: :bar} }

it "returns hash that is used as store internally" do
expect(cache.store).to equal(hash)
end
end

describe "#empty?" do
context "when cache has NO keys" do
before do
Expand All @@ -31,6 +52,124 @@
end
end

describe "#exist?" do
let(:key) { :foo }
let(:value) { :foo }

context "when cache does NOT have `key`" do
before do
cache.clear
end

it "returns `false`" do
expect(cache.exist?(key)).to eq(false)
end
end

context "when cache has `key`" do
before do
cache[key] = value
end

it "returns `true`" do
expect(cache.exist?(key)).to eq(true)
end
end
end

describe "#read" do
let(:key) { :foo }
let(:value) { :foo }

context "when cache does NOT have `key`" do
before do
cache.clear
end

it "returns `nil`" do
expect(cache.read(key)).to eq(nil)
end
end

context "when cache has `key`" do
before do
cache[key] = value
end

it "returns `value` by that `key`" do
expect(cache.read(key)).to eq(value)
end
end
end

describe "#write" do
let(:key) { :foo }
let(:value) { :foo }

context "when cache does NOT have `key`" do
before do
cache.clear
end

it "returns `value`" do
expect(cache.write(key, value)).to eq(value)
end

it "stores `value` by `key`" do
cache.write(key, value)

expect(cache.read(key)).to eq(value)
end
end

context "when cache has `key`" do
before do
cache[key] = value
end

it "returns `value` by that `key`" do
expect(cache.write(key, value)).to eq(value)
end

it "updates `value` by `key`" do
cache.write(key, :bar)

expect(cache.read(key)).to eq(:bar)
end
end
end

describe "#delete" do
let(:key) { :foo }
let(:value) { :foo }

context "when cache does NOT have `key`" do
before do
cache.clear
end

it "returns `nil`" do
expect(cache.delete(key)).to be_nil
end
end

context "when cache has `key`" do
before do
cache[key] = value
end

it "returns `value` by that `key`" do
expect(cache.delete(key)).to eq(value)
end

it "removes `value` by `key`" do
cache.delete(key)

expect(cache.read(key)).to be_nil
end
end
end

describe "#clear" do
context "when cache has NO keys" do
it "returns cache" do
Expand All @@ -43,7 +182,7 @@
cache[:foo] = :bar
end

it "returns cache" do
it "returns `cache`" do
expect(cache.clear).to eq(cache)
end

Expand Down Expand Up @@ -74,6 +213,36 @@
end
end
end

example_group "comparison" do
let(:cache) { described_class.new }

describe "#==" do
context "when caches have different classes" do
let(:other) { 42 }

it "returns `nil`" do
expect(cache == other).to eq(nil)
end
end

context "when caches have different arrays" do
let(:other) { described_class.new([:bar]) }

it "returns `true`" do
expect(cache == other).to eq(false)
end
end

context "when caches have same attributes" do
let(:other) { described_class.new }

it "returns `true`" do
expect(cache == other).to eq(true)
end
end
end
end
end
end
# rubocop:enable RSpec/NestedGroups

0 comments on commit 42f9a37

Please sign in to comment.