Skip to content

Commit

Permalink
some more specs, some spec refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
yoyostile committed Apr 19, 2016
1 parent d547913 commit c3aff7a
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 105 deletions.
6 changes: 3 additions & 3 deletions lib/camt_parser/053/account.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ def initialize(xml_data)
end

def iban
@iban ||= (x = @xml_data.xpath('Id/IBAN')).empty? ? nil : x.first.content
@iban ||= @xml_data.xpath('Id/IBAN/text()').text
end
alias_method :account_number, :iban
alias_method :source, :iban

def bic
@bic ||= (x = @xml_data.xpath('Svcr/FinInstnId/BIC')).empty? ? nil : x.first.content
@bic ||= @xml_data.xpath('Svcr/FinInstnId/BIC/text()').text
end

def bank_name
@bank_name ||= (x = @xml_data.xpath('Svcr/FinInstnId/Nm')).empty? ? nil : x.first.content
@bank_name ||= @xml_data.xpath('Svcr/FinInstnId/Nm/text()').text
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/camt_parser/053/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ def initialize(xml_data)
grphdr = xml_data.xpath('BkToCstmrStmt/GrpHdr')
@group_header = GroupHeader.new(grphdr)
statements = xml_data.xpath('BkToCstmrStmt/Stmt')
@statements = statements.map{ |x| Statement.new(x) }
@statements = statements.map{ |x| Statement.new(x) }
end
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/camt_parser/053/group_header.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ def initialize(xml_data)
@message_id = xml_data.xpath('MsgId/text()').text
@creation_date_time = Time.parse(xml_data.xpath('CreDtTm/text()').text)
@message_pagination = (x = xml_data.xpath('MsgPgntn')).empty? ? nil : MessagePagination.new(x)
@additional_information = (x = xml_data.xpath('AddtlInf')).empty? ? nil : x.first.content
@additional_information = xml_data.xpath('AddtlInf/text()').text
end
end

Expand Down
9 changes: 3 additions & 6 deletions lib/camt_parser/053/statement.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def to_date_time
def account
@account ||= Account.new(@xml_data.xpath('Acct').first)
end
alias_method :account_identification, :account

def entries
@entries ||= @xml_data.xpath('Ntry').map{ |x| Entry.new(x) }
Expand All @@ -42,7 +43,7 @@ def opening_balance
@opening_balance ||= begin
bal = @xml_data.xpath('Bal/Tp//Cd[contains(text(), "PRCD")]').first.ancestors('Bal')
date = bal.xpath('Dt/Dt/text()').text
currency = bal.xpath('Amt').attribute('Ccy')
currency = bal.xpath('Amt').attribute('Ccy').value
AccountBalance.new bal.xpath('Amt/text()').text, currency, date, true
end
end
Expand All @@ -52,16 +53,12 @@ def closing_balance
@closing_balance ||= begin
bal = @xml_data.xpath('Bal/Tp//Cd[contains(text(), "CLBD")]').first.ancestors('Bal')
date = bal.xpath('Dt/Dt/text()').text
currency = bal.xpath('Amt').attribute('Ccy')
currency = bal.xpath('Amt').attribute('Ccy').value
AccountBalance.new bal.xpath('Amt/text()').text, currency, date, true
end
end
alias_method :closing_or_intermediary_balance, :closing_balance

def account_identification
account
end

def source
@xml_data.to_s
end
Expand Down
2 changes: 1 addition & 1 deletion lib/camt_parser/misc.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ def to_amount_in_cents(value)
end

def to_amount(value)
value.gsub(',','.').to_f
BigDecimal.new value.gsub(',', '.')
end
end
end
Expand Down
15 changes: 15 additions & 0 deletions spec/lib/camt_parser/053/account_balance_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe CamtParser::Format053::AccountBalance do
let(:camt) { CamtParser::File.parse('spec/fixtures/valid_example.xml') }
let(:statements) { camt.statements }
let(:ex_stmt) { camt.statements[0] }
subject { ex_stmt.opening_balance }

