ShowMeTheMoney-16 - Tallying up the expenses

Iteration#2 Day 3/10

Some TODOs I have jotted down.
  • place all view strings (displayed to the user) under the control of ApplicationHelper::ResourceManager
  • inflow/list with the table style similar to outflow/list
  • credit and expense rows are listed in order of ID. Ordering by entry date is what is required ( Found this out while I was actually using the app.. Real users! nothing comes close to them for feedback)
For the last item – lets take expense first (Credit should be identical), we add another record to the fixtures file, but out of order – make the year 2007 as shown below. We need to update some existing tests to update @fixtureRecordCount to 3. Done!

file: /test/fixtures/expenses.yml
rent_expense_entry:
id: 1
created_at: 2008-01-19 00:00:00
description: rent
amount: 10000
apparel_expense_entry:
id: 2
created_at: 2008-02-20 00:00:00
description: A pair of Jeans
amount: 1000.50
movies_expense_entry:
id: 3
created_at: 2007-12-20 00:00:00
description: Taare Zameen Par
amount: 300

Next, we need to update the controller test to check if the records are displayed in order. For that we inspect the controller’s @expenses variable and assert that they are retrieved order by date i.e. 3 – 1 - 2

def test_list
get :list
assert_response :success
assert_template 'list'
assert_not_nil assigns(:expenses)
assert_equal(@fixtureRecordCount, assigns(:expenses).size )
assert_equal(3, assigns(:expenses)[0].id,not ordered by date”)
assert_equal(1, assigns(:expenses)[1].id)
assert_equal(2, assigns(:expenses)[2].id)
end


Run the tests to see if it fails. It does. That validates our test. Next some research on how to implement this, how do we tell paginate to order the records by “created_at” ? Search search.. online... can't find it. Once again the book comes to the rescue, Pg 352, Section 17.7 Hurray!
def list
@expense_pages, @expenses = paginate :expenses, :order=>'created_at', :per_page => 10
end


The :order key-value pair is all we need. It’s really that simple!!! I even tried out via the browser – It just works!! I LOOVVEEE Rails!!!
Do it again for the credits page. TODOs are done! Quick run of all tests. Green!

Alrighthy! now we got to build ourselves a balance tracker.

file: /test/unit/balance_tracker_test.rb

require File.dirname(__FILE__) + '/../test_helper'

class BalanceTrackerTest < Test::Unit::TestCase
fixtures :credits, :expenses
def test_balance
assert_in_delta(19700, BalanceTracker.balance, 0.01 )

Credit.create(:description=>"D", :amount=>300.50)
assert_in_delta(20000.5, BalanceTracker.balance, 0.01 )

Expense.create(:description=>"E", :amount=>10000)
assert_in_delta(10000.5, BalanceTracker.balance, 0.01 )
end
end



file: /app/models/balance_tracker.rb
class BalanceTracker
def BalanceTracker.balance
models = Credit.find_by_sql("select sum(amount) as totalAmount from Credits");
fTotalCredit = models[0].attributes["totalAmount"].to_f

models = Expense.find_by_sql("select sum(amount) as totalAmount from Expenses");
return fTotalCredit - models[0].attributes["totalAmount"].to_f;
end
end


Done. Now we have everything to go and work on our acceptance test fixtures to make the actionfixture table and second rowfixture table pass.

file: /AcceptanceTests/track_balance.rb

require File.dirname(__FILE__) + '/../test/test_helper'
require 'inflow_controller'
require 'fit/fixture'

class CreditsHelper < Test::Unit::TestCase
def setup
Credit.delete_all
@controller = InflowController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_dummy
end
end
class ExpensesHelper < Test::Unit::TestCase
def setup
Expense.delete_all
@controller = OutflowController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_dummy
end
end
module AcceptanceTests
class TrackBalance < Fit::Fixture

#enter attributes
attr_accessor :description, :amount, :select_entry

def initialize
@creditHelper = CreditsHelper.new "test_dummy"
@creditHelper.setup

@expenseHelper = ExpensesHelper.new "test_dummy"
@expenseHelper.setup
super
end

#press methods
def add_credit_entry
@creditHelper.get :new
@creditHelper.post :create, :credit => {:description => @description, :amount => @amount}
end
def delete_credit_entry
@creditHelper.post :destroy, :id => getID_ForSelectedEntry(Credit)
end
def add_expense_entry
@expenseHelper.get :new
@expenseHelper.post :create, :expense => {:description => @description, :amount => @amount}
end
def delete_expense_entry
@expenseHelper.post :destroy, :id => getID_ForSelectedEntry(Expense)
end
#check methods
def current_balance
BalanceTracker.balance
end

private
def getID_ForSelectedEntry model
obSelectedEntry = model.find(:all, :order => "id")[@select_entry.to_i - 1]
obSelectedEntry.id
end
end
end


file: /AcceptanceTests/get_inflow_records.rb

puts File.dirname(__FILE__) + '\get_inflow_records'
require File.dirname(__FILE__) + '\get_inflow_records'
require 'fit/row_fixture'
module AcceptanceTests
class GetOutflowRecords < Fit::RowFixture
def query
recordset = []
Expense.find(:all, :order=>'created_at').each{|x|
recordset << InflowRecord.new(x.created_at, x.description, x.amount)
}
end
def get_target_class
InflowRecord
end
end
end


Our Reward !!!

No comments:

Post a Comment