Skip to content

Commit

Permalink
100% coverage, better mocks, and errythin.
Browse files Browse the repository at this point in the history
  • Loading branch information
esigler committed Dec 9, 2014
1 parent 7849dc6 commit 8a79d8a
Show file tree
Hide file tree
Showing 2 changed files with 23 additions and 14 deletions.
6 changes: 4 additions & 2 deletions lib/lita/handlers/datadog.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@ def graph(response)
private

def get_response(args)
return_code, snapshot = graph_snapshot(args[:metric], args[:start],
args[:end], args[:event])
return_code, snapshot = get_graph_url(args[:metric],
args[:start],
args[:end],
args[:event])

if return_code.to_s == '200'
sleep config.waittime
Expand Down
31 changes: 19 additions & 12 deletions spec/lita/handlers/datadog_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,20 @@
EXAMPLE_IMAGE_URL = 'http://www.example.com/path/that/ends/in.png'
EXAMPLE_ERROR_MSG = 'Error requesting Datadog graph'

let(:success) do
client = double
allow(client).to receive(:graph_snapshot) {
[200, { 'snapshot_url' => EXAMPLE_IMAGE_URL }]
}
client
end

let(:error) do
client = double
allow(client).to receive(:graph_snapshot) { [500, {}] }
client
end

it do
is_expected.to route_command(
'graph metric:"system.load.1{*}"')
Expand Down Expand Up @@ -39,38 +53,31 @@

describe '#graph' do
it 'with valid metric returns an image url' do
response = { 'snapshot_url' => EXAMPLE_IMAGE_URL }
allow_any_instance_of(Lita::Handlers::Datadog).to \
receive(:graph_snapshot).with(any_args).and_return([200, response])
expect(Dogapi::Client).to receive(:new) { success }
send_command('graph metric:"system.load.1{*}"')
expect(replies.last).to eq(EXAMPLE_IMAGE_URL)
end

it 'with invalid metric returns an error' do
allow_any_instance_of(Lita::Handlers::Datadog).to \
receive(:graph_snapshot).with(any_args).and_return([500, nil])
expect(Dogapi::Client).to receive(:new) { error }
send_command('graph metric:"omg.wtf.bbq{*}"')
expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
end

it 'with valid metric and event returns an image url' do
response = { 'snapshot_url' => EXAMPLE_IMAGE_URL }
allow_any_instance_of(Lita::Handlers::Datadog).to \
receive(:graph_snapshot).with(any_args).and_return([200, response])
expect(Dogapi::Client).to receive(:new) { success }
send_command('graph metric:"system.load.1{*}"')
expect(replies.last).to eq(EXAMPLE_IMAGE_URL)
end

it 'with an invalid metric returns an error' do
allow_any_instance_of(Lita::Handlers::Datadog).to \
receive(:graph_snapshot).with(any_args).and_return([500, nil])
expect(Dogapi::Client).to receive(:new) { error }
send_command('graph metric:"omg.wtf.bbq{*}" event:"sources:sourcename"')
expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
end

it 'with an invalid event returns an error' do
allow_any_instance_of(Lita::Handlers::Datadog).to \
receive(:graph_snapshot).with(any_args).and_return([500, nil])
expect(Dogapi::Client).to receive(:new) { error }
send_command('graph metric:"system.load.1{*}" event:"omg:wtf"')
expect(replies.last).to eq(EXAMPLE_ERROR_MSG)
end
Expand Down

0 comments on commit 8a79d8a

Please sign in to comment.