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

Improve Test Coverage for github/models #328

Merged
merged 13 commits into from
Jan 16, 2025
4 changes: 2 additions & 2 deletions backend/apps/github/models/repository_contributor.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ def __str__(self):
f"contribution{pluralize(self.contributions_count)} to {self.repository}"
)

def from_github(self, gh_label):
def from_github(self, gh_contributions):
"""Update instance based on GitHub contributor data."""
field_mapping = {
"contributions_count": "contributions",
}

# Direct fields.
for model_field, gh_field in field_mapping.items():
value = getattr(gh_label, gh_field)
value = getattr(gh_contributions, gh_field)
if value is not None:
setattr(self, model_field, value)

Expand Down
Empty file.
70 changes: 70 additions & 0 deletions backend/tests/github/models/issue_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
from unittest.mock import Mock, patch

import pytest

from apps.github.models.issue import Issue
from apps.github.models.repository import Repository
from apps.github.models.user import User


class TestIssueModel:
def test_str(self):
author = User(name="Author", login="author")
issue = Issue(title="Test Issue", author=author)
assert str(issue) == "Test Issue by Author"

def test_open_issues_count(self):
with patch("apps.github.models.issue.IndexBase.get_total_count") as mock_get_total_count:
Issue.open_issues_count()
mock_get_total_count.assert_called_once_with("issues")

def test_bulk_save(self):
mock_issues = [Mock(id=None), Mock(id=1)]
with patch("apps.github.models.issue.BulkSaveModel.bulk_save") as mock_bulk_save:
Issue.bulk_save(mock_issues, fields=["name"])
mock_bulk_save.assert_called_once_with(Issue, mock_issues, fields=["name"])

def test_repository_id(self):
repository = Repository()
issue = Issue(repository=repository)
assert issue.repository_id == repository.id

@patch("apps.github.models.issue.Issue.objects.get")
def test_update_data_project_does_not_exist(self, mock_get):
mock_get.side_effect = Issue.DoesNotExist
gh_issue_mock = Mock()
gh_issue_mock.raw_data = {"node_id": "12345"}

with patch.object(Issue, "save", return_value=None) as mock_save:
Issue.update_data(gh_issue_mock)
mock_save.assert_called_once()

@pytest.mark.parametrize(
("has_hint", "has_summary"),
[
(True, True),
(False, True),
(True, False),
(False, False),
],
)
def test_save_method(self, has_hint, has_summary):
issue = Issue()
issue.generate_hint = Mock()
issue.generate_summary = Mock()

issue.hint = "Test Hint" if has_hint else None
issue.summary = "Test Summary" if has_summary else None

with patch("apps.github.models.issue.BulkSaveModel.save"):
issue.save()

if has_hint:
issue.generate_hint.assert_not_called()
else:
issue.generate_hint.assert_called_once()

if has_summary:
issue.generate_summary.assert_not_called()
else:
issue.generate_summary.assert_called_once()
40 changes: 40 additions & 0 deletions backend/tests/github/models/label_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
from unittest.mock import Mock, patch

from apps.github.models.label import Label


class TestLabelModel:
def test_bulk_save(self):
mock_labels = [Mock(id=None), Mock(id=1)]
with patch("apps.common.models.BulkSaveModel.bulk_save") as mock_bulk_save:
Label.bulk_save(mock_labels)
mock_bulk_save.assert_called_once_with(Label, mock_labels)

def test_update_data(self, mocker):
gh_label_mock = mocker.Mock()
gh_label_mock.raw_data = {"node_id": "12345"}

mock_label = mocker.Mock(spec=Label)
mock_label.node_id = "12345"
mocker.patch("apps.github.models.label.Label.objects.get", return_value=mock_label)

label = Label()
label.from_github = mocker.Mock()

updated_label = Label.update_data(gh_label_mock)

assert updated_label.node_id == mock_label.node_id
assert updated_label.from_github.call_count == 1

