From 2b58286a035a95d1f8979b1709c8827cf385e002 Mon Sep 17 00:00:00 2001 From: Chris Griego Date: Sat, 24 Jul 2010 21:40:45 -0500 Subject: [PATCH] Rails Best Practices grapher now silently ignore dates with missing metric data --- lib/graphs/rails_best_practices_grapher.rb | 18 +++----- .../rails_best_practices_grapher_spec.rb | 45 +++++++++++++------ 2 files changed, 37 insertions(+), 26 deletions(-) diff --git a/lib/graphs/rails_best_practices_grapher.rb b/lib/graphs/rails_best_practices_grapher.rb index 5ee7afd26..6c3474f9d 100644 --- a/lib/graphs/rails_best_practices_grapher.rb +++ b/lib/graphs/rails_best_practices_grapher.rb @@ -1,25 +1,19 @@ module MetricFu - class RailsBestPracticesGrapher < Grapher - attr_accessor :rails_best_practices_count, :labels - + def initialize super @rails_best_practices_count = [] @labels = {} end - + def get_metrics(metrics, date) - if metrics[:rails_best_practices] && metrics[:rails_best_practices][:problems] - size = metrics[:rails_best_practices][:problems].size - else - size = 0 + if metrics && metrics[:rails_best_practices] + size = (metrics[:rails_best_practices][:problems] || []).size + @rails_best_practices_count.push(size) + @labels.update( { @labels.size => date }) end - @rails_best_practices_count.push(size) - @labels.update( { @labels.size => date }) end - end - end diff --git a/spec/graphs/rails_best_practices_grapher_spec.rb b/spec/graphs/rails_best_practices_grapher_spec.rb index c6f4e12ae..55a69334d 100644 --- a/spec/graphs/rails_best_practices_grapher_spec.rb +++ b/spec/graphs/rails_best_practices_grapher_spec.rb @@ -19,25 +19,42 @@ end describe "responding to #get_metrics" do - before(:each) do - @metrics = YAML::load(File.open(File.join(File.dirname(__FILE__), "..", "resources", "yml", "20090630.yml"))) - @date = "01022003" - end + context "when metrics were not generated" do + before(:each) do + @metrics = YAML::load(File.open(File.join(File.dirname(__FILE__), "..", "resources", "yml", "metric_missing.yml"))) + @date = "01022003" + end - it "should push 100 to rails_best_practices_count" do - @stats_grapher.rails_best_practices_count.should_receive(:push).with(2) - @stats_grapher.get_metrics(@metrics, @date) - end + it "should not push to rails_best_practices_count" do + @stats_grapher.rails_best_practices_count.should_not_receive(:push) + @stats_grapher.get_metrics(@metrics, @date) + end - it "should update labels with the date" do - @stats_grapher.labels.should_receive(:update).with({ 0 => "01022003" }) - @stats_grapher.get_metrics(@metrics, @date) + it "should not update labels with the date" do + @stats_grapher.labels.should_not_receive(:update) + @stats_grapher.get_metrics(@metrics, @date) + end end - context "when no metrics have been collected" do - it "should push 0 to rails_best_practices_count" do + context "when metrics have been generated" do + before(:each) do + @metrics = YAML::load(File.open(File.join(File.dirname(__FILE__), "..", "resources", "yml", "20090630.yml"))) + @date = "01022003" + end + + it "should push to rails_best_practices_count" do + @stats_grapher.rails_best_practices_count.should_receive(:push).with(2) + @stats_grapher.get_metrics(@metrics, @date) + end + + it "should push 0 to rails_best_practices_count when no problems were found" do @stats_grapher.rails_best_practices_count.should_receive(:push).with(0) - @stats_grapher.get_metrics({}, @date) + @stats_grapher.get_metrics({ :rails_best_practices => {} }, @date) + end + + it "should update labels with the date" do + @stats_grapher.labels.should_receive(:update).with({ 0 => "01022003" }) + @stats_grapher.get_metrics(@metrics, @date) end end end