Adding new specs and a couple updates the lib/report

This commit is contained in:
Lynne Davis
2014-12-11 12:47:56 +00:00
parent f878e18037
commit 079781576b
2 changed files with 130 additions and 3 deletions

View File

@@ -36,7 +36,7 @@ module OpenFoodNetwork
end
def filter_to_payment_method (orders)
if params[:payment_method_name].to_i > 0
if params[:payment_method_name].present?
orders.with_payment_method_name(params[:payment_method_name])
else
orders
@@ -44,7 +44,7 @@ module OpenFoodNetwork
end
def filter_to_distribution (orders)
if params[:distribution_name].to_i > 0
if params[:distribution_name].present?
orders.joins(:shipping_method).where("spree_shipping_methods.name = ?", params[:distribution_name])
else
orders
@@ -52,7 +52,7 @@ module OpenFoodNetwork
end
def filter_to_order_cycle(orders)
if params[:order_cycle_id].to_i > 0
if params[:order_cycle_id].present?
orders.where(order_cycle_id: params[:order_cycle_id])
else
orders

View File

@@ -0,0 +1,127 @@
require 'spec_helper'
module OpenFoodNetwork
describe OrderCycleManagementReport do
context "as a site admin" do
let(:user) do
user = create(:user)
user.spree_roles << Spree::Role.find_or_create_by_name!("admin")
user
end
subject { OrderCycleManagementReport.new user }
describe "fetching orders" do
it "fetches completed orders" do
o1 = create(:order)
o2 = create(:order, completed_at: 1.day.ago)
subject.orders.should == [o2]
end
it "does not show cancelled orders" do
o1 = create(:order, state: "canceled", completed_at: 1.day.ago)
o2 = create(:order, completed_at: 1.day.ago)
subject.orders.should == [o2]
end
end
end
context "as an enterprise user" do
let(:user) do
user = create(:user)
user.spree_roles = []
user.save!
user
end
subject { OrderCycleManagementReport.new user }
describe "fetching orders" do
let(:supplier) { create(:supplier_enterprise) }
let(:product) { create(:simple_product, supplier: supplier) }
let(:order) { create(:order, completed_at: 1.day.ago) }
it "only shows orders managed by the current user" do
d1 = create(:distributor_enterprise)
d1.enterprise_roles.build(user: user).save
d2 = create(:distributor_enterprise)
d2.enterprise_roles.build(user: create(:user)).save
o1 = create(:order, distributor: d1, completed_at: 1.day.ago)
o2 = create(:order, distributor: d2, completed_at: 1.day.ago)
subject.should_receive(:filter).with([o1]).and_return([o1])
subject.orders.should == [o1]
end
it "does not show orders through a hub that the current user does not manage" do
# Given a supplier enterprise with an order for one of its products
supplier.enterprise_roles.build(user: user).save
order.line_items << create(:line_item, product: product)
# When I fetch orders, I should see no orders
subject.should_receive(:filter).with([]).and_return([])
subject.orders.should == []
end
end
describe "filtering orders" do
let(:orders) { Spree::Order.scoped }
let(:supplier) { create(:supplier_enterprise) }
it "returns all orders sans-params" do
subject.filter(orders).should == orders
end
it "filters to a specific order cycle" do
oc1 = create(:simple_order_cycle)
oc2 = create(:simple_order_cycle)
order1 = create(:order, order_cycle: oc1)
order2 = create(:order, order_cycle: oc2)
subject.stub(:params).and_return(order_cycle_id: oc1.id)
subject.filter(orders).should == [order1]
end
it "filters to a payment method" do
pm1 = create(:payment_method, name: "PM1")
pm2 = create(:payment_method, name: "PM2")
order1 = create(:order)
order2 = create(:order)
payment1 = create(:payment, :order => order1, :payment_method => pm1)
payment2 = create(:payment, :order => order2, :payment_method => pm2)
subject.stub(:params).and_return(payment_method_name: pm1.name)
subject.filter(orders).should == [order1]
end
it "filters to a shipping method" do
sm1 = create(:shipping_method, name: "ship1")
sm2 = create(:shipping_method, name: "ship2")
order1 = create(:order, shipping_method: sm1)
order2 = create(:order, shipping_method: sm2)
subject.stub(:params).and_return(distribution_name: sm1.name)
subject.filter(orders).should == [order1]
end
it "should do all the filters at once" do
pm1 = create(:payment_method, name: "PM1")
sm1 = create(:shipping_method, name: "ship1")
oc1 = create(:simple_order_cycle)
order1 = create(:order, order_cycle: oc1,shipping_method: sm1)
payment1 = create(:payment, :order => order1, :payment_method => pm1)
subject.stub(:params).and_return(
order_cycle_id: oc1.id,
distribution_name: sm1.name,
payment_method_name: pm1.name)
subject.filter(orders)
end
end
end
end
end