-
Notifications
You must be signed in to change notification settings - Fork 173
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
feat: code changes for ai_languages field #4586
Open
zawan-ila
wants to merge
6
commits into
anawaz/prod-4306-1
Choose a base branch
from
anawaz/prod-4306-2
base: anawaz/prod-4306-1
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
3dc1932
chore: swap translation_languages with ai_languages
zawan-ila b191d42
chore: mgmt command changes
zawan-ila 641a2a3
test: fixes
zawan-ila 4a95b4a
temp: experiment with schema validation
zawan-ila 0434db3
fix: typo
zawan-ila b11dc4b
fix: typos
zawan-ila 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
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
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
169 changes: 169 additions & 0 deletions
169
...scovery/apps/course_metadata/management/commands/tests/test_update_course_ai_languages.py
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,169 @@ | ||
""" | ||
Unit tests for the `update_course_ai_languages` management command. | ||
""" | ||
import datetime | ||
from unittest.mock import patch | ||
|
||
import ddt | ||
from django.core.management import CommandError, call_command | ||
from django.test import TestCase | ||
from django.utils.timezone import now | ||
|
||
from course_discovery.apps.course_metadata.models import CourseRun, CourseRunType | ||
from course_discovery.apps.course_metadata.tests.factories import CourseRunFactory, PartnerFactory, SeatFactory | ||
|
||
|
||
@ddt.ddt | ||
@patch('course_discovery.apps.core.api_client.lms.LMSAPIClient.get_course_run_translations_and_transcriptions') | ||
class UpdateCourseAiLanguagesTests(TestCase): | ||
"""Test Suite for the update_course_ai_languages management command.""" | ||
|
||
AI_LANGUAGES_DATA = { | ||
'available_translation_languages': [ | ||
{'code': 'fr', 'label': 'French'}, | ||
{'code': 'cs', 'label': 'Czech'} | ||
], | ||
'feature_enabled': True, | ||
} | ||
|
||
AI_LANGUAGES_DATA_WITH_TRANSCRIPTIONS = { | ||
**AI_LANGUAGES_DATA, | ||
'transcription_languages': ['da', 'fr'] | ||
} | ||
|
||
def setUp(self): | ||
self.partner = PartnerFactory() | ||
self.course_run = CourseRunFactory() | ||
|
||
def assert_ai_langs(self, run, data): | ||
self.assertListEqual( | ||
run.ai_languages['translation_languages'], | ||
data['available_translation_languages'] | ||
) | ||
self.assertListEqual( | ||
run.ai_languages['transcription_languages'], | ||
[{'code': lang_code, 'label': lang_code} for lang_code in data.get('transcription_languages', [])] | ||
) | ||
|
||
|
||
@ddt.data(AI_LANGUAGES_DATA, AI_LANGUAGES_DATA_WITH_TRANSCRIPTIONS) | ||
def test_update_ai_languages(self, mock_data, mock_get_translations_and_transcriptions): | ||
"""Test the command with a valid course run and response data.""" | ||
mock_get_translations_and_transcriptions.return_value = mock_data | ||
|
||
call_command('update_course_ai_languages', partner=self.partner.name) | ||
|
||
course_run = CourseRun.objects.get(id=self.course_run.id) | ||
self.assert_ai_langs(course_run, mock_data) | ||
|
||
@ddt.data(AI_LANGUAGES_DATA, AI_LANGUAGES_DATA_WITH_TRANSCRIPTIONS) | ||
def test_update_ai_languages_draft(self, mock_data, mock_get_translations_and_transcriptions): | ||
""" | ||
Test the command with both draft and non-draft course runs, ensuring that the both draft and non-draft | ||
course runs are updated appropriately. | ||
""" | ||
mock_get_translations_and_transcriptions.return_value = mock_data | ||
draft_course_run = CourseRunFactory( | ||
draft=True, end=now() + datetime.timedelta(days=10) | ||
) | ||
course_run = CourseRunFactory(draft=False, draft_version_id=draft_course_run.id) | ||
|
||
call_command('update_course_ai_languages', partner=self.partner.name) | ||
|
||
course_run.refresh_from_db() | ||
self.assert_ai_langs(course_run, mock_data) | ||
|
||
draft_course_run.refresh_from_db() | ||
self.assert_ai_langs(draft_course_run, mock_data) | ||
|
||
|
||
@ddt.data(AI_LANGUAGES_DATA, AI_LANGUAGES_DATA_WITH_TRANSCRIPTIONS) | ||
def test_update_ai_languages_no_translations(self, mock_data, mock_get_translations_and_transcriptions): | ||
"""Test the command when no translations are returned for a course run.""" | ||
mock_get_translations_and_transcriptions.return_value = { | ||
**mock_data, | ||
'available_translation_languages': [], | ||
} | ||
|
||
call_command('update_course_ai_languages', partner=self.partner.name) | ||
|
||
course_run = CourseRun.objects.get(id=self.course_run.id) | ||
self.assertListEqual(course_run.ai_languages['translation_languages'], []) | ||
|
||
@ddt.data(AI_LANGUAGES_DATA, AI_LANGUAGES_DATA_WITH_TRANSCRIPTIONS) | ||
def test_command_with_active_flag(self, mock_data, mock_get_translations_and_transcriptions): | ||
"""Test the command with the active flag filtering active course runs.""" | ||
mock_get_translations_and_transcriptions.return_value = mock_data | ||
|
||
active_course_run = CourseRunFactory(end=now() + datetime.timedelta(days=10), ai_languages=None) | ||
non_active_course_run = CourseRunFactory(end=now() - datetime.timedelta(days=10), ai_languages=None) | ||
|
||
call_command('update_course_ai_languages', partner=self.partner.name, active=True) | ||
|
||
active_course_run.refresh_from_db() | ||
non_active_course_run.refresh_from_db() | ||
|
||
self.assert_ai_langs(active_course_run, mock_data) | ||
assert non_active_course_run.ai_languages is None | ||
|
||
@ddt.data(AI_LANGUAGES_DATA, AI_LANGUAGES_DATA_WITH_TRANSCRIPTIONS) | ||
def test_command_with_marketable_flag(self, mock_data, mock_get_translations_and_transcriptions): | ||
"""Test the command with the marketable flag filtering marketable course runs.""" | ||
mock_get_translations_and_transcriptions.return_value = mock_data | ||
|
||
verified_and_audit_type = CourseRunType.objects.get(slug='verified-audit') | ||
verified_and_audit_type.is_marketable = True | ||
verified_and_audit_type.save() | ||
|
||
marketable_course_run = CourseRunFactory( | ||
status='published', | ||
slug='test-course-run', | ||
type=verified_and_audit_type, | ||
ai_languages=None | ||
) | ||
seat = SeatFactory(course_run=marketable_course_run) | ||
marketable_course_run.seats.add(seat) | ||
|
||
call_command('update_course_ai_languages', partner=self.partner.name, marketable=True) | ||
|
||
marketable_course_run.refresh_from_db() | ||
self.assert_ai_langs(marketable_course_run, mock_data) | ||
|
||
@ddt.data(AI_LANGUAGES_DATA, AI_LANGUAGES_DATA_WITH_TRANSCRIPTIONS) | ||
def test_command_with_marketable_and_active_flag(self, mock_data, mock_get_translations_and_transcriptions): | ||
"""Test the command with the marketable and active flag filtering both marketable and active course runs.""" | ||
mock_get_translations_and_transcriptions.return_value = mock_data | ||
|
||
non_active_non_marketable_course_run = CourseRunFactory( | ||
end=now() - datetime.timedelta(days=10), ai_languages=None | ||
) | ||
active_non_marketable_course_run = CourseRunFactory( | ||
end=now() + datetime.timedelta(days=10), ai_languages=None | ||
) | ||
|
||
verified_and_audit_type = CourseRunType.objects.get(slug='verified-audit') | ||
verified_and_audit_type.is_marketable = True | ||
verified_and_audit_type.save() | ||
|
||
marketable_non_active_course_run = CourseRunFactory( | ||
status='published', | ||
slug='test-course-run', | ||
type=verified_and_audit_type, | ||
end=now() - datetime.timedelta(days=10), ai_languages=None | ||
) | ||
seat = SeatFactory(course_run=marketable_non_active_course_run) | ||
marketable_non_active_course_run.seats.add(seat) | ||
|
||
call_command('update_course_ai_languages', partner=self.partner.name, marketable=True, active=True) | ||
|
||
marketable_non_active_course_run.refresh_from_db() | ||
active_non_marketable_course_run.refresh_from_db() | ||
non_active_non_marketable_course_run.refresh_from_db() | ||
self.assert_ai_langs(marketable_non_active_course_run, mock_data) | ||
self.assert_ai_langs(active_non_marketable_course_run, mock_data) | ||
assert non_active_non_marketable_course_run.ai_languages is None | ||
|
||
def test_command_no_partner(self, _): | ||
"""Test the command raises an error if no valid partner is found.""" | ||
with self.assertRaises(CommandError): | ||
call_command('update_course_ai_languages', partner='nonexistent-partner') |
Oops, something went wrong.
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.
Log message can be specific i.e. translations_and_transcriptions data for course run.