diff options
author | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 09:53:47 +0100 |
---|---|---|
committer | Florian Apolloner <florian@apolloner.eu> | 2013-02-26 14:36:57 +0100 |
commit | 89f40e36246100df6a11316c31a76712ebc6c501 (patch) | |
tree | 6e65639683ddaf2027908d1ecb1739e0e2ff853b /tests/properties | |
parent | b3d2ccb5bfbaf6e7fe1f98843baaa48c35a70950 (diff) | |
download | django-89f40e36246100df6a11316c31a76712ebc6c501.tar.gz |
Merged regressiontests and modeltests into the test root.
Diffstat (limited to 'tests/properties')
-rw-r--r-- | tests/properties/__init__.py | 0 | ||||
-rw-r--r-- | tests/properties/models.py | 22 | ||||
-rw-r--r-- | tests/properties/tests.py | 24 |
3 files changed, 46 insertions, 0 deletions
diff --git a/tests/properties/__init__.py b/tests/properties/__init__.py new file mode 100644 index 0000000000..e69de29bb2 --- /dev/null +++ b/tests/properties/__init__.py diff --git a/tests/properties/models.py b/tests/properties/models.py new file mode 100644 index 0000000000..5c2656570c --- /dev/null +++ b/tests/properties/models.py @@ -0,0 +1,22 @@ +""" +22. Using properties on models + +Use properties on models just like on any other Python object. +""" + +from django.db import models + + +class Person(models.Model): + first_name = models.CharField(max_length=30) + last_name = models.CharField(max_length=30) + + def _get_full_name(self): + return "%s %s" % (self.first_name, self.last_name) + + def _set_full_name(self, combined_name): + self.first_name, self.last_name = combined_name.split(' ', 1) + + full_name = property(_get_full_name) + + full_name_2 = property(_get_full_name, _set_full_name) diff --git a/tests/properties/tests.py b/tests/properties/tests.py new file mode 100644 index 0000000000..8a40d06e98 --- /dev/null +++ b/tests/properties/tests.py @@ -0,0 +1,24 @@ +from __future__ import absolute_import + +from django.test import TestCase + +from .models import Person + + +class PropertyTests(TestCase): + + def setUp(self): + self.a = Person(first_name='John', last_name='Lennon') + self.a.save() + + def test_getter(self): + self.assertEqual(self.a.full_name, 'John Lennon') + + def test_setter(self): + # The "full_name" property hasn't provided a "set" method. + self.assertRaises(AttributeError, setattr, self.a, 'full_name', 'Paul McCartney') + + # But "full_name_2" has, and it can be used to initialise the class. + a2 = Person(full_name_2 = 'Paul McCartney') + a2.save() + self.assertEqual(a2.first_name, 'Paul') |