<commit_before>require "spec_helper"

describe Balanced::Account do
  use_vcr_cassette
  before do
    api_key = Balanced::ApiKey.new.save
    Balanced.configure api_key.secret
    @marketplace = Balanced::Marketplace.new.save
  end

  describe "Account.uri" do
    use_vcr_cassette

    context "when ApiKey is not configured" do
      use_vcr_cassette
      before do
        Balanced.configure nil
      end

      it "throw an exception that it can not generate the URI" do
        expect {
          Balanced::Account.uri
        }.to raise_error Balanced::Error
      end
    end

    context "when ApiKey is configured" do
      use_vcr_cassette
      before do
        api_key = Balanced::ApiKey.new.save
        Balanced.configure api_key.secret
        @marketplace = Balanced::Marketplace.new.save
      end

      it "it matches the resource's uri structure" do
        uri = Balanced::Account.uri
        uri.should_not be_nil
        uri.should match ACCOUNTS_URI_REGEX
      end
    end
  end

  describe "merchant" do
    use_vcr_cassette

    before do
      @merchant_attributes = {
        :type => "person",
        :name => "<NAME>",
        :street_address => "801 High St.",
        :postal_code => "94301",
        :country => "USA",
        :dob => "1842-01",
        :phone_number => "+16505551234",
      }

      @card = Balanced::Card.new(
        :card_number => "4111111111111111",
        :expiration_month => "12",
        :expiration_year => "2015",
      ).save

      @buyer = Balanced::Account.new(
        :uri => @marketplace.accounts_uri,
        :email_address => "<EMAIL>",
        :card_uri => @card.uri,
        :name => "<NAME>"
      ).save

      @bank_account = Balanced::BankAccount.new(
        :account_number => "1234567890",
        :bank_code => "321174851",
        :name => "<NAME>"
      ).save

      @merchant = Balanced::Account.new(
        :uri => @marketplace.accounts_uri,
        :email_address => "<EMAIL>",
        :merchant => @merchant_attributes,
        :bank_account_uri => @bank_account.uri,
        :name => "<NAME>"
      ).save


    end

    describe "#credit" do
      use_vcr_cassette
      before do
        @buyer.debit 1250
      end

      context "all args passed directly" do
        subject { 
          @merchant.credit 1250, "description", {}, @bank_account.uri
        }

        its(:amount) { should == 1250 }
        its(:meta) { should == {} }
        its(:description) { should == "description" }
      end

      context "args passed by name via options hash" do
      end
    end

    describe "#save" do
      describe "after #save" do
        describe "attributes" do
          describe "#id" do
            subject { @merchant.id }
            it { should_not be_nil }
            it { should_not be_empty }
          end
          describe "#roles" do
            subject { @merchant.roles }
            it { should include("merchant") }
          end
          describe "#email_address" do
            subject { @merchant.email_address }
            it { should eql "<EMAIL>" }<commit_msg>Cover the options passed as hash case (#38)<commit_after>require "spec_helper"

describe Balanced::Account do
  use_vcr_cassette
  before do
    api_key = Balanced::ApiKey.new.save
    Balanced.configure api_key.secret
    @marketplace = Balanced::Marketplace.new.save
  end

  describe "Account.uri" do
    use_vcr_cassette

    context "when ApiKey is not configured" do
      use_vcr_cassette
      before do
        Balanced.configure nil
      end

      it "throw an exception that it can not generate the URI" do
        expect {
          Balanced::Account.uri
        }.to raise_error Balanced::Error
      end
    end

    context "when ApiKey is configured" do
      use_vcr_cassette
      before do
        api_key = Balanced::ApiKey.new.save
        Balanced.configure api_key.secret
        @marketplace = Balanced::Marketplace.new.save
      end

      it "it matches the resource's uri structure" do
        uri = Balanced::Account.uri
        uri.should_not be_nil
        uri.should match ACCOUNTS_URI_REGEX
      end
    end
  end

  describe "merchant" do
    use_vcr_cassette

    before do
      @merchant_attributes = {
        :type => "person",
        :name => "<NAME>",
        :street_address => "801 High St.",
        :postal_code => "94301",
        :country => "USA",
        :dob => "1842-01",
        :phone_number => "+16505551234",
      }

      @card = Balanced::Card.new(
        :card_number => "4111111111111111",
        :expiration_month => "12",
        :expiration_year => "2015",
      ).save

      @buyer = Balanced::Account.new(
        :uri => @marketplace.accounts_uri,
        :email_address => "<EMAIL>",
        :card_uri => @card.uri,
        :name => "<NAME>"
      ).save

      @bank_account = Balanced::BankAccount.new(
        :account_number => "1234567890",
        :bank_code => "321174851",
        :name => "<NAME>"
      ).save

      @merchant = Balanced::Account.new(
        :uri => @marketplace.accounts_uri,
        :email_address => "<EMAIL>",
        :merchant => @merchant_attributes,
        :bank_account_uri => @bank_account.uri,
        :name => "<NAME>"
      ).save


    end

    describe "#credit" do
      use_vcr_cassette
      before do
        @buyer.debit 1250
      end

      context "all args passed directly" do
        subject { 
          @merchant.credit 1250, "description", {}, @bank_account.uri
        }

        its(:amount) { should == 1250 }
        its(:meta) { should == {} }
        its(:description) { should == "description" }
      end

      context "args passed by name via options hash" do
        subject {
          @merchant.credit(
            amount: 1250, 
            description: "description", 
            meta: {},
            destination_uri: @bank_account.uri
          )
        }

        its(:amount) { should == 1250 }
        its(:meta) { should == {} }
        its(:description) { should == "description" }
      end
    end

    describe "#save" do
      describe "after #save" do
        describe "attributes" do
          describe "#id" do
            subject { @merchant.id }
            it { should_not be_nil }
            it { should_not be_empty }
          end
          describe "#roles" do
            subject { @merchant.roles }
            it { should include("merchant") }
          end
          describe "#email_address" do
            subject { @merchant.email_address }
            it { should eql "<EMAIL>" }
{'fraction_non_alphanumeric': 0.10892147876565841, 'fraction_numerical': 0.025358997861289336, 'mean_word_length': 1.9201605709188225, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 61, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 2, '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': '117444', 'n_tokens_mistral': 2108, 'n_tokens_neox': 1937, 'n_words': 486}