specify { expect(subject.currency).to eq "EUR" }
specify { expect(subject.date).to eq Date.new(2013,12,27) }
specify { expect(subject.sign).to eq 1 }
specify { expect(subject.credit?).to be_truthy }
specify { expect(subject.amount).to eq BigDecimal.new("33.06") }
specify { expect(subject.amount_in_cents).to eq 3306 }
end
15 changes: 15 additions & 0 deletions spec/lib/camt_parser/053/account_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe CamtParser::Format053::Account do
let(:camt) { CamtParser::File.parse('spec/fixtures/valid_example.xml') }
let(:statements) { camt.statements }
let(:ex_stmt) { camt.statements[0] }
let(:account) { ex_stmt.account }

it { expect(account.iban).to eq("DE14740618130000033626") }
it { expect(account.iban).to eq(account.account_number) }
it { expect(account.iban).to eq(account.source) }
it { expect(account.bic).to eq("GENODEF1PFK") }
it { expect(account.bank_name).to eq("VR-Bank Rottal-Inn eG") }
end

18 changes: 12 additions & 6 deletions spec/lib/camt_parser/053/base_spec.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,17 @@
require 'spec_helper'

describe CamtParser::Format053::Base do
it "parses the group_header and the statements" do
expect(CamtParser::Format053::GroupHeader).to receive(:new).and_call_original
expect(CamtParser::Format053::Statement).to receive(:new).and_call_original
camt = CamtParser::File.parse 'spec/fixtures/valid_example.xml'
expect(camt.group_header).to_not eq(nil)
expect(camt.statements).to_not eq([])

context 'initialization' do
after do
CamtParser::File.parse 'spec/fixtures/valid_example.xml'
end

it { expect(CamtParser::Format053::GroupHeader).to receive(:new).and_call_original }
it { expect(CamtParser::Format053::Statement).to receive(:new).and_call_original }
end

let(:camt) { CamtParser::File.parse 'spec/fixtures/valid_example.xml' }
it { expect(camt.group_header).to_not be_nil }
it { expect(camt.statements).to_not eq([]) }
end
15 changes: 15 additions & 0 deletions spec/lib/camt_parser/053/creditor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe CamtParser::Format053::Creditor do
let(:camt) { CamtParser::File.parse('spec/fixtures/valid_example.xml') }
let(:statements) { camt.statements }
let(:ex_stmt) { camt.statements[0] }
let(:entries) { ex_stmt.entries }
let(:ex_entry) { ex_stmt.entries[0] }
let(:creditor) { ex_entry.creditor }

it { expect(creditor.name).to eq("Testkonto Nummer 2") }
it { expect(creditor.iban).to eq("DE09300606010012345671") }
it { expect(creditor.bic).to eq("DAAEDEDDXXX") }
it { expect(creditor.bank_name).to eq("Bank") }
end
15 changes: 15 additions & 0 deletions spec/lib/camt_parser/053/debitor_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require 'spec_helper'

describe CamtParser::Format053::Debitor do
let(:camt) { CamtParser::File.parse('spec/fixtures/valid_example.xml') }
let(:statements) { camt.statements }
let(:ex_stmt) { camt.statements[0] }
let(:entries) { ex_stmt.entries }
let(:ex_entry) { ex_stmt.entries[0] }
let(:debitor) { ex_entry.debitor }

it { expect(debitor.name).to eq("Wayne Enterprises") }
it { expect(debitor.iban).to eq("DE24302201900609832118") }
it { expect(debitor.bic).to eq("DAAEDEDDXXX") }
it { expect(debitor.bank_name).to eq("") }
end
35 changes: 35 additions & 0 deletions spec/lib/camt_parser/053/entry_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
require 'spec_helper'

describe CamtParser::Format053::Entry do
let(:camt) { CamtParser::File.parse('spec/fixtures/valid_example.xml') }
let(:statements) { camt.statements }
let(:ex_stmt) { camt.statements[0] }
let(:entries) { ex_stmt.entries }
let(:ex_entry) { ex_stmt.entries[0] }

specify { expect(entries).to all(be_kind_of(described_class)) }

context '#amount' do
specify { expect(ex_entry.amount).to be_kind_of(BigDecimal) }
specify { expect(ex_entry.amount).to eq(BigDecimal.new('2')) }
specify { expect(ex_entry.amount_in_cents).to eq(200) }
end

