Skip to content

Commit c73f29c

Browse files
committed
Add query tests
1 parent a860622 commit c73f29c

File tree

1 file changed

+79
-0
lines changed

1 file changed

+79
-0
lines changed
Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
from unittest.mock import Mock, patch
2+
3+
import pytest
4+
5+
from apps.owasp.graphql.nodes.project_health_metrics import ProjectHealthMetricsNode
6+
from apps.owasp.graphql.queries.project_health_metrics import ProjectHealthMetricsQuery
7+
8+
9+
class TestProjectHealthMetricsQuery:
10+
"""Test cases for ProjectHealthMetricsQuery class."""
11+
12+
def test_unhealthy_projects_query_has_strawberry_definition(self):
13+
"""Check if ProjectHealthMetricsQuery has valid Strawberry definition."""
14+
assert hasattr(ProjectHealthMetricsQuery, "__strawberry_definition__")
15+
16+
field_names = [
17+
field.name for field in ProjectHealthMetricsQuery.__strawberry_definition__.fields
18+
]
19+
assert "unhealthy_projects" in field_names
20+
21+
def test_unhealthy_projects_field_configuration(self):
22+
"""Test if 'unhealthy_projects' field is configured properly."""
23+
unhealthy_projects_field = next(
24+
field
25+
for field in ProjectHealthMetricsQuery.__strawberry_definition__.fields
26+
if field.name == "unhealthy_projects"
27+
)
28+
29+
assert unhealthy_projects_field.type.of_type is ProjectHealthMetricsNode
30+
31+
arg_names = [arg.python_name for arg in unhealthy_projects_field.arguments]
32+
expected_arg_names = [
33+
"contributors_count_requirement_compliant",
34+
"funding_requirement_compliant",
35+
"no_recent_commits",
36+
"no_recent_releases",
37+
"leaders_requirement_compliant",
38+
"limit",
39+
"long_open_issues",
40+
"long_unanswered_issues",
41+
"long_unassigned_issues",
42+
"low_score",
43+
]
44+
assert set(arg_names) == set(expected_arg_names)
45+
46+
47+
class TestProjectHealthMetricsResolution:
48+
"""Test cases for resolving the unhealthy_projects field."""
49+
50+
@pytest.fixture(autouse=True)
51+
def _setup(self):
52+
with patch(
53+
"apps.owasp.models.project_health_metrics.ProjectHealthMetrics.objects"
54+
) as mocked_objects:
55+
self.mocked_objects = mocked_objects
56+
yield
57+
58+
def test_resolve_unhealthy_projects(self):
59+
"""Test the resolution of unhealthy projects."""
60+
# Mock the queryset
61+
mock_queryset = Mock()
62+
self.mocked_objects.select_related.return_value = mock_queryset
63+
mock_queryset.order_by.return_value.distinct.return_value = mock_queryset
64+
# Filters
65+
mock_filter = Mock()
66+
mock_queryset.filter.return_value = mock_filter
67+
mock_filter.select_related.return_value = [mock_filter]
68+
# Create an instance of the query class
69+
query_instance = ProjectHealthMetricsQuery()
70+
71+
# Call the method
72+
result = query_instance.unhealthy_projects()
73+
74+
# Assert that the mocked queryset was called correctly
75+
self.mocked_objects.select_related.assert_called_with("project")
76+
mock_queryset.order_by.assert_called_with("project__key", "-nest_created_at")
77+
78+
assert isinstance(result, list)
79+
mock_filter.select_related.assert_called_with("project")

0 commit comments

Comments
 (0)