在Django中为内容类型模型进行单元测试

时间:2014-10-02 05:45:10

标签: python django unit-testing tdd

我对Django很新,并且已经完成了一些测试驱动开发。我尝试遵循TDD的原则,但在某些情况下,我不知道如何继续(如下面的模型)。我有一个与我在这里展示的模型非常相似的模型。从本质上讲,这个想法是构建一本书。有时候这本书包含章节,有时候书中还有章节和其他书籍。所以,我的问题实际上是关于尝试测试,直到我得到类似于下面具有相同功能的模型。我已经在我的python shell中测试了这个模型,它输出了我的预期,但我想要更强大的东西。 我也希望能够在我的项目核心中使用这个模型,并且需要能够在我继续构建的基础上进行测试。用于测试这样的模型的一些好的示例单元测试是什么?或者关于在哪里寻找适用于ContentType和其他抽象模型的测试的任何建议?谢谢!

from django.db import models
from django.contrib.contenttypes import generic
from django.contrib.contenttypes.models import ContentType

class Element(models.Model):
    title = models.TextField()

    class Meta:
        abstract = True

class Chapter(Element):
    body = models.TextField()

    def __unicode__(self):
        return self.body

class Book(Element):
    description = models.TextField()

    def __unicode__(self):
        return self.description

class BookElement(models.Model):
    protocol_id = models.PositiveIntegerField()
    element_content_type = models.ForeignKey(ContentType)
    element_id = models.PositiveIntegerField()
    element = generic.GenericForeignKey('element_content_type', 'element_id')
    # Sort order, heavy things sink.
    element_weight = models.PositiveIntegerField()

def __unicode__(self):
    return u'%s %s' % (self.protocol_id, self.element , self.element_weight)

更新 这是我为将元素输入数据库并检索它们而进行的测试。它有效,但似乎很长时间测试不止一件事。如果有更好的方法,我愿意接受建议。

class BookAndChapterModelTest(TestCase):

    def test_saving_and_retrieving_book_elements(self):
        # input data objects and save
        book = Book()
        book.title = "First book"
        book.description = "Testing, round one"
        book.save()

        first_chapter = Chapter()
        first_chapter.title = 'step 1'
        first_chapter.body ='This is step 1'
        first_chapter.save()

        second_chapter = Chapter()
        second_chapter.title = 'step 2'
        second_chapter.body = 'This is step 2'
        second_chapter.save()

        # link content types to chapter or book model
        chapter_ct = ContentType.objects.get_for_model(first_chapter)
        book_ct = ContentType.objects.get_for_model(book)

        # Assemble BookElement order by weight of chapter or book
        BookElement.objects.create(
            book_id=book.pk,
            element_content_type=chapter_ct, 
            element_id=first_chapter.pk, 
            element_weight='1')
        BookElement.objects.create(
            book_id=book.pk, 
            element_content_type=chapter_ct, 
            element_id=second_chapter.pk, 
            element_weight='2')
        BookElement.objects.create(
            book_id=book.pk, 
            element_content_type=book_ct, 
            element_id=book.pk, 
            element_weight='3')

        # Test number of elements
        saved_book_element = BookElement.objects.all()
        self.assertEqual(saved_book_element.count(), 3)

        # Test elements are in the proper position based on weighting
        first_book_element = saved_book_element[0]
        self.assertEqual(str(first_book_element), 'This is step 1')
        third_book_element = saved_book_element[2]
        self.assertEqual(str(third_book_element), "Testing, round one")     

1 个答案:

答案 0 :(得分:0)

工厂男孩来救援!它使测试变得有趣

http://factoryboy.readthedocs.org/en/latest/examples.html