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

Fix timezones in tests

This commit is contained in:
Zach Gollwitzer 2025-05-14 21:19:53 -04:00
parent 246343a4bf
commit 7d32423d8e
4 changed files with 47 additions and 47 deletions

View file

@ -15,19 +15,19 @@ class Balance::ForwardCalculatorTest < ActiveSupport::TestCase
test "balance generation respects user timezone and last generated date is current user date" do test "balance generation respects user timezone and last generated date is current user date" do
# Simulate user in EST timezone # Simulate user in EST timezone
Time.zone = "America/New_York" Time.use_zone("America/New_York") do
# Set current time to 1am UTC on Jan 5, 2025
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate balances for)
travel_to Time.utc(2025, 01, 05, 1, 0, 0)
# Set current time to 1am UTC on Jan 5, 2025 # Create a valuation for Jan 3, 2025
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate balances for) create_valuation(account: @account, date: "2025-01-03", amount: 17000)
travel_to Time.utc(2025, 01, 05, 1, 0, 0)
# Create a valuation for Jan 3, 2025 expected = [ [ "2025-01-02", 0 ], [ "2025-01-03", 17000 ], [ "2025-01-04", 17000 ] ]
create_valuation(account: @account, date: "2025-01-03", amount: 17000) calculated = Balance::ForwardCalculator.new(@account).calculate
expected = [ [ "2025-01-02", 0 ], [ "2025-01-03", 17000 ], [ "2025-01-04", 17000 ] ] assert_equal expected, calculated.map { |b| [ b.date.to_s, b.balance ] }
calculated = Balance::ForwardCalculator.new(@account).calculate end
assert_equal expected, calculated.map { |b| [ b.date.to_s, b.balance ] }
end end
# When syncing forwards, we don't care about the account balance. We generate everything based on entries, starting from 0. # When syncing forwards, we don't care about the account balance. We generate everything based on entries, starting from 0.

View file

@ -25,18 +25,18 @@ class Balance::ReverseCalculatorTest < ActiveSupport::TestCase
test "balance generation respects user timezone and last generated date is current user date" do test "balance generation respects user timezone and last generated date is current user date" do
# Simulate user in EST timezone # Simulate user in EST timezone
Time.zone = "America/New_York" Time.use_zone("America/New_York") do
# Set current time to 1am UTC on Jan 5, 2025
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate balances for)
travel_to Time.utc(2025, 01, 05, 1, 0, 0)
# Set current time to 1am UTC on Jan 5, 2025 create_valuation(account: @account, date: "2025-01-03", amount: 17000)
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate balances for)
travel_to Time.utc(2025, 01, 05, 1, 0, 0)
create_valuation(account: @account, date: "2025-01-03", amount: 17000) expected = [ [ "2025-01-02", 17000 ], [ "2025-01-03", 17000 ], [ "2025-01-04", @account.balance ] ]
calculated = Balance::ReverseCalculator.new(@account).calculate
expected = [ [ "2025-01-02", 17000 ], [ "2025-01-03", 17000 ], [ "2025-01-04", @account.balance ] ] assert_equal expected, calculated.sort_by(&:date).map { |b| [ b.date.to_s, b.balance ] }
calculated = Balance::ReverseCalculator.new(@account).calculate end
assert_equal expected, calculated.sort_by(&:date).map { |b| [ b.date.to_s, b.balance ] }
end end
test "valuations sync" do test "valuations sync" do

View file

