The personal finance app for everyone (by everyone)
Ruby
fix(exports): include merchants.csv in family data export (#2758)
kai39273aac31f896d
diff --git a/app/models/family/data_exporter.rb b/app/models/family/data_exporter.rbindex 20e08a70d9..9765eae2e5 100644--- a/app/models/family/data_exporter.rb+++ b/app/models/family/data_exporter.rb@@ -31,6 +31,10 @@ class Family::DataExporterzipfile.put_next_entry("categories.csv")zipfile.write generate_categories_csv+ # Add merchants.csv+ zipfile.put_next_entry("merchants.csv")+ zipfile.write generate_merchants_csv+# Add rules.csvzipfile.put_next_entry("rules.csv")zipfile.write generate_rules_csv@@ -139,6 +143,22 @@ class Family::DataExporterendend+ def generate_merchants_csv+ CSV.generate do |csv|+ # Headers match MerchantImport's expected columns so the export round-trips+ csv << [ "name", "color", "website_url" ]++ # Only export family merchants belonging to this family+ @family.merchants.find_each do |merchant|+ csv << [+ merchant.name,+ merchant.color,+ merchant.website_url+ ]+ end+ end+ end+def generate_rules_csvCSV.generate do |csv|csv << [ "name", "resource_type", "active", "effective_date", "conditions", "actions" ]diff --git a/test/models/family/data_exporter_test.rb b/test/models/family/data_exporter_test.rbindex 00d4759d78..cc981e3a41 100644--- a/test/models/family/data_exporter_test.rb+++ b/test/models/family/data_exporter_test.rb@@ -55,7 +55,7 @@ class Family::DataExporterTest < ActiveSupport::TestCaseassert zip_data.is_a?(StringIO)# Check that the zip contains all expected files- expected_files = [ "version.txt", "accounts.csv", "transactions.csv", "trades.csv", "categories.csv", "rules.csv", "attachments.json", "all.ndjson" ]+ expected_files = [ "version.txt", "accounts.csv", "transactions.csv", "trades.csv", "categories.csv", "merchants.csv", "rules.csv", "attachments.json", "all.ndjson" ]Zip::File.open_buffer(zip_data) do |zip|actual_files = zip.entries.map(&:name)@@ -194,12 +194,62 @@ class Family::DataExporterTest < ActiveSupport::TestCasecategories_csv = zip.read("categories.csv")assert categories_csv.include?("name,color,parent_category,lucide_icon")+ # Check merchants.csv+ merchants_csv = zip.read("merchants.csv")+ assert merchants_csv.include?("name,color,website_url")+# Check rules.csvrules_csv = zip.read("rules.csv")assert rules_csv.include?("name,resource_type,active,effective_date,conditions,actions")endend+ test "exports merchants in CSV format scoped to the family" do+ merchant = @family.merchants.create!(name: "Coffee Shop", website_url: "https://coffeeshop.com")+ other_merchant = @other_family.merchants.create!(name: "Other Family Store")++ zip_data = @exporter.generate_export++ Zip::File.open_buffer(zip_data) do |zip|+ rows = CSV.parse(zip.read("merchants.csv"), headers: true)+ row = rows.find { |csv_row| csv_row["name"] == merchant.name }++ assert_not_nil row+ assert_equal merchant.color, row["color"]+ assert_equal merchant.website_url, row["website_url"]+ refute rows.any? { |csv_row| csv_row["name"] == other_merchant.name }+ end+ end++ test "exported merchants CSV round-trips through MerchantImport" do+ merchant = @family.merchants.create!(name: "Pizza Palace", website_url: "https://pizzapalace.com")++ zip_data = @exporter.generate_export++ Zip::File.open_buffer(zip_data) do |zip|+ import = @other_family.imports.create!(+ type: "MerchantImport",+ raw_file_str: zip.read("merchants.csv"),+ col_sep: ","+ )+ import.generate_rows_from_csv+ row = import.rows.reload.find_by!(name: "Pizza Palace")++ assert_equal merchant.color, row.merchant_color+ assert_equal "https://pizzapalace.com", row.merchant_website++ # The export carries every family merchant (incl. fixtures), so scope the+ # count assertion to the merchant under test.+ assert_difference -> { @other_family.merchants.where(name: "Pizza Palace").count }, 1 do+ import.send(:import!)+ end++ imported = @other_family.merchants.find_by!(name: "Pizza Palace")+ assert_equal merchant.color, imported.color+ assert_equal "https://pizzapalace.com", imported.website_url+ end+ end+test "exports transaction CSV rows with restored legacy ISO date stored amount account name and comma tags" dotag2 = @family.tags.create!(name: "Food, Dining|Cafe", color: "#0000FF")entry = @account.entries.create!(