Skip to content

Commit

Permalink
pragma: no cover and pytest.mark.parametrize(
Browse files Browse the repository at this point in the history
  • Loading branch information
entorb committed Mar 2, 2024
1 parent 2aab399 commit 44ffe2b
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 44 deletions.
26 changes: 16 additions & 10 deletions helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@

Path("cache").mkdir(exist_ok=True)
# delete cache files older 1h
for file_path in Path("cache/").glob("*.json"):
for file_path in Path("cache/").glob("*.json"): # pragma: no cover
if time.time() - file_path.stat().st_mtime > 3600: # noqa: PLR2004
file_path.unlink()

Expand Down Expand Up @@ -81,7 +81,9 @@ def json_read(file_path: Path) -> list[dict[str, str]]:
return json_data


def json_write(file_path: Path, json_data: list[dict[str, str]]) -> None:
def json_write(
file_path: Path, json_data: list[dict[str, str]]
) -> None: # pragma: no cover
"""
Write JSON data to file.
"""
Expand Down Expand Up @@ -124,7 +126,7 @@ def check_cache_file_available_and_recent(
return cache_good


def perform_rest_call(url: str) -> str:
def perform_rest_call(url: str) -> str: # pragma: no cover
"""
Perform a simple REST call to an url.
Expand Down Expand Up @@ -184,7 +186,9 @@ def rtm_append_key_and_token_and_sig(d: dict[str, str]) -> dict[str, str]:
return d


def rtm_call_method(method: str, arguments: dict[str, str]) -> dict[str, str]:
def rtm_call_method(
method: str, arguments: dict[str, str]
) -> dict[str, str]: # pragma: no cover
"""
Call any rtm API method.
Expand Down Expand Up @@ -227,13 +231,13 @@ def get_lists() -> list[dict[str, str]]:
cache_file = Path("cache/lists.json")
if check_cache_file_available_and_recent(file_path=cache_file, max_age=3600):
lists = json_read(cache_file)
else:
lists = get_rmt_lists()
else: # pragma: no cover
lists = get_rmt_lists() # pragma: no cover
json_write(cache_file, lists)
return lists


def get_rmt_lists() -> list[dict[str, str]]:
def get_rmt_lists() -> list[dict[str, str]]: # pragma: no cover
"""Fetch lists from RTM."""
json_data = rtm_call_method(method="rtm.lists.getList", arguments={})
lists = json_data["lists"]["list"] # type: ignore
Expand Down Expand Up @@ -262,13 +266,14 @@ def get_tasks(my_filter: str) -> list[dict[str, str]]:
if check_cache_file_available_and_recent(file_path=cache_file, max_age=3 * 3600):
print(f"Using cache file: {cache_file}")
tasks = json_read(cache_file)
else:
else: # pragma: no cover
tasks = get_rtm_tasks(my_filter)
json_write(cache_file, tasks)
return tasks


def get_rtm_tasks(my_filter: str) -> list[dict[str, str]]:
def get_rtm_tasks(my_filter: str) -> list[dict[str, str]]: # pragma: no cover
# pragma: no cover
"""Fetch filtered tasks from RTM."""
arguments = {
"filter": my_filter,
Expand Down Expand Up @@ -437,9 +442,10 @@ def df_name_url_to_html(df: pd.DataFrame) -> pd.DataFrame:
return df


def df_to_html(df: pd.DataFrame, filename: str) -> None:
def df_to_html(df: pd.DataFrame, filename: str) -> None: # pragma: no cover
"""Export DF to html."""
print(f"Exporting to {filename}")

df.to_html(
filename,
index=False,
Expand Down
70 changes: 36 additions & 34 deletions tests/helper_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,12 @@
df_name_url_to_html,
dict_to_url_param,
flatten_tasks,
# gen_api_sig,
gen_md5_string,
get_lists,
get_lists_dict,
get_tasks,
get_tasks_as_df,
json_parse_response,
task_est_to_minutes,
tasks_to_df,
Expand Down Expand Up @@ -63,18 +65,16 @@ def cache_cleanup_test_data() -> None:
cache_target.unlink(missing_ok=True)


def test_dict_to_url_param() -> None:
# Test empty dictionary
assert dict_to_url_param({}) == ""

# Test dictionary with single key-value pair
assert dict_to_url_param({"key": "value"}) == "key=value"

# Test dictionary with multiple key-value pairs
assert (
dict_to_url_param({"key1": "value1", "key2": "value2"})
== "key1=value1&key2=value2"
)
@pytest.mark.parametrize(
("test_input", "expected"),
[
({}, ""), # empty
({"key": "value"}, "key=value"), # single
({"key1": "value1", "key2": "value2"}, "key1=value1&key2=value2"), # multiple
],
)
def test_dict_to_url_param(test_input: dict[str, str], expected: str) -> None:
assert dict_to_url_param(test_input) == expected


def test_gen_md5_string() -> None:
Expand All @@ -86,11 +86,19 @@ def test_gen_md5_string() -> None:
assert gen_md5_string(my_filter) == "85d7cb53077789572349a3aabf8eb369"


# def test_gen_api_sig() -> None:
# # problem: SHARED_SECRET is from config file and will hence fail in GitHub Actions
# d = {"key1": "value1", "key2": "value2"}
# assert gen_api_sig(d) == "ef309aa2591f0cff9491b3b0e6df8019"
# d = {"key2": "value2", "key1": "value1"}
# assert gen_api_sig(d) == "ef309aa2591f0cff9491b3b0e6df8019"


def test_json_parse_response() -> None:
# Test with single key-value pair
assert json_parse_response('{"rsp": {"stat": "ok", "key1": "value1"}}') == {
"key1": "value1"
}
assert json_parse_response(
'{"rsp": {"stat": "ok", "key1": "value1", "key2": "value2"}}'
) == {"key1": "value1", "key2": "value2"}


def test_get_lists() -> None:
Expand Down Expand Up @@ -149,6 +157,14 @@ def test_flatten_tasks() -> None:
assert json.dumps(tasks_list_flat, indent=2) == json.dumps(data_expected, indent=2)


@pytest.mark.parametrize(
("test_input", "expected"),
[("", None), ("PT30M", 30), ("PT3H", 3 * 60), ("PT2H30M", 2 * 60 + 30)],
)
def test_task_est_to_minutes(test_input: str, expected: int) -> None:
assert task_est_to_minutes(test_input) == expected


def test_convert_task_fields() -> None:
my_filter = "list:unit-tests"
tasks = get_tasks(my_filter)
Expand All @@ -172,28 +188,14 @@ def test_convert_task_fields() -> None:
)


@pytest.mark.parametrize(
("test_input", "expected"),
[("", None), ("PT30M", 30), ("PT3H", 3 * 60), ("PT2H30M", 2 * 60 + 30)],
)
def test_task_est_to_minutes(test_input: str, expected: int) -> None:
assert task_est_to_minutes(test_input) == expected


def test_get_tasks_as_df() -> None:
my_filter = "list:unit-tests"
tasks = get_tasks(my_filter)
lists_dict = get_lists_dict()
tasks_list_flat = flatten_tasks(rtm_tasks=tasks, lists_dict=lists_dict)
tasks_list_flat = sorted(
tasks_list_flat, key=lambda row: (row["task_id"]), reverse=False
)
tasks_list_flat2 = convert_task_fields(tasks_list_flat)

df = tasks_to_df(tasks_list_flat2)
assert len(df) == 6
assert df["task_id"].loc[3] == 1029525734
assert df["estimate"].loc[3] == 90
df = get_tasks_as_df(my_filter=my_filter, lists_dict=lists_dict)
assert len(df.index) == 6
df2 = df.query("task_id == 1029525734")
assert len(df2) == 1
assert df2["estimate"].loc[0] == 90


def test_df_name_url_to_html() -> None:
Expand Down

0 comments on commit 44ffe2b

Please sign in to comment.