Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added tests for optional fields feature

  • Loading branch information...
commit d7a74b1964294a79ed369713c53415f8707cdeab 1 parent e467744
Numan S authored
Showing with 61 additions and 1 deletion.
  1. +9 −1 tests/core/tests/fields.py
  2. +52 −0 tests/core/tests/resources.py
10 tests/core/tests/fields.py
View
@@ -23,8 +23,9 @@ def test_init(self):
self.assertEqual(field_1.null, False)
self.assertEqual(field_1.value, None)
self.assertEqual(field_1.help_text, '')
+ self.assertEqual(field_1.use_in, 'all')
- field_2 = ApiField(attribute='foo', default=True, null=True, readonly=True, help_text='Foo.')
+ field_2 = ApiField(attribute='foo', default=True, null=True, readonly=True, help_text='Foo.', use_in="foo")
self.assertEqual(field_2.instance_name, None)
self.assertEqual(field_2.attribute, 'foo')
self.assertEqual(field_2._default, True)
@@ -32,6 +33,13 @@ def test_init(self):
self.assertEqual(field_2.value, None)
self.assertEqual(field_2.readonly, True)
self.assertEqual(field_2.help_text, 'Foo.')
+ self.assertEqual(field_1.use_in, 'all')
+
+ field_3 = ApiField(use_in="list")
+ self.assertEqual(field_3.use_in, 'list')
+
+ field_4 = ApiField(use_in="detail")
+ self.assertEqual(field_4.use_in, 'detail')
def test_dehydrated_type(self):
field_1 = ApiField()
52 tests/core/tests/resources.py
View
@@ -39,6 +39,28 @@ class TestObject(object):
date_joined = None
+class BasicResourceWithDifferentListAndDetailFields(Resource):
+ name = fields.CharField(attribute='name', use_in="all")
+ view_count = fields.IntegerField(attribute='view_count', default=0, use_in="detail")
+ date_joined = fields.DateTimeField(null=True, use_in="list")
+
+ def dehydrate_date_joined(self, bundle):
+ if getattr(bundle.obj, 'date_joined', None) is not None:
+ return bundle.obj.date_joined
+
+ if bundle.data.get('date_joined') is not None:
+ return bundle.data.get('date_joined')
+
+ return aware_datetime(2010, 3, 27, 22, 30, 0)
+
+ def hydrate_date_joined(self, bundle):
+ bundle.obj.date_joined = bundle.data['date_joined']
+ return bundle
+
+ class Meta:
+ object_class = TestObject
+ resource_name = 'basic'
+
class BasicResource(Resource):
name = fields.CharField(attribute='name')
view_count = fields.IntegerField(attribute='view_count', default=0)
@@ -276,6 +298,36 @@ def test_inheritance(self):
self.assertEqual(mrofr.fields['test'].default, 'test_a')
self.assertEqual(mrofr.fields['name'].default, 'Mr. Field')
+ def test_full_dehydrate_with_use_in(self):
+ test_object_1 = TestObject()
+ test_object_1.name = 'Daniel'
+ test_object_1.view_count = 12
+ test_object_1.date_joined = aware_datetime(2010, 3, 30, 9, 0, 0)
+ test_object_1.foo = "Hi, I'm ignored."
+
+ basic = BasicResourceWithDifferentListAndDetailFields()
+ test_bundle_1 = basic.build_bundle(obj=test_object_1)
+
+ # Sanity check.
+ self.assertEqual(basic.name.value, None)
+ self.assertEqual(basic.view_count.value, None)
+ self.assertEqual(basic.date_joined.value, None)
+
+ #check hydration with details
+ bundle_1 = basic.full_dehydrate(test_bundle_1)
+ self.assertEqual(bundle_1.data['name'], 'Daniel')
+ self.assertEqual(bundle_1.data['view_count'], 12)
+ self.assertEqual(bundle_1.data.get('date_joined'), None)
+
+ #now check dehydration with lists
+ test_bundle_2 = basic.build_bundle(obj=test_object_1)
+
+ bundle_2 = basic.full_dehydrate(test_bundle_2, for_list=True)
+ self.assertEqual(bundle_2.data['name'], 'Daniel')
+ self.assertEqual(bundle_2.data.get('view_count'), None)
+ self.assertEqual(bundle_2.data['date_joined'].year, 2010)
+ self.assertEqual(bundle_2.data['date_joined'].day, 30)
+
def test_full_dehydrate(self):
test_object_1 = TestObject()
test_object_1.name = 'Daniel'
Please sign in to comment.
Something went wrong with that request. Please try again.