Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
More tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Mishbah committed Oct 4, 2014
1 parent e9816b2 commit 3f37eb8
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
3 changes: 3 additions & 0 deletions responsive/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ class ResponsiveAppConf(AppConf):
'verbose_name': _('XXLarge screens'),
'min_width': 1921,
'max_width': None,
},
'retina': {
'pixel_ratio': 2
}
}

Expand Down
77 changes: 77 additions & 0 deletions tests/test_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

from django.core.exceptions import ImproperlyConfigured
from django.test import TestCase
from django.test.client import RequestFactory
from django.test.utils import override_settings
from django.utils.translation import ugettext_lazy as _
from django.core.cache import cache

from responsive.conf import settings
from responsive.utils import Device



class UtilsTest(TestCase):

def setUp(self):
# Every test needs access to the request factory.
self.factory = RequestFactory()

# Implement assertIsNotNone for Python runtimes < 2.7 or < 3.1
if not hasattr(TestCase, 'assertIsNotNone'):
def assertIsNotNone(self, value, *args):
self.assertNotEqual(value, None, *args)

def test_device_obj_str(self):
device = Device()
self.assertEqual(str(device), 'Device :: width=0 height=0 pixel-ratio=1.0')

def test_device_default_values(self):
device = Device()
self.assertEqual(device.width, settings.RESPONSIVE_DEFAULT_WIDTH)
self.assertEqual(device.height, settings.RESPONSIVE_DEFAULT_HEIGHT)
self.assertEqual(device.pixel_ratio, settings.RESPONSIVE_DEFAULT_PIXEL_RATIO)

@override_settings(RESPONSIVE_MEDIA_QUERIES={
'small': {
'verbose_name': _('Small screens'),
'min_width': 0,
'max_width': '640px',
},
})
def test_get_media_queries_fails(self):
# get_media_queries() should raise ImproperlyConfigured with above settings
device = Device()
self.assertRaises(ImproperlyConfigured, device.get_media_queries)

@override_settings(RESPONSIVE_MEDIA_QUERIES={
'small': {
'verbose_name': _('Small screens'),
'max_width': 640,
},
})
def test_get_media_queries_works(self):
# min_width, min_height, max_height, pixel_ratio is not defined in above settings
# get_media_queries() should set them to None
device = Device()
media_queries = device.get_media_queries()
for attr in ('min_width', 'min_height', 'max_height', 'pixel_ratio'):
self.assertEqual(media_queries['small'][attr], None)

def test_caching(self):
device = Device(width=1366, height=768)
# matched devices should be cached
self.assertIsNotNone(cache.get(device.cache_key))

# clear the cache
device.clear()
self.assertIsNone(cache.get(device.cache_key))

def test_matched_media_queries(self):
device = Device(width=1366, height=768, pixel_ratio=2)
self.assertIn('large', device.matched)
self.assertIn('retina', device.matched)


0 comments on commit 3f37eb8

Please sign in to comment.