Skip to content

Commit

Permalink
5/Test GrantCLRCalculation model (#9396)
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

* add directories

* add initial test case

* add test case for 'latest' attribute

* add test case for associated Grant

* add test case for associated GrantCLR

* add test case for 'clr_prediction_curve'

* refactor

* Update grant.py

Co-authored-by: Aditya Anand M C <aditya.anandmc@gmail.com>
  • Loading branch information
Jeremy Schuurmans and thelostone-mc committed Sep 29, 2021
1 parent 445e27a commit ae86908
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/grants/tests/models/factories/grant_clr_calculation_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import factory
import pytest
from grants.models.grant_clr_calculation import GrantCLRCalculation

from .grant_clr_factory import GrantCLRFactory
from .grant_factory import GrantFactory


@pytest.mark.django_db
class GrantCLRCalculationFactory(factory.django.DjangoModelFactory):
"""Create mock GrantCLRCalculation for testing."""

class Meta:
model = GrantCLRCalculation

grant = factory.SubFactory(GrantFactory)
grantclr = factory.SubFactory(GrantCLRFactory)
17 changes: 17 additions & 0 deletions app/grants/tests/models/factories/grant_clr_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
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)
53 changes: 53 additions & 0 deletions app/grants/tests/models/test_grant_clr_calculation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import pytest
from grants.models.grant import Grant, GrantCLR
from grants.models.grant_clr_calculation import GrantCLRCalculation

from .factories.grant_clr_calculation_factory import GrantCLRCalculationFactory


@pytest.mark.django_db
class TestGrantCLRCalculation:
"""Test GrantCLRCalculation model."""

def test_creation(self):
"""Test GrantCLRCalculation returned by factory is valid."""

grant_clr_calulation = GrantCLRCalculationFactory()

assert isinstance(grant_clr_calulation, GrantCLRCalculation)

def test_grant_clr_calculation_has_latest_attribute(self):
"""Test 'latest' attribute is present and defaults to False."""

grant_clr_calulation = GrantCLRCalculationFactory()

assert hasattr(grant_clr_calulation, 'latest')
assert grant_clr_calulation.latest == False

def test_grant_clr_calculation_has_associated_grant(self):
"""Test 'grant' attribute is present and is an instance of Grant."""

grant_clr_calculation = GrantCLRCalculationFactory()

assert hasattr(grant_clr_calculation, 'grant')
assert isinstance(grant_clr_calculation.grant, Grant)

def test_grant_clr_calculation_has_associated_grant_clr(self):
"""Test 'grantclr' attribute is present and is an instance of GrantCLR."""

grant_clr_calculation = GrantCLRCalculationFactory()

assert hasattr(grant_clr_calculation, 'grantclr')
assert isinstance(grant_clr_calculation.grantclr, GrantCLR)

def test_grant_clr_calculation_has_clr_prediction_curve(self):
"""Test 'clr_prediction_curve' attribute is present and defaults to an empty list."""

grant_clr_calculation = GrantCLRCalculationFactory()

assert hasattr(grant_clr_calculation, 'clr_prediction_curve')
assert grant_clr_calculation.clr_prediction_curve == []
assert len(grant_clr_calculation.clr_prediction_curve) == 0



0 comments on commit ae86908

Please sign in to comment.