Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

General | Fix comparison operator for ioki models #155

Merged
merged 2 commits into from
Nov 22, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions lib/ioki/model/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ def serialize(usecase = :read)
end

def ==(other)
return false unless other.respond_to?(:attributes)

attributes == other.attributes
end

Expand Down
47 changes: 47 additions & 0 deletions spec/ioki/model/base_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -685,4 +685,51 @@
expect(Ioki::Model::Platform::Pause.new.send(:constantize_in_module, 'Place')).to eq Ioki::Model::Platform::Place
end
end

describe '#==' do
context 'when a model is compared with nil' do
it 'returns false' do
expect(model).not_to be_nil
end
end

context 'when a model is compared with a String' do
it 'returns false' do
expect(model == 'some_string').to be_falsy
end
end

context 'when a model is compared with another Ioki::Model' do
let(:other_example_class) do
Class.new(Ioki::Model::Base) do
attribute :baz, on: :read
end
end

let(:other_model) { other_example_class.new }

it 'returns false' do
expect(model == other_model).to be_falsy
end
end

context 'when a model is compared with the same Ioki::Model' do
context 'with different attributes' do
let(:same_model) { example_class.new(other_attributes) }
let(:other_attributes) { { bar: 'baz' } }

it 'returns false' do
expect(model == same_model).to be_falsy
end
end

context 'with the same attributes' do
let(:same_model) { example_class.new(attributes) }

it 'returns true' do
expect(model == same_model).to be_truthy
end
end
end
end
end