mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-25 20:46:48 +00:00
Before:
```
$ bundle exec rspec -e ".render_as"
(...)
Run options: include {:full_description=>/\.render_as/}
WARNING: Using the `raise_error` matcher without providing a specific error or message risks false positives, since `raise_error` will match when Ruby raises a `NoMethodError`, `NameError` or `ArgumentError`, potentially allowing the expectation to pass without even executing the method you are intending to call. Actual error raised was #<ActionController::BadRequest: report_format should be in [:csv, :json, :html, :xlsx, :pdf]>. Instead consider providing a specific error class or message. This message can be suppressed by setting: `RSpec::Expectations.configuration.on_potential_false_positives = :nothing`. Called from /path/to/spec/lib/reports/report_renderer_spec.rb:34:in `block (3 levels) in <main>'.
.
Finished in 0.02544 seconds (files took 4.08 seconds to load)
1 example, 0 failures
```
After this patch:
```
$ bundle exec rspec -e ".render_as"
(...)
Run options: include {:full_description=>/\.render_as/}
.
Finished in 0.02488 seconds (files took 4.09 seconds to load)
1 example, 0 failures
```
89 lines
2.4 KiB
Ruby
89 lines
2.4 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
require 'spec_helper'
|
|
|
|
RSpec.describe Reporting::ReportRenderer do
|
|
let(:data) {
|
|
[
|
|
{ "id" => 1, "name" => "carrots", "quantity" => 3 },
|
|
{ "id" => 2, "name" => "onions", "quantity" => 6 }
|
|
]
|
|
}
|
|
let(:report) {
|
|
OpenStruct.new(
|
|
columns: {
|
|
id: proc { |row| row["id"] },
|
|
name: proc { |row| row["name"] },
|
|
quantity: proc { |row| row["quantity"] },
|
|
},
|
|
rows: data,
|
|
table_headers: data.first.keys,
|
|
table_rows: data.map(&:values)
|
|
)
|
|
}
|
|
let(:subject) { described_class.new(report) }
|
|
|
|
describe ".as_json" do
|
|
it "returns the report's data as hashes" do
|
|
expect(subject.as_json).to eq data.as_json
|
|
end
|
|
end
|
|
|
|
describe ".render_as" do
|
|
it "raise an error if format is not supported" do
|
|
expect {
|
|
subject.render_as("give_me_everything")
|
|
}.to raise_error(ActionController::BadRequest)
|
|
end
|
|
end
|
|
|
|
# metadata headers
|
|
|
|
describe '#metadata_headers' do
|
|
let(:user) { create(:user) }
|
|
let(:from_key) { Reporting::ReportMetadataBuilder::DATE_FROM_KEYS.first }
|
|
let(:to_key) { Reporting::ReportMetadataBuilder::DATE_TO_KEYS.first }
|
|
|
|
let(:meta_report) do
|
|
double(
|
|
'MetaReport',
|
|
rows: data,
|
|
params: {
|
|
display_metadata_rows: true,
|
|
report_type: :order_cycle_customer_totals,
|
|
report_subtype: 'by_distributor',
|
|
report_format: 'csv'
|
|
},
|
|
ransack_params: {
|
|
from_key => '2025-01-01',
|
|
to_key => '2025-01-31'
|
|
},
|
|
user:,
|
|
table_headers: nil
|
|
)
|
|
end
|
|
|
|
let(:renderer) { described_class.new(meta_report) }
|
|
|
|
it 'appends empty base headers when report.table_headers is nil
|
|
and metadata rows are enabled' do
|
|
expect(renderer.table_headers.last).to eq []
|
|
end
|
|
|
|
it 'builds rows via ReportMetadataBuilder when display_metadata_rows?
|
|
is true and report_format is csv' do
|
|
rows = renderer.metadata_headers
|
|
|
|
labels = rows.map(&:first)
|
|
expect(labels).to include('Report Title')
|
|
expect(labels).to include('Date Range')
|
|
expect(labels).to include('Printed')
|
|
|
|
values = rows.map(&:second)
|
|
expect(values).to include('Order Cycle Customer Totals - By Distributor')
|
|
expect(values).to include('2025-01-01 - 2025-01-31')
|
|
expect(values).to include(Time.now.utc.strftime('%F %T %Z'))
|
|
end
|
|
end
|
|
end
|