Skip to content

Commit

Permalink
chore: add tests for XBlockSkillsViewSet caching behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
hamza-56 committed Oct 9, 2024
1 parent 9f3aadf commit 42aed42
Showing 1 changed file with 25 additions and 1 deletion.
26 changes: 25 additions & 1 deletion tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from django.db.models import Count, Sum
from django.test import Client, RequestFactory, TestCase
from django.urls import reverse

from edx_django_utils.cache import TieredCache
from taxonomy.models import JobPath, JobSkills, Skill, SkillCategory, SkillValidationConfiguration
from taxonomy.utils import generate_and_store_job_to_job_description
from taxonomy.views import JobSkillsView, admin
Expand Down Expand Up @@ -626,6 +626,30 @@ def test_xblocks_api_with_skill_validation_disabled(self):
assert api_response.status_code == 200
assert api_response.json() == []

def test_list_xblock_skills_cache_hit(self):
cached_data = {'results': [{'id': str(self.xblock_skills[0].id)}]}
with patch.object(
TieredCache,
'get_cached_response',
wraps=TieredCache.get_cached_response
) as mock_get_cached_response:
mock_get_cached_response.return_value = mock.MagicMock(is_found=True, value=cached_data)
response = self.client.get(self.view_url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(response.data, cached_data)
mock_get_cached_response.assert_called_once()

def test_list_xblock_skills_caching(self):
with patch.object(TieredCache, 'set_all_tiers') as mock_set_all_tiers, \
patch.object(TieredCache, 'get_cached_response', wraps=TieredCache.get_cached_response) as mock_get_cached_response:

mock_get_cached_response.return_value = mock.MagicMock(is_found=False)
response = self.client.get(self.view_url)

self.assertEqual(response.status_code, status.HTTP_200_OK)
mock_get_cached_response.assert_called_once()
mock_set_all_tiers.assert_called_once()


@mark.django_db
class TestJobPathAPIView(TestCase):
Expand Down

0 comments on commit 42aed42

Please sign in to comment.