-
Notifications
You must be signed in to change notification settings - Fork 2
/
conftest.py
106 lines (67 loc) · 2.33 KB
/
conftest.py
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
from itertools import count
import django
import pytest
from django.conf import settings
from django.db import models
from django_factories import Factory, SubFactory
settings.configure(DEBUG=False)
django.setup()
class Author(models.Model):
name = models.CharField(max_length=255)
age = models.PositiveSmallIntegerField()
class Meta:
app_label = "dummy_app"
def __str__(self):
return self.name
class Book(models.Model):
author = models.ForeignKey(Author, on_delete=models.CASCADE)
title = models.CharField(max_length=255)
class Meta:
app_label = "dummy_app"
class Chapter(models.Model):
book = models.ForeignKey(Book, on_delete=models.CASCADE)
title = models.CharField(max_length=255, default="Chapter 1")
class Meta:
app_label = "dummy_app"
class ModelA(models.Model):
class Meta:
app_label = "dummy_app"
class ModelB(models.Model):
model_a = models.ForeignKey(ModelA, on_delete=models.CASCADE)
class Meta:
app_label = "dummy_app"
@pytest.fixture
def author_factory(request):
factory = Factory(Author, name="Default Author")
return factory(request)
@pytest.fixture
def book_factory(request):
return Factory(Book, title="Default Title")(request)
@pytest.fixture
def watterson_author_factory(request):
return Factory(Author, name="Bill Watterson")(request)
@pytest.fixture
def bill_watterson(author_factory):
return author_factory(name="Bill Watterson")
@pytest.fixture
def watterson_book_factory(request, watterson_author_factory):
"""This factory builds objects with "Bill Watterson" as default author."""
return Factory(Book, author=SubFactory("watterson_author_factory"))(request)
@pytest.fixture
def broken_factory(request):
return Factory(Book, author=SubFactory("bill_watterson"))(request)
@pytest.fixture
def chapter_factory(request):
return Factory(Chapter)(request)
@pytest.fixture
def model_b_factory(request):
"""Please note that a factory function for ModelA is missing intentionally."""
return Factory(ModelB)(request)
@pytest.fixture
def enumerated_book_factory(request):
counter = count(1)
class BookFactory(Factory):
model = Book
def create(self, **kwargs):
return super().create(**kwargs, title=f"Book {next(counter)}")
return BookFactory()(request)