Skip to content

Commit

Permalink
Specs for entities
Browse files Browse the repository at this point in the history
  • Loading branch information
yivo committed Mar 30, 2018
1 parent 425b350 commit 3dbe67c
Show file tree
Hide file tree
Showing 7 changed files with 115 additions and 11 deletions.
8 changes: 4 additions & 4 deletions app/api/management_api_v1/entities/deposit.rb
Expand Up @@ -2,14 +2,14 @@ module ManagementAPIv1
module Entities
class Deposit < Base
expose :id
expose(:currency) { |deposit| deposit.currency.code }
expose(:type) { |deposit| deposit.class.demodulize.underscore }
expose(:currency) { |d| d.currency.code }
expose(:type) { |d| d.class.name.demodulize.underscore }
expose :amount, format_with: :decimal
expose :aasm_state, as: :state
expose :created_at, format_with: :iso8601
expose :done_at, as: :completed_at, format_with: :iso8601
expose :txid, if: -> (deposit) { deposit.coin? }
expose :confirmations, if: -> (deposit) { deposit.coin? }
expose :txid, if: -> (d, _) { d.coin? }
expose :confirmations, if: -> (d, _) { d.coin? }
end
end
end
8 changes: 4 additions & 4 deletions app/api/management_api_v1/entities/withdraw.rb
Expand Up @@ -4,11 +4,11 @@ class Withdraw < Base
expose :id, documentation: { type: Integer, desc: 'The withdraw ID.' }
expose(:currency, documentation: { type: String, desc: 'The currency code.' }) { |w| w.currency.code }
expose(:type, documentation: { type: String, desc: 'The withdraw type (fiat or coin).' }) { |w| w.class.name.demodulize.underscore }
expose :sum, as: :amount, documentation: { type: String, desc: 'The withdraw amount excluding fee.' }
expose :fee, documentation: { type: String, desc: 'The exchange fee.' }
expose :txid, documentation: { type: String, desc: 'The transaction ID.' }, unless: :coin?
expose :amount, documentation: { type: String, desc: 'The withdraw amount excluding fee.' }, format_with: :decimal
expose :fee, documentation: { type: String, desc: 'The exchange fee.' }, format_with: :decimal
expose :txid, documentation: { type: String, desc: 'The transaction ID.' }, if: -> (w, _) { w.coin? }
expose :destination, using: ManagementAPIv1::Entities::WithdrawDestination
expose :state, documentation: { type: String, desc: 'The withdraw state.' }
expose :aasm_state, as: :state, documentation: { type: String, desc: 'The withdraw state.' }
expose :created_at, format_with: :iso8601
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/api/management_api_v1/entities/withdraw_destination.rb
Expand Up @@ -7,7 +7,7 @@ class WithdrawDestination < Base
expose(:type, documentation: { type: String, desc: 'The withdraw destination type (fiat or coin).' }) { |w| w.class.name.demodulize.underscore }
%w[ fiat coin ].each do |type|
"withdraw_destination/#{type}".camelize.constantize.fields.each do |field, desc|
expose(field, documentation: { type: String, desc: desc }) { |w| w.try(field) }
expose(field, documentation: { type: String, desc: desc }, if: -> (w, _) { w.respond_to?(field) }) { |w| w.public_send(field) }
end
end
end
Expand Down
33 changes: 33 additions & 0 deletions spec/api/management_api_v1/entities/deposit_spec.rb
@@ -0,0 +1,33 @@
describe ManagementAPIv1::Entities::Deposit do
context 'fiat' do
let(:record) { create(:deposit_usd) }

subject { OpenStruct.new ManagementAPIv1::Entities::Deposit.represent(record).serializable_hash }

it { expect(subject.id).to eq record.id }
it { expect(subject.currency).to eq 'usd' }
it { expect(subject.type).to eq 'fiat' }
it { expect(subject.amount).to eq record.amount.to_s }
it { expect(subject.state).to eq record.aasm_state }
it { expect(subject.created_at).to eq record.created_at.iso8601 }
it { expect(subject.completed_at).to eq record.done_at&.iso8601 }
it { expect(subject.respond_to?(:txid)).to be_falsey }
it { expect(subject.respond_to?(:confirmations)).to be_falsey }
end

context 'coin' do
let(:record) { create(:deposit_btc) }

subject { OpenStruct.new ManagementAPIv1::Entities::Deposit.represent(record).serializable_hash }