specify { expect(ex_entry.currency).to eq('EUR') }
specify { expect(ex_entry.value_date).to be_kind_of(Date) }
specify { expect(ex_entry.value_date).to eq(Date.new(2013, 12, 27)) }
specify { expect(ex_entry.booking_date).to be_kind_of(Date) }
specify { expect(ex_entry.booking_date).to eq(Date.new(2013, 12, 27)) }
specify { expect(ex_entry.creditor).to be_kind_of(CamtParser::Format053::Creditor) }
specify { expect(ex_entry.debitor).to be_kind_of(CamtParser::Format053::Debitor) }
specify { expect(ex_entry.remittance_information).to eq("TEST BERWEISUNG MITTELS BLZUND KONTONUMMER - DTA") }
specify { expect(ex_entry.additional_information).to eq("Überweisungs-Gutschrift; GVC: SEPA Credit Transfer (Einzelbuchung-Haben)") }
specify { expect(ex_entry.debit?).to eq(true) }
specify { expect(ex_entry.credit?).to eq(false) }
specify { expect(ex_entry.sign).to eq(-1) }

specify { expect(ex_entry.name).to eq("Testkonto Nummer 2") }
specify { expect(ex_entry.iban).to eq("DE09300606010012345671") }
specify { expect(ex_entry.bic).to eq("DAAEDEDDXXX") }
specify { expect(ex_entry.swift_code).to eq("NTRF") }
end
30 changes: 14 additions & 16 deletions spec/lib/camt_parser/053/group_header_spec.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,21 @@
require 'spec_helper'

describe CamtParser::Format053::GroupHeader do
it "parses the GroupHeader" do
camt = CamtParser::File.parse 'spec/fixtures/valid_example.xml'
group_header = camt.group_header
expect(group_header.class).to eq(described_class)
expect(group_header.message_id).to eq("053D2013-12-27T22:05:03.0N130000005")
expect(group_header.creation_date_time.class).to eq(Time)
expect(group_header.message_pagination.class).to eq(CamtParser::Format053::MessagePagination)
expect(group_header.additional_information).to eq(nil)
end
let(:camt) { CamtParser::File.parse 'spec/fixtures/valid_example.xml' }
let(:group_header) { camt.group_header }

specify { expect(group_header).to be_kind_of(described_class) }
specify { expect(group_header.message_id).to eq("053D2013-12-27T22:05:03.0N130000005") }
specify { expect(group_header.creation_date_time).to be_kind_of(Time) }
specify { expect(group_header.message_pagination).to be_kind_of(CamtParser::Format053::MessagePagination) }
specify { expect(group_header.additional_information).to eq("") }
end

describe CamtParser::Format053::MessagePagination do
it "parses the MessagePagination information" do
camt = CamtParser::File.parse 'spec/fixtures/valid_example.xml'
message_pagination = camt.group_header.message_pagination
expect(message_pagination.class).to eq(described_class)
expect(message_pagination.page_number).to eq(1)
expect(message_pagination.last_page?).to eq(true)
end
let(:camt) { CamtParser::File.parse 'spec/fixtures/valid_example.xml' }
let(:message_pagination) { camt.group_header.message_pagination }

specify { expect(message_pagination).to be_kind_of(described_class) }
specify { expect(message_pagination.page_number).to eq(1) }
specify { expect(message_pagination.last_page?).to be_truthy }
end
82 changes: 11 additions & 71 deletions spec/lib/camt_parser/053/statement_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,75 +5,15 @@
let(:statements) { camt.statements }
let(:ex_stmt) { camt.statements[0] }

it "parses the statements properly into classes" do
classes = statements.collect(&:class).uniq
expect(classes.length).to eq(1)
expect(classes.first).to eq(described_class)
end

it "contains certain attributes" do
expect(ex_stmt.identification).to eq("0352C5320131227220503")
expect(ex_stmt.generation_date.class).to eq(Time)
expect(ex_stmt.from_date_time).to eq(nil)
expect(ex_stmt.to_date_time).to eq(nil)
expect(ex_stmt.account.class).to eq(CamtParser::Format053::Account)
expect(ex_stmt.entries.class).to eq(Array)
end

