Commit 5d2256980a40311dd3563882c68e8a28eac2a1ee

Authored by Rafael Manzo
1 parent 473eb6cc

Refactored new graphic methods

Covered with unit tests
KalibroEntites updated to rc6

Signed-off By: Fellipe Souto <fllsouto@gmail.com>
Signed-off By: Renan Fichberg <rfichberg@gmail.com>
Gemfile
... ... @@ -37,7 +37,7 @@ gem &#39;jbuilder&#39;, &#39;~&gt; 1.2&#39;
37 37 gem 'devise', '~> 3.2.0'
38 38  
39 39 # Kalibro integration
40   -gem 'kalibro_entities', "~> 0.0.1.rc5"
  40 +gem 'kalibro_entities', "~> 0.0.1.rc6"
41 41  
42 42 # PostgreSQL integration
43 43 gem "pg", "~> 0.17.0"
... ...
Gemfile.lock
... ... @@ -103,8 +103,8 @@ GEM
103 103 railties (>= 3.0, < 5.0)
104 104 thor (>= 0.14, < 2.0)
105 105 json (1.8.1)
106   - kalibro_entities (0.0.1.rc5)
107   - activesupport (~> 4.0.0)
  106 + kalibro_entities (0.0.1.rc6)
  107 + activesupport (~> 4.0.1)
108 108 savon-ng-1.6 (~> 2.4.1)
109 109 konacha (3.0.0)
110 110 actionpack (>= 3.1, < 5)
... ... @@ -259,7 +259,7 @@ DEPENDENCIES
259 259 factory_girl_rails (~> 4.3.0)
260 260 jbuilder (~> 1.2)
261 261 jquery-rails
262   - kalibro_entities (~> 0.0.1.rc5)
  262 + kalibro_entities (~> 0.0.1.rc6)
263 263 konacha (~> 3.0.0)
264 264 mocha
265 265 modernizr-rails
... ...
app/models/module_result.rb
... ... @@ -5,24 +5,22 @@ class ModuleResult &lt; KalibroEntities::Entities::ModuleResult
5 5 KalibroEntities::Entities::MetricResult.metric_results_of(@id)
6 6 end
7 7  
8   - def history_of()
9   - KalibroEntities::Entities::ModuleResult.history_of(@id).map { |date_module_result| DateModuleResult.new date_module_result.to_hash }
  8 + def history
  9 + self.class.history_of(@id).map { |date_module_result| DateModuleResult.new date_module_result.to_hash }
10 10 end
11 11  
12   - def history_of_grades_of(metric_name)
13   - history_of_grades = Hash.new
  12 + def metric_history(name)
  13 + grade_history = Hash.new
14 14  
15   - date_module_result_list = history_of(@id)
16   - date_module_result_list.each do |date_module_result|
17   - date_module_result.module_result.metric_results.each do |metric_result|
18   - if metric_result.metric_configuration_snapshot.metric.name == metric_name then
19   - metric_grade = metric_result.metric_configuration_snapshot.grade
20   - break
21   - end
22   - end
23   - history_of_grades[date_module_result.date] = metric_grade
24   - end
25   - history_of_grades
  15 + history.each { |date_module_result| grade_history[date_module_result.date] =
  16 + find_grade_by_metric_name(date_module_result.module_result.metric_results, name) }
  17 +
  18 + grade_history
26 19 end
27 20  
  21 + private
  22 +
  23 + def find_grade_by_metric_name(metric_results, name)
  24 + metric_results.each { |metric_result| return metric_result.value if metric_result.metric_configuration_snapshot.metric.name == name }
  25 + end
28 26 end
29 27 \ No newline at end of file
... ...
spec/factories/date_module_results.rb 0 → 100644
... ... @@ -0,0 +1,6 @@
  1 +FactoryGirl.define do
  2 + factory :date_module_result, class: DateModuleResult do
  3 + date "2011-10-20T18:26:43.151+00:00"
  4 + module_result {FactoryGirl.build(:module_result)}
  5 + end
  6 +end
0 7 \ No newline at end of file
... ...
spec/factories/module_results.rb
... ... @@ -7,7 +7,7 @@ FactoryGirl.define do
7 7 height "6"
8 8 end
9 9  
10   - factory :root_module_result, class: ModuleResult do
  10 + factory :root_module_result, class: ModuleResult do
11 11 id "21"
12 12 self.module { FactoryGirl.build(:module) }
13 13 grade "6.0"
... ...
spec/models/module_result_spec.rb
... ... @@ -2,14 +2,38 @@ require &#39;spec_helper&#39;
2 2  
3 3 describe ModuleResult do
4 4 describe 'methods' do
  5 + subject { FactoryGirl.build(:module_result) }
  6 +
5 7 describe 'metric_results' do
6   - subject { FactoryGirl.build(:module_result) }
7   -
8 8 it 'should call the metric_results_of method' do
9 9 KalibroEntities::Entities::MetricResult.expects(:metric_results_of).with(subject.id).returns(nil)
10 10  
11 11 subject.metric_results
12 12 end
13 13 end
  14 +
  15 + describe 'history' do
  16 + before :each do
  17 + ModuleResult.expects(:history_of).with(subject.id).returns([FactoryGirl.build(:date_module_result)])
  18 + end
  19 +
  20 + it 'should return a array of DateModuleResults' do
  21 + subject.history.first.should be_a(DateModuleResult)
  22 + end
  23 + end
  24 +
  25 + describe 'metric_history' do
  26 + let(:date_module_result) {FactoryGirl.build(:date_module_result)}
  27 + let(:metric_result) {FactoryGirl.build(:metric_result)}
  28 +
  29 + before :each do
  30 + subject.expects(:history).returns([date_module_result])
  31 + ModuleResult.any_instance.expects(:metric_results).returns([metric_result])
  32 + end
  33 +
  34 + it 'should return the history for the given metric name' do
  35 + subject.metric_history(metric_result.metric_configuration_snapshot.metric.name).should eq({date_module_result.date => metric_result.value})
  36 + end
  37 + end
14 38 end
15 39 end
16 40 \ No newline at end of file
... ...