From 48896ab3d88cbca6d0c147d113c0523f024aa0da Mon Sep 17 00:00:00 2001 From: stveep Date: Tue, 22 Dec 2015 15:09:24 +0000 Subject: [PATCH] More serializer specs --- app/helpers/injection_helper.rb | 3 +- spec/helpers/injection_helper_spec.rb | 11 +++++--- spec/serializers/order_serializer_spec.rb | 2 -- .../orders_by_distributor_serializer_spec.rb | 28 +++++++++++++++++++ 4 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 spec/serializers/orders_by_distributor_serializer_spec.rb diff --git a/app/helpers/injection_helper.rb b/app/helpers/injection_helper.rb index 28d896e6a0..34c991d55d 100644 --- a/app/helpers/injection_helper.rb +++ b/app/helpers/injection_helper.rb @@ -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 diff --git a/spec/helpers/injection_helper_spec.rb b/spec/helpers/injection_helper_spec.rb index 362ca6479b..0eecdeeaf9 100644 --- a/spec/helpers/injection_helper_spec.rb +++ b/spec/helpers/injection_helper_spec.rb @@ -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 diff --git a/spec/serializers/order_serializer_spec.rb b/spec/serializers/order_serializer_spec.rb index 7b71788f17..d3d8c98dd1 100644 --- a/spec/serializers/order_serializer_spec.rb +++ b/spec/serializers/order_serializer_spec.rb @@ -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 diff --git a/spec/serializers/orders_by_distributor_serializer_spec.rb b/spec/serializers/orders_by_distributor_serializer_spec.rb new file mode 100644 index 0000000000..4a75f76fea --- /dev/null +++ b/spec/serializers/orders_by_distributor_serializer_spec.rb @@ -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