-
-
Notifications
You must be signed in to change notification settings - Fork 771
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Extract grants models into individual files (#9341) * create new directory for models, copy over Contribution model * extract grants models to individual files * rename relocated_models directory, remove original models directory, add imports, resolve circular dependencies * extract CLRMatch into separate file * extract Flag into separate file * extract MatchPledge to separate file * extract Donation and PhantomFunding * extract GrantStat into separate file * refactor * extract GrantBrandingRoutingPolicy to separate file * update migration * add missing import to MatchPledge, remove imports from __init__.py * add missing import * decouple GrantCLRCalculation and move to own file * extract GrantType to own file * extract GrantCLR to own file * add missing import * refactor, add missing imports * remove whitespace * resolve circular dependency * run 'make fix' * import changes from #9314 * add try/except to migration file instead of editing migration directly * refactor * add pytest-factoryboy to requirements * add test directory * add initial test case * add test case for 'name' * test 'label' attribute * test 'is_active' attribute * add test case for 'is_visible' attribute * run one test instead of entire test suite * add test case for relation to GrantCategory * test 'logo' attribute * add initial test for clrs method * add initial test for active_clrs method * add test for active_clrs_sum method * add docstrings * refactor * refactor * refactor * refactor * refactor * fix failing test
- Loading branch information
Jeremy Schuurmans
authored
Sep 29, 2021
1 parent
d8e54f9
commit f7447ab
Showing
6 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
import factory | ||
from grants.models.grant_category import GrantCategory | ||
|
||
|
||
class GrantCategoryFactory(factory.django.DjangoModelFactory): | ||
"""Create mock GrantCategory for testing.""" | ||
|
||
class Meta: | ||
model = GrantCategory | ||
|
||
category = factory.Sequence(lambda n: "Category #%s" % n) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
from datetime import datetime, timedelta | ||
|
||
import factory | ||
import pytest | ||
from grants.models.grant import GrantCLR | ||
|
||
|
||
@pytest.mark.django_db | ||
class GrantCLRFactory(factory.django.DjangoModelFactory): | ||
"""Create mock GrantCLR for testing.""" | ||
|
||
class Meta: | ||
model = GrantCLR | ||
|
||
round_num = 2 | ||
start_date = datetime.now() | ||
end_date = start_date + timedelta(weeks=2) | ||
is_active = True |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import factory | ||
from grants.models.grant_type import GrantType | ||
|
||
|
||
class GrantTypeFactory(factory.django.DjangoModelFactory): | ||
"""Create mock GrantType for testing.""" | ||
|
||
class Meta: | ||
model = GrantType | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
from unittest.mock import patch | ||
|
||
from django.db.models import QuerySet | ||
|
||
import pytest | ||
from grants.models.grant import GrantCLR | ||
from grants.models.grant_category import GrantCategory | ||
from grants.models.grant_type import GrantType | ||
|
||
from .factories.grant_category_factory import GrantCategoryFactory | ||
from .factories.grant_clr_factory import GrantCLRFactory | ||
from .factories.grant_type_factory import GrantTypeFactory | ||
|
||
|
||
@pytest.mark.django_db | ||
class TestGrantType: | ||
"""Test GrantType model.""" | ||
|
||
def test_creation(self): | ||
"""Test instance of GrantType returned by factory is valid.""" | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
assert isinstance(grant_type, GrantType) | ||
|
||
def test_grant_type_has_a_name(self): | ||
"""Test 'name' attribute is present.""" | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
assert hasattr(grant_type, 'name') | ||
|
||
def test_grant_type_has_a_label(self): | ||
"Test 'label' attribute is present." | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
assert hasattr(grant_type, 'label') | ||
|
||
def test_grant_type_has_a_is_active_attribute(self): | ||
"Test 'is_active' attribute is present and defaults to True." | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
assert hasattr(grant_type, 'is_active') | ||
assert grant_type.is_active == True | ||
|
||
def test_grant_type_has_a_is_visible_attribute(self): | ||
"Test 'is_visible' attribute is present and defaults to True." | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
assert hasattr(grant_type, 'is_visible') | ||
assert grant_type.is_visible == True | ||
|
||
def test_grant_type_has_a_logo(self): | ||
"""Test 'logo' attribute is present.""" | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
assert hasattr(grant_type, 'logo') | ||
|
||
def test_clrs_method_calls_collaborator_with_appropriate_parameters(self): | ||
"""Test GrantType.clrs method calls filter on GrantCLR.objects with appropriate parameters.""" | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
with patch.object(GrantCLR.objects, 'filter') as filter: | ||
grant_type.clrs | ||
|
||
filter.assert_called_with(grant_filters__grant_type=str(grant_type.pk)) | ||
|
||
def test_active_clrs_method_calls_collaborator_with_appropriate_parameters(self): | ||
"""Test GrantType.active_clrs method calls filter on GrantCLR.objects with appropriate parameters.""" | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
with patch.object(GrantCLR.objects, 'filter') as filter: | ||
grant_type.active_clrs | ||
|
||
filter.assert_called_with(is_active=True, grant_filters__grant_type=str(grant_type.pk)) | ||
|
||
def test_grant_type_has_active_clrs_sum_method(self): | ||
"""Test GrantType.active_clrs_sum method.""" | ||
|
||
grant_type = GrantTypeFactory() | ||
|
||
assert grant_type.active_clrs_sum == 0 |