From 61273bd66fa971e70b4670bc113324d5f94bb0d8 Mon Sep 17 00:00:00 2001 From: Peng DU Date: Mon, 11 Jul 2016 11:40:20 +0200 Subject: [PATCH] fix test --- test/fixtures/wallet_transactions.yml | 4 ++-- test/fixtures/wallets.yml | 4 ++-- test/integration/events_test.rb | 4 ++-- test/integration/wallets_test.rb | 18 ++++++++-------- test/services/wallet_service_test.rb | 30 +++++++++++++++++++-------- 5 files changed, 36 insertions(+), 24 deletions(-) diff --git a/test/fixtures/wallet_transactions.yml b/test/fixtures/wallet_transactions.yml index 9edb192b1..012604359 100644 --- a/test/fixtures/wallet_transactions.yml +++ b/test/fixtures/wallet_transactions.yml @@ -1,7 +1,7 @@ # Read about fixtures at http://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html transaction1: - user_id: 4 - wallet: wallet_4 + user_id: 5 + wallet: wallet_5 transaction_type: credit amount: 1000 diff --git a/test/fixtures/wallets.yml b/test/fixtures/wallets.yml index e86e3a29f..696573c94 100644 --- a/test/fixtures/wallets.yml +++ b/test/fixtures/wallets.yml @@ -4,7 +4,7 @@ wallet_2: wallet_4: user_id: 4 - amount: 1000 + amount: 0 wallet_6: user_id: 6 @@ -12,7 +12,7 @@ wallet_6: wallet_5: user_id: 5 - amount: 0 + amount: 1000 wallet_3: user_id: 3 diff --git a/test/integration/events_test.rb b/test/integration/events_test.rb index ca821ad45..98701c69d 100644 --- a/test/integration/events_test.rb +++ b/test/integration/events_test.rb @@ -69,7 +69,7 @@ class EventsTest < ActionDispatch::IntegrationTest reservable_type: 'Event', nb_reserve_places: 2, nb_reserve_reduced_places: 0, - slot_attributes: [ + slots_attributes: [ { start_at: e.availability.start_at, end_at: e.availability.end_at, @@ -114,4 +114,4 @@ class EventsTest < ActionDispatch::IntegrationTest assert_equal 20, e.nb_total_places, 'Total number of places was not updated' assert_equal 18, e.nb_free_places, 'Number of free places was not updated' end -end \ No newline at end of file +end diff --git a/test/integration/wallets_test.rb b/test/integration/wallets_test.rb index 25c84e1c4..5ace5e4af 100644 --- a/test/integration/wallets_test.rb +++ b/test/integration/wallets_test.rb @@ -3,8 +3,8 @@ class WalletsTest < ActionDispatch::IntegrationTest # Called before every test method runs. Can be used # to set up fixture information. def setup - @kdumas = User.find_by(username: 'kdumas') - login_as(@kdumas, scope: :user) + @vlonchamp = User.find_by(username: 'vlonchamp') + login_as(@vlonchamp, scope: :user) end # Called after every test method runs. Can be used to tear @@ -15,12 +15,12 @@ class WalletsTest < ActionDispatch::IntegrationTest end test 'get my wallet' do - get "/api/wallet/by_user/#{@kdumas.id}" + get "/api/wallet/by_user/#{@vlonchamp.id}" assert_equal 200, response.status assert_equal Mime::JSON, response.content_type wallet = json_response(response.body) - assert_equal @kdumas.wallet.user_id, wallet[:user_id] - assert_equal @kdumas.wallet.amount, wallet[:amount] + assert_equal @vlonchamp.wallet.user_id, wallet[:user_id] + assert_equal @vlonchamp.wallet.amount, wallet[:amount] end test 'admin can get wallet by user id' do @@ -36,13 +36,13 @@ class WalletsTest < ActionDispatch::IntegrationTest end test 'cant get wallet of an user if not admin' do - user5 = users(:user_5) + user5 = users(:user_4) get "/api/wallet/by_user/#{user5.id}" assert_equal 403, response.status end test 'get all transactions of wallet' do - w = @kdumas.wallet + w = @vlonchamp.wallet get "/api/wallet/#{w.id}/transactions" assert_equal 200, response.status assert_equal Mime::JSON, response.content_type @@ -52,7 +52,7 @@ class WalletsTest < ActionDispatch::IntegrationTest end test 'only admin and wallet owner can show their transactions' do - user5 = users(:user_5) + user5 = users(:user_4) get "/api/wallet/#{user5.wallet.id}/transactions" assert_equal 403, response.status end @@ -60,7 +60,7 @@ class WalletsTest < ActionDispatch::IntegrationTest test 'admin can credit amount to a wallet' do admin = users(:user_1) login_as(admin, scope: :user) - w = @kdumas.wallet + w = @vlonchamp.wallet amount = 10 expected_amount = w.amount + amount put "/api/wallet/#{w.id}/credit", diff --git a/test/services/wallet_service_test.rb b/test/services/wallet_service_test.rb index 46536423f..fc33ddcea 100644 --- a/test/services/wallet_service_test.rb +++ b/test/services/wallet_service_test.rb @@ -3,25 +3,37 @@ require 'test_helper' class WalletServiceTest < ActiveSupport::TestCase setup do @admin = User.find_by(username: 'admin') - @user = User.find_by(username: 'jdupond') - @wallet = @user.wallet + @jdupond = User.find_by(username: 'jdupond') + @jdupond_wallet = @jdupond.wallet + @vlonchamp = User.find_by(username: 'vlonchamp') + @vlonchamp_wallet = @vlonchamp.wallet end test 'admin can credit a wallet' do - service = WalletService.new(user: @admin, wallet: @wallet) - expected_amount = @wallet.amount + 5 + service = WalletService.new(user: @admin, wallet: @jdupond_wallet) + expected_amount = @jdupond_wallet.amount + 5 assert service.credit(5) - assert_equal @wallet.amount, expected_amount + assert_equal @jdupond_wallet.amount, expected_amount end test 'create a credit transaction after credit amount to wallet' do - service = WalletService.new(user: @admin, wallet: @wallet) - assert_equal 0, @wallet.wallet_transactions.count + service = WalletService.new(user: @admin, wallet: @jdupond_wallet) + assert_equal 0, @jdupond_wallet.wallet_transactions.count assert service.credit(10) - transaction = @wallet.wallet_transactions.first + transaction = @jdupond_wallet.wallet_transactions.first assert_equal transaction.transaction_type, 'credit' assert_equal transaction.amount, 10 assert_equal transaction.user, @admin - assert_equal transaction.wallet, @wallet + assert_equal transaction.wallet, @jdupond_wallet + end + + test 'create a debit transaction after debit amoutn to wallet' do + service = WalletService.new(user: @vlonchamp, wallet: @vlonchamp_wallet) + assert service.debit(5, nil) + transaction = @vlonchamp_wallet.wallet_transactions.last + assert_equal transaction.transaction_type, 'debit' + assert_equal transaction.amount, 5 + assert_equal transaction.user, @vlonchamp + assert_equal transaction.wallet, @vlonchamp_wallet end end