Skip to content

Commit

Permalink
Updates in tests and making them modular
Browse files Browse the repository at this point in the history
  • Loading branch information
yashpandey06 committed Jan 13, 2025
1 parent 054d6aa commit 70d0d16
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 69 deletions.
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
68 changes: 36 additions & 32 deletions backend/tests/github/models/mixins/issue_tests.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
from unittest.mock import MagicMock

import pytest

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

CONTRIBUTORS_COUNT = 10
Expand All @@ -9,7 +11,8 @@


class TestIssueIndexMixin:
def test_issue_index(self):
@pytest.fixture()
def mock_model(self):
mock_author = MagicMock()
mock_author.login = "test_user"
mock_author.name = "Test User"
Expand Down Expand Up @@ -41,7 +44,7 @@ def __init__(self):
self.author = mock_author
self.project = mock_project
self.repository = mock_repository
self.comments_count = 5
self.comments_count = COMMENTS_COUNT
self.created_at = "2021-09-01T00:00:00Z"
self.updated_at = "2021-09-02T00:00:00Z"
self.url = "https://example.com/issue"
Expand All @@ -50,34 +53,35 @@ def __init__(self):
self.hint = "Issue Hint"
self.labels = MagicMock(all=lambda: [mock_label_1, mock_label_2])

mock_instance = MockModel()

assert isinstance(mock_instance, IssueIndexMixin)

assert mock_instance.idx_author_login == "test_user"
assert mock_instance.idx_author_name == "Test User"

assert mock_instance.idx_project_description == "Project description"
assert mock_instance.idx_project_level == "High"
assert mock_instance.idx_project_tags == ["tag1", "tag2"]
assert mock_instance.idx_project_topics == ["topic1", "topic2"]
assert mock_instance.idx_project_name == "Project Name"
assert mock_instance.idx_project_url == "https://example.com/project"

assert mock_instance.idx_repository_contributors_count == CONTRIBUTORS_COUNT
assert mock_instance.idx_repository_description == "Repository description"
assert mock_instance.idx_repository_forks_count == FORKS_COUNT
assert mock_instance.idx_repository_languages == ["Python", "JavaScript"]
assert mock_instance.idx_repository_name == "Repository Name"
assert mock_instance.idx_repository_stars_count == STARS_COUNT
assert mock_instance.idx_repository_topics == ["repo_topic1", "repo_topic2"]

assert mock_instance.idx_labels == ["bug", "feature"]
return MockModel()

assert mock_instance.idx_comments_count == COMMENTS_COUNT
assert mock_instance.idx_created_at == "2021-09-01T00:00:00Z"
assert mock_instance.idx_updated_at == "2021-09-02T00:00:00Z"
assert mock_instance.idx_url == "https://example.com/issue"
assert mock_instance.idx_title == "Issue Title"
assert mock_instance.idx_summary == "Issue Summary"
assert mock_instance.idx_hint == "Issue Hint"
@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", CONTRIBUTORS_COUNT),
("idx_repository_description", "Repository description"),
("idx_repository_forks_count", FORKS_COUNT),
("idx_repository_languages", ["Python", "JavaScript"]),
("idx_repository_name", "Repository Name"),
("idx_repository_stars_count", STARS_COUNT),
("idx_repository_topics", ["repo_topic1", "repo_topic2"]),
("idx_labels", ["bug", "feature"]),
("idx_comments_count", COMMENTS_COUNT),
("idx_created_at", "2021-09-01T00:00:00Z"),
("idx_updated_at", "2021-09-02T00:00:00Z"),
("idx_url", "https://example.com/issue"),
("idx_title", "Issue Title"),
("idx_summary", "Issue Summary"),
("idx_hint", "Issue Hint"),
],
)
def test_issue_index(self, mock_model, attr, expected):
assert getattr(mock_model, attr) == expected
15 changes: 11 additions & 4 deletions backend/tests/github/models/mixins/organization_tests.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,18 @@
import pytest

from apps.github.models.organization import Organization


class TestOrganizationIndexMixin:
def test_organization_index(self):
@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 organization.idx_name == "Organization Name login"
assert organization.idx_company == "Company Location"
assert getattr(organization, attr) == expected
30 changes: 18 additions & 12 deletions backend/tests/github/models/mixins/repository_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

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

