Files
sure/test/models/trade_test.rb
Michael Studman ea7ce13a7d Increasing trades.price decimal scale (#89)
* Changing trades.price to have a larger scale - a scale of 4 causes destructive rounding when calculating transaction cost; changes to the UI to allow for inputting and showing increased scale trade prices; test case
2025-10-21 18:22:24 +02:00

58 lines
1.6 KiB
Ruby

require "test_helper"
class TradeTest < ActiveSupport::TestCase
test "build_name generates buy trade name" do
name = Trade.build_name("buy", 10, "AAPL")
assert_equal "Buy 10.0 shares of AAPL", name
end
test "build_name generates sell trade name" do
name = Trade.build_name("sell", 5, "MSFT")
assert_equal "Sell 5.0 shares of MSFT", name
end
test "build_name handles absolute value for negative quantities" do
name = Trade.build_name("sell", -5, "GOOGL")
assert_equal "Sell 5.0 shares of GOOGL", name
end
test "build_name handles decimal quantities" do
name = Trade.build_name("buy", 0.25, "BTC")
assert_equal "Buy 0.25 shares of BTC", name
end
test "price scale is preserved at 10 decimal places" do
security = Security.create!(ticker: "TEST", exchange_operating_mic: "XNAS")
# up to 10 decimal places — should persist exactly
precise_price = BigDecimal("12.3456789012")
trade = Trade.create!(
security: security,
price: precise_price,
qty: 10000,
currency: "USD"
)
trade.reload
assert_equal precise_price, trade.price
end
test "price is rounded to 10 decimal places" do
security = Security.create!(ticker: "TEST", exchange_operating_mic: "XNAS")
# over 10 decimal places — will be rounded
price_with_too_many_decimals = BigDecimal("1.123456789012345")
trade = Trade.create!(
security: security,
price: price_with_too_many_decimals,
qty: 1,
currency: "USD"
)
trade.reload
assert_equal BigDecimal("1.1234567890"), trade.price
end
end