Skip to content

Commit

Permalink
test-fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
yashpandey06 committed Jan 13, 2025
1 parent 70d0d16 commit 9ac2d7d
Show file tree
Hide file tree
Showing 6 changed files with 170 additions and 119 deletions.
88 changes: 39 additions & 49 deletions backend/tests/github/models/mixins/issue_tests.py
Original file line number Diff line number Diff line change
@@ -1,59 +1,50 @@
from datetime import datetime, timezone
from unittest.mock import MagicMock

import pytest

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

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


class TestIssueIndexMixin:
@pytest.fixture()
def mock_model(self):
mock_author = MagicMock()
mock_author.login = "test_user"
mock_author.name = "Test User"
class MockModel(IssueIndexMixin):
def __init__(self):
self.author = MagicMock()
self.author.login = "test_user"
self.author.name = "Test User"

mock_project = MagicMock()
mock_project.idx_description = "Project description"
mock_project.idx_level = "High"
mock_project.idx_tags = ["tag1", "tag2"]
mock_project.idx_topics = ["topic1", "topic2"]
mock_project.idx_name = "Project Name"
mock_project.idx_url = "https://example.com/project"
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"

mock_repository = MagicMock()
mock_repository.idx_contributors_count = CONTRIBUTORS_COUNT
mock_repository.idx_description = "Repository description"
mock_repository.idx_forks_count = FORKS_COUNT
mock_repository.idx_languages = ["Python", "JavaScript"]
mock_repository.idx_name = "Repository Name"
mock_repository.idx_stars_count = STARS_COUNT
mock_repository.idx_topics = ["repo_topic1", "repo_topic2"]
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"]

mock_label_1 = MagicMock()
mock_label_1.name = "bug"
mock_label_2 = MagicMock()
mock_label_2.name = "feature"
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 MockModel(IssueIndexMixin):
def __init__(self):
self.author = mock_author
self.project = mock_project
self.repository = mock_repository
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"
self.title = "Issue Title"
self.summary = "Issue Summary"
self.hint = "Issue Hint"
self.labels = MagicMock(all=lambda: [mock_label_1, mock_label_2])

return MockModel()
class TestIssueIndexMixin:
"""Test suite for IssueIndexMixin."""

@pytest.mark.parametrize(
("attr", "expected"),
Expand All @@ -66,22 +57,21 @@ def __init__(self):
("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_contributors_count", FOLLOWERS_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_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, mock_model, attr, expected):
assert getattr(mock_model, attr) == expected
def test_issue_index(self, attr, expected):
mock_instance = MockModel()
assert getattr(mock_instance, attr) == expected
39 changes: 24 additions & 15 deletions backend/tests/github/models/mixins/repository_tests.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
from datetime import datetime, timezone

import pytest

from apps.github.models.mixins.repository import RepositoryIndexMixin
Expand All @@ -8,7 +10,27 @@
STARS_COUNT = 5


class TestRepositoryIndexMixin:
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"),
[
Expand All @@ -18,24 +40,11 @@ class TestRepositoryIndexMixin:
("idx_languages", ["Python", "JavaScript"]),
("idx_name", "Name"),
("idx_open_issues_count", OPEN_ISSUES_COUNT),
("idx_pushed_at", "2021-01-01"),
("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):
class MockModel(RepositoryIndexMixin):
def __init__(self):
self.contributors_count = 5
self.description = "Description"
self.forks_count = 5
self.top_languages = ["Python", "JavaScript"]
self.name = "Name"
self.open_issues_count = 5
self.pushed_at = "2021-01-01"
self.stars_count = 5
self.topics = ["Topic1", "Topic2"]

mock_instance = MockModel()

assert getattr(mock_instance, attr) == expected
105 changes: 67 additions & 38 deletions backend/tests/github/models/mixins/user_tests.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,79 @@
from datetime import datetime, timezone
from unittest.mock import MagicMock

import pytest

from apps.github.models.mixins.user import UserIndexMixin
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"

FOLLOWERS_COUNT = 5
FOLLOWING_COUNT = 3
REPOSITORIES_COUNT = 42
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_languages = ["Python", "JavaScript"]
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."""

class TestUserIndexMixin:
@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"),
("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_languages", ["Python", "JavaScript"]),
("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_user_index(self, attr, expected):
class MockModel(UserIndexMixin):
def __init__(self):
self.avatar_url = "https://avatars.githubusercontent.com/u/1"
self.bio = "Bio"
self.company = "Company"
self.created_at = "2021-01-01T00:00:00Z"
self.email = "[email protected]"
self.followers_count = 5
self.following_count = 3
self.location = "Earth"
self.login = "user_login"
self.name = "John Doe"
self.public_repositories_count = 42
self.title = "GitHub User"
self.updated_at = "2021-12-31T23:59:59Z"
self.url = "https://api.github.com/users/user_login"

def test_issue_index(self, attr, expected):
mock_instance = MockModel()

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_contributions = Mock(contributions=default_contribution_value)
repository_contributor.from_github(gh_contributions)
gh_contributions_mock = Mock(contributions=default_contribution_value)
repository_contributor.from_github(gh_contributions_mock)

assert repository_contributor.contributions_count == default_contribution_value

Expand Down
4 changes: 2 additions & 2 deletions backend/tests/github/models/repository_tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def test_is_indexable(self, mock_gh_repository):
mock_gh_repository.is_indexable = False
assert not mock_gh_repository.is_indexable

def test_from_github_with_missing_funding(self, mock_gh_repository, mocker):
def test_from_github_with_missing_funding(self, mock_gh_repository):
mock_gh_repository.get_contents.side_effect = GithubException(
data={"status": "404"}, status=404
)
Expand All @@ -68,7 +68,7 @@ def test_from_github_with_missing_funding(self, mock_gh_repository, mocker):
assert not repository.has_funding_yml
assert repository.is_funding_policy_compliant

def test_from_github_with_funding(self, mock_gh_repository, mocker):
def test_from_github_with_funding(self, mock_gh_repository):
mock_gh_repository.get_contents.return_value = MagicMock(
content=b64encode(b"test: test").decode()
)
Expand Down
Loading

0 comments on commit 9ac2d7d

Please sign in to comment.