add withdrawl and deposit to FiatBalance and Balance
This commit is contained in:
@@ -23,6 +23,6 @@ FactoryBot.define do
|
||||
factory :fiat_balance do
|
||||
association :user
|
||||
amount_currency { "BRL" }
|
||||
amount_cents { Faker::Number.number(digits: 10) }
|
||||
amount_cents { Faker::Number.number(digits: 4) }
|
||||
end
|
||||
end
|
||||
|
||||
@@ -1,25 +0,0 @@
|
||||
# frozen_string_literal: true
|
||||
|
||||
# == Schema Information
|
||||
#
|
||||
# Table name: admin_users
|
||||
#
|
||||
# id :bigint not null, primary key
|
||||
# email :string default(""), not null
|
||||
# encrypted_password :string default(""), not null
|
||||
# remember_created_at :datetime
|
||||
# reset_password_sent_at :datetime
|
||||
# reset_password_token :string
|
||||
# created_at :datetime not null
|
||||
# updated_at :datetime not null
|
||||
#
|
||||
# Indexes
|
||||
#
|
||||
# index_admin_users_on_email (email) UNIQUE
|
||||
# index_admin_users_on_reset_password_token (reset_password_token) UNIQUE
|
||||
#
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe(AdminUser, type: :model) do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
end
|
||||
@@ -32,4 +32,32 @@ RSpec.describe(Balance, type: :model) do
|
||||
it { is_expected.to(belong_to(:user)) }
|
||||
it { is_expected.to(belong_to(:currency)) }
|
||||
end
|
||||
|
||||
describe ".withdrwal!" do
|
||||
context "when value is greater than the balance" do
|
||||
it "raise ActiveRecord::RecordInvalid" do
|
||||
balance = build(:balance, amount: 70.342)
|
||||
|
||||
expect { balance.withdrawal!(80) }.to(
|
||||
raise_error(ActiveRecord::RecordInvalid, "A validação falhou: Quantia saldo insuficiente")
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when value is equals to the balance" do
|
||||
it "returns true" do
|
||||
balance = build(:balance, amount: 70.342)
|
||||
|
||||
expect(balance.withdrawal!(70.342)).to(eq(true))
|
||||
end
|
||||
end
|
||||
|
||||
context "when value is smaller than the balance" do
|
||||
it "returns true" do
|
||||
balance = build(:balance, amount: 70.342)
|
||||
|
||||
expect(balance.withdrawal!(20)).to(eq(true))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -25,4 +25,32 @@ RSpec.describe(FiatBalance, type: :model) do
|
||||
describe "associations" do
|
||||
it { is_expected.to(belong_to(:user)) }
|
||||
end
|
||||
|
||||
describe ".withdrwal!" do
|
||||
context "when value is greater than the balance" do
|
||||
it "raise ActiveRecord::RecordInvalid" do
|
||||
balance = build(:fiat_balance, amount_cents: 100_00)
|
||||
|
||||
expect { balance.withdrawal!(100_50) }.to(
|
||||
raise_error(ActiveRecord::RecordInvalid, "A validação falhou: Quantia saldo insuficiente")
|
||||
)
|
||||
end
|
||||
end
|
||||
|
||||
context "when value is equals to the balance" do
|
||||
it "returns true" do
|
||||
balance = build(:fiat_balance, amount_cents: 100_00)
|
||||
|
||||
expect(balance.withdrawal!(100_00)).to(eq(true))
|
||||
end
|
||||
end
|
||||
|
||||
context "when value is smaller than the balance" do
|
||||
it "returns true" do
|
||||
balance = build(:fiat_balance, amount_cents: 100_00)
|
||||
|
||||
expect(balance.withdrawal!(90_00)).to(eq(true))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,5 +2,41 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe(BalancePolicy, type: :policy) do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
context "when user has balances" do
|
||||
it "return only balances from a user" do
|
||||
create(:balance)
|
||||
create(:balance)
|
||||
|
||||
user = build(:user)
|
||||
balance = create(:balance, user: user)
|
||||
|
||||
balances = BalancePolicy::Scope.new(user, Balance).resolve
|
||||
|
||||
expect(balances).to(eq([balance]))
|
||||
end
|
||||
end
|
||||
|
||||
context "when user has not balances" do
|
||||
it "return empty array" do
|
||||
create(:balance)
|
||||
create(:balance)
|
||||
|
||||
user = build(:user)
|
||||
|
||||
balances = BalancePolicy::Scope.new(user, Balance).resolve
|
||||
|
||||
expect(balances).to(eq([]))
|
||||
end
|
||||
end
|
||||
|
||||
context "when user is nil" do
|
||||
it "return empty array" do
|
||||
create(:balance)
|
||||
create(:balance)
|
||||
|
||||
balances = BalancePolicy::Scope.new(nil, Balance).resolve
|
||||
|
||||
expect(balances).to(eq([]))
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
@@ -2,5 +2,43 @@
|
||||
require "rails_helper"
|
||||
|
||||
RSpec.describe(FiatBalancePolicy, type: :policy) do
|
||||
pending "add some examples to (or delete) #{__FILE__}"
|
||||
describe "::Scope" do
|
||||
context "when user has balances" do
|
||||
it "return only balances from a user" do
|
||||
create(:fiat_balance)
|
||||
create(:fiat_balance)
|
||||
|
||||
user = build(:user)
|
||||
balance = create(:fiat_balance, user: user)
|
||||
|
||||
balances = FiatBalancePolicy::Scope.new(user, FiatBalance).resolve
|
||||
|
||||
expect(balances).to(eq([balance]))
|
||||
end
|
||||
end
|
||||
|
||||
context "when user has not balances" do
|
||||
it "return empty array" do
|
||||
create(:fiat_balance)
|
||||
create(:fiat_balance)
|
||||
|
||||
user = build(:user)
|
||||
|
||||
balances = FiatBalancePolicy::Scope.new(user, FiatBalance).resolve
|
||||
|
||||
expect(balances).to(eq([]))
|
||||
end
|
||||
end
|
||||
|
||||
context "when user is nil" do
|
||||
it "return empty array" do
|
||||
create(:fiat_balance)
|
||||
create(:fiat_balance)
|
||||
|
||||
balances = FiatBalancePolicy::Scope.new(nil, FiatBalance).resolve
|
||||
|
||||
expect(balances).to(eq([]))
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user