1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-22 06:39:39 +02:00
Maybe/test/models/transaction_import_test.rb
Daniel Esteves 077694bbde
Some checks are pending
Publish Docker image / ci (push) Waiting to run
Publish Docker image / Build docker image (push) Blocked by required conditions
feat(import): add currency and number format support for CSV imports (#1819)
* feat(import): add currency and number format support for CSV imports

* feat(import): add currency and format for mint and trade

* fix(imports): remove currency field in favor of currency csv col

* fix(imports): remove validate column from import model

* test(import): add some tests for imports

* test(import): add some tests for generate_rows_from_csv

* fix: change method name for import model

* fix: change before validation

---------

Co-authored-by: danestves <danestves@users.noreply.github.com>
2025-02-10 15:31:28 -05:00

71 lines
2.6 KiB
Ruby

require "test_helper"
class TransactionImportTest < ActiveSupport::TestCase
include ActiveJob::TestHelper, ImportInterfaceTest
setup do
@subject = @import = imports(:transaction)
end
test "uploaded? if raw_file_str is present" do
@import.expects(:raw_file_str).returns("test").once
assert @import.uploaded?
end
test "configured? if uploaded and rows are generated" do
@import.expects(:uploaded?).returns(true).once
assert @import.configured?
end
test "cleaned? if rows are generated and valid" do
@import.expects(:configured?).returns(true).once
assert @import.cleaned?
end
test "publishable? if cleaned and mappings are valid" do
@import.expects(:cleaned?).returns(true).once
assert @import.publishable?
end
test "imports transactions, categories, tags, and accounts" do
import = <<~CSV
date,name,amount,category,tags,account,notes
01/01/2024,Txn1,100,TestCategory1,TestTag1,TestAccount1,notes1
01/02/2024,Txn2,200,TestCategory2,TestTag1|TestTag2,TestAccount2,notes2
01/03/2024,Txn3,300,,,,notes3
CSV
@import.update!(
raw_file_str: import,
date_col_label: "date",
amount_col_label: "amount",
date_format: "%m/%d/%Y"
)
@import.generate_rows_from_csv
@import.mappings.create! key: "TestCategory1", create_when_empty: true, type: "Import::CategoryMapping"
@import.mappings.create! key: "TestCategory2", mappable: categories(:food_and_drink), type: "Import::CategoryMapping"
@import.mappings.create! key: "", create_when_empty: false, mappable: nil, type: "Import::CategoryMapping" # Leaves uncategorized
@import.mappings.create! key: "TestTag1", create_when_empty: true, type: "Import::TagMapping"
@import.mappings.create! key: "TestTag2", mappable: tags(:one), type: "Import::TagMapping"
@import.mappings.create! key: "", create_when_empty: false, mappable: nil, type: "Import::TagMapping" # Leaves untagged
@import.mappings.create! key: "TestAccount1", create_when_empty: true, type: "Import::AccountMapping"
@import.mappings.create! key: "TestAccount2", mappable: accounts(:depository), type: "Import::AccountMapping"
@import.mappings.create! key: "", mappable: accounts(:depository), type: "Import::AccountMapping"
@import.reload
assert_difference -> { Account::Entry.count } => 3,
-> { Account::Transaction.count } => 3,
-> { Tag.count } => 1,
-> { Category.count } => 1,
-> { Account.count } => 1 do
@import.publish
end
assert_equal "complete", @import.status
end
end