Skip to content

Instantly share code, notes, and snippets.

@t-io
Last active Feb 22, 2016
Embed
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):
self.app = AppFactory.create(name='Config Test App')
self.user = UserFactory.create(username='test_user')
self.app.account.add(self.user)
def test_card_list_count(self):
""" Test card-list length for AppConfig endpoint
"""
self.client.credentials(HTTP_AUTHORIZATION='App-Token ' + self.app.app_key)
CardFactory.create_batch(5, app=self.app, content_type=ContentType.article.value)
for card in Card.objects.filter(app=self.app):
AppCardFactory.create(card=card, app=self.app)
url = reverse('api_1:app-config')
response = self.client.get(url)
self.assertEqual(len(response.data['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 ' + self.app.app_key)
url = reverse('api_1:app-config')
card = CardFactory.create(content_type=ContentType.article.value)
ArticleFactory.create(card=card)
AppCardFactory.create(card=card, content_type=ContentType.article.value, app=self.app, position=1)
response = self.client.get(url)
self.assertEqual(len(response.data['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)
ArticleFactory.create(card=card_2)
AppCardFactory.create(card=card_2, content_type=ContentType.article.value, app=self.app, position=2)
response = self.client.get(url)
self.assertEqual(len(response.data['cards']), 1) # <-- here it fails! (2 != 1)
# check if after clearing the cache both cards are returned
cache.clear()
response = self.client.get(url)
self.assertEqual(len(response.data['cards']), 2)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment