Skip to content

Split APIv3 tests on different files #5911

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

Merged
merged 6 commits into from
Jul 16, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
135 changes: 135 additions & 0 deletions readthedocs/api/v3/tests/mixins.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
import datetime
import json
from pathlib import Path

import django_dynamic_fixture as fixture
from django.contrib.auth.models import User
from django.core.cache import cache
from django.test import TestCase
from django.utils.timezone import make_aware
from rest_framework.authtoken.models import Token
from rest_framework.test import APIClient

from readthedocs.builds.models import Build, Version
from readthedocs.projects.models import Project
from readthedocs.redirects.models import Redirect


class APIEndpointMixin(TestCase):

fixtures = []

def setUp(self):
created = make_aware(datetime.datetime(2019, 4, 29, 10, 0, 0))
modified = make_aware(datetime.datetime(2019, 4, 29, 12, 0, 0))

self.me = fixture.get(
User,
date_joined=created,
username='testuser',
projects=[],
)
self.token = fixture.get(Token, key='me', user=self.me)
# Defining all the defaults helps to avoid creating ghost / unwanted
# objects (like a Project for translations/subprojects)
self.project = fixture.get(
Project,
pub_date=created,
modified_date=modified,
description='Project description',
repo='https://github.com/rtfd/project',
project_url='http://project.com',
name='project',
slug='project',
related_projects=[],
main_language_project=None,
users=[self.me],
versions=[],
)
for tag in ('tag', 'project', 'test'):
self.project.tags.add(tag)

self.redirect = fixture.get(
Redirect,
create_dt=created,
update_dt=modified,
from_url='/docs/',
to_url='/documentation/',
redirect_type='page',
project=self.project,
)

self.subproject = fixture.get(
Project,
pub_date=created,
modified_date=modified,
description='SubProject description',
repo='https://github.com/rtfd/subproject',
project_url='http://subproject.com',
name='subproject',
slug='subproject',
related_projects=[],
main_language_project=None,
users=[],
versions=[],
)
# self.translation = fixture.get(Project, slug='translation')
Copy link
Member

Choose a reason for hiding this comment

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

we don't need this I guess


self.project.add_subproject(self.subproject)
# self.project.add_translation(self.translation)
Copy link
Member

Choose a reason for hiding this comment

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

and this one


self.version = fixture.get(
Version,
slug='v1.0',
verbose_name='v1.0',
identifier='a1b2c3',
project=self.project,
active=True,
built=True,
type='tag',
Copy link
Member

Choose a reason for hiding this comment

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

It would be better to get this from the readthedocs/projects/constants.py

Copy link
Member

Choose a reason for hiding this comment

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

or builds/constants.py can't remember the exact file

)

self.build = fixture.get(
Build,
date=created,
type='html',
state='finished',
error='',
success=True,
_config = {'property': 'test value'},
version=self.version,
project=self.project,
builder='builder01',
commit='a1b2c3',
length=60,
)

self.other = fixture.get(User, projects=[])
self.others_token = fixture.get(Token, key='other', user=self.other)
self.others_project = fixture.get(
Project,
slug='others_project',
related_projects=[],
main_language_project=None,
users=[self.other],
versions=[],
)

self.client = APIClient()

def tearDown(self):
# Cleanup cache to avoid throttling on tests
cache.clear()

def _get_response_dict(self, view_name):
filename = Path(__file__).absolute().parent / 'responses' / f'{view_name}.json'
return json.load(open(filename))

def assertDictEqual(self, d1, d2):
"""
Show the differences between the dicts in a human readable way.

It's just a helper for debugging API responses.
"""
import datadiff
return super().assertDictEqual(d1, d2, datadiff.diff(d1, d2))
54 changes: 54 additions & 0 deletions readthedocs/api/v3/tests/test_builds.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from .mixins import APIEndpointMixin
from django.urls import reverse


class BuildsEndpointTests(APIEndpointMixin):

def test_projects_builds_list(self):
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
response = self.client.get(
reverse(
'projects-builds-list',
kwargs={
'parent_lookup_project__slug': self.project.slug,
}),
)
self.assertEqual(response.status_code, 200)

def test_projects_builds_detail(self):
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
response = self.client.get(
reverse(
'projects-builds-detail',
kwargs={
'parent_lookup_project__slug': self.project.slug,
'build_pk': self.build.pk,
}),
)
self.assertEqual(response.status_code, 200)

self.assertDictEqual(
response.json(),
self._get_response_dict('projects-builds-detail'),
)

def test_projects_versions_builds_list_post(self):
self.client.credentials(HTTP_AUTHORIZATION=f'Token {self.token.key}')
self.assertEqual(self.project.builds.count(), 1)
response = self.client.post(
reverse(
'projects-versions-builds-list',
kwargs={
'parent_lookup_project__slug': self.project.slug,
'parent_lookup_version__slug': self.version.slug,
}),
)
self.assertEqual(response.status_code, 202)
self.assertEqual(self.project.builds.count(), 2)

response_json = response.json()
response_json['build']['created'] = '2019-04-29T14:00:00Z'
self.assertDictEqual(
response_json,
self._get_response_dict('projects-versions-builds-list_POST'),
)
Loading