Skip to content

Commit

Permalink
Merge pull request #364 from laf-rge/master
Browse files Browse the repository at this point in the history
handle Decimal on amount values
  • Loading branch information
ej2 authored Feb 10, 2025
2 parents 8d38c63 + c61efc8 commit a8b5ed4
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 6 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/python-package.yml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
strategy:
fail-fast: false
matrix:
python-version: ["3.7", "3.8", "3.9", "3.10", "3.11", "3.12"]
python-version: ["3.8", "3.9", "3.10", "3.11", "3.12", "3.13"]

steps:
- uses: actions/checkout@v3
Expand Down
10 changes: 5 additions & 5 deletions quickbooks/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,10 @@
from .utils import build_choose_clause, build_where_clause

class DecimalEncoder(json.JSONEncoder):
def default(self, obj):
if isinstance(obj, decimal.Decimal):
return str(obj)
return super(DecimalEncoder, self).default(obj)
def default(self, o):
if isinstance(o, decimal.Decimal):
return str(o)
return super(DecimalEncoder, self).default(o)

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


Expand Down
21 changes: 21 additions & 0 deletions tests/unit/test_decimal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from decimal import Decimal
import unittest
from quickbooks.objects.bill import Bill
from quickbooks.objects.detailline import DetailLine


class DecimalTestCase(unittest.TestCase):
def test_bill_with_decimal_amount(self):
"""Test that a Bill with decimal line amounts can be converted to JSON without errors"""
bill = Bill()
line = DetailLine()
line.Amount = Decimal('42.42')
line.DetailType = "AccountBasedExpenseLineDetail"

bill.Line.append(line)

# This should not raise any exceptions
json_data = bill.to_json()

# Verify the amount was converted correctly
self.assertIn('"Amount": "42.42"', json_data)

0 comments on commit a8b5ed4

Please sign in to comment.