1
0
Fork 0
mirror of https://github.com/maybe-finance/maybe.git synced 2025-07-19 13:19:39 +02:00

Add support for different column separator in csv import logic (#1096)

* add col_sep to import model

* add validation for col_sep column

* add col_sep option to csv import model

* make use of col_sep option in import model

* add column separator field to new/edit action of an import

* add col_sep parameter to create/update action

* fix spacing between fields

Co-authored-by: Zach Gollwitzer <zach.gollwitzer@gmail.com>
Signed-off-by: Alexander Schrot <alexander@axs-labs.com>

---------

Signed-off-by: Alexander Schrot <alexander@axs-labs.com>
Co-authored-by: Zach Gollwitzer <zach.gollwitzer@gmail.com>
This commit is contained in:
Alexander Schrot 2024-08-16 20:00:16 +02:00 committed by GitHub
parent 707c5ca0ca
commit 4527482aa2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
11 changed files with 117 additions and 19 deletions

View file

@ -36,6 +36,14 @@ class Import::CsvTest < ActiveSupport::TestCase
assert_not invalid_csv.valid?
end
test "CSV with semicolon column separator" do
csv = Import::Csv.new(valid_csv_str_with_semicolon_separator, col_sep: ";")
assert_equal %w[ date name category tags amount ], csv.table.headers
assert_equal 4, csv.table.size
assert_equal "Paycheck", csv.table[3][1]
end
test "csv with additional columns and empty values" do
csv = Import::Csv.new valid_csv_with_missing_data
assert csv.valid?
@ -81,6 +89,35 @@ class Import::CsvTest < ActiveSupport::TestCase
assert_equal "Amazon stuff", csv.table[1][1]
end
test "can create CSV with expected columns, field mappings with validators and semicolon column separator" do
date_field = Import::Field.new \
key: "date",
label: "Date",
validator: method(:validate_iso_date)
name_field = Import::Field.new \
key: "name",
label: "Name"
fields = [ date_field, name_field ]
raw_csv_str = <<-ROWS
date;Custom Field Header;extra_field
invalid_date_value;Starbucks drink;Food
2024-01-02;Amazon stuff;Shopping
ROWS
mappings = {
"name" => "Custom Field Header"
}
csv = Import::Csv.create_with_field_mappings(raw_csv_str, fields, mappings, ";")
assert_equal %w[ date name ], csv.table.headers
assert_equal 2, csv.table.size
assert_equal "Amazon stuff", csv.table[1][1]
end
private
def validate_iso_date(value)