Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Added tests for optional fields feature

  • Loading branch information...
commit d7a74b1964294a79ed369713c53415f8707cdeab 1 parent e467744
Numan S authored
10 tests/core/tests/fields.py
@@ -23,8 +23,9 @@ def test_init(self):
23 23 self.assertEqual(field_1.null, False)
24 24 self.assertEqual(field_1.value, None)
25 25 self.assertEqual(field_1.help_text, '')
  26 + self.assertEqual(field_1.use_in, 'all')
26 27
27   - field_2 = ApiField(attribute='foo', default=True, null=True, readonly=True, help_text='Foo.')
  28 + field_2 = ApiField(attribute='foo', default=True, null=True, readonly=True, help_text='Foo.', use_in="foo")
28 29 self.assertEqual(field_2.instance_name, None)
29 30 self.assertEqual(field_2.attribute, 'foo')
30 31 self.assertEqual(field_2._default, True)
@@ -32,6 +33,13 @@ def test_init(self):
32 33 self.assertEqual(field_2.value, None)
33 34 self.assertEqual(field_2.readonly, True)
34 35 self.assertEqual(field_2.help_text, 'Foo.')
  36 + self.assertEqual(field_1.use_in, 'all')
  37 +
  38 + field_3 = ApiField(use_in="list")
  39 + self.assertEqual(field_3.use_in, 'list')
  40 +
  41 + field_4 = ApiField(use_in="detail")
  42 + self.assertEqual(field_4.use_in, 'detail')
35 43
36 44 def test_dehydrated_type(self):
37 45 field_1 = ApiField()
52 tests/core/tests/resources.py
@@ -39,6 +39,28 @@ class TestObject(object):
39 39 date_joined = None
40 40
41 41
  42 +class BasicResourceWithDifferentListAndDetailFields(Resource):
  43 + name = fields.CharField(attribute='name', use_in="all")
  44 + view_count = fields.IntegerField(attribute='view_count', default=0, use_in="detail")
  45 + date_joined = fields.DateTimeField(null=True, use_in="list")
  46 +
  47 + def dehydrate_date_joined(self, bundle):
  48 + if getattr(bundle.obj, 'date_joined', None) is not None:
  49 + return bundle.obj.date_joined
  50 +
  51 + if bundle.data.get('date_joined') is not None:
  52 + return bundle.data.get('date_joined')
  53 +
  54 + return aware_datetime(2010, 3, 27, 22, 30, 0)
  55 +
  56 + def hydrate_date_joined(self, bundle):
  57 + bundle.obj.date_joined = bundle.data['date_joined']
  58 + return bundle
  59 +
  60 + class Meta:
  61 + object_class = TestObject
  62 + resource_name = 'basic'
  63 +
42 64 class BasicResource(Resource):
43 65 name = fields.CharField(attribute='name')
44 66 view_count = fields.IntegerField(attribute='view_count', default=0)
@@ -276,6 +298,36 @@ def test_inheritance(self):
276 298 self.assertEqual(mrofr.fields['test'].default, 'test_a')
277 299 self.assertEqual(mrofr.fields['name'].default, 'Mr. Field')
278 300
  301 + def test_full_dehydrate_with_use_in(self):
  302 + test_object_1 = TestObject()
  303 + test_object_1.name = 'Daniel'
  304 + test_object_1.view_count = 12
  305 + test_object_1.date_joined = aware_datetime(2010, 3, 30, 9, 0, 0)
  306 + test_object_1.foo = "Hi, I'm ignored."
  307 +
  308 + basic = BasicResourceWithDifferentListAndDetailFields()
  309 + test_bundle_1 = basic.build_bundle(obj=test_object_1)
  310 +
  311 + # Sanity check.
  312 + self.assertEqual(basic.name.value, None)
  313 + self.assertEqual(basic.view_count.value, None)
  314 + self.assertEqual(basic.date_joined.value, None)
  315 +
  316 + #check hydration with details
  317 + bundle_1 = basic.full_dehydrate(test_bundle_1)
  318 + self.assertEqual(bundle_1.data['name'], 'Daniel')
  319 + self.assertEqual(bundle_1.data['view_count'], 12)
  320 + self.assertEqual(bundle_1.data.get('date_joined'), None)
  321 +
  322 + #now check dehydration with lists
  323 + test_bundle_2 = basic.build_bundle(obj=test_object_1)
  324 +
  325 + bundle_2 = basic.full_dehydrate(test_bundle_2, for_list=True)
  326 + self.assertEqual(bundle_2.data['name'], 'Daniel')
  327 + self.assertEqual(bundle_2.data.get('view_count'), None)
  328 + self.assertEqual(bundle_2.data['date_joined'].year, 2010)
  329 + self.assertEqual(bundle_2.data['date_joined'].day, 30)
  330 +
279 331 def test_full_dehydrate(self):
280 332 test_object_1 = TestObject()
281 333 test_object_1.name = 'Daniel'

0 comments on commit d7a74b1

Please sign in to comment.
Something went wrong with that request. Please try again.