Skip to content

Commit a8b5ed4

Browse files
authored
Merge pull request #364 from laf-rge/master
handle Decimal on amount values
2 parents 8d38c63 + c61efc8 commit a8b5ed4

File tree

3 files changed

+27
-6
lines changed

3 files changed

+27
-6
lines changed

.github/workflows/python-package.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ jobs:
1616
strategy:
1717
fail-fast: false
1818
matrix:
19-
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
19+
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]
2020

2121
steps:
2222
- uses: actions/checkout@v3

quickbooks/mixins.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,10 @@
77
from .utils import build_choose_clause, build_where_clause
88

99
class DecimalEncoder(json.JSONEncoder):
10-
def default(self, obj):
11-
if isinstance(obj, decimal.Decimal):
12-
return str(obj)
13-
return super(DecimalEncoder, self).default(obj)
10+
def default(self, o):
11+
if isinstance(o, decimal.Decimal):
12+
return str(o)
13+
return super(DecimalEncoder, self).default(o)
1414

1515
class ToJsonMixin(object):
1616
def to_json(self):
@@ -21,7 +21,7 @@ def json_filter(self):
2121
filter out properties that have names starting with _
2222
or properties that have a value of None
2323
"""
24-
return lambda obj: dict((k, v) for k, v in obj.__dict__.items()
24+
return lambda obj: str(obj) if isinstance(obj, decimal.Decimal) else dict((k, v) for k, v in obj.__dict__.items()
2525
if not k.startswith('_') and getattr(obj, k) is not None)
2626

2727

tests/unit/test_decimal.py

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from decimal import Decimal
2+
import unittest
3+
from quickbooks.objects.bill import Bill
4+
from quickbooks.objects.detailline import DetailLine
5+
6+
7+
class DecimalTestCase(unittest.TestCase):
8+
def test_bill_with_decimal_amount(self):
9+
"""Test that a Bill with decimal line amounts can be converted to JSON without errors"""
10+
bill = Bill()
11+
line = DetailLine()
12+
line.Amount = Decimal('42.42')
13+
line.DetailType = "AccountBasedExpenseLineDetail"
14+
15+
bill.Line.append(line)
16+
17+
# This should not raise any exceptions
18+
json_data = bill.to_json()
19+
20+
# Verify the amount was converted correctly
21+
self.assertIn('"Amount": "42.42"', json_data)

0 commit comments

Comments
 (0)