-
-
Notifications
You must be signed in to change notification settings - Fork 776
/
Copy pathtest_grant_clr.py
302 lines (190 loc) · 10.1 KB
/
test_grant_clr.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
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
from unittest.mock import patch
from django.utils import timezone
import pytest
from dashboard.models import Profile
from grants.models.grant import Grant, GrantCLR
from grants.models.grant_clr_calculation import GrantCLRCalculation
from grants.models.grant_collection import GrantCollection
from grants.tests.factories import GrantCLRFactory, GrantCollectionFactory, GrantFactory
@pytest.mark.django_db
class TestGrantCLR:
"""Test GrantCLR model."""
def test_creation(self):
"""Test GrantCLR returned by factory is valid."""
grant_clr = GrantCLRFactory()
assert isinstance(grant_clr, GrantCLR)
def test_grant_clr_has_a_customer_name(self):
"""Test customer_name attribute is present and defaults to empty string."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'customer_name')
assert grant_clr.customer_name == ''
def test_grant_clr_has_round_num_attribute(self):
"""Test round_num attribute is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'round_num')
def test_grant_clr_has_sub_round_slug_attribute(self):
"""Test sub_round_slug attribute is present and defaults to empty string."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'sub_round_slug')
assert grant_clr.sub_round_slug == ''
def test_grant_clr_has_display_text_attribute(self):
"""Test display_text attribute is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'display_text')
def test_grant_clr_has_owner_attribute(self):
"""Test owner attribute is present and is an instance of Profile."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'owner')
assert isinstance(grant_clr.owner, Profile)
def test_grant_clr_has_is_active_attribute(self):
"""Test is_active attribute is present and defaults to False."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'is_active')
assert grant_clr.is_active == False
def test_grant_clr_has_start_date_attribute(self):
"""Test start_date is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'start_date')
def test_grant_clr_has_end_date_attribute(self):
"""Test end_date is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'end_date')
def test_grant_clr_has_grant_filters(self):
"""Test grant_filters attribute is present and defaults to an empty dictionary."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'grant_filters')
assert grant_clr.grant_filters == {}
assert len(grant_clr.grant_filters) == 0
def test_grant_clr_has_grant_excludes(self):
"""Test grant_excludes attribute is present and defaults to an empty dictionary."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'grant_excludes')
assert grant_clr.grant_excludes == {}
assert len(grant_clr.grant_excludes) == 0
def test_grant_clr_has_subscription_filters(self):
"""Test subscription_filters attribute is present and defaults to an empty dictionary."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'subscription_filters')
assert grant_clr.subscription_filters == {}
assert len(grant_clr.subscription_filters) == 0
def test_grant_clr_has_collection_filters(self):
"""Test collection_filters attribute is present and defaults to an empty dictionary."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'collection_filters')
assert grant_clr.collection_filters == {}
assert len(grant_clr.collection_filters) == 0
def test_grant_clr_has_verified_threshold(self):
"""Test verified_threshold is present and defaults to 25.0."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'verified_threshold')
assert grant_clr.verified_threshold == 25.0
def test_grant_clr_has_unverified_threshold(self):
"""Test unverified_threshold is present and defaults to 5.0."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'unverified_threshold')
assert grant_clr.unverified_threshold == 5.0
def test_grant_clr_has_total_pot(self):
"""Test total_pot is present and defaults to 0."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'total_pot')
assert grant_clr.total_pot == 0
def test_grant_clr_contribution_multiplier(self):
"""Test contribution_multiplier is present and defaults to 1.0."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'contribution_multiplier')
assert grant_clr.contribution_multiplier == 1.0
def test_grant_clr_has_logo_attribute(self):
"""Test logo attribute is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'logo')
def test_grant_clr_has_type_attribute(self):
"""Test type attribute is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'type')
def test_grant_clr_has_logo_text_hex_attribute(self):
"""Test logo_text_hex attribute is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'logo_text_hex')
def test_grant_clr_has_banner_bg_hex_attribute(self):
"""Test banner_bg_hex attribute is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'banner_bg_hex')
def test_grant_clr_has_banner_text_hex_attribute(self):
"""Test banner_text_hex attribute is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'banner_text_hex')
def test_grant_clr_has_banner_text_attribute(self):
"""Test banner_text attribute is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'banner_text')
def test_happening_now_returns_true_if_round_is_currently_happening(self):
"""Test happening_now method returns true if we are within the time range for this round."""
grant_clr = GrantCLRFactory()
grant_clr.start_date = timezone.now() - timezone.timedelta(days=3)
grant_clr.end_date = timezone.now() + timezone.timedelta(days=4)
assert grant_clr.happening_now == True
def test_happening_now_returns_false_if_round_is_not_currently_happening(self):
"""Test happening_now method returns false if we are outside the time range for this round."""
grant_clr = GrantCLRFactory()
grant_clr.start_date = timezone.now() + timezone.timedelta(days=3)
grant_clr.end_date = timezone.now() + timezone.timedelta(days=10)
assert grant_clr.happening_now == False
def test_happened_recently_returns_true_if_round_happened_recently(self):
"""Test happened_recently method returns true if grant_clr.end_date is within two weeks of current round."""
grant_clr = GrantCLRFactory()
grant_clr.start_date = timezone.now()
grant_clr.end_date = timezone.now() - timezone.timedelta(days=10)
assert grant_clr.happened_recently == True
def test_happened_recently_returns_false_if_round_did_not_happen_recently(self):
"""Test happened_recently method returns false if grant_clr.end_date is not within two weeks of current round."""
grant_clr = GrantCLRFactory()
grant_clr.start_date = timezone.now()
grant_clr.end_date = timezone.now() - timezone.timedelta(days=20)
assert grant_clr.happened_recently == False
def test_grants_method_calls_filter_with_expected_parameters(self):
grant_clr = GrantCLRFactory()
with patch.object(Grant.objects, 'filter') as filter:
grant_clr.grants
filter.assert_called_with(
hidden=False,
active=True,
is_clr_eligible=True,
link_to_new_grant=None
)
def test_grants_method_calls_filter_with_expected_parameters_if_collection_filters_are_present(self):
grant_clr = GrantCLRFactory()
grant_clr.collection_filters={'1': GrantCollectionFactory()}
grants = Grant.objects.filter(hidden=False, active=True, is_clr_eligible=True, link_to_new_grant=None)
with patch.object(GrantCollection.objects, 'filter') as filter:
with patch.object(GrantCollection.objects.filter(**grant_clr.collection_filters), 'values_list') as values_list:
grant_clr.grants
filter.assert_called_with(**grant_clr.collection_filters)
values_list.assert_called_with('grants', flat=True)
def test_grants_method_does_not_call_filter_if_collection_filters_are_not_present(self):
grant_clr = GrantCLRFactory()
with patch.object(GrantCollection.objects, 'filter') as filter:
with patch.object(GrantCollection.objects.filter(**grant_clr.collection_filters), 'values_list') as values_list:
grant_clr.grants
filter.assert_not_called
values_list.assert_not_called
def test_record_clr_prediction_curve_calls_collaborator_with_expected_parameters(self):
"""Test record_clr_prediction_curve calls create on GrantCLRCalculation.objects with expected params."""
grant = GrantFactory()
grant_clr = GrantCLRFactory()
with patch.object(GrantCLRCalculation.objects, 'create') as create:
grant_clr.record_clr_prediction_curve(grant, grant.clr_prediction_curve)
create.assert_called_with(
grantclr=grant_clr,
grant=grant,
clr_prediction_curve=grant.clr_prediction_curve,
active=False, # new factory obj defaults to inactive
latest=True
)
def test_grant_clr_has_claim_start_date_attribute(self):
"""Test claim_start_date is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'claim_start_date')
def test_grant_clr_has_claim_end_date_attribute(self):
"""Test claim_end_date is present."""
grant_clr = GrantCLRFactory()
assert hasattr(grant_clr, 'claim_end_date')