Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add non-composite custom money #1137

Merged
merged 19 commits into from
Mar 8, 2022
Merged

Add non-composite custom money #1137

merged 19 commits into from
Mar 8, 2022

Conversation

mathemancer
Copy link
Contributor

@mathemancer mathemancer commented Mar 4, 2022

Fixes #1094

This adds a custom money type called MATHESAR_TYPES.MATHESAR_MONEY based on underlying NUMERIC storage.

Technical details

Extra features over NUMERIC:

  • a column of MATHESAR_TYPES.MATHESAR_MONEY will be reflected as such, notifying the UI that it should apply whatever display options are relevant.

  • For casting from text types (TEXT, VARCHAR, CHAR(n), etc.), many inputs which wouldn't be allowed for NUMERIC are handled properly. Each of

    • '$1,000.00'
    • '1,000.00$'
    • '$1'
    • '-$1'
    • '($1)' -- common way to write negative numbers in accounting
    • '$1-' -- negative number in some locales
    • '$-1' -- some locales put the negative between the currency symbol and the number
    • '1$'
    • '$ 1'
    • '$1,000,000'
    • '$1,234,567.1234'
    • '1,000,000$'
    • '$1 000,000'
    • '1 000,000$'
    • '1.000,000$'
    • '$1.000,00 HK'
    • 'EUR 1.000,00'
    • '€1.000,00'
    • '1.000,00€'
    • '€1 000'
    • '1 000€'
    • '₿1,324.23466 BTC'
    • '₹1,00,000'
    • '1,00,000₹'
    • '₹1,00,000.00'
    • '1,00,000.00₹'
    • '10,00,000.00₹'
    • '₹10,00,00,000.00'
    • '10,00,00,000.00₹'

    is cast to the proper numeric value. Note that we allow currency symbols (non-numeric characters) at the beginning, end, or both of the numeric portion. We allow ,, ., or (space) as group separators. We allow either , or . as decimal points (but the decimal point needs to be distinct from the group separator). For grouping, we handle both the international 3-digit group system or the lakh, crore, etc. system. There are some notable things we can't allow, since we're locale-agnostic when reading text as money, or other reasons:

    • '1' -- no currency symbol
    • '$1,000' -- this would be $1 in the German locale, but $1000 in the American English locale.
    • '1$1000$1' -- we only allow one continuous substring of number data (while handling group separators and decimal points)
    • '₹10 00 00 000.00' -- only , and . are allowed for the group separator and decimal point respectively in the lakh system
    • '₹10.00.00.000,00' -- same

In any case where we handle the input, it will be translated appropriately into the DB's locale for parsing as a NUMERIC for storage.

Checklist

  • My pull request has a descriptive title (not a vague title like Update index.md).
  • My pull request targets the master branch of the repository
  • My commit messages follow best practices.
  • My code follows the established code style of the repository.
  • I added tests for the changes I made (if applicable).
  • I added or updated documentation (if applicable).
  • I tried running the project locally and verified that there are no
    visible errors.

Developer Certificate of Origin

Developer Certificate of Origin
Developer Certificate of Origin
Version 1.1

Copyright (C) 2004, 2006 The Linux Foundation and its contributors.
1 Letterman Drive
Suite D4700
San Francisco, CA, 94129

Everyone is permitted to copy and distribute verbatim copies of this
license document, but changing it is not allowed.


Developer's Certificate of Origin 1.1

By making a contribution to this project, I certify that:

(a) The contribution was created in whole or in part by me and I
    have the right to submit it under the open source license
    indicated in the file; or

(b) The contribution is based upon previous work that, to the best
    of my knowledge, is covered under an appropriate open source
    license and I have the right under that license to submit that
    work with modifications, whether created in whole or in part
    by me, under the same open source license (unless I am
    permitted to submit under a different license), as indicated
    in the file; or

(c) The contribution was provided directly to me by some other
    person who certified (a), (b) or (c) and I have not modified
    it.

(d) I understand and agree that this project and the contribution
    are public and that a record of the contribution (including all
    personal information I submit with it, including my sign-off) is
    maintained indefinitely and may be redistributed consistent with
    this project or the open source license(s) involved.

@codecov-commenter
Copy link

codecov-commenter commented Mar 4, 2022

Codecov Report

Merging #1137 (1cac166) into master (8631e1c) will increase coverage by 0.09%.
The diff coverage is 100.00%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #1137      +/-   ##
==========================================
+ Coverage   93.30%   93.39%   +0.09%     
==========================================
  Files         112      113       +1     
  Lines        4269     4332      +63     
==========================================
+ Hits         3983     4046      +63     
  Misses        286      286              
Flag Coverage Δ
pytest-backend 93.39% <100.00%> (+0.09%) ⬆️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
db/columns/operations/infer_types.py 97.14% <ø> (ø)
mathesar/database/types.py 97.67% <ø> (ø)
db/types/__init__.py 100.00% <100.00%> (ø)
db/types/base.py 97.56% <100.00%> (+0.01%) ⬆️
db/types/install.py 100.00% <100.00%> (ø)
db/types/money.py 100.00% <100.00%> (ø)
db/types/multicurrency.py 100.00% <100.00%> (ø)
db/types/operations/cast.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 8631e1c...1cac166. Read the comment docs.

@mathemancer mathemancer marked this pull request as ready for review March 4, 2022 11:08
@mathemancer mathemancer requested review from a team and silentninja March 4, 2022 11:08
@kgodey kgodey added the pr-status: review A PR awaiting review label Mar 4, 2022
Copy link
Contributor

@silentninja silentninja left a comment

Choose a reason for hiding this comment

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

Great work @brent!. I removed a stale print statement in line db/types/operations/cast.py:314

@@ -297,6 +311,7 @@ def create_cast_functions(target_type, type_body_map, engine):
"""
for type_, body in type_body_map.items():
query = assemble_function_creation_sql(type_, target_type, body)
print(query)
Copy link
Contributor

Choose a reason for hiding this comment

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

Please remove this print statement

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Thanks for catching that.

@Jyuart Jyuart mentioned this pull request May 16, 2022
7 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
pr-status: review A PR awaiting review
Projects
No open projects
Development

Successfully merging this pull request may close these issues.

Custom money type
4 participants