Mercurial > public > sg101
changeset 1217:e2409dab30c3 modernize tip
Add unit test for banners templatetags.
author | Brian Neal <bgneal@gmail.com> |
---|---|
date | Wed, 12 Feb 2025 20:18:50 -0600 |
parents | 42fef17a89a4 |
children | |
files | banners/templatetags/banner_tags.py banners/tests/__init__.py banners/tests/test_templatetags.py |
diffstat | 2 files changed, 50 insertions(+), 1 deletions(-) [+] |
line wrap: on
line diff
--- a/banners/templatetags/banner_tags.py Wed Feb 12 19:03:43 2025 -0600 +++ b/banners/templatetags/banner_tags.py Wed Feb 12 20:18:50 2025 -0600 @@ -25,7 +25,7 @@ For each campaign, a list of banner URLs are kept in Redis. Each time this tag is called, the front banner is popped off the list. When the list is empty, we refresh the list from the database. In this way the banners for a - campaign are cycled through. + campaign are cycled through. """ key = BANNER_URL_KEY % slug
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/banners/tests/test_templatetags.py Wed Feb 12 20:18:50 2025 -0600 @@ -0,0 +1,49 @@ +""" +Tests for the banners application's template tags. + +""" +from django.test import TestCase + +from mock import call, patch, Mock + + +class BannersTemplateTagsTestCase(TestCase): + def setUp(self): + self.url = 'http://example.com/some/campaign' + self.slug = 'slug' + + @patch('banners.templatetags.banner_tags.get_redis_connection') + def test_banner_url_cache_hit(self, connection_mock): + redis_mock = Mock() + connection_mock.return_value = redis_mock + redis_mock.lpop.return_value = self.url + + self.assertEqual(banner_url(self.slug), self.url) + + self.assertEqual(redis_mock.mock_calls, [ + call.lpop('banners:url:{}'.format(self.slug)), + ]) + + @patch('banners.templatetags.banner_tags.Banner') + @patch('banners.templatetags.banner_tags.get_redis_connection') + def test_banner_url_cache_miss(self, connection_mock, banner_mock): + redis_mock = Mock() + connection_mock.return_value = redis_mock + redis_mock.lpop.return_value = None + + qs = [Mock(), Mock()] + qs[0].image.configure_mock(url='url0') + qs[1].image.configure_mock(url='url1') + + banner_mock.objects.filter.return_value = qs + + self.assertEqual(banner_url(self.slug), 'url0') + + redis_key = 'banners:url:{}'.format(self.slug) + self.assertEqual(redis_mock.mock_calls, [ + call.lpop(redis_key), + call.rpush(redis_key, 'url1'), + ]) + self.assertEqual(banner_mock.objects.filter.mock_calls, [ + call(campaign__slug=self.slug), + ])