Skip to content

Commit

Permalink
Merge pull request ManageIQ#312 from himdel/report-formatter-spec
Browse files Browse the repository at this point in the history
Move report_formatter specs from manageiq
  • Loading branch information
martinpovolny authored Feb 7, 2017
2 parents 3c07b88 + 0bd7d98 commit 678e0ca
Show file tree
Hide file tree
Showing 4 changed files with 419 additions and 0 deletions.
95 changes: 95 additions & 0 deletions spec/lib/report_formater/c3_formatter_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
describe ReportFormatter::C3Formatter do
include Spec::Support::ReportHelper

before(:each) do
allow(Charting).to receive(:backend).and_return(:c3)
allow(Charting).to receive(:format).and_return(:c3)
end

describe "#add_series" do
it "does not raise error for 'stack' chart" do
report = numeric_chart_3d(true)
expect { render_report(report) }.to_not raise_error
end
end

context '#build_numeric_chart_grouped' do
[true, false].each do |other|
it "builds 2d numeric charts from summaries #{other ? 'with' : 'without'} 'other'" do
report = numeric_charts_2d_from_summaries(other)

expect_any_instance_of(described_class).to receive(:build_numeric_chart_grouped).once.and_call_original
render_report(report)
expect(report.chart[:data][:columns][0][1]).to eq(4.0)
expect(report.chart[:data][:columns][0][-1]).to eq(4) if other
end
end
end

context '#build_numeric_chart_simple' do
let(:report) { numeric_chart_simple }
let(:long_report) { numeric_chart_simple_with_long_strings }

it "report chart have right data in ascending order" do
report.col_formats = [nil, :general_number_precision_0]
render_report(report)
expect(report.chart[:data][:columns][0].count).to eq(report.table.data.count + 1)
expect(report.chart[:data][:columns][0][1]).to eq(2024)
end

it "handles null data in chart column" do
report = null_data_chart

expect_any_instance_of(described_class).to receive(:build_numeric_chart_simple).once.and_call_original
render_report(report)
end

it "handle long strings" do
render_report(long_report)
expect(long_report.chart[:miq][:category_table][2]).to eq(long_category)
expect(long_report.chart[:miq][:name_table]['1']).to eq('RAM Size (MB)')
end
end

context '#build_numeric_chart_simple' do
[true, false].each do |other|
it "builds 2d numeric charts #{other ? 'with' : 'without'} 'other'" do
report = numeric_chart_simple2(other)

expect_any_instance_of(described_class).to receive(:build_numeric_chart_simple).once.and_call_original
render_report(report)
expect(report.chart[:data][:columns][0][1]).to eq(15)
expect(report.chart[:data][:columns][0][-1]).to eq(1) if other
end
end

it "handles null data in chart column" do
report = null_data_chart

expect_any_instance_of(described_class).to receive(:build_numeric_chart_simple).once.and_call_original
render_report(report)
end
end

context '#build_numeric_chart_grouped_2dim' do
[true, false].each do |other|
it "builds 3d numeric charts #{other ? 'with' : 'without'} 'other'" do
report = numeric_chart_3d(other)

expect_any_instance_of(described_class).to receive(:build_numeric_chart_grouped_2dim).once.and_call_original
render_report(report)
expect(report.chart[:data][:columns][0][1]).to eq(6_656)
expect(report.chart[:data][:columns][0][2]).to eq(4_096)
expect(report.chart[:data][:columns][1][1]).to eq(1_024)
expect(report.chart[:data][:columns][0][-1]).to eq(1_024) if other
end
end

it 'handles namespace-prefixed class names in chart column' do
report = chart_with_namespace_prefix

expect_any_instance_of(described_class).to receive(:build_numeric_chart_grouped_2dim).once.and_call_original
render_report(report)
end
end
end
54 changes: 54 additions & 0 deletions spec/lib/report_formater/chart_common_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
# describe ReportFormatter::ChartCommon do
#
# We have to operate on the specific class although we are testing the common behavior.
# Otherwise expect_any_instance_of(described_class).to receive(:build_performance_chart_area).once.and_call_original
# leads to with:
# SystemStackError:
# stack level too deep
# # ./lib/report_formatter/chart_common.rb:555:in `build_performance_chart'
# # ./lib/report_formatter/chart_common.rb:57:in `call'

describe ReportFormatter::C3Formatter do
include Spec::Support::ReportHelper

