Skip to content

Commit

Permalink
complete testing helpers,py
Browse files Browse the repository at this point in the history
Signed-off-by: Grant Ramsay <[email protected]>
  • Loading branch information
seapagan committed Jun 18, 2024
1 parent f42ee1d commit 5833169
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 3 deletions.
2 changes: 1 addition & 1 deletion github_changelog_md/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ def get_section_name(label: str | None) -> str | None:


def get_index_of_tuple(
tuple_list: list[SectionHeadings], index: int, value: str
tuple_list: list[SectionHeadings], index: int, value: str | None
) -> int:
"""Return the index of a tuple in a list."""
for pos, t in enumerate(tuple_list):
Expand Down
53 changes: 51 additions & 2 deletions tests/test_helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,21 +6,34 @@
from pathlib import Path
from typing import TYPE_CHECKING

from github_changelog_md.constants import ExitErrors
import pytest

from github_changelog_md.constants import ExitErrors, SectionHeadings
from github_changelog_md.helpers import (
cap_first_letter,
get_app_version,
get_index_of_tuple,
get_repo_name,
get_section_name,
header,
)

if TYPE_CHECKING:
import pytest
from pyfakefs.fake_filesystem import FakeFileSystem
from pytest_mock import MockerFixture


@pytest.fixture()
def sample_section_headings() -> list[SectionHeadings]:
"""Fixture for providing a sample list of section headings."""
return [
("Introduction", None),
("Methods", "1.0"),
("Results", "2.0"),
("Discussion", None),
]


class TestHelpers:
"""Test class for the 'helpers' module."""

Expand Down Expand Up @@ -135,3 +148,39 @@ def test_get_section_name(self) -> None:
assert get_section_name("dependencies") == "Dependency Updates"
assert get_section_name("not_a_label") is None
assert get_section_name(None) == "Merged Pull Requests"

def test_get_index_of_tuple_found(self, sample_section_headings) -> None:
"""Test get_index_of_tuple returns correct index when value is found."""
index = get_index_of_tuple(sample_section_headings, 0, "Methods")
assert index == 1, "Expected index of 1"

def test_get_index_of_tuple_not_found(
self, sample_section_headings
) -> None:
"""Test get_index_of_tuple raises ValueError when value is not found."""
with pytest.raises(
ValueError, match="not in the supplied list of Tuples"
) as exc_info:
get_index_of_tuple(sample_section_headings, 0, "Conclusion")
assert "'Conclusion' is not in the supplied list of Tuples" in str(
exc_info.value
), "Expected a ValueError indicating 'Conclusion' was not found"

def test_get_index_of_tuple_with_none_value(
self, sample_section_headings
) -> None:
"""Test get_index_of_tuple when searching for None value."""
index = get_index_of_tuple(sample_section_headings, 1, None)
assert (
index == 0
), "Expected index of 0 for the first occurrence of None value"

def test_get_index_of_tuple_empty_list(self) -> None:
"""Test get_index_of_tuple with an empty list."""
with pytest.raises(
ValueError, match="not in the supplied list of Tuples"
) as exc_info:
get_index_of_tuple([], 0, "Introduction")
assert "'Introduction' is not in the supplied list of Tuples" in str(
exc_info.value
), "Expected a ValueError, 'Introduction' was not found in empty list"

0 comments on commit 5833169

Please sign in to comment.