Skip to content

Commit 91c5409

Browse files
trucoddarkid15r
andauthored
test- inmproved test coverage of github internal nodes more than 80% … (#2042)
* test- inmproved test coverage of github internal nodes more than 80% updated * Update code --------- Co-authored-by: Arkadii Yakovets <[email protected]>
1 parent 0ae9943 commit 91c5409

File tree

7 files changed

+627
-47
lines changed

7 files changed

+627
-47
lines changed

backend/tests/apps/github/api/internal/nodes/issue_test.py

Lines changed: 59 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test cases for IssueNode."""
22

3+
from unittest.mock import Mock
4+
35
from apps.github.api.internal.nodes.issue import IssueNode
46

57

@@ -22,3 +24,60 @@ def test_issue_node_fields(self):
2224
"repository_name",
2325
}
2426
assert field_names == expected_field_names
27+
28+
def test_author_field(self):
29+
"""Test author field resolution."""
30+
mock_issue = Mock()
31+
mock_author = Mock()
32+
mock_issue.author = mock_author
33+
34+
result = IssueNode.author(mock_issue)
35+
assert result == mock_author
36+
37+
def test_organization_name_with_organization(self):
38+
"""Test organization_name field when organization exists."""
39+
mock_issue = Mock()
40+
mock_repository = Mock()
41+
mock_organization = Mock()
42+
mock_organization.login = "test-org"
43+
mock_repository.organization = mock_organization
44+
mock_issue.repository = mock_repository
45+
46+
result = IssueNode.organization_name(mock_issue)
47+
assert result == "test-org"
48+
49+
def test_organization_name_without_organization(self):
50+
"""Test organization_name field when organization doesn't exist."""
51+
mock_issue = Mock()
52+
mock_repository = Mock()
53+
mock_repository.organization = None
54+
mock_issue.repository = mock_repository
55+
56+
result = IssueNode.organization_name(mock_issue)
57+
assert result is None
58+
59+
def test_organization_name_without_repository(self):
60+
"""Test organization_name field when repository doesn't exist."""
61+
mock_issue = Mock()
62+
mock_issue.repository = None
63+
64+
result = IssueNode.organization_name(mock_issue)
65+
assert result is None
66+
67+
def test_repository_name_with_repository(self):
68+
"""Test repository_name field when repository exists."""
69+
mock_issue = Mock()
70+
mock_repository = Mock()
71+
mock_repository.name = "test-repo"
72+
mock_issue.repository = mock_repository
73+
74+
result = IssueNode.repository_name(mock_issue)
75+
assert result == "test-repo"
76+
77+
def test_repository_name_without_repository(self):
78+
"""Test repository_name field when repository doesn't exist."""
79+
mock_issue = Mock()
80+
mock_issue.repository = None
81+
82+
result = IssueNode.repository_name(mock_issue)
83+
assert result is None

backend/tests/apps/github/api/internal/nodes/milestone_test.py

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
"""Test cases for MilestoneNode."""
22

3+
from unittest.mock import Mock
4+
5+
import pytest
6+
37
from apps.github.api.internal.nodes.milestone import MilestoneNode
48

59

@@ -26,3 +30,78 @@ def test_milestone_node_fields(self):
2630
"url",
2731
}
2832
assert field_names == expected_field_names
33+
34+
def test_author_field(self):
35+
"""Test author field resolution."""
36+
mock_milestone = Mock()
37+
mock_author = Mock()
38+
mock_milestone.author = mock_author
39+
40+
result = MilestoneNode.author(mock_milestone)
41+
assert result == mock_author
42+
43+
def test_organization_name_with_organization(self):
44+
"""Test organization_name field when organization exists."""
45+
mock_milestone = Mock()
46+
mock_repository = Mock()
47+
mock_organization = Mock()
48+
mock_organization.login = "test-org"
49+
mock_repository.organization = mock_organization
50+
mock_milestone.repository = mock_repository
51+
52+
result = MilestoneNode.organization_name(mock_milestone)
53+
assert result == "test-org"
54+
55+
def test_organization_name_without_organization(self):
56+
"""Test organization_name field when organization doesn't exist."""
57+
mock_milestone = Mock()
58+
mock_repository = Mock()
59+
mock_repository.organization = None
60+
mock_milestone.repository = mock_repository
61+
62+
result = MilestoneNode.organization_name(mock_milestone)
63+
assert result is None
64+
65+
def test_organization_name_without_repository(self):
66+
"""Test organization_name field when repository doesn't exist."""
67+
mock_milestone = Mock()
68+
mock_milestone.repository = None
69+
70+
result = MilestoneNode.organization_name(mock_milestone)
71+
assert result is None
72+
73+
def test_progress_with_issues(self):
74+
"""Test progress calculation with issues."""
75+
mock_milestone = Mock()
76+
mock_milestone.closed_issues_count = 7
77+
mock_milestone.open_issues_count = 3
78+
79+
result = MilestoneNode.progress(mock_milestone)
80+
assert result == pytest.approx(70.0)
81+
82+
def test_progress_without_issues(self):
83+
"""Test progress calculation without issues."""
84+
mock_milestone = Mock()
85+
mock_milestone.closed_issues_count = 0
86+
mock_milestone.open_issues_count = 0
87+
88+
result = MilestoneNode.progress(mock_milestone)
89+
assert result == pytest.approx(0.0)
90+
91+
def test_repository_name_with_repository(self):
92+
"""Test repository_name field when repository exists."""
93+
mock_milestone = Mock()
94+
mock_repository = Mock()
95+
mock_repository.name = "test-repo"
96+
mock_milestone.repository = mock_repository
97+
98+
result = MilestoneNode.repository_name(mock_milestone)
99+
assert result == "test-repo"
100+
101+
def test_repository_name_without_repository(self):
102+
"""Test repository_name field when repository doesn't exist."""
103+
mock_milestone = Mock()
104+
mock_milestone.repository = None
105+
106+
result = MilestoneNode.repository_name(mock_milestone)
107+
assert result is None

