When exporting ResourceStatus as CSV, we don't want to include id, report_id,
and tags, so we override the default to_csv_properties.

We exclude id and report_id because they are completely internal fields to
Dashboard. Tags are excluded because, as an array, they don't export well to
CSV.

Paired-with: Jacob Helwig <[email protected]>
Signed-off-by: Nick Lewis <[email protected]>
---
Local-branch: ticket/next/7007
 app/models/resource_status.rb       |    4 ++++
 spec/factories.rb                   |   23 +++++++++++++++++++++++
 spec/models/resource_status_spec.rb |   21 +++++++++++++++++++++
 3 files changed, 48 insertions(+), 0 deletions(-)

diff --git a/app/models/resource_status.rb b/app/models/resource_status.rb
index 3e62ebf..7fa3b34 100644
--- a/app/models/resource_status.rb
+++ b/app/models/resource_status.rb
@@ -53,6 +53,10 @@ class ResourceStatus < ActiveRecord::Base
     }
   }
 
+  def self.to_csv_properties
+    [:resource_type, :title, :evaluation_time, :file, :line, :time, 
:change_count, :out_of_sync_count, :skipped, :failed]
+  end
+
   def name
     "#{resource_type}[#{title}]"
   end
diff --git a/spec/factories.rb b/spec/factories.rb
index 9de5e2f..b2b5fd4 100644
--- a/spec/factories.rb
+++ b/spec/factories.rb
@@ -37,12 +37,25 @@ Factory.define :inspect_report, :parent => :report do 
|inspect|
 end
 
 Factory.define :resource_status do |status|
+  status.resource_type 'File'
+  status.title { Factory.next(:filename) }
+  status.evaluation_time { rand(60)+1 }
+  status.file { Factory.next(:filename) }
+  status.line { rand(60)+1 }
+  status.time { Factory.next(:time) }
+  status.change_count 0
+  status.out_of_sync_count 0
+  status.skipped false
+  status.failed false
 end
 
 Factory.define :failed_resource, :parent => :resource_status do |status|
   status.failed true
   status.after_create do |status|
     status.events.generate!(:status => 'failed')
+    status.change_count += 1
+    status.out_of_sync_count += 1
+    status.save
   end
 end
 
@@ -50,12 +63,18 @@ Factory.define :successful_resource, :parent => 
:resource_status do |status|
   status.failed false
   status.after_create do |status|
     status.events.generate!(:status => 'success')
+    status.change_count += 1
+    status.out_of_sync_count += 1
+    status.save
   end
 end
 
 Factory.define :pending_resource, :parent => :successful_resource do |status|
+  status.failed false
   status.after_create do |status|
     status.events.generate!(:status => 'noop')
+    status.out_of_sync_count += 1
+    status.save
   end
 end
 
@@ -118,6 +137,10 @@ Factory.sequence :name do |n|
   "name_#{n}"
 end
 
+Factory.sequence :filename do |n|
+  File.join('/', *(1..3).map {Factory.next(:name)})
+end
+
 Factory.sequence :time do |n|
   # each things created will be 1 hour newer than the last
   # might be a problem if creating more than 1000 objects
diff --git a/spec/models/resource_status_spec.rb 
b/spec/models/resource_status_spec.rb
index 380372e..8491323 100644
--- a/spec/models/resource_status_spec.rb
+++ b/spec/models/resource_status_spec.rb
@@ -97,5 +97,26 @@ describe ResourceStatus do
       end
     end
   end
+
+  describe '.to_csv' do
+    before :each do
+      report = Report.generate!
+      @pending_resource = Factory(:pending_resource, :title => 'pending', 
:report => report)
+      @successful_resource = Factory(:successful_resource, :title => 
'successful', :report => report)
+      @failed_resource = Factory(:failed_resource, :title => 'failed', :report 
=> report)
+    end
+
+    it 'should use a custom list of properties to export as CSV' do
+      custom_properties = [:resource_type, :title, :evaluation_time, :file, 
:line, :time, :change_count, :out_of_sync_count, :skipped, :failed]
+
+      csv_lines = ResourceStatus.find(:all).to_csv.split("\n")
+      csv_lines.first.should == custom_properties.join(',')
+      csv_lines[1..-1].should =~ [@pending_resource, @failed_resource, 
@successful_resource].map do |res|
+        custom_properties.map do |field|
+          res.send(field)
+        end.join(',')
+      end
+    end
+  end
 end
 
-- 
1.7.5.1

-- 
You received this message because you are subscribed to the Google Groups 
"Puppet Developers" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to 
[email protected].
For more options, visit this group at 
http://groups.google.com/group/puppet-dev?hl=en.

Reply via email to