describe CamtParser::Format053::Account do
let(:account) { ex_stmt.account }

it "parses the account properly into a class" do
expect(account.iban).to eq("DE14740618130000033626")
expect(account.bic).to eq("GENODEF1PFK")
expect(account.bank_name).to eq("VR-Bank Rottal-Inn eG")
end
end

describe CamtParser::Format053::Entry do
let(:entries) { ex_stmt.entries }
let(:ex_entry) { ex_stmt.entries[0] }

it "parses the entries properly into classes" do
classes = entries.collect(&:class).uniq
expect(classes.length).to eq(1)
expect(classes.first).to eq(described_class)
end

it "contains certain attributes" do
expect(ex_entry.amount.class).to eq(Float)
expect(ex_entry.amount).to eq(BigDecimal.new('2'))
expect(ex_entry.currency).to eq('EUR')
expect(ex_entry.value_date.class).to eq(Date)
expect(ex_entry.value_date).to eq(Date.new(2013, 12, 27))
expect(ex_entry.creditor.class).to eq(CamtParser::Format053::Creditor)
expect(ex_entry.debitor.class).to eq(CamtParser::Format053::Debitor)
expect(ex_entry.remittance_information).to eq("TEST BERWEISUNG MITTELS BLZUND KONTONUMMER - DTA")
expect(ex_entry.additional_information).to eq(
"Überweisungs-Gutschrift; GVC: SEPA Credit Transfer (Einzelbuchung-Haben)"
)
end

describe CamtParser::Format053::Debitor do
let(:debitor) { ex_entry.debitor }

it "contains certain attributes" do
expect(debitor.name).to eq("Wayne Enterprises")
expect(debitor.iban).to eq("DE24302201900609832118")
expect(debitor.bic).to eq("DAAEDEDDXXX")
expect(debitor.bank_name).to eq("")
end
end

describe CamtParser::Format053::Creditor do
let(:creditor) { ex_entry.creditor }

it "contains certain attributes" do
expect(creditor.name).to eq("Testkonto Nummer 2")
expect(creditor.iban).to eq("DE09300606010012345671")
expect(creditor.bic).to eq("DAAEDEDDXXX")
expect(creditor.bank_name).to eq("Bank")
end
end
end
specify { expect(statements).to all(be_kind_of(described_class)) }
specify { expect(ex_stmt.identification).to eq("0352C5320131227220503") }
specify { expect(ex_stmt.generation_date).to be_kind_of(Time) }
specify { expect(ex_stmt.from_date_time).to be_nil }
specify { expect(ex_stmt.to_date_time).to be_nil }
specify { expect(ex_stmt.account).to be_kind_of(CamtParser::Format053::Account) }
specify { expect(ex_stmt.entries).to be_kind_of(Array) }
specify { expect(ex_stmt.electronic_sequence_number).to eq('130000005') }

specify { expect(ex_stmt.opening_balance).to be_kind_of(CamtParser::Format053::AccountBalance) }
specify { expect(ex_stmt.closing_balance).to be_kind_of(CamtParser::Format053::AccountBalance) }
end
18 changes: 18 additions & 0 deletions spec/lib/camt_parser/misc_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
require 'spec_helper'

describe CamtParser::Misc do
let(:dot_value) { "30.12" }
let(:comma_value) { "30,12" }

context '#to_amount_in_cents' do
specify { expect(described_class.to_amount_in_cents(dot_value)).to be_kind_of(Fixnum) }
specify { expect(described_class.to_amount_in_cents(dot_value)).to eq(3012) }
specify { expect(described_class.to_amount_in_cents(comma_value)).to eq(3012) }
end

context '#to_amount' do
specify { expect(described_class.to_amount(dot_value)).to be_kind_of(BigDecimal) }
specify { expect(described_class.to_amount(dot_value)).to eq(30.12) }
specify { expect(described_class.to_amount(comma_value)).to eq(30.12) }
end
end

0 comments on commit c3aff7a

Please sign in to comment.