Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

9/Test MatchPledge model #9401

Merged
merged 17 commits into from
Sep 29, 2021
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'