Skip to content

Commit

Permalink
refactor(utils): move hash specs into separate files
Browse files Browse the repository at this point in the history
  • Loading branch information
marian13 committed Oct 14, 2022
1 parent 2a7905b commit 30b3978
Show file tree
Hide file tree
Showing 7 changed files with 94 additions and 56 deletions.
31 changes: 24 additions & 7 deletions lib/convenient_service/utils/hash/except.rb
Original file line number Diff line number Diff line change
@@ -1,24 +1,41 @@
# frozen_string_literal: true

##
# Usage example:
#
# ConvenientService::Utils::Hash::Except.call({foo: :bar, baz: :qux}, keys: [:foo])
# @example:
# ConvenientService::Utils::Hash::Except.call({foo: :bar, baz: :qux}, [:foo])
#
module ConvenientService
module Utils
module Hash
class Except < Support::Command
attr_reader :hash, :keys
##
# @!attribute [r] hash
# @return [Hash]
#
attr_reader :hash

def initialize(hash, keys:)
##
# @!attribute [r] keys
# @return [Array]
#
attr_reader :keys

##
# @param hash [Hash]
# @param keys [Array]
# @return [void]
#
def initialize(hash, keys)
@hash = hash
@keys = keys
end

##
# NOTE: Copied with minimal modifications from:
# https://api.rubyonrails.org/classes/Hash.html#method-i-except
# @return [Hash]
#
# @internal
# NOTE: Copied with minimal modifications from:
# https://api.rubyonrails.org/classes/Hash.html#method-i-except
#
def call
hash.slice(*hash.keys - keys)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,15 +109,15 @@ def result
end

context "when `service` is NOT passed" do
let(:result) { service_class.success(**ConvenientService::Utils::Hash.except(params, keys: [:service])) }
let(:result) { service_class.success(**ConvenientService::Utils::Hash.except(params, [:service])) }

it "defaults `service` to `ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_SERVICE_INSTANCE`" do
expect(result.service).to eq(ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_SERVICE_INSTANCE)
end
end

context "when `data` is NOT passed" do
let(:result) { service_class.success(**ConvenientService::Utils::Hash.except(params, keys: [:data])) }
let(:result) { service_class.success(**ConvenientService::Utils::Hash.except(params, [:data])) }

it "defaults `data` to `ConvenientService::Service::Plugins::HasResult::Constants::DEFAUTL_SUCCESS_DATA`" do
expect(result.data).to eq(ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_SUCCESS_DATA)
Expand Down Expand Up @@ -173,30 +173,30 @@ def result
end

context "when `service` is NOT passed" do
let(:result) { service_class.failure(**ConvenientService::Utils::Hash.except(params, keys: [:service])) }
let(:result) { service_class.failure(**ConvenientService::Utils::Hash.except(params, [:service])) }

it "defaults `service` to `ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_SERVICE_INSTANCE`" do
expect(result.service).to eq(ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_SERVICE_INSTANCE)
end
end

context "when `data` is NOT passed" do
let(:result) { service_class.failure(**ConvenientService::Utils::Hash.except(params, keys: [:data])) }
let(:result) { service_class.failure(**ConvenientService::Utils::Hash.except(params, [:data])) }

it "defaults `data` to `ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_FAILURE_DATA`" do
expect(result.data).to eq(ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_FAILURE_DATA)
end
end

context "when `message` is NOT passed" do
let(:result) { service_class.failure(**ConvenientService::Utils::Hash.except(params, keys: [:message])) }
let(:result) { service_class.failure(**ConvenientService::Utils::Hash.except(params, [:message])) }

it "defaults `message` to concatenated by space first key and first value from `data`" do
expect(result.message).to eq(first_pair_message)
end

context "when `data` is empty" do
let(:result) { service_class.failure(**ConvenientService::Utils::Hash.except(params, keys: [:message]).merge(data: {})) }
let(:result) { service_class.failure(**ConvenientService::Utils::Hash.except(params, [:message]).merge(data: {})) }

it "internally passes `ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_FAILURE_MESSAGE` as `message` to result constructor" do
allow(service_class.result_class).to receive(:new).with(hash_including(message: ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_FAILURE_MESSAGE)).and_call_original
Expand Down Expand Up @@ -248,23 +248,23 @@ def result
end

context "when `service` is NOT passed" do
let(:result) { service_class.error(**ConvenientService::Utils::Hash.except(params, keys: [:service])) }
let(:result) { service_class.error(**ConvenientService::Utils::Hash.except(params, [:service])) }

it "defaults service to `ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_SERVICE_INSTANCE`" do
expect(result.service).to eq(ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_SERVICE_INSTANCE)
end
end

context "when `message` is NOT passed" do
let(:result) { service_class.error(**ConvenientService::Utils::Hash.except(params, keys: [:message])) }
let(:result) { service_class.error(**ConvenientService::Utils::Hash.except(params, [:message])) }

it "defaults `message` to `ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_ERROR_MESSAGE`" do
expect(result.message).to eq(ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_ERROR_MESSAGE)
end
end

