From 42f9a3739349865233e4c7c2743bb32157fedb60 Mon Sep 17 00:00:00 2001 From: Marian13 Date: Tue, 11 Apr 2023 02:11:03 +0300 Subject: [PATCH] test(cache): add specs for hash based cache --- .../support/cache/entities/caches/array.rb | 16 +- .../caches/array/entities/null_pair.rb | 40 ++++ .../support/cache/entities/caches/base.rb | 14 +- .../support/cache/entities/caches/hash.rb | 16 +- .../cache/entities/caches/hash_spec.rb | 171 +++++++++++++++++- 5 files changed, 225 insertions(+), 32 deletions(-) create mode 100644 lib/convenient_service/support/cache/entities/caches/array/entities/null_pair.rb diff --git a/lib/convenient_service/support/cache/entities/caches/array.rb b/lib/convenient_service/support/cache/entities/caches/array.rb index 06716e293b..4d87b89715 100644 --- a/lib/convenient_service/support/cache/entities/caches/array.rb +++ b/lib/convenient_service/support/cache/entities/caches/array.rb @@ -81,19 +81,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 array != other.array - - true - end - - protected + private ## # @!attribute [r] array @@ -101,8 +89,6 @@ def ==(other) # attr_reader :array - private - ## # @param key [Object] Can be any type. # @return [Integer, nil] diff --git a/lib/convenient_service/support/cache/entities/caches/array/entities/null_pair.rb b/lib/convenient_service/support/cache/entities/caches/array/entities/null_pair.rb new file mode 100644 index 0000000000..69d04d418d --- /dev/null +++ b/lib/convenient_service/support/cache/entities/caches/array/entities/null_pair.rb @@ -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 diff --git a/lib/convenient_service/support/cache/entities/caches/base.rb b/lib/convenient_service/support/cache/entities/caches/base.rb index b1c3fc7456..20ccacb046 100644 --- a/lib/convenient_service/support/cache/entities/caches/base.rb +++ b/lib/convenient_service/support/cache/entities/caches/base.rb @@ -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 diff --git a/lib/convenient_service/support/cache/entities/caches/hash.rb b/lib/convenient_service/support/cache/entities/caches/hash.rb index 6f1367182c..12030c893b 100644 --- a/lib/convenient_service/support/cache/entities/caches/hash.rb +++ b/lib/convenient_service/support/cache/entities/caches/hash.rb @@ -14,7 +14,7 @@ def initialize(hash = {}) end ## - # @return [Hash{ConvenientService::Support::Cache::Entities::Key => Object}] + # @return [Hash{Object => Object}] # def store hash @@ -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 diff --git a/spec/lib/convenient_service/support/cache/entities/caches/hash_spec.rb b/spec/lib/convenient_service/support/cache/entities/caches/hash_spec.rb index 1e340ffd04..cee03f6211 100644 --- a/spec/lib/convenient_service/support/cache/entities/caches/hash_spec.rb +++ b/spec/lib/convenient_service/support/cache/entities/caches/hash_spec.rb @@ -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 @@ -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 @@ -43,7 +182,7 @@ cache[:foo] = :bar end - it "returns cache" do + it "returns `cache`" do expect(cache.clear).to eq(cache) end @@ -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