File: external_factory_boy_noerror.py

package info (click to toggle)
pylint-django 2.0.13-5
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 660 kB
  • sloc: python: 1,807; sh: 13; makefile: 5
file content (55 lines) | stat: -rw-r--r-- 1,547 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
"""
Test to validate that pylint_django doesn't produce
Instance of 'SubFactory' has no 'pk' member (no-member) warnings
"""
# pylint: disable=attribute-defined-outside-init, missing-docstring, too-few-public-methods
import factory

from django import test
from django.db import models


class Author(models.Model):
    name = models.CharField()


class Book(models.Model):
    title = models.CharField()
    author = models.ForeignKey(Author, related_name='books', on_delete=models.CASCADE)


class AuthorFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = 'Author'

    name = factory.Sequence(lambda n: 'Author %d' % n)


class BookFactory(factory.django.DjangoModelFactory):
    class Meta:
        model = 'Book'

    title = factory.Sequence(lambda n: 'Book %d' % n)
    author = factory.SubFactory(AuthorFactory)
    reviewer = factory.LazyFunction(Author.objects.first())


class BookTestCase(test.LiveServerTestCase):
    serialized_rollback = True

    def _fixture_setup(self):
        super(BookTestCase, self)._fixture_setup()
        self.book = BookFactory()
        _author = AuthorFactory()
        _first_book = _author.books.first()
        self.assertIsNotNone(_first_book)

    def test_author_is_not_none(self):
        self.assertGreater(self.book.pk, 0)
        self.assertGreater(self.book.author.pk, 0)

        self.assertIsNotNone(self.book.title)
        self.assertIsNotNone(self.book.author.name)

    def test_reviewer_is_not_none(self):
        self.assertGreater(self.book.reviewer.pk, 0)