CONTRIBUTORS_COUNT = 5
Expand All @@ -7,7 +9,21 @@


class TestRepositoryIndexMixin:
def test_repository_index(self):
@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", "2021-01-01"),
("idx_stars_count", STARS_COUNT),
("idx_topics", ["Topic1", "Topic2"]),
],
)
def test_repository_index(self, attr, expected):
class MockModel(RepositoryIndexMixin):
def __init__(self):
self.contributors_count = 5
Expand All @@ -22,14 +38,4 @@ def __init__(self):

mock_instance = MockModel()

assert isinstance(mock_instance, RepositoryIndexMixin)

assert mock_instance.idx_contributors_count == CONTRIBUTORS_COUNT
assert mock_instance.idx_description == "Description"
assert mock_instance.idx_forks_count == FORKS_COUNT
assert mock_instance.idx_languages == ["Python", "JavaScript"]
assert mock_instance.idx_name == "Name"
assert mock_instance.idx_open_issues_count == OPEN_ISSUES_COUNT
assert mock_instance.idx_pushed_at == "2021-01-01"
assert mock_instance.idx_stars_count == STARS_COUNT
assert mock_instance.idx_topics == ["Topic1", "Topic2"]
assert getattr(mock_instance, attr) == expected
40 changes: 23 additions & 17 deletions backend/tests/github/models/mixins/user_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import pytest

from apps.github.models.mixins.user import UserIndexMixin

FOLLOWERS_COUNT = 5
Expand All @@ -6,7 +8,26 @@


class TestUserIndexMixin:
def test_user_index(self):
@pytest.mark.parametrize(
("attr", "expected"),
[
("idx_avatar_url", "https://avatars.githubusercontent.com/u/1"),
("idx_bio", "Bio"),
("idx_company", "Company"),
("idx_created_at", "2021-01-01T00:00:00Z"),
("idx_email", "[email protected]"),
("idx_followers_count", FOLLOWERS_COUNT),
("idx_following_count", FOLLOWING_COUNT),
("idx_location", "Earth"),
("idx_key", "user_login"),
("idx_name", "John Doe"),
("idx_public_repositories_count", REPOSITORIES_COUNT),
("idx_title", "GitHub User"),
("idx_updated_at", "2021-12-31T23:59:59Z"),
("idx_url", "https://api.github.com/users/user_login"),
],
)
def test_user_index(self, attr, expected):
class MockModel(UserIndexMixin):
def __init__(self):
self.avatar_url = "https://avatars.githubusercontent.com/u/1"
Expand All @@ -26,19 +47,4 @@ def __init__(self):

mock_instance = MockModel()

assert isinstance(mock_instance, UserIndexMixin)

assert mock_instance.idx_avatar_url == "https://avatars.githubusercontent.com/u/1"
assert mock_instance.idx_bio == "Bio"
assert mock_instance.idx_company == "Company"
assert mock_instance.idx_created_at == "2021-01-01T00:00:00Z"
assert mock_instance.idx_email == "[email protected]"
assert mock_instance.idx_followers_count == FOLLOWERS_COUNT
assert mock_instance.idx_following_count == FOLLOWING_COUNT
assert mock_instance.idx_location == "Earth"
assert mock_instance.idx_key == "user_login"
assert mock_instance.idx_name == "John Doe"
assert mock_instance.idx_public_repositories_count == REPOSITORIES_COUNT
assert mock_instance.idx_title == "GitHub User"
assert mock_instance.idx_updated_at == "2021-12-31T23:59:59Z"
assert mock_instance.idx_url == "https://api.github.com/users/user_login"
assert getattr(mock_instance, attr) == expected
4 changes: 2 additions & 2 deletions backend/tests/github/models/repository_contributor_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ class TestRepositoryContributor:
def test_from_github(self):
default_contribution_value = 5
repository_contributor = RepositoryContributor()
gh_label_mock = Mock(contributions=default_contribution_value)
repository_contributor.from_github(gh_label_mock)
gh_contributions = Mock(contributions=default_contribution_value)
repository_contributor.from_github(gh_contributions)

assert repository_contributor.contributions_count == default_contribution_value

Expand Down

0 comments on commit 70d0d16

Please sign in to comment.