Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow filtering versions by active #4414

Merged
merged 2 commits into from Aug 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 2 additions & 0 deletions docs/api/v2.rst
Expand Up @@ -168,6 +168,8 @@ Version list
:>json array results: Array of ``Version`` objects.

:query string project__slug: Narrow to the versions for a specific ``Project``
:query boolean active: Pass ``true`` or ``false`` to show only active or inactive versions.
By default, the API returns all versions.

.. _api-version-detail:

Expand Down
2 changes: 1 addition & 1 deletion readthedocs/restapi/views/model_views.py
Expand Up @@ -233,7 +233,7 @@ class VersionViewSet(UserSelectViewSet):
serializer_class = VersionSerializer
admin_serializer_class = VersionAdminSerializer
model = Version
filter_fields = ('project__slug',)
filter_fields = ('active', 'project__slug',)


class BuildViewSetBase(UserSelectViewSet):
Expand Down
35 changes: 35 additions & 0 deletions readthedocs/rtd_tests/tests/test_api.py
Expand Up @@ -11,6 +11,7 @@
from allauth.socialaccount.models import SocialAccount
from django.contrib.auth.models import User
from django.core.urlresolvers import reverse
from django.http import QueryDict
from django.test import TestCase
from django.utils import six
from django_dynamic_fixture import get
Expand Down Expand Up @@ -968,6 +969,40 @@ def test_get_version_by_id(self):
version_data,
)

def test_get_active_versions(self):
"""
Test the full response of ``/api/v2/version/?project__slug=pip&active=true``
"""
pip = Project.objects.get(slug='pip')

data = QueryDict('', mutable=True)
data.update({
'project__slug': pip.slug,
'active': 'true',
})
url = '{base_url}?{querystring}'.format(
base_url=reverse('version-list'),
querystring=data.urlencode()
)

resp = self.client.get(url, content_type='application/json')
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data['count'], pip.versions.filter(active=True).count())

# Do the same thing for inactive versions
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: this could be achieved in a fancy way by using pytest parametrize: https://docs.pytest.org/en/latest/parametrize.html

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I actually think that would make the test less clear.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

data.update({
'project__slug': pip.slug,
'active': 'false',
})
url = '{base_url}?{querystring}'.format(
base_url=reverse('version-list'),
querystring=data.urlencode()
)

resp = self.client.get(url, content_type='application/json')
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.data['count'], pip.versions.filter(active=False).count())


class TaskViewsTests(TestCase):

Expand Down