Skip to content

Commit

Permalink
Improve utility functions and tests
Browse files Browse the repository at this point in the history
  • Loading branch information
ghukill committed May 30, 2024
1 parent af9f68f commit 7de614e
Show file tree
Hide file tree
Showing 2 changed files with 41 additions and 3 deletions.
12 changes: 10 additions & 2 deletions hrqb/utils/__init__.py
Original file line number Diff line number Diff line change
@@ -1,22 +1,30 @@
"""hrqb.utils"""

import datetime
import logging

import click
import pandas as pd
import us # type: ignore[import-untyped]
from dateutil.parser import ParserError # type: ignore[import-untyped]
from dateutil.parser import parse as date_parser # type: ignore[import-untyped]

logger = logging.getLogger(__name__)


def today_date() -> datetime.date:
return datetime.datetime.now(tz=datetime.UTC).date()


def normalize_date(date: str | datetime.datetime) -> str | None:
if date is None or date == "" or pd.isna(date):
if date == "" or pd.isna(date):
return None
if isinstance(date, str):
date = date_parser(date)
try:
date = date_parser(date)
except ParserError:
message = f"Unable to parse date from '{date}'"
logger.warning(message)
if isinstance(date, datetime.datetime):
return date.strftime("%Y-%m-%d")
return None
Expand Down
32 changes: 31 additions & 1 deletion tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,11 @@

from hrqb.utils import (
click_argument_to_dict,
convert_oracle_bools_to_qb_bools,
normalize_dataframe_dates,
normalize_date,
today_date,
us_state_abbreviation_to_name,
)


Expand Down Expand Up @@ -40,11 +42,13 @@ def test_normalize_date_date_object_or_string_return_yyyy_mm_dd():
assert normalize_date("2000-01-01 01:23:45") == "2000-01-01"


def test_normalize_date_unparsable_values_return_none():
def test_normalize_date_unparsable_values_return_none(caplog):
assert normalize_date("") is None
assert normalize_date(None) is None
assert normalize_date(np.nan) is None
assert normalize_date(42) is None
assert normalize_date("I cannot be parsed.") is None
assert "Unable to parse date from 'I cannot be parsed.'" in caplog.text


def test_normalize_dataframe_dates_success():
Expand Down Expand Up @@ -83,3 +87,29 @@ def test_normalize_dataframe_dates_success():
]
)
)


def test_us_state_abbreviation_to_name_success():
assert us_state_abbreviation_to_name("WA") == "Washington"


def test_us_state_abbreviation_to_name_missing_abbrevation_return_none():
assert us_state_abbreviation_to_name("BAD") is None


def test_convert_oracle_bools_to_qb_bools_success():
df = pd.DataFrame(
[
{"x": "Y", "y": 42},
{"x": "N", "y": 42},
]
)
new_df = convert_oracle_bools_to_qb_bools(df, columns=["x"])
assert new_df.equals(
pd.DataFrame(
[
{"x": "Yes", "y": 42},
{"x": "No", "y": 42},
]
)
)

0 comments on commit 7de614e

Please sign in to comment.