Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix Python GncNumeric for non (int, int) pairs
At current the Python GncNumeric has issues with type conversion eg. * GncNumeric(1.3) = 1.00 * GncNumeric("1.3") is OK but any future methods error This behaviour was relied on for the Account tests to pass as it used GncNumeric(0.5) == GncNumeric(1.0) but this is not what many users would expect. This fix alows GncNumeric to be constructed from a (int, int) numerator/denominator pair or int/float/str where double_to_gnc_numeric and string_to_gnc_numeric from C is used.
- Loading branch information
1 parent
5aa048e
commit 1ef379a
Showing
4 changed files
with
149 additions
and
20 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
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) | ||
|
||
# Safest outcome at current. This can be fixed but correct bounds checks | ||
# are required to ensure gint64 is not overflowed. | ||
def test_from_long(self): | ||
with self.assertRaises(TypeError): | ||
GncNumeric(3L) | ||
|
||
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_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)) | ||
|
||
if __name__ == '__main__': | ||
main() |