def test_from_github(self):
gh_label_mock = Mock()
gh_label_mock.color = "000000"
gh_label_mock.description = "Description"
gh_label_mock.name = "Name"

label = Label()
label.from_github(gh_label_mock)

assert label.color == gh_label_mock.color
assert label.description == gh_label_mock.description
assert label.name == gh_label_mock.name
Empty file.
77 changes: 77 additions & 0 deletions backend/tests/github/models/mixins/issue_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
from datetime import datetime, timezone
from unittest.mock import MagicMock

import pytest

from apps.github.models.mixins.issue import IssueIndexMixin

COMMENTS_COUNT = 5
FOLLOWERS_COUNT = 10
FORKS_COUNT = 3
STARS_COUNT = 100


class MockModel(IssueIndexMixin):
def __init__(self):
self.author = MagicMock()
self.author.login = "test_user"
self.author.name = "Test User"

self.project = MagicMock()
self.project.idx_description = "Project description"
self.project.idx_level = "High"
self.project.idx_tags = ["tag1", "tag2"]
self.project.idx_topics = ["topic1", "topic2"]
self.project.idx_name = "Project Name"
self.project.idx_url = "https://example.com/project"

self.repository = MagicMock()
self.repository.idx_contributors_count = FOLLOWERS_COUNT
self.repository.idx_description = "Repository description"
self.repository.idx_forks_count = FORKS_COUNT
self.repository.idx_name = "Repository Name"
self.repository.idx_stars_count = STARS_COUNT
self.repository.idx_topics = ["repo_topic1", "repo_topic2"]

self.comments_count = COMMENTS_COUNT
self.created_at = datetime(2021, 9, 1, tzinfo=timezone.utc)
self.updated_at = datetime(2021, 9, 2, tzinfo=timezone.utc)
self.url = "https://example.com/issue"
self.title = "Issue Title"
self.summary = "Issue Summary"
self.hint = "Issue Hint"
self.labels = MagicMock(all=lambda: [MagicMock(name="bug"), MagicMock(name="feature")])


class TestIssueIndexMixin:
"""Test suite for IssueIndexMixin."""

@pytest.mark.parametrize(
("attr", "expected"),
[
("idx_author_login", "test_user"),
("idx_author_name", "Test User"),
("idx_project_description", "Project description"),
("idx_project_level", "High"),
("idx_project_tags", ["tag1", "tag2"]),
("idx_project_topics", ["topic1", "topic2"]),
("idx_project_name", "Project Name"),
("idx_project_url", "https://example.com/project"),
("idx_repository_contributors_count", FOLLOWERS_COUNT),
("idx_repository_description", "Repository description"),
("idx_repository_forks_count", FORKS_COUNT),
("idx_repository_name", "Repository Name"),
("idx_repository_stars_count", STARS_COUNT),
("idx_repository_topics", ["repo_topic1", "repo_topic2"]),
("idx_comments_count", COMMENTS_COUNT),
("idx_created_at", datetime(2021, 9, 1, tzinfo=timezone.utc).timestamp()),
("idx_updated_at", datetime(2021, 9, 2, tzinfo=timezone.utc).timestamp()),
("idx_url", "https://example.com/issue"),
("idx_title", "Issue Title"),
("idx_summary", "Issue Summary"),
("idx_hint", "Issue Hint"),
],
)
def test_issue_index(self, attr, expected):
mock_instance = MockModel()
assert getattr(mock_instance, attr) == expected
18 changes: 18 additions & 0 deletions backend/tests/github/models/mixins/organization_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import pytest

from apps.github.models.organization import Organization


class TestOrganizationIndexMixin:
@pytest.mark.parametrize(
("attr", "expected"),
[
("idx_name", "Organization Name login"),
("idx_company", "Company Location"),
],
)
def test_organization_index(self, attr, expected):
organization = Organization(
name="Organization Name", login="login", company="Company", location="Location"
)
assert getattr(organization, attr) == expected
66 changes: 66 additions & 0 deletions backend/tests/github/models/mixins/release_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
from datetime import datetime, timezone
from unittest.mock import MagicMock

