mirror of
https://github.com/maybe-finance/maybe.git
synced 2025-07-24 07:39:39 +02:00
CSV Imports Overhaul (Transactions, Trades, Accounts, and Mint import support) (#1209)
* Remove stale 1.0 import logic and model * Fresh start * Checkpoint before removing nav * First working prototype * Add trade, account, and mint import flows * Basic working version with tests * System tests for each import type * Clean up mappings flow * Clean up PR, refactor stale code, tests * Add back row validations * Row validations * Fix import job test * Fix import navigation * Fix mint import configuration form * Currency preset for new accounts
This commit is contained in:
parent
23786b444a
commit
398b246965
103 changed files with 2420 additions and 1689 deletions
25
test/controllers/import/cleans_controller_test.rb
Normal file
25
test/controllers/import/cleans_controller_test.rb
Normal file
|
@ -0,0 +1,25 @@
|
|||
require "test_helper"
|
||||
|
||||
class Import::CleansControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
end
|
||||
|
||||
test "shows if configured" do
|
||||
import = imports(:transaction)
|
||||
|
||||
TransactionImport.any_instance.stubs(:configured?).returns(true)
|
||||
|
||||
get import_clean_path(import)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "redirects if not configured" do
|
||||
import = imports(:transaction)
|
||||
|
||||
TransactionImport.any_instance.stubs(:configured?).returns(false)
|
||||
|
||||
get import_clean_path(import)
|
||||
assert_redirected_to import_configuration_path(import)
|
||||
end
|
||||
end
|
33
test/controllers/import/configurations_controller_test.rb
Normal file
33
test/controllers/import/configurations_controller_test.rb
Normal file
|
@ -0,0 +1,33 @@
|
|||
require "test_helper"
|
||||
|
||||
class Import::ConfigurationsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
@import = imports(:transaction)
|
||||
end
|
||||
|
||||
test "show" do
|
||||
get import_configuration_url(@import)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "updating a valid configuration regenerates rows" do
|
||||
TransactionImport.any_instance.expects(:generate_rows_from_csv).once
|
||||
|
||||
patch import_configuration_url(@import), params: {
|
||||
import: {
|
||||
date_col_label: "Date",
|
||||
date_format: "%Y-%m-%d",
|
||||
name_col_label: "Name",
|
||||
category_col_label: "Category",
|
||||
tags_col_label: "Tags",
|
||||
amount_col_label: "Amount",
|
||||
signage_convention: "inflows_positive",
|
||||
account_col_label: "Account"
|
||||
}
|
||||
}
|
||||
|
||||
assert_redirected_to import_clean_url(@import)
|
||||
assert_equal "Import configured successfully.", flash[:notice]
|
||||
end
|
||||
end
|
26
test/controllers/import/confirms_controller_test.rb
Normal file
26
test/controllers/import/confirms_controller_test.rb
Normal file
|
@ -0,0 +1,26 @@
|
|||
require "test_helper"
|
||||
|
||||
class Import::ConfirmsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
end
|
||||
|
||||
test "shows if cleaned" do
|
||||
import = imports(:transaction)
|
||||
|
||||
TransactionImport.any_instance.stubs(:cleaned?).returns(true)
|
||||
|
||||
get import_confirm_path(import)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "redirects if not cleaned" do
|
||||
import = imports(:transaction)
|
||||
|
||||
TransactionImport.any_instance.stubs(:cleaned?).returns(false)
|
||||
|
||||
get import_confirm_path(import)
|
||||
assert_redirected_to import_clean_path(import)
|
||||
assert_equal "You have invalid data, please edit until all errors are resolved", flash[:alert]
|
||||
end
|
||||
end
|
29
test/controllers/import/mappings_controller_test.rb
Normal file
29
test/controllers/import/mappings_controller_test.rb
Normal file
|
@ -0,0 +1,29 @@
|
|||
require "test_helper"
|
||||
|
||||
class Import::MappingsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
|
||||
@import = imports(:transaction)
|
||||
end
|
||||
|
||||
test "updates mapping" do
|
||||
mapping = import_mappings(:one)
|
||||
new_category = categories(:income)
|
||||
|
||||
patch import_mapping_path(@import, mapping), params: {
|
||||
import_mapping: {
|
||||
mappable_type: "Category",
|
||||
mappable_id: new_category.id,
|
||||
key: "Food"
|
||||
}
|
||||
}
|
||||
|
||||
mapping.reload
|
||||
|
||||
assert_equal new_category, mapping.mappable
|
||||
assert_equal "Food", mapping.key
|
||||
|
||||
assert_redirected_to import_confirm_path(@import)
|
||||
end
|
||||
end
|
79
test/controllers/import/rows_controller_test.rb
Normal file
79
test/controllers/import/rows_controller_test.rb
Normal file
|
@ -0,0 +1,79 @@
|
|||
require "test_helper"
|
||||
|
||||
class Import::RowsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
|
||||
@import = imports(:transaction)
|
||||
@row = import_rows(:one)
|
||||
end
|
||||
|
||||
test "show transaction row" do
|
||||
get import_row_path(@import, @row)
|
||||
|
||||
assert_row_fields(@row, [ :date, :name, :amount, :currency, :category, :tags, :account, :notes ])
|
||||
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "show trade row" do
|
||||
import = @user.family.imports.create!(type: "TradeImport")
|
||||
row = import.rows.create!(date: "01/01/2024", currency: "USD", qty: 10, price: 100, ticker: "AAPL")
|
||||
|
||||
get import_row_path(import, row)
|
||||
|
||||
assert_row_fields(row, [ :date, :ticker, :qty, :price, :currency, :account, :name ])
|
||||
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "show account row" do
|
||||
import = @user.family.imports.create!(type: "AccountImport")
|
||||
row = import.rows.create!(name: "Test Account", amount: 10000, currency: "USD")
|
||||
|
||||
get import_row_path(import, row)
|
||||
|
||||
assert_row_fields(row, [ :entity_type, :name, :amount, :currency ])
|
||||
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "show mint row" do
|
||||
import = @user.family.imports.create!(type: "MintImport")
|
||||
row = import.rows.create!(date: "01/01/2024", amount: 100, currency: "USD")
|
||||
|
||||
get import_row_path(import, row)
|
||||
|
||||
assert_row_fields(row, [ :date, :name, :amount, :currency, :category, :tags, :account, :notes ])
|
||||
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "update" do
|
||||
patch import_row_path(@import, @row), params: {
|
||||
import_row: {
|
||||
account: "Checking Account",
|
||||
date: "2024-01-01",
|
||||
qty: nil,
|
||||
ticker: nil,
|
||||
price: nil,
|
||||
amount: 100,
|
||||
currency: "USD",
|
||||
name: "Test",
|
||||
category: "Food",
|
||||
tags: "grocery, dinner",
|
||||
entity_type: nil,
|
||||
notes: "Weekly shopping"
|
||||
}
|
||||
}
|
||||
|
||||
assert_redirected_to import_row_path(@import, @row)
|
||||
end
|
||||
|
||||
private
|
||||
def assert_row_fields(row, fields)
|
||||
fields.each do |field|
|
||||
assert_select "turbo-frame##{dom_id(row, field)}"
|
||||
end
|
||||
end
|
||||
end
|
46
test/controllers/import/uploads_controller_test.rb
Normal file
46
test/controllers/import/uploads_controller_test.rb
Normal file
|
@ -0,0 +1,46 @@
|
|||
require "test_helper"
|
||||
|
||||
class Import::UploadsControllerTest < ActionDispatch::IntegrationTest
|
||||
setup do
|
||||
sign_in @user = users(:family_admin)
|
||||
@import = imports(:transaction)
|
||||
end
|
||||
|
||||
test "show" do
|
||||
get import_upload_url(@import)
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
test "uploads valid csv by copy and pasting" do
|
||||
patch import_upload_url(@import), params: {
|
||||
import: {
|
||||
raw_file_str: file_fixture("imports/valid.csv").read
|
||||
}
|
||||
}
|
||||
|
||||
assert_redirected_to import_configuration_url(@import)
|
||||
assert_equal "CSV uploaded successfully.", flash[:notice]
|
||||
end
|
||||
|
||||
test "uploads valid csv by file" do
|
||||
patch import_upload_url(@import), params: {
|
||||
import: {
|
||||
csv_file: file_fixture_upload("imports/valid.csv")
|
||||
}
|
||||
}
|
||||
|
||||
assert_redirected_to import_configuration_url(@import)
|
||||
assert_equal "CSV uploaded successfully.", flash[:notice]
|
||||
end
|
||||
|
||||
test "invalid csv cannot be uploaded" do
|
||||
patch import_upload_url(@import), params: {
|
||||
import: {
|
||||
csv_file: file_fixture_upload("imports/invalid.csv")
|
||||
}
|
||||
}
|
||||
|
||||
assert_response :unprocessable_entity
|
||||
assert_equal "Must be valid CSV with headers and at least one row of data", flash[:alert]
|
||||
end
|
||||
end
|
Loading…
Add table
Add a link
Reference in a new issue