Skip to content

Instantly share code, notes, and snippets.

Last active February 22, 2016 20:48
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
Star You must be signed in to star a gist
What would you like to do?
from rest_framework.test import APITransactionTestCase
from django.test import TestCase, override_settings
from django.core.urlresolvers import reverse
from django.core.cache import cache
from django.conf import settings
from django.utils import timezone
from ad_on.users.tests.factories import UserFactory
from ..models import Card, Device, Log
from ..enums import ContentType
from .factories import AppFactory, CardFactory, AppCardFactory, ArticleFactory, VideoFactory, HtmlFactory, DeviceFactory
class AppConfigTests(APITransactionTestCase):
def setUp(self): = AppFactory.create(name='Config Test App')
self.user = UserFactory.create(username='test_user')
def test_card_list_count(self):
""" Test card-list length for AppConfig endpoint
self.client.credentials(HTTP_AUTHORIZATION='App-Token ' +
CardFactory.create_batch(5,, content_type=ContentType.article.value)
for card in Card.objects.filter(
url = reverse('api_1:app-config')
response = self.client.get(url)
self.assertEqual(len(['cards']), 5)
@override_settings(CACHES={'default': {'BACKEND': 'django.core.cache.backends.locmem.LocMemCache'}})
def test_caching(self):
"""Test caching for AppConfig endpoint
# create a card
self.client.credentials(HTTP_AUTHORIZATION='App-Token ' +
url = reverse('api_1:app-config')
card = CardFactory.create(content_type=ContentType.article.value)
AppCardFactory.create(card=card, content_type=ContentType.article.value,, position=1)
response = self.client.get(url)
self.assertEqual(len(['cards']), 1)
# create another card and check if cache is active and only one card is returned
card_2 = CardFactory.create(content_type=ContentType.article.value)
AppCardFactory.create(card=card_2, content_type=ContentType.article.value,, position=2)
response = self.client.get(url)
self.assertEqual(len(['cards']), 1) # <-- here it fails! (2 != 1)
# check if after clearing the cache both cards are returned
response = self.client.get(url)
self.assertEqual(len(['cards']), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment