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

Fix sqlite decimal warning #129

Merged
merged 1 commit into from
May 29, 2022
Merged

Fix sqlite decimal warning #129

merged 1 commit into from
May 29, 2022

Conversation

sea-kelp
Copy link
Collaborator

Description of Changes

Fixes #126.

Since we use sqlite in our unit tests and sqlite doesn't have a decimal type, sqlalchemy shows a floating-point warning when we work with salaries during testing.

This change fixes the warnings by adding a TypeDecorator to convert currency values to integers by multiplying by 100 when storing values and dividing by 100 when retrieving them. It also changes parts of the salary import code to use Decimal instead of float.

Notes for Deployment

Might be a good idea to test the csv imports a little more before deploying?

Screenshots (if appropriate)

N/A

Tests and linting

  • I have rebased my changes on main

  • just lint passes

  • just test passes

@sea-kelp sea-kelp marked this pull request as ready for review May 22, 2022 04:30
Copy link
Collaborator

@sarayourfriend sarayourfriend left a comment

Choose a reason for hiding this comment

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

LGTM! Agree testing import wouldn't be a bad idea too, I'm not sure how to do that 😅

Comment on lines +54 to 57
def parse_decimal(value: Optional[Union[str, Decimal]]) -> Optional[Decimal]:
if value == Decimal(0) or value:
return Decimal(value)
return None
Copy link
Collaborator

Choose a reason for hiding this comment

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

Is the goal of this function just to filter None? Other than None, all the potential inputs I can think of seem to be equivalent to just using the Decimal constructor:

input parse_decimal(input) Decimal(input)
0 0 0
1 1 1
1.1 1.1 1.1
0.0 0.0 0.0
0.01 0.01000000000000000020816681711721685132943093776702880859375 0.01000000000000000020816681711721685132943093776702880859375
0.0 0 0
0.0 0 0
-1 -1 -1
0 0 0
1000.01 1000.009999999999990905052982270717620849609375 1000.009999999999990905052982270717620849609375

It seems like the function might be able to be simplified (from a reader's perspective) like this:

Suggested change
def parse_decimal(value: Optional[Union[str, Decimal]]) -> Optional[Decimal]:
if value == Decimal(0) or value:
return Decimal(value)
return None
def parse_decimal(value: Optional[Union[str, Decimal]]) -> Optional[Decimal]:
return Decimal(value) if value is not None else None

What do you think?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I'm not familiar with this part of the code at all but the type hints seem to suggest that this function takes both string and decimal, so I think the original function might have been trying to filter out empty string as well as None. Does that make sense?

Copy link
Collaborator

Choose a reason for hiding this comment

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

Ahh, you're right, I totally missed that in my test. Decimal("") throws an error 👍

Copy link
Collaborator

@AetherUnbound AetherUnbound 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! I've tested that this works using the following:

  1. just fresh-start
  2. Navigate to http://localhost:3000/download/all and download the officers.csv.
  3. Run python data-munging/spd_2021_salary_data.py ~/Downloads/Seattle_Police_Department_Officers.csv data/2022_salaries.csv (the script is for 2021 salaries but unfortunately it's not run for a specific data but point-in-time when the script gets run 😓) I made Make salary 2021 script year agnostic #135 for this
  4. just import --salaries-csv /data/2022_salaries.csv "Seattle\ Police\ Department"
  5. Browse for officers and look at their salaries!

Everything there went okay 🙂

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.

Convert decimal db type to floating point
3 participants