context "when `code` is NOT passed" do
let(:result) { service_class.error(**ConvenientService::Utils::Hash.except(params, keys: [:code])) }
let(:result) { service_class.error(**ConvenientService::Utils::Hash.except(params, [:code])) }

it "defaults `code` to `ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_ERROR_CODE`" do
expect(result.code).to eq(ConvenientService::Service::Plugins::HasResult::Constants::DEFAULT_ERROR_CODE)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@

describe "#has_orgnizer" do
context "when `method` does NOT have `organizer`" do
let(:method_instance) { method_class.new(**ConvenientService::Utils::Hash.except(method_kwargs, keys: [:organizer])) }
let(:method_instance) { method_class.new(**ConvenientService::Utils::Hash.except(method_kwargs, [:organizer])) }

it "returns `false`" do
expect(method.has_organizer?).to eq(false)
Expand Down Expand Up @@ -231,7 +231,7 @@

describe "#value" do
context "when `method` does NOT have `organizer`" do
let(:method_instance) { method_class.new(**ConvenientService::Utils::Hash.except(method_kwargs, keys: [:organizer])) }
let(:method_instance) { method_class.new(**ConvenientService::Utils::Hash.except(method_kwargs, [:organizer])) }

let(:error_message) do
<<~TEXT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
end

context "when `kwargs[:in]` is NOT passed" do
let(:kwargs) { ConvenientService::Utils::Hash.except(default_kwargs, keys: [:in]) }
let(:kwargs) { ConvenientService::Utils::Hash.except(default_kwargs, [:in]) }

it "defaults `inputs` to empty array" do
expect(command_result.inputs).to eq([])
Expand All @@ -49,7 +49,7 @@
end

context "when `kwargs[:out]` is NOT passed" do
let(:kwargs) { ConvenientService::Utils::Hash.except(default_kwargs, keys: [:out]) }
let(:kwargs) { ConvenientService::Utils::Hash.except(default_kwargs, [:out]) }

it "defaults `outputs` to empty array" do
expect(command_result.outputs).to eq([])
Expand Down
48 changes: 48 additions & 0 deletions spec/lib/convenient_service/utils/hash/except_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
# frozen_string_literal: true

require "spec_helper"

require "convenient_service"

RSpec.describe ConvenientService::Utils::Hash::Except do
describe ".call" do
let(:hash) { {foo: "foo", bar: "bar", baz: "baz"} }
let(:result) { described_class.call(hash, keys) }

context "when hash does NOT contain any key from `keys`" do
let(:keys) { [:qux] }

it "returns hash with same keys as original hash" do
expect(result.keys).to eq(hash.keys)
end

it "copies hash" do
expect(result.object_id).not_to eq(hash.object_id)
end
end

context "when hash contain single key from `keys`" do
let(:keys) { [:baz] }

it "returns hash without that key" do
expect(result.keys).to eq(hash.keys - keys)
end

it "copies hash" do
expect(result.object_id).not_to eq(hash.object_id)
end
end

context "when hash contain multiple keys from `keys`" do
let(:keys) { [:foo, :baz] }

it "returns hash without those keys" do
expect(result.keys).to eq(hash.keys - keys)
end

it "copies hash" do
expect(result.object_id).not_to eq(hash.object_id)
end
end
end
end
43 changes: 8 additions & 35 deletions spec/lib/convenient_service/utils/hash_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,44 +5,17 @@
require "convenient_service"

RSpec.describe ConvenientService::Utils::Hash do
include ConvenientService::RSpec::Matchers::DelegateTo

describe ".except" do
let(:hash) { {foo: "foo", bar: "bar", baz: "baz"} }
let(:util_result) { described_class.except(hash, keys: keys) }

context "when hash does NOT contain any key from `keys`" do
let(:keys) { [:qux] }

it "returns hash with same keys as original hash" do
expect(util_result.keys).to eq(hash.keys)
end

it "copies hash" do
expect(util_result.object_id).not_to eq(hash.object_id)
end
end

context "when hash contain single key from `keys`" do
let(:keys) { [:baz] }

it "returns hash without that key" do
expect(util_result.keys).to eq(hash.keys - keys)
end

it "copies hash" do
expect(util_result.object_id).not_to eq(hash.object_id)
end
end

context "when hash contain multiple keys from `keys`" do
let(:keys) { [:foo, :baz] }

it "returns hash without those keys" do
expect(util_result.keys).to eq(hash.keys - keys)
end
let(:keys) { [:qux] }

it "copies hash" do
expect(util_result.object_id).not_to eq(hash.object_id)
end
specify do
expect { described_class.except(hash, keys) }
.to delegate_to(ConvenientService::Utils::Hash::Except, :call)
.with_arguments(hash, keys)
.and_return_its_value
end
end
end
2 changes: 1 addition & 1 deletion spec/lib/convenient_service/utils/module_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def foo
end

context "when `private` is NOT passed" do
let(:kwargs) { ConvenientService::Utils::Hash.except(default_kwargs, keys: [:private]) }
let(:kwargs) { ConvenientService::Utils::Hash.except(default_kwargs, [:private]) }

it "defaults to `false`" do
##
Expand Down

0 comments on commit 30b3978

Please sign in to comment.