More serializer specs

This commit is contained in:
stveep
2015-12-22 15:09:24 +00:00
parent 668c6ff74f
commit 48896ab3d8
4 changed files with 37 additions and 7 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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

View 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