before(:each) do
allow(Charting).to receive(:backend).and_return(:c3)
allow(Charting).to receive(:format).and_return(:c3)
end
context '#build_performance_chart_area' do
it "builds a daily chart with all nils" do
report = MiqReport.new(
:db => "VimPerformanceDaily",
:cols => cols = %w(timestamp cpu_usagemhz_rate_average min_cpu_usagemhz_rate_average max_cpu_usagemhz_rate_average trend_max_cpu_usagemhz_rate_average resource.cpu_usagemhz_rate_average_high_over_time_period resource.cpu_usagemhz_rate_average_low_over_time_period),
:include => {
"resource" => {
"columns" => %w(cpu_usagemhz_rate_average_high_over_time_period cpu_usagemhz_rate_average_low_over_time_period derived_memory_used_high_over_time_period derived_memory_used_low_over_time_period),
}
},
:col_order => cols,
:headers => ["Date/Time", "Avg Used", "Max Available", "Max Reserved", "Trend Max Used", "foo", "bar"],
:order => "ascending",
:sortby => "timestamp",
:group => "n",
:graph => {
:type => "Line",
:columns => %w(cpu_usagemhz_rate_average min_cpu_usagemhz_rate_average max_cpu_usagemhz_rate_average trend_max_cpu_usagemhz_rate_average resource.cpu_usagemhz_rate_average_high_over_time_period resource.cpu_usagemhz_rate_average_low_over_time_period),
:legends => nil,
:max_col_size => nil
},
:dims => nil,
:col_formats => nil,
:col_options => nil,
:rpt_options => nil,
)

report.table = Ruport::Data::Table.new(
:column_names => %w(timestamp cpu_usagemhz_rate_average min_cpu_usagemhz_rate_average max_cpu_usagemhz_rate_average trend_max_cpu_usagemhz_rate_average),
:data => [["Sun, 20 Mar 2016 00:00:00 UTC +00:00", 0.0, nil, nil, 0]])

expect_any_instance_of(described_class).to receive(:build_performance_chart_area).once.and_call_original
render_report(report) { |e| e.options.graph_options[:chart_type] = :performance }
expect(report.chart[:data]).to be
end
end
end
77 changes: 77 additions & 0 deletions spec/lib/report_formater/jqplot_formater_charting_counts_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
describe ReportFormatter::JqplotFormatter do
include Spec::Support::ReportHelper

before(:each) do
allow(Charting).to receive(:backend).and_return(:jqplot)
allow(Charting).to receive(:format).and_return(:jqplot)
end
context '#build_reporting_chart_dim2' do
it 'builds a stacked chart' do
report = MiqReport.new(
:db => "Vm",
:cols => %w(os_image_name),
:include => {"ext_management_system" => {"columns" => ["name"]}},
:col_order => ["ext_management_system.name", "os_image_name"],
:headers => ["Cloud/Infrastructure Provider Name", "OS Name"],
:order => "Ascending",
:group => nil,
:graph => {:type => "StackedBar", :mode => "counts", :column => nil, :count => 10, :other => false},
:dims => 2,
:col_options => {},
:rpt_options => {},
:sortby => %w(ext_management_system.name os_image_name)
)

report.table = Ruport::Data::Table.new(
:column_names => %w(os_image_name ext_management_system.name id),
:data => [
%w(linux_centos MTC-RHEVM-3.0 10000000000012),
%w(linux_centos MTC-RHEVM-3.0 10000000000013),
%w(linux_redhat MTC-RHEVM-3.1 10000000000014),
%w(linux_centos MTC-RHEVM-3.1 10000000000015),
]
)

expect_any_instance_of(described_class).to receive(:build_reporting_chart_dim2).once.and_call_original
render_report(report)
expect(report.chart[:data]).to eq([[2, 1], [0, 1]])
expect(report.chart[:options][:seriesDefaults][:renderer]).to eq("jQuery.jqplot.BarRenderer")
expect(report.chart[:options][:series]).to eq([{:label => "linux_centos"}, {:label => "linux_redhat"}])
end
end

context "#build_reporting_chart_other" do
it 'builds a pie chart' do
report = MiqReport.new(
:db => "Host",
:cols => %w(os_image_name),
:include => {},
:col_order => ["os_image_name"],
:headers => ["OS Name"],
:order => "Ascending",
:sortby => ["os_image_name"],
:group => nil,
:graph => {:type => "Pie", :mode => "counts", :column => nil, :count => 10, :other => true},
:dims => 1,
:col_options => {},
:rpt_options => {},
)

report.table = Ruport::Data::Table.new(
:column_names => %w(os_image_name id),
:data => [
["linux_esx", 5],
["linux_esx", 6],
["linux_esx", 7],
["widloze", 8],
]
)

expect_any_instance_of(described_class).to receive(:build_reporting_chart_other).once.and_call_original
render_report(report)
expect(report.chart[:data][0]).to eq([["linux_esx: 3", 3], ["widloze: 1", 1]])
expect(report.chart[:options][:seriesDefaults][:renderer]).to eq("jQuery.jqplot.PieRenderer")
expect(report.chart[:options][:highlighter]).to be_truthy
end
end
end
Loading

0 comments on commit 678e0ca

Please sign in to comment.