Advertisement
Guest User

Untitled

a guest
Jan 25th, 2014
111
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.94 KB | None | 0 0
  1.  
  2. Model:
  3.  
  4. class Post(models.Model):
  5.     title = models.CharField(max_length=255)
  6.     machine_name = models.SlugField(max_length=255, unique=True)
  7.     description = models.CharField(max_length=255, blank=True)
  8.     content = models.TextField(blank=True)
  9.     publication_date = models.DateTimeField()
  10.     published = models.BooleanField(default=True)
  11.     owner = models.ForeignKey(auth.models.User)
  12.     tags=TaggableManager(blank=True)
  13.     keywords = models.CharField(max_length=255, blank=True)
  14.     images = models.ManyToManyField(Image, blank=True )
  15.  
  16. tests.py
  17.  
  18. """
  19. This file demonstrates writing tests using the unittest module. These will pass
  20. when you run "manage.py test".
  21.  
  22. Replace this with more appropriate tests for your application.
  23. """
  24.  
  25. from django.test import TestCase
  26. from posts.models import Post, Image
  27. from django.utils import timezone
  28. from django.contrib.auth.models import User
  29.  
  30. class PostsTest(TestCase):
  31.  
  32.     def setUp(self):
  33.  
  34.         self.user = User.objects.create_user(username='testuser', email='testuser@test.com', password='top_secret')
  35.    
  36.  
  37.     def create_image(self, name = 'test_image', image = 'test.jpg'):
  38.         return Image.objects.create(name = name, image = image)
  39.  
  40.     # Tag list is using django-taggit, in the admin adding tag text separated by a comma adds the tags
  41.     def create_post(self):
  42.  
  43.         return Post.objects.create(title = 'title',
  44.                                     machine_name = 'machine_name',
  45.                                     description = 'description',    
  46.                                     content = 'content',
  47.                                     publication_date = timezone.now(),
  48.                                     owner = self.user,
  49.                                     keywords = 'key')
  50.  
  51.  
  52.     def test_image_creation(self):
  53.         i = self.create_image('testImage', 'test.jpg')
  54.         self.assertTrue(isinstance(i, Image))
  55.         self.assertEqual(i.__unicode__(), i.name)
  56.  
  57.  
  58.     def test_post_creation(self):
  59.         a = self.create_post()
  60.         self.assertTrue(isinstance(a, Post))
  61.         self.assertEqual(a.__unicode__(), a.title)
  62.  
  63.  
  64.     def tearDown(self):
  65.         self.user.delete()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement