<commit_before>require_relative '../../spec_helper'
require_relative "../../helpers/faraday_stub"

describe Bvr::Credit do
  describe '.add(customer_id, amount)' do
    include FaradayStub
    let(:customer_id) { 'foo' }
    let(:amount)   { 10    }
    let(:options) do
      {
        command: Bvr::Credit::API_COMMANDS[:add],
        customer: customer_id,
        amount: amount
      }
    end
    let(:response) do
      File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
    end

    subject { Bvr::Credit.add(customer_id, amount) }

    before do
      faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
    end

    it 'returns a result' do
      subject['Result'].must_be_instance_of String
    end
  end


  describe '#add(amount)' do
    include FaradayStub
    let(:raw_specific_balance) { 10.12345 }
    let(:raw_balance) { 10.12 }
    let(:amount) { 1 }
    let(:customer) { Bvr::Customer.new }
    let(:credit)   { Bvr::Credit.new(raw_specific_balance, raw_balance, customer) }
    let(:customer_id) { 'foo' }
    let(:options) do
      {
        command: Bvr::Credit::API_COMMANDS[:add],
        customer: customer_id,
        amount: amount
      }
    end
    let(:response) do
      File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
    end

    subject { credit.add(amount) }

    before do
      faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
    end

    it 'increase the amout' do
      customer.stub(:id, customer_id) do
        credit.specific_balance.must_equal raw_specific_balance
        credit.balance.must_equal raw_balance

        subject.must_equal true

        credit.specific_balance.must_equal raw_specific_balance + amount
        credit.balance.must_equal raw_balance + amount
      end
    end

    describe 'when the amount if more than .5f' do
      let(:amount) { 1.000001 }

      it 'round up' do
        customer.id = customer_id

        credit.specific_balance.must_equal raw_specific_balance
        credit.balance.must_equal raw_balance

        subject.must_equal true

        credit.specific_balance.must_equal raw_specific_balance + Float("%.5f" % amount)
        credit.balance.must_equal          raw_balance          + Float("%.2f" % amount)
      end
    end

    describe 'when there is an error' do
      let(:response) do
        File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction_failed.xml'))
      end

      it 'does not change the amout and return false' do
        customer.stub(:id, customer_id) do
          credit.raw_specific_balance.must_equal raw_specific_balance
          credit.raw_balance.must_equal raw_balance

          subject.must_equal false

          credit.raw_specific_balance.must_equal raw_specific_balance
          credit.raw_balance.must_equal raw_balance
        end
      end
    end
  end

  describe '#rm(amount)' do
    include FaradayStub
    let(:raw_specific_balance) { 10.12345 }
    let(:raw_balance) { 10.12 }
    let(:amount) { 1 }
    let(:customer) { Bvr::Customer.new }
    let(:credit)   { Bvr::Credit.new(raw_specific_balance, raw_balance, customer) }
    let(:customer_id) { 'foo' }
    let(:options) do
      {
        command: Bvr::Credit::API_COMMANDS[:add],
        customer: customer_id,
        amount: - amount
      }
    end
    let(:response) do
      File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
    end

    subject { credit.rm(amount) }
<commit_msg>fix broken specs due to customer update<commit_after>require_relative '../../spec_helper'
require_relative "../../helpers/faraday_stub"

describe Bvr::Credit do
  describe '.add(customer_id, amount)' do
    include FaradayStub
    let(:customer_id) { 'foo' }
    let(:amount)   { 10    }
    let(:options) do
      {
        command: Bvr::Credit::API_COMMANDS[:add],
        customer: customer_id,
        amount: amount
      }
    end
    let(:response) do
      File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
    end

    subject { Bvr::Credit.add(customer_id, amount) }

    before do
      faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
    end

    it 'returns a result' do
      subject['Result'].must_be_instance_of String
    end
  end


  describe '#add(amount)' do
    include FaradayStub
    let(:raw_specific_balance) { 10.12345 }
    let(:raw_balance) { 10.12 }
    let(:amount) { 1 }
    let(:customer) { Bvr::Customer.new(customer_id) }
    let(:credit)   { Bvr::Credit.new(raw_specific_balance, raw_balance, customer) }
    let(:customer_id) { 'foo' }
    let(:options) do
      {
        command: Bvr::Credit::API_COMMANDS[:add],
        customer: customer_id,
        amount: amount
      }
    end
    let(:response) do
      File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
    end

    subject { credit.add(amount) }

    before do
      faraday_adapter.get(Bvr::Connection.uri_from_h(options)) { [200, {}, response] }
    end

    it 'increase the amout' do
      customer.stub(:id, customer_id) do
        credit.specific_balance.must_equal raw_specific_balance
        credit.balance.must_equal raw_balance

        subject.must_equal true

        credit.specific_balance.must_equal raw_specific_balance + amount
        credit.balance.must_equal raw_balance + amount
      end
    end

    describe 'when the amount if more than .5f' do
      let(:amount) { 1.000001 }

      it 'round up' do
        customer.id = customer_id

        credit.specific_balance.must_equal raw_specific_balance
        credit.balance.must_equal raw_balance

        subject.must_equal true

        credit.specific_balance.must_equal raw_specific_balance + Float("%.5f" % amount)
        credit.balance.must_equal          raw_balance          + Float("%.2f" % amount)
      end
    end

    describe 'when there is an error' do
      let(:response) do
        File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction_failed.xml'))
      end

      it 'does not change the amout and return false' do
        customer.stub(:id, customer_id) do
          credit.raw_specific_balance.must_equal raw_specific_balance
          credit.raw_balance.must_equal raw_balance

          subject.must_equal false

          credit.raw_specific_balance.must_equal raw_specific_balance
          credit.raw_balance.must_equal raw_balance
        end
      end
    end
  end

  describe '#rm(amount)' do
    include FaradayStub
    let(:raw_specific_balance) { 10.12345 }
    let(:raw_balance) { 10.12 }
    let(:amount) { 1 }
    let(:customer) { Bvr::Customer.new(customer_id) }
    let(:credit)   { Bvr::Credit.new(raw_specific_balance, raw_balance, customer) }
    let(:customer_id) { 'foo' }
    let(:options) do
      {
        command: Bvr::Credit::API_COMMANDS[:add],
        customer: customer_id,
        amount: - amount
      }
    end
    let(:response) do
      File.read(File.join(File.dirname(__FILE__), '/..', '/..', '/fixtures/settransaction.xml'))
    end

    subject { credit.rm(amount) }

{'fraction_non_alphanumeric': 0.12224061810154525, 'fraction_numerical': 0.011589403973509934, 'mean_word_length': 2.644544997486174, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 8}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7458173', 'n_tokens_mistral': 2356, 'n_tokens_neox': 2238, 'n_words': 444}