Skip to content

Commit

Permalink
Fixed issues with enum that contains type float (#186)
Browse files Browse the repository at this point in the history
  • Loading branch information
zabil-maooz authored Sep 28, 2024
1 parent ce8b8e9 commit 1d37821
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 8 deletions.
2 changes: 1 addition & 1 deletion lib/store_model/types/enum_type.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def cast_value(value)

case value
when String, Symbol then cast_symbol_value(value)
when Integer then cast_integer_value(value)
when Integer, Float then cast_integer_value(value)
else
raise StoreModel::Types::CastError,
"failed casting #{value.inspect}, only String, Symbol or " \
Expand Down
33 changes: 26 additions & 7 deletions spec/store_model/types/enum_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

RSpec.describe StoreModel::Types::EnumType do
let(:type) { described_class.new({ active: 1, archived: 0 }, raise_on_invalid_values) }
let(:float_type) { described_class.new({ pi: 3.14, tau: 6.28 }, raise_on_invalid_values) }
let(:raise_on_invalid_values) { true }

describe "#type" do
Expand Down Expand Up @@ -95,15 +96,33 @@

it { is_expected.to be_nil }
end
end

describe "float#cast_value" do
subject { float_type.cast_value(value) }

context "when Float is passed" do
let(:value) { 3.14 }

context "when instance of illegal class is passed" do
let(:value) { 3.5 }
it { is_expected.to eq(3.14) }

context "when value is not in the list" do
let(:value) { 1.5 }

it "raises exception" do
expect { subject }.to raise_error(
StoreModel::Types::CastError,
"failed casting #{value}, only String, Symbol or Integer instances are allowed"
)
context "when raise_on_invalid_values is true" do
it "raises exception" do
expect { subject }.to raise_error(
ArgumentError,
"invalid value '#{value}' is assigned"
)
end
end

context "when raise_on_invalid_values is false" do
let(:raise_on_invalid_values) { false }

it { is_expected.to eq(value) }
end
end
end
end
Expand Down

0 comments on commit 1d37821

Please sign in to comment.