-
Notifications
You must be signed in to change notification settings - Fork 15
[WIP] Ensure references_many no nil value #322
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -22,7 +22,6 @@ def assert_valid(args = {}) | |
| # Do not override this method. Override private method cast_value | ||
| def cast(args = {}) | ||
| assert_valid(args) | ||
| return nil if args.fetch(:value).nil? | ||
|
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We remove here this responsibility to |
||
|
|
||
| cast_value(args) | ||
| end | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| # Usage: bundle exec rspec spec/deserializer/acceptance_artifact_spec.rb | ||
|
|
||
| require 'ledger_sync' | ||
| require 'rspec' | ||
|
|
||
| class Package < LedgerSync::Resource | ||
| attribute :blabla, type: LedgerSync::Type::String | ||
| end | ||
|
|
||
| class Package | ||
| class Deserializer < LedgerSync::Deserializer | ||
| attribute :blabla, hash_attribute: :blabla | ||
| end | ||
| end | ||
|
|
||
| class AcceptanceArtifact < LedgerSync::Resource | ||
| attribute :identifier, type: LedgerSync::Type::String | ||
| references_many :packages, to: Package | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What if we add a param called @jozefvaclavik do you agree?
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's exactly what I searched at the beginning, a For my first implementation, yes, it was a naive approach, I didn't take time to check the whole code base. My goal was to even avoid But still, need to think about the implementation of a |
||
| end | ||
|
|
||
| class AcceptanceArtifact | ||
| class Deserializer < LedgerSync::Deserializer | ||
| attribute :identifier, hash_attribute: :identifier | ||
| references_many :packages, deserializer: Package::Deserializer, hash_attribute: :packages | ||
| end | ||
| end | ||
|
|
||
| RSpec.describe AcceptanceArtifact::Deserializer do | ||
| describe '#deserialize' do | ||
| let(:deserializer) { described_class.new } | ||
| let(:acceptance_artifact) { AcceptanceArtifact.new } | ||
|
|
||
| subject(:deserialized_object) { deserializer.deserialize(hash: response, resource: acceptance_artifact) } | ||
|
|
||
| context "with empty `packages` in the API's response" do | ||
| let(:response) { { "identifier" => "42", "packages" => [] } } | ||
|
|
||
| it 'deserializes the identifier correctly' do | ||
| expect(deserialized_object.identifier).to eq("42") | ||
| end | ||
| end | ||
|
|
||
| context "with filled `packages` in the API's response" do | ||
| let(:response) { { "identifier" => "42", "packages" => [ { "blabla" => "ok" } ] } } | ||
|
|
||
| it 'deserializes the identifier correctly' do | ||
| expect(deserialized_object.identifier).to eq("42") | ||
| end | ||
| end | ||
|
|
||
| context "without `packages` in the API's response" do | ||
| let(:response) { { "identifier" => "42" } } | ||
|
|
||
| it 'deserializes the packages correctly' do | ||
| expect(deserialized_object.packages).to eq([]) | ||
| end | ||
| end | ||
| end | ||
| end | ||
|
|
||
| # Manual trigger | ||
| # deserializer = AcceptanceArtifact::Deserializer.new | ||
| # response_1 = { "identifier" => "42", "packages" => [] } | ||
| # response_2 = { "identifier" => "42" } | ||
| # accept_art = deserializer.deserialize(hash: response_1, resource: AcceptanceArtifact.new) | ||
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In this proposition (thanks @jozefvaclavik for your time and your research), we're being more precise/meticulous.
We're working directly on the references_many file.
Nonetheless, we also touched the
value_mixinfile (see below) which could lead to edge cases since it's a transversal file. I cannot guarantee here. I did ofc several tests with several values and types. I put some breaking points in the code execution trying to understand and check. But still.