From c749b179045a731cc29f3d368ff410065803e877 Mon Sep 17 00:00:00 2001 From: Josh Holtrop Date: Sun, 11 Aug 2013 20:36:27 -0400 Subject: [PATCH] rspec Transaction; store transaction descriptions --- lib/gnucash/account.rb | 3 ++- lib/gnucash/transaction.rb | 4 +++- spec/gnucash/transaction_spec.rb | 38 ++++++++++++++++++++++++++++++++ 3 files changed, 43 insertions(+), 2 deletions(-) create mode 100644 spec/gnucash/transaction_spec.rb diff --git a/lib/gnucash/account.rb b/lib/gnucash/account.rb index a62a798..6660dab 100644 --- a/lib/gnucash/account.rb +++ b/lib/gnucash/account.rb @@ -28,10 +28,11 @@ module Gnucash prefix + name end - def add_transaction(txn, value) + def add_transaction(txn, value, description) @transactions << { txn: txn, value: value, + description: description, } end diff --git a/lib/gnucash/transaction.rb b/lib/gnucash/transaction.rb index 92ca16a..9b5d384 100644 --- a/lib/gnucash/transaction.rb +++ b/lib/gnucash/transaction.rb @@ -3,12 +3,14 @@ module Gnucash attr_accessor :date attr_accessor :value attr_accessor :id + attr_accessor :description def initialize(book, node) @book = book @node = node @id = node.xpath('trn:id').text @date = node.xpath('trn:date-posted/ts:date').text.split(' ').first + @description = node.xpath('trn:description').text @splits = node.xpath('trn:splits/trn:split').map do |split_node| value = Value.new(split_node.xpath('split:value').text) account_id = split_node.xpath('split:account').text @@ -16,7 +18,7 @@ module Gnucash unless account raise "Could not find account with ID #{account_id} for transaction #{@id}" end - account.add_transaction(self, value) + account.add_transaction(self, value, description) { account_id: account_id, value: value, diff --git a/spec/gnucash/transaction_spec.rb b/spec/gnucash/transaction_spec.rb new file mode 100644 index 0000000..c5da7d5 --- /dev/null +++ b/spec/gnucash/transaction_spec.rb @@ -0,0 +1,38 @@ +module Gnucash + describe Transaction do + context "with errors" do + it "raises an error for unexpected XML" do + book = "book" + node = "node" + text = "text" + split_node = "split_node" + value_text = "value_text" + account_text = "account_text" + + node.should_receive(:xpath).with('trn:id').and_return(text) + text.stub(:text) {"hi there"} + node.should_receive(:xpath).with('trn:date-posted/ts:date').and_return(text) + value_text.should_receive(:text).and_return("1177/100") + account_text.should_receive(:text).and_return("a1s2d3f4") + split_node.should_receive(:xpath).with("split:value").and_return(value_text) + split_node.should_receive(:xpath).with("split:account").and_return(account_text) + node.should_receive(:xpath).with('trn:description').and_return(text) + node.should_receive(:xpath).with('trn:splits/trn:split').and_return([split_node]) + book.should_receive(:find_account_by_id).and_return(nil) + + expect { Transaction.new(book, node) }.to raise_error /Could not find account/ + end + end + + context "without errors" do + before(:all) do + @book = Gnucash.open("spec/books/sample.gnucash") + @txn = @book.find_account_by_full_name("Assets::Current Assets::Cash in Wallet").transactions.first[:txn] + end + + it "keeps track of the transaction description" do + @txn.description.should == "Opening Balance" + end + end + end +end