import pytest

from apps.github.models.mixins.release import ReleaseIndexMixin


class MockModel(ReleaseIndexMixin):
def __init__(self):
self.author = MagicMock()

self.author.avatar_url = "https://example.com/avatar.png"
self.author.login = "test_user"
self.author.name = "Test User"

self.repository = MagicMock(
path="mock/repository",
project=MagicMock(nest_key="mock/project"),
)
self.created_at = datetime(2023, 1, 1, tzinfo=timezone.utc)
self.published_at = datetime(2023, 6, 1, tzinfo=timezone.utc)
self.description = "This is a long description"
self.is_pre_release = True
self.name = "Release v1.0.0"
self.tag_name = "v1.0.0"


@pytest.mark.parametrize(
("attr", "expected"),
[
(
"idx_author",
[
{
"avatar_url": "https://example.com/avatar.png",
"login": "test_user",
"name": "Test User",
}
],
),
("idx_created_at", datetime(2023, 1, 1, tzinfo=timezone.utc).timestamp()),
(
"idx_description",
"This is a long description",
),
("idx_is_pre_release", True),
("idx_name", "Release v1.0.0"),
("idx_project", "mock/project"),
("idx_published_at", datetime(2023, 6, 1, tzinfo=timezone.utc).timestamp()),
("idx_repository", "mock/repository"),
("idx_tag_name", "v1.0.0"),
("idx_author", []),
("idx_project", ""),
("idx_published_at", None),
],
)
def test_release_index(attr, expected):
mock_instance = MockModel()
if attr == "idx_author" and not expected:
mock_instance.author = None
elif attr == "idx_project" and expected == "":
mock_instance.repository.project = None
elif attr == "idx_published_at" and expected is None:
mock_instance.published_at = None
assert getattr(mock_instance, attr) == expected
50 changes: 50 additions & 0 deletions backend/tests/github/models/mixins/repository_tests.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
from datetime import datetime, timezone

import pytest

from apps.github.models.mixins.repository import RepositoryIndexMixin

CONTRIBUTORS_COUNT = 5
FORKS_COUNT = 5
OPEN_ISSUES_COUNT = 5
STARS_COUNT = 5


class MockModel(RepositoryIndexMixin):
def __init__(self):
self.contributors_count = CONTRIBUTORS_COUNT
self.description = "Description"
self.forks_count = FORKS_COUNT
self.languages = ["Python", "JavaScript"]
self.name = "Name"
self.open_issues_count = OPEN_ISSUES_COUNT
self.pushed_at = datetime(2021, 1, 1, tzinfo=timezone.utc)
self.stars_count = STARS_COUNT
self.topics = ["Topic1", "Topic2"]
self.created_at = datetime(2020, 1, 1, tzinfo=timezone.utc)
self.size = 1024
self.has_funding_yml = True
self.license = "MIT"
self.project = None


class TestRepositoryIndex:
"""Test suite for RepositoryIndexMixin."""

@pytest.mark.parametrize(
("attr", "expected"),
[
("idx_contributors_count", CONTRIBUTORS_COUNT),
("idx_description", "Description"),
("idx_forks_count", FORKS_COUNT),
("idx_languages", ["Python", "JavaScript"]),
("idx_name", "Name"),
("idx_open_issues_count", OPEN_ISSUES_COUNT),
("idx_pushed_at", datetime(2021, 1, 1, tzinfo=timezone.utc).timestamp()),
("idx_stars_count", STARS_COUNT),
("idx_topics", ["Topic1", "Topic2"]),
],
)
def test_repository_index(self, attr, expected):
mock_instance = MockModel()
assert getattr(mock_instance, attr) == expected
Loading
Loading