it { expect(subject.id).to eq record.id }
it { expect(subject.currency).to eq 'btc' }
it { expect(subject.type).to eq 'coin' }
it { expect(subject.amount).to eq record.amount.to_s }
it { expect(subject.state).to eq record.aasm_state }
it { expect(subject.created_at).to eq record.created_at.iso8601 }
it { expect(subject.completed_at).to eq record.done_at&.iso8601 }
it { expect(subject.txid).to eq record.txid }
it { expect(subject.confirmations).to eq record.confirmations }
end
end
37 changes: 37 additions & 0 deletions spec/api/management_api_v1/entities/withdraw_destination_spec.rb
@@ -0,0 +1,37 @@
describe ManagementAPIv1::Entities::WithdrawDestination do
context 'fiat' do
let(:record) { create(:fiat_withdraw_destination) }

subject { OpenStruct.new ManagementAPIv1::Entities::WithdrawDestination.represent(record).serializable_hash }

it { expect(subject.id).to eq record.id }
it { expect(subject.currency).to eq record.currency.code }
it { expect(subject.label).to eq record.label }
it { expect(subject.type).to eq 'fiat' }
it { expect(subject.bank_name).to eq record.bank_name }
it { expect(subject.bank_branch_name).to eq record.bank_branch_name }
it { expect(subject.bank_branch_address).to eq record.bank_branch_address }
it { expect(subject.bank_identifier_code).to eq record.bank_identifier_code }
it { expect(subject.bank_account_number).to eq record.bank_account_number }
it { expect(subject.bank_account_holder_name).to eq record.bank_account_holder_name }
it { expect(subject.respond_to?(:address)).to be_falsey }
end

context 'coin' do
let(:record) { create(:coin_withdraw_destination) }

subject { OpenStruct.new ManagementAPIv1::Entities::WithdrawDestination.represent(record).serializable_hash }

it { expect(subject.id).to eq record.id }
it { expect(subject.currency).to eq record.currency.code }
it { expect(subject.label).to eq record.label }
it { expect(subject.type).to eq 'coin' }
it { expect(subject.respond_to?(:bank_name)).to be_falsey }
it { expect(subject.respond_to?(:bank_branch_name)).to be_falsey }
it { expect(subject.respond_to?(:bank_branch_address)).to be_falsey }
it { expect(subject.respond_to?(:bank_identifier_code)).to be_falsey }
it { expect(subject.respond_to?(:bank_account_number)).to be_falsey }
it { expect(subject.respond_to?(:bank_account_holder_name)).to be_falsey }
it { expect(subject.address).to eq record.address }
end
end
33 changes: 33 additions & 0 deletions spec/api/management_api_v1/entities/withdraw_spec.rb
@@ -0,0 +1,33 @@
describe ManagementAPIv1::Entities::Withdraw do
context 'fiat' do
let(:record) { create(:bank_withdraw).reload }

subject { OpenStruct.new ManagementAPIv1::Entities::Withdraw.represent(record).serializable_hash }

it { expect(subject.id).to eq record.id }
it { expect(subject.currency).to eq 'usd' }
it { expect(subject.type).to eq 'fiat' }
it { expect(subject.amount).to eq record.amount.to_s }
it { expect(subject.fee).to eq record.fee.to_s }
it { expect(subject.respond_to?(:txid)).to be_falsey }
it { expect(subject.destination).to be_a_kind_of(Hash) }
it { expect(subject.state).to eq record.aasm_state }
it { expect(subject.created_at).to eq record.created_at.iso8601 }
end

context 'coin' do
let(:record) { create(:satoshi_withdraw) }

subject { OpenStruct.new ManagementAPIv1::Entities::Withdraw.represent(record).serializable_hash }

it { expect(subject.id).to eq record.id }
it { expect(subject.currency).to eq 'btc' }
it { expect(subject.type).to eq 'coin' }
it { expect(subject.amount).to eq record.amount.to_s }
it { expect(subject.fee).to eq record.fee.to_s }
it { expect(subject.txid).to eq record.txid }
it { expect(subject.destination).to be_a_kind_of(Hash) }
it { expect(subject.state).to eq record.aasm_state }
it { expect(subject.created_at).to eq record.created_at.iso8601 }
end
end
5 changes: 3 additions & 2 deletions spec/factories/deposits.rb
Expand Up @@ -9,14 +9,15 @@
trait :btc_account do
currency { Currency.find_by!(code: :btc) }
account { member.get_account(:btc) }
payment_transaction { create(:payment_transaction) }
end

trait :usd_account do
currency { Currency.find_by!(code: :usd) }
account { member.get_account(:usd) }
end

factory :deposit_btc, traits: [:btc_account]
factory :deposit_usd, traits: [:usd_account]
factory :deposit_btc, class: 'Deposits::Coin', traits: [:btc_account]
factory :deposit_usd, class: 'Deposits::Fiat', traits: [:usd_account]
end
end

0 comments on commit 3dbe67c

Please sign in to comment.