mirror of
https://github.com/we-promise/sure.git
synced 2026-06-08 20:29:05 +00:00
perf(transactions): preload new form options (#2189)
* perf(transactions): preload new form options * refactor(transactions): reuse new form account scope
This commit is contained in:
@@ -462,6 +462,56 @@ end
|
||||
end
|
||||
end
|
||||
|
||||
test "new preloads transaction form option data" do
|
||||
family = families(:empty)
|
||||
user = users(:empty)
|
||||
sign_in user
|
||||
|
||||
manual_account_ids = []
|
||||
4.times do |idx|
|
||||
account = family.accounts.create!(
|
||||
name: "Manual Account #{idx}",
|
||||
balance: 0,
|
||||
currency: "USD",
|
||||
accountable: Depository.new
|
||||
)
|
||||
assert Account.manual.active.exists?(id: account.id), "Account should be included in the manual active scope"
|
||||
manual_account_ids << account.id
|
||||
family.categories.create!(
|
||||
name: "Category #{idx}",
|
||||
color: "#000000",
|
||||
lucide_icon: "shapes"
|
||||
)
|
||||
family.merchants.create!(name: "Merchant #{idx}")
|
||||
family.tags.create!(name: "Tag #{idx}")
|
||||
end
|
||||
|
||||
inaccessible_account = families(:dylan_family).accounts.create!(
|
||||
name: "Other Family Account",
|
||||
balance: 0,
|
||||
currency: "EUR",
|
||||
accountable: Depository.new
|
||||
)
|
||||
|
||||
queries = capture_sql_queries { get new_transaction_url }
|
||||
|
||||
assert_response :success
|
||||
assert_select "input[name='entry[account_id]']"
|
||||
assert_select "input[name='entry[entryable_attributes][category_id]']"
|
||||
assert_select "input[name='entry[entryable_attributes][merchant_id]']"
|
||||
assert_select "form[data-transaction-form-account-currencies-value]" do |forms|
|
||||
account_currencies = JSON.parse(forms.first["data-transaction-form-account-currencies-value"])
|
||||
manual_account_ids.each do |account_id|
|
||||
assert_equal "USD", account_currencies[account_id.to_s]
|
||||
end
|
||||
assert_nil account_currencies[inaccessible_account.id.to_s]
|
||||
end
|
||||
|
||||
assert_empty queries.grep(/FROM "account_providers" WHERE "account_providers"\."account_id" =/)
|
||||
assert_operator queries.grep(/FROM "active_storage_attachments" WHERE "active_storage_attachments"\."record_id" =/).size, :<=, 1
|
||||
assert_operator queries.grep(/SELECT "categories"\.\* FROM "categories" WHERE "categories"\."family_id" =/).size, :<=, 1
|
||||
end
|
||||
|
||||
test "unlock clears import_locked flag" do
|
||||
family = families(:empty)
|
||||
sign_in users(:empty)
|
||||
@@ -634,4 +684,21 @@ end
|
||||
created_entry = Entry.order(:created_at).last
|
||||
assert_nil created_entry.transaction.extra["exchange_rate"]
|
||||
end
|
||||
|
||||
private
|
||||
def capture_sql_queries
|
||||
queries = []
|
||||
callback = lambda do |_name, _started, _finished, _unique_id, payload|
|
||||
next if payload[:cached]
|
||||
next if %w[SCHEMA TRANSACTION].include?(payload[:name])
|
||||
|
||||
queries << payload[:sql].squish
|
||||
end
|
||||
|
||||
ActiveSupport::Notifications.subscribed(callback, "sql.active_record") do
|
||||
yield
|
||||
end
|
||||
|
||||
queries
|
||||
end
|
||||
end
|
||||
|
||||
Reference in New Issue
Block a user