require 'spec_helper'

describe Supplier::Counters do
  describe '.reset_counters!' do
    it 'sets the counters to the right values for all suppliers' do
      supplier1 = create :supplier
      supplier2 = create :supplier
      create_list :order, 2, :placed, supplier: supplier1
      create_list :order, 7, :active, supplier: supplier1
      create_list :order, 3, :placed, supplier: supplier2
      Supplier.reset_counters!
      supplier1.orders_placed_count.should == 2
      supplier1.orders_in_process_count.should == 7
      supplier2.orders_placed_count.should == 3
    end

    # it 'cleans counter values if orders are no longer available', broken: defined?($cb) do
    #   old_connection = Qwaiter::Counter.connection
    #   # this spec should run on the couchbase database
    #   Qwaiter::Counter.connection = $cb
    #   supplier = create :supplier
    #   Qwaiter::Counter.set "supplier_counter:#{supplier.id}:orders_placed", 9
    #   supplier.orders_placed_count.should == 9
    #   Supplier.reset_counters!
    #   sleep 1
    #   supplier.orders_placed_count.should == 0
    #   Qwaiter::Counter.connection = old_connection
    # end
    #
    it 'sets the counters to the right values for one supplier' do
      supplier = create :supplier
      create_list :order, 5, :placed, supplier: supplier
      create_list :order, 9, :active, supplier: supplier
      supplier.reset_counters!
      supplier.orders_placed_count.should == 5
      supplier.orders_in_process_count.should == 9
    end
  end

end
