Skip to content

Commit a9e1d2e

Browse files
committed
squash!: Add tests for export task
1 parent 78c716f commit a9e1d2e

1 file changed

Lines changed: 45 additions & 0 deletions

File tree

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
"""
2+
Unit tests for content libraries Celery tasks
3+
"""
4+
5+
from ..models import ContentLibrary
6+
from .base import ContentLibrariesRestApiTest
7+
8+
from openedx.core.djangoapps.content_libraries.tasks import backup_library
9+
from user_tasks.models import UserTaskArtifact
10+
11+
12+
class ContentLibraryBackupTaskTest(ContentLibrariesRestApiTest):
13+
"""
14+
Tests for Content Library export task.
15+
"""
16+
17+
def setUp(self) -> None:
18+
super().setUp()
19+
20+
# Create Content Libraries
21+
self._create_library("test-lib-task-1", "Test Library Task 1")
22+
23+
# Fetch the created ContentLibrary objects so we can access their learning_package.id
24+
self.lib1 = ContentLibrary.objects.get(slug="test-lib-task-1")
25+
self.wrong_task_id = '11111111-1111-1111-1111-111111111111'
26+
27+
def test_backup_task_returns_task_id(self):
28+
result = backup_library.delay(self.user.id, str(self.lib1.library_key))
29+
assert result.task_id is not None
30+
31+
def test_backup_task_success(self):
32+
result = backup_library.delay(self.user.id, str(self.lib1.library_key))
33+
assert result.state == 'SUCCESS'
34+
# Ensure an artifact was created with the output file
35+
artifact = UserTaskArtifact.objects.filter(status__task_id=result.task_id, name='Output').first()
36+
assert artifact is not None
37+
assert artifact.file.name.endswith('.zip')
38+
39+
def test_backup_task_failure(self):
40+
result = backup_library.delay(self.user.id, self.wrong_task_id)
41+
assert result.state == 'FAILURE'
42+
# Ensure an error artifact was created
43+
artifact = UserTaskArtifact.objects.filter(status__task_id=result.task_id, name='Error').first()
44+
assert artifact is not None
45+
assert artifact.text is not None

0 commit comments

Comments
 (0)