From f7447abeb694c7fdf18c5a01ab8e7796ca4edf2f Mon Sep 17 00:00:00 2001 From: Jeremy Schuurmans Date: Wed, 29 Sep 2021 08:19:38 -0500 Subject: [PATCH] 8/Test GrantType (#9399) * 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 --- Makefile | 3 + app/grants/tests/factories/__init__.py | 0 .../tests/factories/grant_category_factory.py | 11 +++ .../tests/factories/grant_clr_factory.py | 18 ++++ .../tests/factories/grant_type_factory.py | 10 +++ app/grants/tests/test_grant_type.py | 88 +++++++++++++++++++ 6 files changed, 130 insertions(+) create mode 100644 app/grants/tests/factories/__init__.py create mode 100644 app/grants/tests/factories/grant_category_factory.py create mode 100644 app/grants/tests/factories/grant_clr_factory.py create mode 100644 app/grants/tests/factories/grant_type_factory.py create mode 100644 app/grants/tests/test_grant_type.py diff --git a/Makefile b/Makefile index e47e8200543..7802d23f62d 100644 --- a/Makefile +++ b/Makefile @@ -91,6 +91,9 @@ pytest: ## Run pytest (Backend) pytest-pdb: ## Run pytest with pdb support (Backend) @docker-compose exec -e PYTHONPATH=/code/app/ -e DJANGO_SETTINGS_MODULE="app.settings" web pytest -p no:ethereum --pdb --pdbcls=IPython.terminal.debugger:Pdb +pytest-file: ## Run pytest for a single file (Backend) + @docker-compose exec -e PYTHONPATH=/code/app/ -e DJANGO_SETTINGS_MODULE="app.settings" web pytest -p no:ethereum ${file} + stylelint: ## Run stylelint against the project directory. Requires node, npm, and project dependencies. @npm run stylelint diff --git a/app/grants/tests/factories/__init__.py b/app/grants/tests/factories/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/app/grants/tests/factories/grant_category_factory.py b/app/grants/tests/factories/grant_category_factory.py new file mode 100644 index 00000000000..092883ca06e --- /dev/null +++ b/app/grants/tests/factories/grant_category_factory.py @@ -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) diff --git a/app/grants/tests/factories/grant_clr_factory.py b/app/grants/tests/factories/grant_clr_factory.py new file mode 100644 index 00000000000..35321c4f0dc --- /dev/null +++ b/app/grants/tests/factories/grant_clr_factory.py @@ -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 diff --git a/app/grants/tests/factories/grant_type_factory.py b/app/grants/tests/factories/grant_type_factory.py new file mode 100644 index 00000000000..1d054e0a274 --- /dev/null +++ b/app/grants/tests/factories/grant_type_factory.py @@ -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 + diff --git a/app/grants/tests/test_grant_type.py b/app/grants/tests/test_grant_type.py new file mode 100644 index 00000000000..c5d406ba95f --- /dev/null +++ b/app/grants/tests/test_grant_type.py @@ -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