Anonymous avatar Anonymous committed 1352882

[soc2010/test-refactor] Updated field_defaults modeltest to unittest

Comments (0)

Files changed (2)

tests/modeltests/field_defaults/models.py

 
     def __unicode__(self):
         return self.headline
-
-__test__ = {'API_TESTS': u"""
->>> from datetime import datetime
-
-# No articles are in the system yet.
->>> Article.objects.all()
-[]
-
-# Create an Article.
->>> a = Article(id=None)
-
-# Grab the current datetime it should be very close to the default that just
-# got saved as a.pub_date
->>> now = datetime.now()
-
-# Save it into the database. You have to call save() explicitly.
->>> a.save()
-
-# Now it has an ID. Note it's a long integer, as designated by the trailing "L".
->>> a.id
-1L
-
-# Access database columns via Python attributes.
->>> a.headline
-u'Default headline'
-
-# make sure the two dates are sufficiently close
->>> d = now - a.pub_date
->>> d.seconds < 5
-True
-
-# make sure that SafeString/SafeUnicode fields work
->>> from django.utils.safestring import SafeUnicode, SafeString
->>> a.headline = SafeUnicode(u'Iñtërnâtiônàlizætiøn1')
->>> a.save()
->>> a.headline = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8'))
->>> a.save()
-"""}

tests/modeltests/field_defaults/tests.py

+# coding: utf-8
+from datetime import datetime
+
+from django.test import TestCase
+from django.utils.safestring import SafeUnicode, SafeString
+
+from models import Article
+
+class FieldDefaultsTestCase(TestCase):
+    def test_article_defaults(self):
+        # No articles are in the system yet.
+        self.assertEqual(len(Article.objects.all()), 0)
+        
+        # Create an Article.
+        a = Article(id=None)
+
+        # Grab the current datetime it should be very close to the
+        # default that just got saved as a.pub_date
+        now = datetime.now()
+
+        # Save it into the database. You have to call save() explicitly.
+        a.save()
+
+        # Now it has an ID. Note it's a long integer, as designated by
+        # the trailing "L".
+        self.assertEqual(a.id, 1L)
+
+        # Access database columns via Python attributes.
+        self.assertEqual(a.headline, u'Default headline')
+
+        # make sure the two dates are sufficiently close
+        #fixme, use the new unittest2 function
+        d = now - a.pub_date
+        self.assertTrue(d.seconds < 5)
+
+        # make sure that SafeString/SafeUnicode fields work
+        a.headline = SafeUnicode(u'Iñtërnâtiônàlizætiøn1')
+        a.save()
+        a.headline = SafeString(u'Iñtërnâtiônàlizætiøn1'.encode('utf-8'))
+        a.save()
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.