Skip to content

Commit

Permalink
Make get_stats_sum return dict instead of AggregatedStats instance
Browse files Browse the repository at this point in the history
Django 3.2 throws an error when instantiating an abstract model.
This commit also adds tests for the views that were previously not covered.
  • Loading branch information
PFischbeck committed Apr 16, 2021
1 parent 5eed239 commit ef38d9c
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 35 deletions.
16 changes: 8 additions & 8 deletions pontoon/base/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -428,14 +428,14 @@ def get_stats_sum(cls, qs):
"""
Get sum of stats for all items in the queryset.
"""
return cls(
total_strings=sum(x.total_strings for x in qs),
approved_strings=sum(x.approved_strings for x in qs),
fuzzy_strings=sum(x.fuzzy_strings for x in qs),
strings_with_errors=sum(x.strings_with_errors for x in qs),
strings_with_warnings=sum(x.strings_with_warnings for x in qs),
unreviewed_strings=sum(x.unreviewed_strings for x in qs),
)
return {
"total_strings": sum(x.total_strings for x in qs),
"approved_strings": sum(x.approved_strings for x in qs),
"fuzzy_strings": sum(x.fuzzy_strings for x in qs),
"strings_with_errors": sum(x.strings_with_errors for x in qs),
"strings_with_warnings": sum(x.strings_with_warnings for x in qs),
"unreviewed_strings": sum(x.unreviewed_strings for x in qs),
}

@classmethod
def get_top_instances(cls, qs):
Expand Down
52 changes: 25 additions & 27 deletions pontoon/projects/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@
from django.http import HttpResponse
from django.shortcuts import render

from pontoon.base.models import Project
from pontoon.base.tests import (
ProjectFactory,
ResourceFactory,
TranslationFactory,
)
from pontoon.base.tests import TranslationFactory


@pytest.mark.django_db
def test_projects_list(client, project_a, resource_a):
"""
Checks if list of projects is properly rendered.
"""
response = client.get("/projects/")
assert response.status_code == 200
assert response.resolver_match.view_name == "pontoon.projects"


@pytest.mark.django_db
Expand All @@ -24,53 +29,46 @@ def test_project_doesnt_exist(client):


@pytest.mark.django_db
def test_project_view(client):
def test_project_view(client, project_a, resource_a):
"""
Checks if project page is returned properly.
"""
project = ProjectFactory.create(visibility=Project.Visibility.PUBLIC)
ResourceFactory.create(project=project)

with patch("pontoon.projects.views.render", wraps=render) as mock_render:
client.get(f"/projects/{project.slug}/")
assert mock_render.call_args[0][2]["project"] == project
client.get(f"/projects/{project_a.slug}/")
assert mock_render.call_args[0][2]["project"] == project_a


@pytest.mark.django_db
def test_project_top_contributors(client):
def test_project_top_contributors(client, project_a, project_b):
"""
Tests if view returns top contributors specific for given project.
"""
first_project = ProjectFactory.create(visibility=Project.Visibility.PUBLIC)
ResourceFactory.create(project=first_project)
first_project_contributor = TranslationFactory.create(
entity__resource__project=first_project
project_a_contributor = TranslationFactory.create(
entity__resource__project=project_a
).user

second_project = ProjectFactory.create(visibility=Project.Visibility.PUBLIC)
ResourceFactory.create(project=second_project)
second_project_contributor = TranslationFactory.create(
entity__resource__project=second_project
project_b_contributor = TranslationFactory.create(
entity__resource__project=project_b
).user

with patch(
"pontoon.projects.views.ProjectContributorsView.render_to_response",
return_value=HttpResponse(""),
) as mock_render:
client.get(
f"/projects/{first_project.slug}/ajax/contributors/",
f"/projects/{project_a.slug}/ajax/contributors/",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
assert mock_render.call_args[0][0]["project"] == first_project
assert mock_render.call_args[0][0]["project"] == project_a
assert list(mock_render.call_args[0][0]["contributors"]) == [
first_project_contributor
project_a_contributor
]

client.get(
f"/projects/{second_project.slug}/ajax/contributors/",
f"/projects/{project_b.slug}/ajax/contributors/",
HTTP_X_REQUESTED_WITH="XMLHttpRequest",
)
assert mock_render.call_args[0][0]["project"] == second_project
assert mock_render.call_args[0][0]["project"] == project_b
assert list(mock_render.call_args[0][0]["contributors"]) == [
second_project_contributor
project_b_contributor
]
10 changes: 10 additions & 0 deletions pontoon/teams/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ def managers():
return _get_sorted_users()


@pytest.mark.django_db
def test_teams_list(client, locale_a):
"""
Tests if the teams list is rendered properly.
"""
response = client.get("/teams/")
assert response.status_code == 200
assert response.resolver_match.view_name == "pontoon.teams"


@pytest.mark.django_db
def test_missing_locale(client):
"""
Expand Down

0 comments on commit ef38d9c

Please sign in to comment.