Skip to content

Commit

Permalink
Merge pull request #101 from mezuro/fix_find_by_methods_fail_behaviour
Browse files Browse the repository at this point in the history
Fix find by methods fail behaviour
  • Loading branch information
diegoamc committed Mar 18, 2016
2 parents ad6f49c + e1a7669 commit 9a20ac9
Show file tree
Hide file tree
Showing 4 changed files with 100 additions and 16 deletions.
16 changes: 12 additions & 4 deletions features/metric_collector_details/find_by_name.feature
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,18 @@ Feature: Find By Name
As a developer
I want to get a metric collector by name

Scenario: get a metric collector by name!
When I search metric collector Analizo by name!
Then I should get "Analizo" metric collector

Scenario: get a metric collector by inexistent name!
When I search metric collector Avalio by name!
Then I should get an error

Scenario: get a metric collector by name
When I search metric collector Analizo by name
Then I should get Analizo metric collector
When I search metric collector "Analizo" by name
Then I should get "Analizo" metric collector

Scenario: get a metric collector by inexistent name
When I search metric collector Avalio by name
Then I should get an error
When I search metric collector "Avalio" by name
Then I should get nil
16 changes: 10 additions & 6 deletions features/step_definitions/metric_collector_details_steps.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,25 +2,29 @@
@metric_collector_names = KalibroClient::Entities::Processor::MetricCollectorDetails.all_names
end

When(/^I search metric collector Analizo by name$/) do
@result = KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name("Analizo")
When(/^I search metric collector Analizo by name\!$/) do
@response = KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name!("Analizo")
end

When(/^I search metric collector Avalio by name$/) do
When(/^I search metric collector Avalio by name\!$/) do
@is_error = false
begin
KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name("Avalio")
KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name!("Avalio")
rescue KalibroClient::Errors::RecordNotFound
@is_error = true
end
end

When(/^I search metric collector "(.+)" by name$/) do |name|
@response = KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(name)
end

Then(/^it should return Analizo string inside of an array$/) do
expect(@metric_collector_names.include?("Analizo")).to be_truthy
end

Then(/^I should get Analizo metric collector$/) do
expect(@result.name).to eq("Analizo")
Then(/^I should get "(.+)" metric collector$/) do |name|
expect(@response.name).to eq(name)
end

Then(/^I should get an error$/) do
Expand Down
22 changes: 19 additions & 3 deletions lib/kalibro_client/entities/processor/metric_collector_details.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,18 +36,34 @@ def find_metric_by_name(name)
metric.nil? ? nil : metric.last
end

def find_metric_by_name!(name)
metric = self.find_metric_by_name(name)
raise KalibroClient::Errors::RecordNotFound if metric.nil?
metric
end

def find_metric_by_code(metric_code)
@supported_metrics[metric_code]
end

def find_metric_by_code!(metric_code)
metric = self.find_metric_by_code(metric_code)
raise KalibroClient::Errors::RecordNotFound if metric.nil?
metric
end

def self.find_by_name(metric_collector_name)
begin
new request(:find, {name: metric_collector_name})["metric_collector_details"]
rescue
raise KalibroClient::Errors::RecordNotFound
self.find_by_name!(metric_collector_name)
rescue KalibroClient::Errors::RecordNotFound
nil
end
end

def self.find_by_name!(metric_collector_name)
new request(:find, {name: metric_collector_name})["metric_collector_details"]
end

def self.all_names
request(:names, {}, :get)['metric_collector_names'].to_a
end
Expand Down
62 changes: 59 additions & 3 deletions spec/entities/processor/metric_collector_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,16 +90,46 @@
describe 'find_by_name' do
subject { FactoryGirl.build(:metric_collector_details) }

context 'with an inexistent name' do
before :each do
KalibroClient::Entities::Processor::MetricCollectorDetails.
expects(:find_by_name!).
with(subject.name).
raises(KalibroClient::Errors::RecordNotFound)
end

it 'is expected to return nil' do
expect(KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(subject.name)).to be_nil
end
end

context 'with an existent name' do
before :each do
KalibroClient::Entities::Processor::MetricCollectorDetails.
expects(:find_by_name!).
with(subject.name).
returns(subject)
end

it 'should return a metric_collector_details' do
expect(KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(subject.name).name).to eq(subject.name)
end
end
end

describe 'find_by_name!' do
subject { FactoryGirl.build(:metric_collector_details) }

context 'with an inexistent name' do
before :each do
KalibroClient::Entities::Processor::MetricCollectorDetails.
expects(:request).
with(:find, {name: subject.name}).
returns(nil)
raises(KalibroClient::Errors::RecordNotFound)
end

it 'should raise a RecordNotFound error' do
expect { KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name(subject.name)}.
expect { KalibroClient::Entities::Processor::MetricCollectorDetails.find_by_name!(subject.name)}.
to raise_error(KalibroClient::Errors::RecordNotFound)
end
end
Expand All @@ -108,7 +138,7 @@
before :each do
KalibroClient::Entities::Processor::MetricCollectorDetails.
expects(:request).
with(:find,{name: subject.name}).
with(:find, {name: subject.name}).
returns({"metric_collector_details" => subject.to_hash({except: ["supported_metrics"]})})
end

Expand Down Expand Up @@ -146,6 +176,19 @@
end
end

describe 'find_metric_by_name!' do
subject { FactoryGirl.build(:metric_collector_details) }
let(:metric) { subject.supported_metrics["loc"] }

it 'should return nil with an inexistent name' do
expect { subject.find_metric_by_name!("fake name") }.to raise_error(KalibroClient::Errors::RecordNotFound)
end

it 'should return a metric with an existent name' do
expect(subject.find_metric_by_name!(metric.name)).to eq(metric)
end
end

describe 'find_metric_by_code' do
subject { FactoryGirl.build(:metric_collector_details) }
let(:metric) { subject.supported_metrics["loc"] }
Expand All @@ -158,4 +201,17 @@
expect(subject.find_metric_by_code(metric.code)).to eq(metric)
end
end

describe 'find_metric_by_code!' do
subject { FactoryGirl.build(:metric_collector_details) }
let(:metric) { subject.supported_metrics["loc"] }

it 'should return nil with an inexistent code' do
expect{subject.find_metric_by_code!("fake code")}.to raise_error(KalibroClient::Errors::RecordNotFound)
end

it 'should return a metric with an existent code' do
expect(subject.find_metric_by_code!(metric.code)).to eq(metric)
end
end
end

0 comments on commit 9a20ac9

Please sign in to comment.