Skip to content

Commit

Permalink
8/Test GrantType (#9399)
Browse files Browse the repository at this point in the history
* 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
Show file tree
Hide file tree
Showing 6 changed files with 130 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
Empty file.
11 changes: 11 additions & 0 deletions app/grants/tests/factories/grant_category_factory.py
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)
18 changes: 18 additions & 0 deletions app/grants/tests/factories/grant_clr_factory.py
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
10 changes: 10 additions & 0 deletions app/grants/tests/factories/grant_type_factory.py
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

88 changes: 88 additions & 0 deletions app/grants/tests/test_grant_type.py
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

0 comments on commit f7447ab

Please sign in to comment.