def test_get_value_from_lazy_object(self): field = DEDField(attr='translation') instance = NonCallableMock(translation=_("foo")) self.assertIsInstance( field.get_value_from_instance(instance), string_types ) self.assertEqual(field.get_value_from_instance(instance), "foo")
def test_get_value_from_instance_with_unknown_attr(self): class Dummy: attr1 = "foo" field = DEDField(attr='attr2') self.assertRaises(VariableLookupError, field.get_value_from_instance, Dummy())
def test_get_value_from_none(self): field = DEDField(attr='related.none') instance = NonCallableMock(attr1="foo", related=None) self.assertEqual(field.get_value_from_instance(instance), None)
def test_get_value_from_instance_related_callable(self): field = DEDField(attr='related.callable') instance = NonCallableMock(related=NonCallableMock( callable=Mock(return_value="bar"), attr1="foo")) self.assertEqual(field.get_value_from_instance(instance), "bar")
def test_get_value_from_instance_related_attr(self): field = DEDField(attr='related.attr1') instance = NonCallableMock(attr1="foo", related=NonCallableMock(attr1="bar")) self.assertEqual(field.get_value_from_instance(instance), "bar")
def test_get_value_from_instance_attr(self): field = DEDField(attr='attr1') instance = NonCallableMock(attr1="foo", attr2="bar") self.assertEqual(field.get_value_from_instance(instance), "foo")
def test_attr_to_path(self): field = DEDField(attr='field') self.assertEqual(field._path, ['field']) field = DEDField(attr='obj.field') self.assertEqual(field._path, ['obj', 'field'])