Skip to content

Commit

Permalink
Feature/34 introduce unit tests for generator (#67)
Browse files Browse the repository at this point in the history
* Implemented unit tests using Pytest for generator.py and main.py (project code-cov at (96 %).
  • Loading branch information
MobiTikula authored Nov 18, 2024
1 parent fdb2f09 commit 51b688b
Show file tree
Hide file tree
Showing 6 changed files with 1,444 additions and 30 deletions.
2 changes: 1 addition & 1 deletion living_documentation_generator/action_inputs.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ def get_repositories() -> list[ConfigRepository]:
sys.exit(1)

except TypeError:
logger.error("Type error parsing input JSON repositories: `%s.`", repositories_json)
logger.error("Type error parsing input JSON repositories: %s.", repositories_json)
sys.exit(1)

return repositories
Expand Down
13 changes: 7 additions & 6 deletions living_documentation_generator/generator.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,7 @@ def _fetch_github_issues(self) -> dict[str, list[Issue]]:
issues[repository_id] = self.__safe_call(repository.get_issues)(state=ISSUE_STATE_ALL)
amount_of_issues_per_repo = len(list(issues[repository_id]))
logger.debug(
"Fetched `%s` repository issues (%s)`.",
"Fetched `%i` repository issues (%s)`.",
amount_of_issues_per_repo,
repository.full_name,
)
Expand All @@ -170,13 +170,13 @@ def _fetch_github_issues(self) -> dict[str, list[Issue]]:
# Accumulate the count of issues
total_issues_number += amount_of_issues_per_repo
logger.info(
"Fetching repository GitHub issues - fetched `%s` repository issues (%s).",
"Fetching repository GitHub issues - fetched `%i` repository issues (%s).",
amount_of_issues_per_repo,
repository.full_name,
)

logger.info(
"Fetching repository GitHub issues - loaded `%s` repository issues in total.",
"Fetching repository GitHub issues - loaded `%i` repository issues in total.",
total_issues_number,
)
return issues
Expand Down Expand Up @@ -213,7 +213,7 @@ def _fetch_github_project_issues(self) -> dict[str, list[ProjectIssue]]:

if projects:
logger.info(
"Fetching GitHub project data - for repository `%s` found `%s` project/s.",
"Fetching GitHub project data - for repository `%s` found `%i` project/s.",
repository.full_name,
len(projects),
)
Expand Down Expand Up @@ -278,8 +278,8 @@ def _consolidate_issues_data(
for project_issue in project_issues[key]:
consolidated_issue.update_with_project_data(project_issue.project_status)

logging.info(
"Issue and project data consolidation - consolidated `%s` repository issues with extra project data.",
logger.info(
"Issue and project data consolidation - consolidated `%i` repository issues with extra project data.",
len(consolidated_issues),
)
return consolidated_issues
Expand Down Expand Up @@ -651,6 +651,7 @@ def _generate_index_directory_path(repository_id: Optional[str], topic: Optional
Generates a directory path based on if structured output is required.
@param repository_id: The repository id.
@param topic: The topic used for grouping issues.
@return: The generated directory path.
"""
output_path: str = ActionInputs.get_output_directory()
Expand Down
86 changes: 85 additions & 1 deletion tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,19 @@
# See the License for the specific language governing permissions and
# limitations under the License.
#

import datetime
import time
import pytest
from github import Github
from github.Rate import Rate
from github.RateLimit import RateLimit
from github.Repository import Repository

from living_documentation_generator.generator import LivingDocumentationGenerator
from living_documentation_generator.model.config_repository import ConfigRepository
from living_documentation_generator.model.consolidated_issue import ConsolidatedIssue
from living_documentation_generator.model.github_project import GithubProject
from living_documentation_generator.model.project_status import ProjectStatus
from living_documentation_generator.utils.github_rate_limiter import GithubRateLimiter


Expand Down Expand Up @@ -76,3 +80,83 @@ def repository_setup(mocker):
repository.full_name = "test_owner/test_repo"

return repository


@pytest.fixture
def load_all_templates_setup(mocker):
mock_load_all_templates = mocker.patch.object(
LivingDocumentationGenerator,
"_load_all_templates",
return_value=(
"Issue Page Template",
"Index Page Template",
"Root Level Page Template",
"Org Level Template",
"Repo Page Template",
"Data Level Template",
),
)

return mock_load_all_templates


@pytest.fixture
def generator(mocker):
mock_github_class = mocker.patch("living_documentation_generator.generator.Github")
mock_github_instance = mock_github_class.return_value

mock_rate_limit = mocker.Mock()
mock_rate_limit.remaining = 5000
mock_rate_limit.reset = datetime.datetime.now() + datetime.timedelta(minutes=10)

mock_github_instance.get_rate_limit.return_value = mocker.Mock(core=mock_rate_limit)
mock_github_instance.get_repo.return_value = mocker.Mock()

mocker.patch(
"living_documentation_generator.generator.ActionInputs.get_github_token", return_value="FakeGithubToken"
)
return LivingDocumentationGenerator()


@pytest.fixture
def config_repository(mocker):
config_repository = mocker.Mock(spec=ConfigRepository)
config_repository.organization_name = "test_org"
config_repository.repository_name = "test_repo"
config_repository.labels = []
config_repository.projects_title_filter = []

return config_repository


@pytest.fixture
def consolidated_issue(mocker):
consolidated_issue = mocker.Mock(spec=ConsolidatedIssue)
consolidated_issue.repository_id = "TestOrg/TestRepo"
consolidated_issue.organization_name = "TestOrg"
consolidated_issue.repository_name = "TestRepo"
consolidated_issue.number = 42
consolidated_issue.title = "Sample Issue"
consolidated_issue.state = "OPEN"
consolidated_issue.html_url = "https://github.com/TestOrg/TestRepo/issues/42"
consolidated_issue.created_at = "2024-01-01T00:00:00Z"
consolidated_issue.updated_at = "2024-01-02T00:00:00Z"
consolidated_issue.closed_at = None
consolidated_issue.labels = ["bug", "urgent"]
consolidated_issue.body = "This is the issue content."
consolidated_issue.linked_to_project = False
consolidated_issue.topics = ["documentationTopic"]

return consolidated_issue


@pytest.fixture
def project_status(mocker):
project_status = mocker.Mock(spec=ProjectStatus)
project_status.project_title = "Test Project"
project_status.status = "In Progress"
project_status.priority = "High"
project_status.size = "Large"
project_status.moscow = "Must Have"

return project_status
Loading

0 comments on commit 51b688b

Please sign in to comment.