Skip to content

Commit

Permalink
9/Test MatchPledge model (#9401)
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 initial test

* add test case for 'active' attribute

* add test for profile attribute

* add test case for 'amount'

* add test for pledge_type attribute

* add test case for comments

* refactor

* add test case for 'end_date' attribute

* add test case for 'data' attribute

* add test case for 'clr_round_num'

* add test case for data_json property

* remove unnecessary import

* refactor

Co-authored-by: Aditya Anand M C <aditya.anandmc@gmail.com>
Co-authored-by: Graham Dixon <graham@gitcoin.co>
  • Loading branch information
3 people authored Sep 29, 2021
1 parent 7c17e7b commit d8e54f9
Show file tree
Hide file tree
Showing 3 changed files with 114 additions and 1 deletion.
18 changes: 18 additions & 0 deletions app/grants/tests/models/factories/match_pledge_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import json

import factory
from grants.models.match_pledge import MatchPledge

from .grant_clr_factory import GrantCLRFactory
from .profile_factory import ProfileFactory


class MatchPledgeFactory(factory.django.DjangoModelFactory):
"""Create mock MatchPledge for testing."""

class Meta:
model = MatchPledge

profile = factory.SubFactory(ProfileFactory)
data = json.dumps('test string')
clr_round_num = factory.SubFactory(GrantCLRFactory)
2 changes: 1 addition & 1 deletion app/grants/tests/models/factories/profile_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,4 +9,4 @@ class Meta:
model = Profile

handle = factory.Sequence(lambda n: "Contributor_%03d" % n)
data = {}
data = {}
95 changes: 95 additions & 0 deletions app/grants/tests/models/test_match_pledge.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
from datetime import datetime, timedelta

from django.utils import timezone
from django.utils.timezone import localtime

import pytest
from dashboard.models import Profile
from grants.models.grant import GrantCLR
from grants.models.match_pledge import MatchPledge

from .factories.match_pledge_factory import MatchPledgeFactory


@pytest.mark.django_db
class TestMatchPledge:
"""Test MatchPledge model."""

def test_creation(self):
"""Test instance of MatchPledge returned by factory is valid."""

match_pledge = MatchPledgeFactory()

assert isinstance(match_pledge, MatchPledge)

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

match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'active')
assert match_pledge.active == False

def test_match_pledge_has_associated_profile(self):
"""Test 'profile' attribute is present and is an instance of Profile."""

match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'profile')
assert isinstance(match_pledge.profile, Profile)

def test_match_pledge_has_amount_attribute(self):
"""Test 'amount' attribute is present defaults to 1."""

match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'amount')
assert match_pledge.amount == 1

def test_match_pledge_has_pledge_type_attribute(self):
"""Test 'pledge_type' attribute is present."""

match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'pledge_type')

def test_match_pledge_has_comments(self):
"""Test 'comments' attribute is present and defaults to empty string."""

match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'comments')
assert match_pledge.comments == ''

def test_match_pledge_has_end_date(self):
"""Test 'end_date' attribute is present and that default value is 30 days from today's date."""

next_month = localtime(timezone.now() + timedelta(days=30))
match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'end_date')
assert isinstance(match_pledge.end_date, datetime)
assert match_pledge.end_date.replace(microsecond=0) == next_month.replace(microsecond=0)

def test_match_pledge_has_data_attribute(self):
"""Test 'data' attribute."""

match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'data')

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

match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'clr_round_num')
assert isinstance(match_pledge.clr_round_num, GrantCLR)

def test_data_json(self):
"""Test 'data_json' property returns data attribute as valid JSON."""

match_pledge = MatchPledgeFactory()

assert hasattr(match_pledge, 'data_json')
assert match_pledge.data_json == 'test string'

0 comments on commit d8e54f9

Please sign in to comment.