-
-
Notifications
You must be signed in to change notification settings - Fork 181
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
refactor(AccessLogsExport): export in background TASK-1145 #5216
Merged
Merged
Changes from 5 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
2459f33
refactor export_task_in_background to take any subclass of ImportExpo…
RuthShryock 8cbde84
fix quotes
RuthShryock f0d7f11
fix formatting error due to f-string missing placeholders
RuthShryock 03a2bf1
fix root url
RuthShryock bf1aafd
use KOBOFORM_URL for tests
RuthShryock 571097c
adjust how base class is passed to export_task_in_background
RuthShryock e75d9d4
fix quotes
RuthShryock c234d2f
address review comments
RuthShryock 9c69830
add kpi prefix to export task
RuthShryock File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
import datetime | ||
from unittest.mock import Mock, patch | ||
|
||
from django.conf import settings | ||
from django.core.exceptions import ObjectDoesNotExist | ||
from django.test import TestCase | ||
|
||
from kobo.apps.kobo_auth.shortcuts import User | ||
from kpi.models.import_export_task import ProjectViewExportTask | ||
from kpi.tasks import export_task_in_background | ||
|
||
|
||
class ExportTaskInBackgroundTests(TestCase): | ||
def setUp(self): | ||
self.user = User.objects.create(username='someuser', email='[email protected]') | ||
self.mock_data = {'type': 'assets', 'view': 'summary'} | ||
self.task = ProjectViewExportTask.objects.create( | ||
uid='test_uid', data=self.mock_data, user=self.user | ||
) | ||
self.project_view = Mock() | ||
self.project_view.get_countries.return_value = [] | ||
|
||
@patch('django.core.mail.send_mail') | ||
@patch('kobo.apps.project_views.models.project_view.ProjectView.objects.get') | ||
def test_export_task_success(self, mock_get_project_view, mock_send_mail): | ||
mock_get_project_view.return_value = self.project_view | ||
self.task.run = Mock(return_value=self.task) | ||
|
||
export_task_in_background( | ||
self.task.uid, self.user.username, ProjectViewExportTask | ||
) | ||
|
||
self.task.refresh_from_db() | ||
self.assertEqual(self.task.status, 'complete') | ||
|
||
mock_send_mail.assert_called_once() | ||
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. shortcut: you can use |
||
args, kwargs = mock_send_mail.call_args | ||
root_url = settings.KOBOFORM_URL | ||
expected_message = ( | ||
'Hello {},\n\n' | ||
'Your report is complete: {}' | ||
'/private-media/{}/exports/' | ||
'assets-{}-view_summary-{}.csv\n\n' | ||
'Regards,\n' | ||
'KoboToolbox' | ||
).format( | ||
self.user.username, | ||
root_url, | ||
self.user.username, | ||
self.user.username, | ||
datetime.datetime.now().strftime('%Y-%m-%dT%H%M%SZ'), | ||
) | ||
self.assertEqual(kwargs['subject'], 'Project View Report Complete') | ||
self.assertEqual(expected_message, kwargs['message']) | ||
|
||
@patch('django.core.mail.send_mail') | ||
@patch('kobo.apps.project_views.models.project_view.ProjectView.objects.get') | ||
def test_invalid_export_task_uid(self, mock_get_project_view, mock_send_mail): | ||
mock_get_project_view.side_effect = ObjectDoesNotExist | ||
with self.assertRaisesMessage( | ||
ObjectDoesNotExist, 'ProjectViewExportTask matching query does not exist.' | ||
): | ||
export_task_in_background( | ||
'invalid_uid', self.user.username, ProjectViewExportTask | ||
) | ||
|
||
mock_send_mail.assert_not_called() | ||
|
||
@patch('django.core.mail.send_mail') | ||
@patch('kobo.apps.project_views.models.project_view.ProjectView.objects.get') | ||
def test_invalid_username(self, mock_get_project_view, mock_send_mail): | ||
with self.assertRaisesMessage( | ||
ObjectDoesNotExist, 'User matching query does not exist.' | ||
): | ||
export_task_in_background( | ||
self.task.uid, 'invalid_username', ProjectViewExportTask | ||
) | ||
|
||
mock_send_mail.assert_not_called() | ||
|
||
@patch('django.core.mail.send_mail') | ||
@patch('kobo.apps.project_views.models.project_view.ProjectView.objects.get') | ||
@patch('kpi.models.ProjectViewExportTask._run_task') | ||
def test_export_task_error( | ||
self, mock_run_task, mock_get_project_view, mock_send_mail | ||
): | ||
mock_get_project_view.return_value = self.project_view | ||
mock_run_task.side_effect = Exception('Simulated task failure') | ||
|
||
export_task_in_background( | ||
self.task.uid, self.user.username, ProjectViewExportTask | ||
) | ||
|
||
self.task.refresh_from_db() | ||
self.assertEqual(self.task.status, 'error') | ||
|
||
@patch('django.core.mail.send_mail') | ||
@patch('kobo.apps.project_views.models.project_view.ProjectView.objects.get') | ||
@patch('kpi.models.ProjectViewExportTask._run_task') | ||
def test_email_not_sent_if_export_errors( | ||
self, mock_run_task, mock_get_project_view, mock_send_mail | ||
): | ||
mock_get_project_view.return_value = self.project_view | ||
mock_run_task.side_effect = Exception('Simulated task failure') | ||
|
||
export_task_in_background( | ||
self.task.uid, self.user.username, ProjectViewExportTask | ||
) | ||
|
||
mock_send_mail.assert_not_called() |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
You can patch the whole class at once instead of each individual method