|
4 | 4 | from unittest import mock |
5 | 5 |
|
6 | 6 | from django.conf import settings |
| 7 | +from kolibri_content import models as kolibrimodels |
| 8 | +from kolibri_content.router import using_content_database |
7 | 9 | from le_utils.constants import licenses |
8 | 10 |
|
9 | 11 | import contentcuration.models as ccmodels |
|
12 | 14 | from contentcuration.tests.utils.restricted_filesystemstorage import ( |
13 | 15 | RestrictedFileSystemStorage, |
14 | 16 | ) |
| 17 | +from contentcuration.utils.publish import create_content_database |
15 | 18 | from contentcuration.utils.publish import create_draft_channel_version |
16 | 19 | from contentcuration.utils.publish import ensure_versioned_database_exists |
17 | 20 | from contentcuration.utils.publish import increment_channel_version |
@@ -439,3 +442,45 @@ def test_second_draft_publish_refreshes_channel_snapshot_fields(self): |
439 | 442 | draft_version.channel_thumbnail_encoding, {"base64": "new_thumb"} |
440 | 443 | ) |
441 | 444 | self.assertEqual(draft_version.channel_language_id, "fr") |
| 445 | + |
| 446 | + |
| 447 | +class MapChannelToKolibriChannelTestCase(StudioTestCase): |
| 448 | + def setUp(self): |
| 449 | + super().setUp() |
| 450 | + self.channel = testdata.channel() |
| 451 | + # icon_encoding must not be None — the kolibri content DB thumbnail column is NOT NULL. |
| 452 | + # publish_channel calls set_channel_icon_encoding before create_content_database; |
| 453 | + # since we call create_content_database directly we set it here instead. |
| 454 | + self.channel.icon_encoding = "" |
| 455 | + self.channel.save() |
| 456 | + # increment_channel_version + refresh_from_db gives channel.version > 0, |
| 457 | + # so channel.version + 1 is distinguishable from the draft value of 0. |
| 458 | + increment_channel_version(self.channel) |
| 459 | + self.channel.refresh_from_db() |
| 460 | + |
| 461 | + def _get_channel_metadata_version(self, is_draft_version): |
| 462 | + with mock.patch("contentcuration.utils.publish.save_export_database"): |
| 463 | + tempdb = create_content_database( |
| 464 | + self.channel, |
| 465 | + force=True, |
| 466 | + user_id=self.admin_user.id, |
| 467 | + force_exercises=False, |
| 468 | + is_draft_version=is_draft_version, |
| 469 | + ) |
| 470 | + try: |
| 471 | + with using_content_database(tempdb): |
| 472 | + return kolibrimodels.ChannelMetadata.objects.get( |
| 473 | + id=self.channel.id |
| 474 | + ).version |
| 475 | + finally: |
| 476 | + if os.path.exists(tempdb): |
| 477 | + os.remove(tempdb) |
| 478 | + |
| 479 | + def test_draft_publish_sets_version_zero(self): |
| 480 | + self.assertEqual(self._get_channel_metadata_version(is_draft_version=True), 0) |
| 481 | + |
| 482 | + def test_non_draft_publish_sets_version_plus_one(self): |
| 483 | + self.assertEqual( |
| 484 | + self._get_channel_metadata_version(is_draft_version=False), |
| 485 | + self.channel.version + 1, |
| 486 | + ) |
0 commit comments