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