-
Notifications
You must be signed in to change notification settings - Fork 87
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
Tests for adding podcast API [WIP] #336
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,6 +18,7 @@ | |
from mygpo.history.models import EpisodeHistoryEntry | ||
from mygpo.test import create_auth_string | ||
from mygpo.utils import get_timestamp | ||
from mygpo.data.tasks import update_podcasts | ||
|
||
|
||
class AdvancedAPITests(unittest.TestCase): | ||
|
@@ -185,6 +186,30 @@ def test_episode_info(self): | |
|
||
self.assertEqual(resp.status_code, 200) | ||
|
||
def test_add_podcast_existed(self): | ||
"""Test add podcast API for existed podcast""" | ||
url = reverse('api-add-podcast') | ||
body = {'url': self.podcast.url} | ||
location = reverse('api-podcast-info') + '?url=' + self.podcast.url | ||
add_resp = self.client.post(url, body, content_type='application/json') | ||
self.assertEqual(add_resp.status_code, 302) | ||
self.assertEqual(add_resp.get('Location'), location) | ||
|
||
def test_add_podcast_new(self): | ||
"""Test add podcast API for new podcast""" | ||
podcast_url = 'http://example.com/new-podcast.xml' | ||
add_url = reverse('api-add-podcast') | ||
body = {'url': podcast_url} | ||
add_resp = self.client.post(add_url, body, content_type='application/json') | ||
self.assertEqual(add_resp.status_code, 202) | ||
status_url = add_resp.get('Location') | ||
status_resp = self.client.get(status_url) | ||
self.assertEqual(status_resp.status_code, 200) | ||
self.assertEqual(status_resp.json().get('status'), 'pending') | ||
job_id = status_url.split('/')[-1] | ||
result = update_podcasts.apply(task_id=job_id) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could you add a comment describing what this does exactly? That might not be obvious for somebody reading the code in the future. |
||
self.assertTrue(result.ready()) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we're still not testing the end result. Now the test checks whether the task has completed, but what we actually want to achieve is to have a new podcast. I think that the checks should verify that a podcast with the submitted URL has been created. |
||
|
||
|
||
class EpisodeActionTests(TestCase): | ||
def setUp(self): | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If I understand this test correctly, it only tests whether a task has been started - but not its result.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stefankoegl
I add tests for the Celery task result below.
Celery testing is particularly tricky. I've tried the two references you provided but could get neither pytest fixture or pytest-django working for Celery. I turn to celery.app.task.Task.apply() to execute the task ad-hoc.
The problem is that the Celery instance is the one from mygpo.celery, the same as the real one, thus accessing the production database. I think this is a potential problem. But I can't find a better solution at the moment.
Any suggestion is appreciated. 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When running tests, the DATABASE setting is initialized to point to the test database (see https://docs.djangoproject.com/en/dev/topics/testing/overview/#the-test-database). Therefore I don't think its possible to access the prod database through
mygpo.celery
from within a test. These two should be completely isolated from each other.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@stefankoegl Does the new PR for testing meet the requirement? If so, I'll move on to documentation and hopefully close the issue. 😄