Beispiel #1
0
    def test_save(self):
        """Tests the custom save method."""
        
        user = User(username='******', password='******', email='*****@*****.**')
        user.save()
        
        user2 = User(username='******', password='******', email='*****@*****.**')
        user2.save()
        
        p = {
             'title': 'Test Title (1)',
             'author': user,
             'content': 'some content',
             'status': 'U'
             }
        
        post1 = Post(**p)
        post1.save()
        
        # Initially No Slug or pub_date should be assigned.
        self.failUnlessEqual(post1.slug, None, 'Post1 slug was %s when it should be None' % post1.slug)
        self.failUnless(not post1.pub_date, 'PubDate is not Null!')

        # Publishing it should give it a slug and pub_date automatically.
        post1.status = 'P'
        post1.save()
        slugexp = 'test-title-1' 
        slugact = post1.slug
        self.failUnlessEqual(slugexp, slugact, 'Slug was %s but expected %s' % (slugact, slugact))
        self.failUnless(post1.pub_date, 'Post1 pub_date was not set!')
 
        # Set the pub_date back to setup next test.
        new_date = datetime.now() - timedelta(days=1) - timedelta(hours=2)
        post1.pub_date = new_date
        post1.save()
        
        # Setup another post with old pubdate, then change to now to see what happens.
        # Test some slug collisions and such.
        p2 = p.copy()
        p2['status'] = 'P'
        post2 = Post(**p2)
        post2.save()
        
        # Test the slugify feature
        slug = 'test-title-1' 
        self.failUnlessEqual(slug, post2.slug, 'Post2 slug was %s but expected %s' % (post2.slug, slug))
 
        # Now change the pub_date to match p1 and see if it throws a validation error.
        post2.pub_date = datetime.now() - timedelta(days=1) - timedelta(hours=1)
        self.assertRaises(ValidationError, post2.save)
        
        # Test the auto-increment feature of the unique_slugify method.
        post2.slug = None
        post2.save()
        expected = 'test-title-1-2'
        self.failUnlessEqual(post2.slug, expected, 'Post2 slug was %s but expected %s' % (post2.slug, expected))