Skip to content

Commit

Permalink
Implementa alguns testes para os services
Browse files Browse the repository at this point in the history
  • Loading branch information
owalmirneto committed Mar 18, 2021
1 parent c65d009 commit 4a1c4b0
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 5 deletions.
4 changes: 2 additions & 2 deletions app/serializers/cnba_row_to_transaction_serializer.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ class CNBARowToTransactionSerializer < ApplicationSerializer
end

attribute :store_responsible_name do
object[STORE_RESPONSIBLE_NAME_RANGE].strip
object[STORE_RESPONSIBLE_NAME_RANGE].to_s.strip
end

attribute :store_name do
object[STORE_NAME_RANGE].strip
object[STORE_NAME_RANGE].to_s.strip
end

attribute :occurred_at do
Expand Down
2 changes: 1 addition & 1 deletion app/services/cnba_row_to_transaction_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def success?
def transaction_params
{ occurred_at: serialized_transaction[:occurred_at],
amount: serialized_transaction[:amount], credit_card_id: credit_card.id,
kind_id: kind.id, store_id: store.id, user_id: user_id }
kind_id: kind&.id, store_id: store.id, user_id: user_id }
end

def kind
Expand Down
2 changes: 1 addition & 1 deletion spec/factories/financial_entries.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

trait :invalid do
user_id { nil }
content_file { nil }
content_file { '' }
end

factory :invalid_financial_entry, traits: [:invalid]
Expand Down
26 changes: 26 additions & 0 deletions spec/services/cnba_row_to_transaction_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

describe CNBARowToTransactionService do
setup { create(:transaction_kind, code: random_line[0]) }

subject(:service) { described_class.call(random_line, user_id) }

let(:financial_entry) do
create(:financial_entry, content_file: File.open('./CNAB.txt').read)
end
let(:content_file) { financial_entry.content_file }
let(:random_line) { content_file.lines.sample.to_s.strip }
let(:user_id) { financial_entry.user_id }

describe 'when row invalid' do
let(:financial_entry) { build(:invalid_financial_entry) }

it { is_expected.not_to be_success }

it { expect(Transaction.count).to eq(0) }
end

describe 'when row valid' do
it { is_expected.to be_success }
end
end
31 changes: 31 additions & 0 deletions spec/services/import_cnba_file_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# frozen_string_literal: true

describe ImportCNBAFileService do
setup do
content_file.each_line do |line|
next if TransactionKind.exists?(code: line[0])

create(:transaction_kind, code: line[0])
end
end

subject(:service) { described_class.call(financial_entry_params) }

let(:user) { create(:user) }
let(:content_file) { File.open('./CNAB.txt').read }
let(:financial_entry_params) do
{ user_id: user.id, content_file: content_file }
end

describe 'when params invalid' do
let(:financial_entry_params) { { user_id: nil, content_file: '' } }

it { is_expected.not_to be_success }

it { expect(Transaction.count).to eq(0) }
end

describe 'when params valid' do
it { is_expected.to be_success }
end
end
3 changes: 2 additions & 1 deletion spec/support/devise.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
# frozen_string_literal: true

RSpec.configure do |config|
config.include Devise::Test::ControllerHelpers
config.include Devise::Test::ControllerHelpers, type: :controller
config.include Devise::Test::IntegrationHelpers, type: :service
end

0 comments on commit 4a1c4b0

Please sign in to comment.