@ -20,22 +20,22 @@ class Holding::ForwardCalculatorTest < ActiveSupport::TestCase
test "holding generation respects user timezone and last generated date is current user date" do test "holding generation respects user timezone and last generated date is current user date" do
# Simulate user in EST timezone # Simulate user in EST timezone
Time.zone = "America/New_York" Time.use_zone("America/New_York") do
# Set current time to 1am UTC on Jan 5, 2025
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate holdings for)
travel_to Time.utc(2025, 01, 05, 1, 0, 0)
# Set current time to 1am UTC on Jan 5, 2025 voo = Security.create!(ticker: "VOO", name: "Vanguard S&P 500 ETF")
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate holdings for) Security::Price.create!(security: voo, date: "2025-01-02", price: 500)
travel_to Time.utc(2025, 01, 05, 1, 0, 0) Security::Price.create!(security: voo, date: "2025-01-03", price: 500)
Security::Price.create!(security: voo, date: "2025-01-04", price: 500)
create_trade(voo, qty: 10, date: "2025-01-03", price: 500, account: @account)
voo = Security.create!(ticker: "VOO", name: "Vanguard S&P 500 ETF") expected = [ [ "2025-01-02", 0 ], [ "2025-01-03", 5000 ], [ "2025-01-04", 5000 ] ]
Security::Price.create!(security: voo, date: "2025-01-02", price: 500) calculated = Holding::ForwardCalculator.new(@account).calculate
Security::Price.create!(security: voo, date: "2025-01-03", price: 500)
Security::Price.create!(security: voo, date: "2025-01-04", price: 500)
create_trade(voo, qty: 10, date: "2025-01-03", price: 500, account: @account)
expected = [ [ "2025-01-02", 0 ], [ "2025-01-03", 5000 ], [ "2025-01-04", 5000 ] ] assert_equal expected, calculated.map { |b| [ b.date.to_s, b.amount ] }
calculated = Holding::ForwardCalculator.new(@account).calculate end
assert_equal expected, calculated.map { |b| [ b.date.to_s, b.amount ] }
end end
test "forward portfolio calculation" do test "forward portfolio calculation" do

View file

@ -20,26 +20,26 @@ class Holding::ReverseCalculatorTest < ActiveSupport::TestCase
test "holding generation respects user timezone and last generated date is current user date" do test "holding generation respects user timezone and last generated date is current user date" do
# Simulate user in EST timezone # Simulate user in EST timezone
Time.zone = "America/New_York" Time.use_zone("America/New_York") do
# Set current time to 1am UTC on Jan 5, 2025
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate holdings for)
travel_to Time.utc(2025, 01, 05, 1, 0, 0)
# Set current time to 1am UTC on Jan 5, 2025 voo = Security.create!(ticker: "VOO", name: "Vanguard S&P 500 ETF")
# This would be 8pm EST on Jan 4, 2025 (user's time, and the last date we should generate holdings for) Security::Price.create!(security: voo, date: "2025-01-02", price: 500)
travel_to Time.utc(2025, 01, 05, 1, 0, 0) Security::Price.create!(security: voo, date: "2025-01-03", price: 500)
Security::Price.create!(security: voo, date: "2025-01-04", price: 500)
voo = Security.create!(ticker: "VOO", name: "Vanguard S&P 500 ETF") # Today's holdings (provided)
Security::Price.create!(security: voo, date: "2025-01-02", price: 500) @account.holdings.create!(security: voo, date: "2025-01-04", qty: 10, price: 500, amount: 5000, currency: "USD")
Security::Price.create!(security: voo, date: "2025-01-03", price: 500)
Security::Price.create!(security: voo, date: "2025-01-04", price: 500)
# Today's holdings (provided) create_trade(voo, qty: 10, date: "2025-01-03", price: 500, account: @account)
@account.holdings.create!(security: voo, date: "2025-01-04", qty: 10, price: 500, amount: 5000, currency: "USD")
create_trade(voo, qty: 10, date: "2025-01-03", price: 500, account: @account) expected = [ [ "2025-01-02", 0 ], [ "2025-01-03", 5000 ], [ "2025-01-04", 5000 ] ]
calculated = Holding::ReverseCalculator.new(@account).calculate
expected = [ [ "2025-01-02", 0 ], [ "2025-01-03", 5000 ], [ "2025-01-04", 5000 ] ] assert_equal expected, calculated.sort_by(&:date).map { |b| [ b.date.to_s, b.amount ] }
calculated = Holding::ReverseCalculator.new(@account).calculate end
assert_equal expected, calculated.sort_by(&:date).map { |b| [ b.date.to_s, b.amount ] }
end end
# Should be able to handle this case, although we should not be reverse-syncing an account without provided current day holdings # Should be able to handle this case, although we should not be reverse-syncing an account without provided current day holdings