1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-22 22:59: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:
Zach Gollwitzer 2024-10-01 10:47:59 -04:00 committed by GitHub
parent 23786b444a
commit 398b246965
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
103 changed files with 2420 additions and 1689 deletions

View file

@ -1,20 +1,13 @@
require "test_helper"
class ImportsControllerTest < ActionDispatch::IntegrationTest
include ImportTestHelper
setup do
sign_in @user = users(:family_admin)
@empty_import = imports(:empty_import)
@loaded_import = @empty_import.dup
@loaded_import.update! raw_file_str: valid_csv_str
@completed_import = imports(:completed_import)
end
test "should get index" do
test "gets index" do
get imports_url
assert_response :success
@user.family.imports.ordered.each do |import|
@ -22,152 +15,44 @@ class ImportsControllerTest < ActionDispatch::IntegrationTest
end
end
test "should get new" do
test "gets new" do
get new_import_url
assert_response :success
assert_select "turbo-frame#modal"
end
test "should create import" do
assert_difference("Import.count") do
post imports_url, params: { import: { account_id: @user.family.accounts.first.id, col_sep: "," } }
end
assert_redirected_to load_import_path(Import.ordered.first)
end
test "should get edit" do
get edit_import_url(@empty_import)
assert_response :success
end
test "should update import" do
patch import_url(@empty_import), params: { import: { account_id: @empty_import.account_id, col_sep: "," } }
assert_redirected_to load_import_path(@empty_import)
end
test "should destroy import" do
assert_difference("Import.count", -1) do
delete import_url(@empty_import)
end
assert_redirected_to imports_url
end
test "should get load" do
get load_import_url(@empty_import)
assert_response :success
end
test "should save raw CSV if valid" do
patch load_import_url(@empty_import), params: { import: { raw_file_str: valid_csv_str } }
assert_redirected_to configure_import_path(@empty_import)
assert_equal "Import CSV loaded", flash[:notice]
end
test "should upload CSV file if valid" do
Tempfile.open([ "transactions.csv", ".csv" ]) do |temp|
CSV.open(temp, "wb", headers: true) do |csv|
valid_csv_str.split("\n").each { |row| csv << row.split(",") }
end
patch upload_import_url(@empty_import), params: { import: { raw_file_str: Rack::Test::UploadedFile.new(temp, ".csv") } }
assert_redirected_to configure_import_path(@empty_import)
assert_equal "CSV File loaded", flash[:notice]
end
end
test "should flash error message if invalid CSV input" do
patch load_import_url(@empty_import), params: { import: { raw_file_str: malformed_csv_str } }
assert_response :unprocessable_entity
assert_equal "Raw file str is not a valid CSV format", flash[:alert]
end
test "should flash error message if invalid CSV file upload" do
Tempfile.open([ "transactions.csv", ".csv" ]) do |temp|
temp.write(malformed_csv_str)
temp.rewind
patch upload_import_url(@empty_import), params: { import: { raw_file_str: Rack::Test::UploadedFile.new(temp, ".csv") } }
assert_response :unprocessable_entity
assert_equal "Raw file str is not a valid CSV format", flash[:alert]
end
end
test "should flash error message if no fileprovided for upload" do
patch upload_import_url(@empty_import), params: { import: { raw_file_str: nil } }
assert_response :unprocessable_entity
assert_equal "Please select a file to upload", flash[:alert]
end
test "should get configure" do
get configure_import_url(@loaded_import)
assert_response :success
end
test "should redirect back to load step with an alert message if not loaded" do
get configure_import_url(@empty_import)
assert_equal "Please load a CSV first", flash[:alert]
assert_redirected_to load_import_path(@empty_import)
end
test "should update mappings" do
patch configure_import_url(@loaded_import), params: {
import: {
column_mappings: {
date: "date",
name: "name",
category: "category",
amount: "amount"
test "creates import" do
assert_difference "Import.count", 1 do
post imports_url, params: {
import: {
type: "TransactionImport"
}
}
}
end
assert_redirected_to clean_import_path(@loaded_import)
assert_equal "Column mappings saved", flash[:notice]
assert_redirected_to import_upload_url(Import.all.ordered.first)
end
test "can update a cell" do
assert_equal @loaded_import.csv.table[0][1], "Starbucks drink"
test "publishes import" do
import = imports(:transaction)
patch clean_import_url(@loaded_import), params: {
import: {
csv_update: {
row_idx: 0,
col_idx: 1,
value: "new_merchant"
}
}
}
TransactionImport.any_instance.expects(:publish_later).once
assert_response :success
post publish_import_url(import)
@loaded_import.reload
assert_equal "new_merchant", @loaded_import.csv.table[0][1]
assert_equal "Your import has started in the background.", flash[:notice]
assert_redirected_to import_path(import)
end
test "should get clean" do
get clean_import_url(@loaded_import)
assert_response :success
end
test "destroys import" do
import = imports(:transaction)
test "should get confirm if all values are valid" do
get confirm_import_url(@loaded_import)
assert_response :success
end
assert_difference "Import.count", -1 do
delete import_url(import)
end
test "should redirect back to clean if data is invalid" do
@empty_import.update! raw_file_str: valid_csv_with_invalid_values
get confirm_import_url(@empty_import)
assert_equal "You have invalid data, please fix before continuing", flash[:alert]
assert_redirected_to clean_import_path(@empty_import)
end
test "should confirm import" do
patch confirm_import_url(@loaded_import)
assert_redirected_to imports_path
assert_equal "Import has started in the background", flash[:notice]
end
end