mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-02-05 22:26:07 +00:00
More serializer specs
This commit is contained in:
@@ -53,8 +53,9 @@ module InjectionHelper
|
||||
|
||||
def inject_orders_by_distributor
|
||||
# Convert ActiveRecord::Relation to array for serialization
|
||||
# This query could maybe go in a model, or just serialize orders and handle the rest in JS
|
||||
data_array = Enterprise.includes(:distributed_orders).where(enterprises: {id: spree_current_user.enterprises_ordered_from }, spree_orders: {state: :complete, user_id: spree_current_user.id}).to_a
|
||||
data_array.sort!{|a,b| b.distributed_orders.length <=> a.distributed_orders.length} # Better to do in SQL/Angular?
|
||||
data_array.sort!{|a,b| b.distributed_orders.length <=> a.distributed_orders.length}
|
||||
inject_json_ams "orders_by_distributor", data_array, Api::OrdersByDistributorSerializer
|
||||
end
|
||||
|
||||
|
||||
@@ -3,6 +3,13 @@ require 'spec_helper'
|
||||
describe InjectionHelper do
|
||||
let!(:enterprise) { create(:distributor_enterprise, facebook: "roger") }
|
||||
|
||||
let!(:distributor1) { create(:distributor_enterprise) }
|
||||
let!(:distributor2) { create(:distributor_enterprise) }
|
||||
let!(:user) { create(:user)}
|
||||
let!(:d1o1) { create(:completed_order_with_totals, distributor: distributor1, user_id: user.id, total: 10000)}
|
||||
let!(:d1o2) { create(:completed_order_with_totals, distributor: distributor1, user_id: user.id, total: 5000)}
|
||||
let!(:d2o1) { create(:completed_order_with_totals, distributor: distributor2, user_id: user.id)}
|
||||
|
||||
it "will inject via AMS" do
|
||||
helper.inject_json_ams("test", [enterprise], Api::IdSerializer).should match /#{enterprise.id}/
|
||||
end
|
||||
@@ -42,8 +49,4 @@ describe InjectionHelper do
|
||||
helper.inject_taxons.should match taxon.name
|
||||
end
|
||||
|
||||
it "injects taxons" do
|
||||
taxon = create(:taxon)
|
||||
helper.inject_taxons.should match taxon.name
|
||||
end
|
||||
end
|
||||
|
||||
@@ -10,8 +10,6 @@ describe Api::OrderSerializer do
|
||||
end
|
||||
|
||||
it "converts the total to currency and amount" do
|
||||
puts order.inspect
|
||||
puts serializer.serializable_hash.inspect
|
||||
expect(serializer.serializable_hash[:total_money].keys).to include :currency_symbol
|
||||
# Not sure what currency symbol is in test env
|
||||
expect(serializer.serializable_hash[:total_money].keys).to include :amount
|
||||
|
||||
28
spec/serializers/orders_by_distributor_serializer_spec.rb
Normal file
28
spec/serializers/orders_by_distributor_serializer_spec.rb
Normal file
@@ -0,0 +1,28 @@
|
||||
#require 'spec_helper'
|
||||
|
||||
describe Api::OrdersByDistributorSerializer do
|
||||
|
||||
# Banged lets ensure entered into test database
|
||||
let!(:distributor1) { create(:distributor_enterprise) }
|
||||
let!(:distributor2) { create(:distributor_enterprise) }
|
||||
let!(:user) { create(:user)}
|
||||
let!(:d1o1) { create(:completed_order_with_totals, distributor: distributor1, user_id: user.id, total: 10000)}
|
||||
let!(:d1o2) { create(:completed_order_with_totals, distributor: distributor1, user_id: user.id, total: 5000)}
|
||||
let!(:d2o1) { create(:completed_order_with_totals, distributor: distributor2, user_id: user.id)}
|
||||
|
||||
before do
|
||||
@data = Enterprise.includes(:distributed_orders).where(enterprises: {id: user.enterprises_ordered_from }, spree_orders: {state: :complete, user_id: user.id}).to_a
|
||||
@serializer = ActiveModel::ArraySerializer.new(@data, {each_serializer: Api::OrdersByDistributorSerializer})
|
||||
end
|
||||
|
||||
it "serializes orders" do
|
||||
expect(@serializer.to_json).to match "distributed_orders"
|
||||
end
|
||||
|
||||
it "serializes the balance for each distributor" do
|
||||
expect(@serializer.serializable_array[0].keys).to include :balance
|
||||
# Would be good to test adding up balance properly but can't get a non-zero total from the factories...
|
||||
expect(@serializer.serializable_array[0][:balance]).to eq "0.00"
|
||||
end
|
||||
|
||||
end
|
||||
Reference in New Issue
Block a user