Permalink
Browse files

test cases for the money transactions

  • Loading branch information...
1 parent bc4403a commit f83858e63a3b89ed028c44bb79347e702f6b6cdd @pragashonlink pragashonlink committed Apr 29, 2012
Showing with 20 additions and 57 deletions.
  1. +1 −0 Gemfile
  2. +8 −0 Gemfile.lock
  3. +3 −5 app/controllers/transactions_controller.rb
  4. +7 −50 app/models/transaction.rb
  5. +1 −1 config/application.rb
  6. +0 −1 test/test_helper.rb
View
@@ -11,6 +11,7 @@ gem "twitter-bootstrap-rails"
gem 'therubyracer'
gem 'devise'
gem 'acts_as_tenant'
+gem 'pry'
# Gems used only for assets and not required
# in production environments by default.
View
@@ -32,6 +32,7 @@ GEM
arel (3.0.2)
bcrypt-ruby (3.0.1)
builder (3.0.0)
+ coderay (1.0.6)
coffee-rails (3.2.2)
coffee-script (>= 2.2.0)
railties (~> 3.2.0)
@@ -65,11 +66,16 @@ GEM
i18n (>= 0.4.0)
mime-types (~> 1.16)
treetop (~> 1.4.8)
+ method_source (0.7.1)
mime-types (1.18)
multi_json (1.3.2)
mysql (2.8.1)
orm_adapter (0.0.7)
polyglot (0.3.3)
+ pry (0.9.9.4)
+ coderay (~> 1.0.5)
+ method_source (~> 0.7.1)
+ slop (>= 2.4.4, < 3)
rack (1.4.1)
rack-cache (1.2)
rack (>= 0.4)
@@ -100,6 +106,7 @@ GEM
railties (~> 3.2.0)
sass (>= 3.1.10)
tilt (~> 1.3)
+ slop (2.4.4)
sprockets (2.1.3)
hike (~> 1.2)
rack (~> 1.0)
@@ -132,6 +139,7 @@ DEPENDENCIES
devise
jquery-rails
mysql
+ pry
rails (= 3.2.2)
sass-rails (~> 3.2.3)
sqlite3
@@ -1,28 +1,26 @@
class TransactionsController < ApplicationController
- include TransactionsHelper
-
def transfer
params[:info] = "receiver: #{params[:to_user_id]}\ntransferer: #{params[:user_id]}"
params[:type] = "TF"
respond_to do |format|
- format.xml { render :xml => render_ws_response(Transaction.transfer(params))}
+ format.xml { render :xml => Transaction.process_transaction(params)}
end
end
def add
params[:type] = "AC"
respond_to do |format|
- format.xml { render :xml => render_ws_response(Transaction.add_credit(params))}
+ format.xml { render :xml => Transaction.process_transaction(params)}
end
end
def deduct
params[:type] = "DC"
respond_to do |format|
- format.xml { render :xml => render_ws_response(Transaction.deduct_credit(params))}
+ format.xml { render :xml => Transaction.process_transaction(params)}
end
end
@@ -1,59 +1,16 @@
class Transaction < ActiveRecord::Base
- validates :user_id, :numericality => { :message => ResponseStatus.find_by_code("201").code }
- validates :fund, :numericality => { :message => ResponseStatus.find_by_code("202").code }
- validates :currency, :numericality => { :message => ResponseStatus.find_by_code("203").code }
+ extend HandleTransaction
+
+ validates :user_id, :numericality => { :message => "201" }
+ validates :fund, :numericality => { :message => "202" }
acts_as_tenant(:account)
def validation
- self.errors.add :fund, ResponseStatus.find_by_code("200") if self.type == "TF" &&
- Transaction.fund_for_user(self.user_id)
-
- #self.errors.add :user_id, ResponseStatus.find_by_code("201") unless User.find(self.user_id)
- end
-
- class << self
- def fund_for_user
- Transaction.where("user_id = ?", self.user_id).
- sum("fund")
- end
-
- def transfer(params)
- response = []
-
- Transaction.transaction do
- response << deduct_credit(params)
- response << add_credit(params)
- end
-
- response
- end
-
- def add_credit(params)
- receiver = Transaction.new do |p|
- p.account_id = ActsAsTenant.current_tenant.id,
- p.user_id = params[:to_user_id],
- p.currency = params[:currency],
- p.info = params[:info],
- p.fund = params[:fund],
- p.type = params[:type]
- end
-
- receiver.save ? [] : receiver.errors
- end
-
- def deduct_credit(params)
- transferer = Transaction.new do |p|
- p.account_id = ActsAsTenant.current_tenant.id,
- p.user_id = params[:user_id],
- p.currency = params[:currency],
- p.info = params[:info],
- p.fund = "-#{params[:fund]}".to_f
- p.type = params[:type]
- end
+ self.errors.add :fund, "200" if self.type == "TF" &&
+ Transaction.fund_available
- transferer.save ? [] : transferer.errors
- end
+ self.errors.add :user_id, "201" unless User.find(self.user_id)
end
end
@@ -16,7 +16,7 @@ class Application < Rails::Application
# -- all .rb files in that directory are automatically loaded.
# Custom directories with classes and modules you want to be autoloadable.
- # config.autoload_paths += %W(#{config.root}/extras)
+ config.autoload_paths += %W(#{config.root}/lib)
# Only load the plugins named here, in the order given (default is alphabetical).
# :all can be used as a placeholder for all plugins not explicitly named.
View
@@ -8,6 +8,5 @@ class ActiveSupport::TestCase
# Note: You'll currently still have to declare fixtures explicitly in integration tests
# -- they do not yet inherit this setting
fixtures :all
-
# Add more helper methods to be used by all tests here...
end

0 comments on commit f83858e

Please sign in to comment.