diff --git a/tests/test_model_serializer.py b/tests/test_model_serializer.py index ec922b6d5..d518dd581 100644 --- a/tests/test_model_serializer.py +++ b/tests/test_model_serializer.py @@ -24,6 +24,9 @@ class CustomField(models.Field): pass +COLOR_CHOICES = (('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')) + + class RegularFieldsModel(models.Model): """ A model class for testing regular flat fields. @@ -32,6 +35,7 @@ class RegularFieldsModel(models.Model): big_integer_field = models.BigIntegerField() boolean_field = models.BooleanField(default=False) char_field = models.CharField(max_length=100) + choices_field = models.CharField(max_length=100, choices=COLOR_CHOICES) comma_seperated_integer_field = models.CommaSeparatedIntegerField(max_length=100) date_field = models.DateField() datetime_field = models.DateTimeField() @@ -68,6 +72,7 @@ class TestRegularFieldMappings(TestCase): big_integer_field = IntegerField() boolean_field = BooleanField(default=False) char_field = CharField(max_length=100) + choices_field = ChoiceField(choices=[('red', 'Red'), ('blue', 'Blue'), ('green', 'Green')]) comma_seperated_integer_field = CharField(max_length=100, validators=[]) date_field = DateField() datetime_field = DateTimeField() diff --git a/tests/test_modelinfo.py b/tests/test_modelinfo.py deleted file mode 100644 index 04b67f041..000000000 --- a/tests/test_modelinfo.py +++ /dev/null @@ -1,31 +0,0 @@ -from django.test import TestCase -from django.utils import six -from rest_framework.utils.model_meta import _resolve_model -from tests.models import BasicModel - - -class ResolveModelTests(TestCase): - """ - `_resolve_model` should return a Django model class given the - provided argument is a Django model class itself, or a properly - formatted string representation of one. - """ - def test_resolve_django_model(self): - resolved_model = _resolve_model(BasicModel) - self.assertEqual(resolved_model, BasicModel) - - def test_resolve_string_representation(self): - resolved_model = _resolve_model('tests.BasicModel') - self.assertEqual(resolved_model, BasicModel) - - def test_resolve_unicode_representation(self): - resolved_model = _resolve_model(six.text_type('tests.BasicModel')) - self.assertEqual(resolved_model, BasicModel) - - def test_resolve_non_django_model(self): - with self.assertRaises(ValueError): - _resolve_model(TestCase) - - def test_resolve_improper_string_representation(self): - with self.assertRaises(ValueError): - _resolve_model('BasicModel') diff --git a/tests/test_templatetags.py b/tests/test_templatetags.py index 75ee0eaa8..b04a937e0 100644 --- a/tests/test_templatetags.py +++ b/tests/test_templatetags.py @@ -4,6 +4,7 @@ from django.test import TestCase from rest_framework.test import APIRequestFactory from rest_framework.templatetags.rest_framework import add_query_param, urlize_quoted_links + factory = APIRequestFactory() @@ -49,3 +50,37 @@ class Issue1386Tests(TestCase): # example from issue #1386, this shouldn't raise an exception urlize_quoted_links("asdf:[/p]zxcv.com") + + +class URLizerTests(TestCase): + """ + Test if both JSON and YAML URLs are transformed into links well + """ + def _urlize_dict_check(self, data): + """ + For all items in dict test assert that the value is urlized key + """ + for original, urlized in data.items(): + assert urlize_quoted_links(original, nofollow=False) == urlized + + def test_json_with_url(self): + """ + Test if JSON URLs are transformed into links well + """ + data = {} + data['"url": "http://api/users/1/", '] = \ + '"url": "http://api/users/1/", ' + data['"foo_set": [\n "http://api/foos/1/"\n], '] = \ + '"foo_set": [\n "http://api/foos/1/"\n], ' + self._urlize_dict_check(data) + + def test_yaml_with_url(self): + """ + Test if YAML URLs are transformed into links well + """ + data = {} + data['''{users: 'http://api/users/'}'''] = \ + '''{users: 'http://api/users/'}''' + data['''foo_set: ['http://api/foos/1/']'''] = \ + '''foo_set: ['http://api/foos/1/']''' + self._urlize_dict_check(data) diff --git a/tests/test_urlizer.py b/tests/test_urlizer.py deleted file mode 100644 index a77aa22ab..000000000 --- a/tests/test_urlizer.py +++ /dev/null @@ -1,37 +0,0 @@ -from __future__ import unicode_literals -from django.test import TestCase -from rest_framework.templatetags.rest_framework import urlize_quoted_links - - -class URLizerTests(TestCase): - """ - Test if both JSON and YAML URLs are transformed into links well - """ - def _urlize_dict_check(self, data): - """ - For all items in dict test assert that the value is urlized key - """ - for original, urlized in data.items(): - assert urlize_quoted_links(original, nofollow=False) == urlized - - def test_json_with_url(self): - """ - Test if JSON URLs are transformed into links well - """ - data = {} - data['"url": "http://api/users/1/", '] = \ - '"url": "http://api/users/1/", ' - data['"foo_set": [\n "http://api/foos/1/"\n], '] = \ - '"foo_set": [\n "http://api/foos/1/"\n], ' - self._urlize_dict_check(data) - - def test_yaml_with_url(self): - """ - Test if YAML URLs are transformed into links well - """ - data = {} - data['''{users: 'http://api/users/'}'''] = \ - '''{users: 'http://api/users/'}''' - data['''foo_set: ['http://api/foos/1/']'''] = \ - '''foo_set: ['http://api/foos/1/']''' - self._urlize_dict_check(data) diff --git a/tests/test_breadcrumbs.py b/tests/test_utils.py similarity index 67% rename from tests/test_breadcrumbs.py rename to tests/test_utils.py index 780fd5c4d..96c5f997e 100644 --- a/tests/test_breadcrumbs.py +++ b/tests/test_utils.py @@ -1,8 +1,11 @@ from __future__ import unicode_literals from django.conf.urls import patterns, url from django.test import TestCase +from django.utils import six +from rest_framework.utils.model_meta import _resolve_model from rest_framework.utils.breadcrumbs import get_breadcrumbs from rest_framework.views import APIView +from tests.models import BasicModel class Root(APIView): @@ -24,6 +27,7 @@ class NestedResourceRoot(APIView): class NestedResourceInstance(APIView): pass + urlpatterns = patterns( '', url(r'^$', Root.as_view()), @@ -35,9 +39,10 @@ urlpatterns = patterns( class BreadcrumbTests(TestCase): - """Tests the breadcrumb functionality used by the HTML renderer.""" - - urls = 'tests.test_breadcrumbs' + """ + Tests the breadcrumb functionality used by the HTML renderer. + """ + urls = 'tests.test_utils' def test_root_breadcrumbs(self): url = '/' @@ -98,3 +103,30 @@ class BreadcrumbTests(TestCase): get_breadcrumbs(url), [('Root', '/')] ) + + +class ResolveModelTests(TestCase): + """ + `_resolve_model` should return a Django model class given the + provided argument is a Django model class itself, or a properly + formatted string representation of one. + """ + def test_resolve_django_model(self): + resolved_model = _resolve_model(BasicModel) + self.assertEqual(resolved_model, BasicModel) + + def test_resolve_string_representation(self): + resolved_model = _resolve_model('tests.BasicModel') + self.assertEqual(resolved_model, BasicModel) + + def test_resolve_unicode_representation(self): + resolved_model = _resolve_model(six.text_type('tests.BasicModel')) + self.assertEqual(resolved_model, BasicModel) + + def test_resolve_non_django_model(self): + with self.assertRaises(ValueError): + _resolve_model(TestCase) + + def test_resolve_improper_string_representation(self): + with self.assertRaises(ValueError): + _resolve_model('BasicModel')