mirror of
https://github.com/we-promise/sure.git
synced 2026-05-08 05:04:59 +00:00
fix(imports): preserve Sure opening balance history (#1595)
This commit is contained in:
@@ -310,6 +310,34 @@ class Family::DataExporterTest < ActiveSupport::TestCase
|
||||
end
|
||||
end
|
||||
|
||||
test "exports valuation kind in NDJSON" do
|
||||
valuation_entry = @account.entries.create!(
|
||||
date: Date.parse("2020-04-01"),
|
||||
amount: 1000,
|
||||
name: "Opening balance",
|
||||
currency: "USD",
|
||||
entryable: Valuation.new(kind: "opening_anchor")
|
||||
)
|
||||
|
||||
zip_data = @exporter.generate_export
|
||||
|
||||
Zip::File.open_buffer(zip_data) do |zip|
|
||||
ndjson_content = zip.read("all.ndjson")
|
||||
valuation_lines = ndjson_content.split("\n").select do |line|
|
||||
JSON.parse(line)["type"] == "Valuation"
|
||||
end
|
||||
|
||||
assert valuation_lines.any?
|
||||
|
||||
valuation_data = valuation_lines
|
||||
.map { |line| JSON.parse(line) }
|
||||
.find { |line| line.dig("data", "entry_id") == valuation_entry.id }
|
||||
|
||||
assert valuation_data
|
||||
assert_equal "opening_anchor", valuation_data["data"]["kind"]
|
||||
end
|
||||
end
|
||||
|
||||
test "only exports rules from the specified family" do
|
||||
# Create a rule for another family that should NOT be exported
|
||||
other_rule = @other_family.rules.build(
|
||||
|
||||
@@ -77,6 +77,111 @@ class Family::DataImporterTest < ActiveSupport::TestCase
|
||||
assert_equal "active", account.status
|
||||
end
|
||||
|
||||
test "dates synthesized account opening balance before oldest imported activity" do
|
||||
ndjson = build_ndjson([
|
||||
{
|
||||
type: "Account",
|
||||
data: {
|
||||
id: "acct-1",
|
||||
name: "Main Account",
|
||||
balance: "5000",
|
||||
currency: "USD",
|
||||
accountable_type: "Depository"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "Transaction",
|
||||
data: {
|
||||
id: "txn-1",
|
||||
account_id: "acct-1",
|
||||
date: "2020-04-02",
|
||||
amount: "-50.00",
|
||||
name: "Grocery Store",
|
||||
currency: "USD"
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
Family::DataImporter.new(@family, ndjson).import!
|
||||
|
||||
account = @family.accounts.find_by!(name: "Main Account")
|
||||
opening_anchor = account.valuations.opening_anchor.first
|
||||
|
||||
assert_not_nil opening_anchor
|
||||
assert_equal Date.parse("2020-04-01"), opening_anchor.entry.date
|
||||
end
|
||||
|
||||
test "clamps explicit account opening balance dates before imported activity" do
|
||||
ndjson = build_ndjson([
|
||||
{
|
||||
type: "Account",
|
||||
data: {
|
||||
id: "acct-1",
|
||||
name: "Main Account",
|
||||
balance: "5000",
|
||||
currency: "USD",
|
||||
accountable_type: "Depository",
|
||||
opening_balance_date: "2020-04-02"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "Transaction",
|
||||
data: {
|
||||
id: "txn-1",
|
||||
account_id: "acct-1",
|
||||
date: "2020-04-02",
|
||||
amount: "-50.00",
|
||||
name: "Grocery Store",
|
||||
currency: "USD"
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
Family::DataImporter.new(@family, ndjson).import!
|
||||
|
||||
account = @family.accounts.find_by!(name: "Main Account")
|
||||
opening_anchor = account.valuations.opening_anchor.first
|
||||
|
||||
assert_not_nil opening_anchor
|
||||
assert_equal Date.parse("2020-04-01"), opening_anchor.entry.date
|
||||
end
|
||||
|
||||
test "imports explicit opening anchor valuations without synthesizing duplicates" do
|
||||
ndjson = build_ndjson([
|
||||
{
|
||||
type: "Account",
|
||||
data: {
|
||||
id: "acct-1",
|
||||
name: "Main Account",
|
||||
balance: "5000",
|
||||
currency: "USD",
|
||||
accountable_type: "Depository"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "Valuation",
|
||||
data: {
|
||||
id: "val-opening",
|
||||
account_id: "acct-1",
|
||||
date: "2020-04-01",
|
||||
amount: "5000",
|
||||
name: "Opening balance",
|
||||
currency: "USD",
|
||||
kind: "opening_anchor"
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
Family::DataImporter.new(@family, ndjson).import!
|
||||
|
||||
account = @family.accounts.find_by!(name: "Main Account")
|
||||
opening_anchors = account.valuations.opening_anchor.to_a
|
||||
|
||||
assert_equal 1, opening_anchors.count
|
||||
assert_equal Date.parse("2020-04-01"), opening_anchors.first.entry.date
|
||||
assert_equal 5000.0, opening_anchors.first.entry.amount.to_f
|
||||
end
|
||||
|
||||
test "imports categories with parent relationships" do
|
||||
ndjson = build_ndjson([
|
||||
{
|
||||
@@ -284,6 +389,39 @@ class Family::DataImporterTest < ActiveSupport::TestCase
|
||||
assert_equal 520000.0, valuation.entry.amount.to_f
|
||||
end
|
||||
|
||||
test "imports unknown valuation kinds as reconciliations" do
|
||||
ndjson = build_ndjson([
|
||||
{
|
||||
type: "Account",
|
||||
data: {
|
||||
id: "prop-acct-1",
|
||||
name: "Property",
|
||||
balance: "500000",
|
||||
currency: "USD",
|
||||
accountable_type: "Property"
|
||||
}
|
||||
},
|
||||
{
|
||||
type: "Valuation",
|
||||
data: {
|
||||
id: "val-1",
|
||||
account_id: "prop-acct-1",
|
||||
date: "2024-06-15",
|
||||
amount: "520000",
|
||||
name: "Updated valuation",
|
||||
currency: "USD",
|
||||
kind: "legacy_unknown_kind"
|
||||
}
|
||||
}
|
||||
])
|
||||
|
||||
Family::DataImporter.new(@family, ndjson).import!
|
||||
|
||||
account = @family.accounts.find_by!(name: "Property")
|
||||
valuation = account.valuations.joins(:entry).find_by!(entries: { name: "Updated valuation" })
|
||||
assert_equal "reconciliation", valuation.kind
|
||||
end
|
||||
|
||||
test "imports budgets" do
|
||||
ndjson = build_ndjson([
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user