backend/tests/apps/github/api/internal/nodes/organization_test.py

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
"""Test cases for OrganizationNode."""
22

3+
from unittest.mock import Mock, patch
4+
35
from apps.github.api.internal.nodes.organization import (
46
OrganizationNode,
57
OrganizationStatsNode,
@@ -45,6 +47,82 @@ def test_resolve_url(self):
4547
assert url_field is not None
4648
assert url_field.type is str
4749

50+
@patch("apps.github.models.repository.Repository.objects")
51+
@patch("apps.github.models.repository_contributor.RepositoryContributor.objects")
52+
def test_stats_method_with_data(self, mock_repo_contributor_objects, mock_repository_objects):
53+
"""Test stats method with actual data."""
54+
# Mock repository queryset
55+
mock_repositories = Mock()
56+
mock_repositories.count.return_value = 5
57+
mock_repositories.aggregate.return_value = {
58+
"total_stars": 100,
59+
"total_forks": 50,
60+
"total_issues": 25,
61+
}
62+
mock_repository_objects.filter.return_value = mock_repositories
63+
64+
# Mock repository contributor queryset
65+
mock_contributors = Mock()
66+
mock_contributors.values.return_value.distinct.return_value.count.return_value = 15
67+
mock_repo_contributor_objects.filter.return_value = mock_contributors
68+
69+
# Create mock organization
70+
mock_organization = Mock()
71+
72+
# Call stats method
73+
result = OrganizationNode.stats(mock_organization)
74+
75+
# Verify result
76+
assert isinstance(result, OrganizationStatsNode)
77+
assert result.total_repositories == 5
78+
assert result.total_contributors == 15
79+
assert result.total_stars == 100
80+
assert result.total_forks == 50
81+
assert result.total_issues == 25
82+
83+
@patch("apps.github.models.repository.Repository.objects")
84+
@patch("apps.github.models.repository_contributor.RepositoryContributor.objects")
85+
def test_stats_method_with_none_values(
86+
self, mock_repo_contributor_objects, mock_repository_objects
87+
):
88+
"""Test stats method when aggregate returns None values."""
89+
# Mock repository queryset
90+
mock_repositories = Mock()
91+
mock_repositories.count.return_value = 3
92+
mock_repositories.aggregate.return_value = {
93+
"total_stars": None,
94+
"total_forks": None,
95+
"total_issues": None,
96+
}
97+
mock_repository_objects.filter.return_value = mock_repositories
98+
99+
# Mock repository contributor queryset
100+
mock_contributors = Mock()
101+
mock_contributors.values.return_value.distinct.return_value.count.return_value = 8
102+
mock_repo_contributor_objects.filter.return_value = mock_contributors
103+
104+
# Create mock organization
105+
mock_organization = Mock()
106+
107+
# Call stats method
108+
result = OrganizationNode.stats(mock_organization)
109+
110+
# Verify result with default values
111+
assert isinstance(result, OrganizationStatsNode)
112+
assert result.total_repositories == 3
113+
assert result.total_contributors == 8
114+
assert result.total_stars == 0
115+
assert result.total_forks == 0
116+
assert result.total_issues == 0
117+
118+
def test_url_method(self):
119+
"""Test url method."""
120+
mock_organization = Mock()
121+
mock_organization.url = "https://github.com/test-org"
122+
123+
result = OrganizationNode.url(mock_organization)
124+
assert result == "https://github.com/test-org"
125+
48126

49127
class TestOrganizationStatsNode:
50128
def test_organization_stats_node(self):
@@ -59,3 +137,19 @@ def test_organization_stats_node(self):
59137
field.name for field in OrganizationStatsNode.__strawberry_definition__.fields
60138
}
61139
assert field_names == expected_fields
140+
141+
def test_organization_stats_node_creation(self):
142+
"""Test creating OrganizationStatsNode with values."""
143+
stats = OrganizationStatsNode(
144+
total_repositories=10,
145+
total_contributors=25,
146+
total_stars=150,
147+
total_forks=75,
148+
total_issues=30,
149+
)
150+
151+
assert stats.total_repositories == 10
152+
assert stats.total_contributors == 25
153+
assert stats.total_stars == 150
154+
assert stats.total_forks == 75
155+
assert stats.total_issues == 30
Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
"""Test cases for PullRequestNode."""
2+
3+
from unittest.mock import Mock
4+
5+
from apps.github.api.internal.nodes.pull_request import PullRequestNode
6+
7+
8+
class TestPullRequestNode:
9+
"""Test cases for PullRequestNode class."""
10+
11+
def test_pull_request_node_type(self):
12+
assert hasattr(PullRequestNode, "__strawberry_definition__")
13+
14+
def test_meta_configuration(self):
15+
field_names = {field.name for field in PullRequestNode.__strawberry_definition__.fields}
16+
expected_field_names = {
17+
"_id",
18+
"author",
19+
"created_at",
20+
"organization_name",
21+
"repository_name",
22+
"title",
23+
"url",
24+
}
25+
assert field_names == expected_field_names
26+
27+
def test_author_field(self):
28+
"""Test author field resolution."""
29+
mock_pr = Mock()
30+
mock_author = Mock()
31+
mock_pr.author = mock_author
32+
33+
result = PullRequestNode.author(mock_pr)
34+
assert result == mock_author
35+
36+
def test_organization_name_with_organization(self):
37+
"""Test organization_name field when organization exists."""
38+
mock_pr = Mock()
39+
mock_repository = Mock()
40+
mock_organization = Mock()
41+
mock_organization.login = "test-org"
42+
mock_repository.organization = mock_organization
43+
mock_pr.repository = mock_repository
44+
45+
result = PullRequestNode.organization_name(mock_pr)
46+
assert result == "test-org"
47+
48+
def test_organization_name_without_organization(self):
49+
"""Test organization_name field when organization doesn't exist."""
50+
mock_pr = Mock()
51+
mock_repository = Mock()
52+
mock_repository.organization = None
53+
mock_pr.repository = mock_repository
54+
55+
result = PullRequestNode.organization_name(mock_pr)
56+
assert result is None
57+
58+
def test_organization_name_without_repository(self):
59+
"""Test organization_name field when repository doesn't exist."""
60+
mock_pr = Mock()
61+
mock_pr.repository = None
62+
63+
result = PullRequestNode.organization_name(mock_pr)
64+
assert result is None
65+
66+
def test_repository_name_with_repository(self):
67+
"""Test repository_name field when repository exists."""
68+
mock_pr = Mock()
69+
mock_repository = Mock()
70+
mock_repository.name = "test-repo"
71+
mock_pr.repository = mock_repository
72+
73+
result = PullRequestNode.repository_name(mock_pr)
74+
assert result == "test-repo"
75+
76+
def test_repository_name_without_repository(self):
77+
"""Test repository_name field when repository doesn't exist."""
78+
mock_pr = Mock()
79+
mock_pr.repository = None
80+
81+
result = PullRequestNode.repository_name(mock_pr)
82+
assert result is None
83+
84+
def test_url_with_url(self):
85+
"""Test url field when URL exists."""
86+
mock_pr = Mock()
87+
mock_pr.url = "https://github.com/test-org/test-repo/pull/123"
88+
89+
result = PullRequestNode.url(mock_pr)
90+
assert result == "https://github.com/test-org/test-repo/pull/123"
91+
92+
def test_url_without_url(self):
93+
"""Test url field when URL doesn't exist."""
94+
mock_pr = Mock()
95+
mock_pr.url = None
96+
97+
result = PullRequestNode.url(mock_pr)
98+
assert result == ""

0 commit comments

Comments
 (0)