Spec total_units report column

Current reports don't deal with invalid legacy data.
This commit is contained in:
Maikel Linke
2023-05-16 14:09:44 +10:00
parent 00e171a8f1
commit 71e4d2c6ed

View File

@@ -29,6 +29,81 @@ describe Reporting::Reports::OrdersAndFulfillment::OrderCycleSupplierTotals do
expect(report_table.length).to eq(1)
end
describe "total_units column" do
let(:item) { order.line_items.first }
let(:variant) { item.variant }
it "contains a sum of total items" do
variant.product.update!(variant_unit: "items", variant_unit_name: "bottle")
variant.update!(unit_value: 6) # six-pack
item.update!(final_weight_volume: nil) # reset unit information
item.update!(quantity: 3)
expect(table_headers[4]).to eq "Total Units"
expect(report_table[0][4]).to eq 18 # = 3 * 6, three six-packs
end
it "contains a sum of total weight" do
variant.product.update!(variant_unit: "weight")
variant.update!(unit_value: 200) # grams
item.update!(final_weight_volume: nil) # reset unit information
item.update!(quantity: 3)
expect(table_headers[4]).to eq "Total Units"
expect(report_table[0][4]).to eq 0.6 # kg (= 3 * 0.2kg)
end
it "is blank when line items miss a unit" do
# This is not possible with the current code but was possible years ago.
# So I'm using `update_columns` to save invalid data.
# We still have lots of that data in our databases though.
variant.product.update(variant_unit: "items", variant_unit_name: "container")
variant.update_columns(unit_value: nil, unit_description: "vacuum")
item.update!(final_weight_volume: nil) # reset unit information
expect(table_headers[4]).to eq "Total Units"
expect(report_table[0][4]).to eq " "
end
it "is summarised" do
expect(report).to receive(:display_summary_row?).and_return(true)
variant.product.update!(variant_unit: "weight")
variant.update!(unit_value: 200) # grams
item.update!(final_weight_volume: nil) # reset unit information
item.update!(quantity: 3)
# And a second item to add up with:
item2 = create(:line_item, order: order)
expect(table_headers[4]).to eq "Total Units"
expect(report_table[0][4]).to eq 0.6 # kg (= 3 * 0.2kg)
expect(report_table[1][4]).to eq 0.001 # 1 gram default value
expect(report_table[2][4]).to eq 0.601 # summary
end
pending "is blank in summary when one line item misses a unit and another not" do
expect(report).to receive(:display_summary_row?).and_return(true)
# This is not possible with the current code but was possible years ago.
# So I'm using `update_columns` to save invalid data.
# We still have lots of that data in our databases though.
variant.product.update(variant_unit: "items", variant_unit_name: "container")
variant.update_columns(unit_value: nil, unit_description: "vacuum")
item.update!(final_weight_volume: nil) # reset unit information
# This second line item will have a default a bigint value.
order.line_items << create(:line_item)
# Generating the report used to raise:
# > TypeError: no implicit conversion of BigDecimal into String
expect(table_headers[4]).to eq "Total Units"
expect(report_table[0][4]).to eq " "
expect(report_table[1][4]).to eq 0.001 # 1 gram default value
expect(report_table[2][4]).to eq " " # summary
end
end
context "with a VAT/GST-free supplier" do
before(:each) do
supplier.update(charges_sales_tax: false)