mirror of
https://github.com/Gnucash/gnucash.git
synced 2026-10-30 10:00:06 -05:00
PR #2187 made GncPrice.get_value() return a GncNumeric wrapper and clone() a fully wrapped GncPrice. The price example scripts still used the pre-5.15 raw _gnc_numeric idiom and broke -- reported as Bug 799796 (cloned prices silently kept the original value). Example scripts (bindings/python/example_scripts/): * quotes_historic.py -- reworked to the modern idiom; it now actually runs on 5.x (it had further latent breakage beyond the reported one): - Construct each price directly with GncPrice(book) + setters instead of cloning an existing price, so the script no longer needs a pre-existing price in the book. Look the pricing currency (USD) up from the commodity table rather than deriving it from a split. - Build the value with the double->GncNumeric constructor at GnuCash's standard price precision -- the pricing currency's SCU * COMMODITY_DENOM_MULT rounded HALF_UP (gnc-pricedb.h "Price policy") -- dropping the Fraction.from_float().limit_denominator(100000) dance and its import. - Set the source with the PriceSource enum -- set_source(PriceSource.FINANCE_QUOTE) (the enum added in #2317). The original set_source("Finance::Quotes::Historic") passed a string to what has been an enum setter since 2.6, which raises TypeError on modern bindings. - Open with Session(url), which defaults to SESSION_NORMAL_OPEN. The old Session(url, True, False, False) passed the deprecated ignore_lock=True, silently bypassing the book's lock file. * priceDB_test.py, price_database_example.py -- num/denom are methods now, so read v.num() / v.denom(). Prevent silent recurrence (gnucash_core.py): The write path failed *silently*, which is how this slipped through review and CI. num/denom are read-only accessor methods, so assigning to them merely shadows the method and changes nothing. Add a GncNumeric.__setattr__ that raises AttributeError on assignment to num/denom, pointing at the correct idiom GncNumeric(num, denom). Test (bindings/python/tests/test_numeric.py): New test_num_denom_read_only covering the guard. It needs no backend, so it runs in the existing python-bindings CTest on all platforms. The wiki page Stocks/get_prices, which the reporter copied from, is being updated separately.
116 lines
3.9 KiB
Python
116 lines
3.9 KiB
Python
from unittest import TestCase, main
|
|
|
|
from gnucash import GncNumeric, GNC_DENOM_AUTO, GNC_HOW_DENOM_FIXED, \
|
|
GNC_HOW_RND_NEVER, GNC_HOW_RND_FLOOR, GNC_HOW_RND_CEIL
|
|
|
|
class TestGncNumeric(TestCase):
|
|
def test_defaut(self):
|
|
num = GncNumeric()
|
|
self.assertEqual(str(num), "0/1")
|
|
self.assertEqual(num.num(), 0)
|
|
self.assertEqual(num.denom(), 1)
|
|
|
|
def test_from_num_denom(self):
|
|
num = GncNumeric(1, 2)
|
|
self.assertEqual(str(num), "1/2")
|
|
self.assertEqual(num.num(), 1)
|
|
self.assertEqual(num.denom(), 2)
|
|
|
|
def test_from_int(self):
|
|
num = GncNumeric(3)
|
|
self.assertEqual(str(num), "3/1")
|
|
self.assertEqual(num.num(), 3)
|
|
self.assertEqual(num.denom(), 1)
|
|
|
|
with self.assertRaises(Exception) as context:
|
|
GncNumeric((2**64)+1)
|
|
|
|
#On Linux it raises an OverflowError while on MacOS it's a TypeError.
|
|
self.assertTrue(isinstance(context.exception, TypeError) or
|
|
isinstance(context.exception, OverflowError))
|
|
|
|
def test_from_float(self):
|
|
num = GncNumeric(3.1, 20, GNC_HOW_DENOM_FIXED | GNC_HOW_RND_NEVER)
|
|
self.assertEqual(str(num), "62/20")
|
|
self.assertEqual(num.num(), 62)
|
|
self.assertEqual(num.denom(), 20)
|
|
|
|
num = GncNumeric(1/3.0, 10000000000, GNC_HOW_RND_FLOOR)
|
|
self.assertEqual(str(num), "3333333333/10000000000")
|
|
self.assertEqual(num.num(), 3333333333)
|
|
self.assertEqual(num.denom(), 10000000000)
|
|
|
|
num = GncNumeric(1/3.0, 10000000000, GNC_HOW_RND_CEIL)
|
|
self.assertEqual(str(num), "3333333334/10000000000")
|
|
self.assertEqual(num.num(), 3333333334)
|
|
self.assertEqual(num.denom(), 10000000000)
|
|
|
|
def test_from_float_auto(self):
|
|
num = GncNumeric(3.1)
|
|
self.assertEqual(str(num), "31/10")
|
|
self.assertEqual(num.num(), 31)
|
|
self.assertEqual(num.denom(), 10)
|
|
|
|
def test_from_instance(self):
|
|
orig = GncNumeric(3)
|
|
num = GncNumeric(instance=orig.instance)
|
|
self.assertEqual(str(num), "3/1")
|
|
self.assertEqual(num.num(), 3)
|
|
self.assertEqual(num.denom(), 1)
|
|
|
|
def test_from_str(self):
|
|
num = GncNumeric("3.1")
|
|
self.assertEqual(str(num), "31/10")
|
|
self.assertEqual(num.num(), 31)
|
|
self.assertEqual(num.denom(), 10)
|
|
|
|
num = GncNumeric("1/3")
|
|
self.assertEqual(str(num), "1/3")
|
|
self.assertEqual(num.num(), 1)
|
|
self.assertEqual(num.denom(), 3)
|
|
|
|
def test_to_str(self):
|
|
num = GncNumeric("1000/3")
|
|
self.assertEqual(str(num), "1000/3")
|
|
|
|
num = GncNumeric(1, 0)
|
|
self.assertEqual(str(num), "1/0")
|
|
|
|
def test_to_double(self):
|
|
for test_num in [0.0, 1.1, -1.1, 1/3.0]:
|
|
self.assertEqual(GncNumeric(test_num).to_double(), test_num)
|
|
|
|
def test_to_fraction(self):
|
|
fraction = GncNumeric("1000/3").to_fraction()
|
|
self.assertEqual(fraction.numerator, 1000)
|
|
self.assertEqual(fraction.denominator, 3)
|
|
|
|
def test_incorect_args(self):
|
|
with self.assertRaises(TypeError):
|
|
GncNumeric(1, 2, 3)
|
|
|
|
with self.assertRaises(TypeError):
|
|
GncNumeric("1", 2)
|
|
|
|
with self.assertRaises(TypeError):
|
|
GncNumeric(1.1, "round")
|
|
|
|
with self.assertRaises(TypeError):
|
|
GncNumeric(complex(1, 1))
|
|
|
|
def test_num_denom_read_only(self):
|
|
# num/denom are read-only accessor methods, not writable fields.
|
|
# Assigning to them used to silently do nothing and corrupt data
|
|
# (Bug 799796); it must now raise instead.
|
|
num = GncNumeric(1, 2)
|
|
with self.assertRaises(AttributeError):
|
|
num.num = 5
|
|
with self.assertRaises(AttributeError):
|
|
num.denom = 5
|
|
# The value is unchanged and the accessors still work.
|
|
self.assertEqual(num.num(), 1)
|
|
self.assertEqual(num.denom(), 2)
|
|
|
|
if __name__ == '__main__':
|
|
main()
|