Skip to content

Conversation

@laf-rge
Copy link
Contributor

@laf-rge laf-rge commented May 23, 2024

This pull request changes the datatype returned from the library to Decimal where floats are returned in the current version.

Rationale:

Using Decimal ensures precise handling of financial data, which is critical for the integrity of financial applications. The json library provides support through parse_float on decode and a helper cls for encoding. I have used a monkeypatched version and decided to contribute a clean version upstream. Here is what I did:

Convert to Using Decimal with json:

UPDATE:

Changed json parsing and added a decimaldecorder helper to seamlessly convert into and out of Decimal type when encoding an decoding json.

Update Tests:

Modified the existing tests to align with the changes introduced.
Specifically, fixed the test_make_request to ensure compatibility with handling of Decimal.

Changes Made:

  • Updated Import Statements:
  • Removed simplejson importing with import json.
  • Updated JSON Parsing:
  • Modified JSON parsing in the make_request method to use with parse_float=True.

Test Fix:

  • Adjusted the MockResponse class in the tests to ensure it uses the proper cis for JSON encoding.
  • Updated the test_make_request to mock the process_request method correctly with the changes.

Testing:

  • Unit Tests: All existing unit tests have been updated and pass successfully.
    Manual Testing: Conducted manual testing to ensure that the changes do not introduce any regressions.

Additional Notes:

  • Backward Compatibility: This change could break other projects that expect the amounts to be in floats, expecially if they are serializing the data. In general, however, Decimals work almost everywhere in python where floats work and I think anyone doing maths for currencies have, like me, been converting to Decimal all along anyway.

  • Documentation: No changes have been made yet I wanted to get feedback first before investing more time.

laf-rge added 2 commits May 22, 2024 18:10
Modified imports and removed unused json imports
Modified calls to set use_decimal for serialization/deserialization
def content(self):
return ''

class MockResponseSimpleJson:
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I was not sure why MockResponse had set the text field in the way that it did, so I created my own mock object that replaces it for the test that needed text to provide a valid json string.

from ..client import QuickBooks
from .creditcardpayment import CreditCardPayment
from ..mixins import DeleteMixin, VoidMixin
import json
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unused import

@laf-rge laf-rge mentioned this pull request Jun 1, 2024
laf-rge added 2 commits June 1, 2024 12:09
Put the decode on the test, removed the name for clarity
@laf-rge laf-rge changed the title Convert to Using Decimal with simplejson and Fix Related Test Convert to Using Decimal and Fix Related Test Jun 1, 2024
@laf-rge
Copy link
Contributor Author

laf-rge commented Jun 1, 2024

Added toggle to enable the decoding to decimals as per @justmobilize suggestion

@ej2 ej2 merged commit ea194e8 into ej2:master Jul 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants