-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathtest_models.py
More file actions
327 lines (231 loc) · 11 KB
/
test_models.py
File metadata and controls
327 lines (231 loc) · 11 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
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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
from unittest.mock import patch
import pytest
from allauth.socialaccount.models import SocialAccount
from django.conf import settings
from django.db import IntegrityError
from factory.faker import faker
from democrasite.users.models import User
from democrasite.users.tests.factories import UserFactory
from democrasite.webiscite.models import Bill
from democrasite.webiscite.models import ClosedBillVoteError
from democrasite.webiscite.models import PullRequest
from democrasite.webiscite.models import Vote
from .factories import BillFactory
from .factories import GithubPullRequestFactory
from .factories import PullRequestFactory
# Used to generate random data inline
FAKE = faker.Faker()
class TestPullRequestManager:
def test_create_from_github_create(self, caplog):
pr = GithubPullRequestFactory.create(number=FAKE.random_int())
assert not PullRequest.objects.filter(title=pr["title"]).exists()
pull_request: PullRequest = PullRequest.objects.create_from_github(pr)
assert hasattr(pull_request, "created")
assert PullRequest.objects.filter(title=pr["title"]).exists()
def test_create_from_github_update(self, bill: Bill, caplog):
# PullRequest is created during creation of bill fixture
pr = GithubPullRequestFactory.create(bill=bill)
assert PullRequest.objects.filter(title=bill.pull_request.title).exists()
pr["title"] = "New Title"
PullRequest.objects.create_from_github(pr)
assert PullRequest.objects.filter(title="New Title").exists()
class TestPullRequest:
def test_close(self, bill: Bill):
pull_request = bill.pull_request
pull_request.close()
pull_request.refresh_from_db()
assert pull_request.status == "closed"
assert not pull_request.bill_set.filter(status=Bill.Status.OPEN).exists()
def test_close_draft_bill(self):
bill = BillFactory.create(status=Bill.Status.DRAFT)
pull_request = bill.pull_request
pull_request.close()
pull_request.refresh_from_db()
bill.refresh_from_db()
assert pull_request.status == "closed"
assert bill.status == Bill.Status.CLOSED
def test_close_no_bill(self, caplog):
pull_request = PullRequestFactory.create()
assert pull_request.status == "open"
bill = pull_request.close()
pull_request.refresh_from_db()
assert pull_request.status == "closed"
assert bill is None
class TestVote:
def test_vote_str(self, bill: Bill, user: User):
bill.vote(user, support=True)
assert str(Vote.objects.get(user=user, support=True)) == f"{user} for {bill}"
def test_unique_user_bill_vote(self, bill: Bill, user: User):
bill.vote(user, support=True)
with pytest.raises(IntegrityError, match='"unique_user_bill_vote"'):
Vote(user=user, bill=bill, support=False).save()
class TestBillManager:
def test_get_queryset(self, bill, user: User):
bill = Bill.objects.first()
assert bill.yes_percent == 0
assert bill.no_percent == 0
bill.vote(user, support=True)
bill = Bill.objects.first()
assert bill.yes_percent == 100 # noqa: PLR2004
assert bill.no_percent == 0
bill.vote(user, support=False)
bill = Bill.objects.first()
assert bill.yes_percent == 0
assert bill.no_percent == 100 # noqa: PLR2004
user2 = UserFactory.create()
user3 = UserFactory.create()
bill.vote(user2, support=True)
bill.vote(user3, support=False)
bill = Bill.objects.first()
assert bill.yes_percent == 100 / 3
assert bill.no_percent == 200 / 3
bill_queryset = Bill.objects.all()
assert bill_queryset.ordered
def test_annotate_user_vote(self, bill: Bill, user: User):
bill.vote(user, support=True)
assert Bill.objects.annotate_user_vote(user).first().user_vote is True
bill.vote(user, support=False)
assert Bill.objects.annotate_user_vote(user).first().user_vote is False
bill.vote(user, support=False)
assert Bill.objects.annotate_user_vote(user).first().user_vote is None
@patch("requests.get")
def test_create_from_github(self, mock_get, user: User):
mock_get.return_value.text = ""
pr = PullRequestFactory.create()
uid = SocialAccount.objects.create(
user=user, provider="github", uid=FAKE.random_int()
).uid
bill = Bill.objects.create_from_github(pr, FAKE.text(), uid)
assert bill is not None
assert bill.pk is not None
bill.refresh_from_db()
assert bill._submit_task is not None # noqa: SLF001
assert bill._submit_task.enabled is True # noqa: SLF001
@patch("requests.get")
def test_create_from_github_draft(self, mock_get, user: User):
mock_get.return_value.text = ""
pr = PullRequestFactory.create(draft=True)
uid = SocialAccount.objects.create(
user=user, provider="github", uid=FAKE.random_int()
).uid
bill = Bill.objects.create_from_github(pr, FAKE.text(), uid)
assert bill is not None
assert bill.pk is not None
assert bill.status == Bill.Status.DRAFT
assert bill._submit_task is None # noqa: SLF001
def test_create_from_github_no_user(self):
pr = PullRequestFactory.create()
result = Bill.objects.create_from_github(pr, FAKE.text(), "nonexistent-uid")
assert result is None
class TestBill:
def test_unique_active_pull_request(self, bill: Bill):
bill.status = "closed"
bill.save()
BillFactory.create(pull_request=bill.pull_request)
with pytest.raises(IntegrityError, match='"unique_active_pull_request"'):
BillFactory.create(pull_request=bill.pull_request)
def test_unique_active_pull_request_draft(self):
bill = BillFactory.create(status=Bill.Status.DRAFT)
with pytest.raises(IntegrityError, match='"unique_active_pull_request"'):
BillFactory.create(pull_request=bill.pull_request, status=Bill.Status.DRAFT)
def test_str(self):
bill = BillFactory.create(name="The Test Act", pk=1, pull_request__number="-2")
assert str(bill) == "Bill 1: The Test Act (PR #-2)"
def test_get_absolute_url(self, bill: Bill):
assert bill.get_absolute_url() == f"/bills/{bill.id}/"
def test_get_update_url(self, bill: Bill):
assert bill.get_update_url() == f"/bills/{bill.id}/update/"
def test_get_vote_url(self, bill: Bill):
assert bill.get_vote_url() == f"/bills/{bill.id}/vote/"
def test_close(self, bill: Bill):
assert bill._submit_task is not None # noqa: SLF001
assert bill._submit_task.enabled is True # noqa: SLF001
bill.close()
bill.refresh_from_db()
assert bill.status == Bill.Status.CLOSED
assert bill._submit_task is not None # noqa: SLF001
assert bill._submit_task.enabled is False # noqa: SLF001
def test_close_amended(self, bill: Bill):
assert bill._submit_task is not None # noqa: SLF001
assert bill._submit_task.enabled is True # noqa: SLF001
bill.close(status=Bill.Status.AMENDED)
bill.refresh_from_db()
assert bill.status == Bill.Status.AMENDED
assert bill._submit_task is not None # noqa: SLF001
assert bill._submit_task.enabled is False # noqa: SLF001
class TestBillPublish:
def test_publish(self):
bill = BillFactory.create(status=Bill.Status.DRAFT)
assert bill._submit_task is None # noqa: SLF001
bill.publish()
bill.refresh_from_db()
assert bill.status == Bill.Status.OPEN
assert bill._submit_task is not None # noqa: SLF001
assert bill._submit_task.enabled is True # noqa: SLF001
assert bill._submit_task.last_run_at is not None # noqa: SLF001
def test_publish_not_draft(self, bill: Bill):
with pytest.raises(ValueError, match="Only draft bills can be published"):
bill.publish()
class TestBillVote:
def test_bill_vote_yes_toggle(self, bill: Bill, user: User):
bill.vote(user, support=True)
assert bill.yes_votes.filter(pk=user.id).exists()
bill.vote(user, support=True)
assert not bill.yes_votes.filter(pk=user.id).exists()
def test_bill_vote_no_toggle(self, bill: Bill, user: User):
bill.vote(user, support=False)
assert bill.no_votes.filter(pk=user.id).exists()
bill.vote(user, support=False)
assert not bill.no_votes.filter(pk=user.id).exists()
def test_bill_vote_yes_to_no_switch(self, bill: Bill, user: User):
bill.vote(user, support=True)
bill.vote(user, support=False)
assert not bill.yes_votes.filter(pk=user.id).exists()
assert bill.no_votes.filter(pk=user.id).exists()
def test_bill_vote_no_to_yes_switch(self, bill: Bill, user: User):
bill.vote(user, support=False)
bill.vote(user, support=True)
assert not bill.no_votes.filter(pk=user.id).exists()
assert bill.yes_votes.filter(pk=user.id).exists()
def test_bill_not_open(self, user: User):
bill = BillFactory.create(status=Bill.Status.CLOSED)
with pytest.raises(ClosedBillVoteError, match="Bill is not open for voting"):
bill.vote(user, support=True)
def test_draft_bill_not_votable(self, user: User):
bill = BillFactory.create(status=Bill.Status.DRAFT)
with pytest.raises(ClosedBillVoteError, match="Bill is not open for voting"):
bill.vote(user, support=True)
class TestBillSubmit:
def test_draft_bill_not_submitted(self):
bill: Bill = BillFactory.create(status=Bill.Status.DRAFT)
bill.submit()
assert bill.status == Bill.Status.DRAFT
def test_bill_not_open(self):
bill: Bill = BillFactory.create(status=Bill.Status.CLOSED)
bill.submit()
assert bill.status == Bill.Status.CLOSED
def test_insufficient_votes(self, bill: Bill):
bill.submit()
assert bill.status == Bill.Status.FAILED
def test_bill_rejected(self, bill: Bill):
voters = UserFactory.create_batch(settings.WEBISCITE_MINIMUM_QUORUM)
Vote.objects.bulk_create(
[Vote(bill=bill, user=voter, support=False) for voter in voters]
)
bill.submit()
assert bill.status == Bill.Status.REJECTED
def test_constitutional_bill_rejected(self):
bill = BillFactory.create(constitutional=True)
voters = UserFactory.create_batch(settings.WEBISCITE_MINIMUM_QUORUM)
Vote.objects.bulk_create(
[Vote(bill=bill, user=voter, support=False) for voter in voters]
)
bill.submit()
assert bill.status == Bill.Status.REJECTED
def test_bill_passed(self, bill: Bill):
voters = UserFactory.create_batch(settings.WEBISCITE_MINIMUM_QUORUM)
Vote.objects.bulk_create(
[Vote(bill=bill, user=voter, support=True) for voter in voters]
)
bill.submit()
assert bill.status == Bill.Status.APPROVED