-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathfactories.py
More file actions
76 lines (60 loc) · 2.58 KB
/
factories.py
File metadata and controls
76 lines (60 loc) · 2.58 KB
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
from typing import Any
import factory
from django_celery_beat.models import IntervalSchedule
from django_celery_beat.models import PeriodicTask
from democrasite.users.tests.factories import UserFactory
from democrasite.webiscite.models import Bill
from democrasite.webiscite.models import PullRequest
class PullRequestFactory(factory.django.DjangoModelFactory[PullRequest]):
number = factory.Sequence(
lambda n: -n - 1
) # Use negative numbers to represent fake PRs, starting at -1
title = factory.Faker("text", max_nb_chars=50)
author_name = factory.Faker("user_name")
status = "open"
draft = False
additions = factory.Faker("random_int")
deletions = factory.Faker("random_int")
sha = factory.Faker("pystr", min_chars=40, max_chars=40)
class Meta:
model = PullRequest
class TaskFactory(factory.django.DjangoModelFactory[PeriodicTask]):
interval = factory.LazyFunction(
lambda: IntervalSchedule.objects.get_or_create(
every=999, period=IntervalSchedule.DAYS
)[0]
)
name = factory.Faker("text", max_nb_chars=50)
class Meta:
model = PeriodicTask
class BillFactory(factory.django.DjangoModelFactory[Bill]):
name = factory.Faker("text", max_nb_chars=50)
description = factory.Faker("paragraph")
author = factory.SubFactory(UserFactory)
pull_request = factory.SubFactory(PullRequestFactory)
# Fields with defaults
status = Bill.Status.OPEN
constitutional = False
_submit_task = factory.SubFactory(TaskFactory)
# Currently yes_votes and no_votes are initialized as empty. If values are needed
# for them, a post-generation hook can be written to generate and insert the users
class Meta:
model = Bill
class GithubPullRequestFactory(factory.Factory[dict[str, Any]]):
"""Generate a dict representing a pull request from the GitHub API"""
bill = factory.SubFactory(BillFactory, status=Bill.Status.CLOSED)
user = factory.DictFactory(
id=factory.Faker("random_int"), login=factory.Faker("user_name")
)
head = factory.DictFactory(sha=factory.Faker("pystr", min_chars=40, max_chars=40))
title = factory.SelfAttribute("bill.name")
body = factory.SelfAttribute("bill.description")
number = factory.SelfAttribute("bill.pull_request.number")
state = "open"
additions = factory.SelfAttribute("bill.pull_request.additions")
deletions = factory.SelfAttribute("bill.pull_request.deletions")
draft = False
diff_url = "" # Keep blank so request.get raises an error
class Meta:
model = dict
exclude = ("bill",)