Skip to content

Commit

Permalink
0/Test Donation model (#9391)
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 test directory for models and directory for factories

* add initial test for donation model

* add test case for from_address

* test donation has a to_address

* add test case for relation to profile

* add test case for token_address

* add test case for donation.token_symbol

* add test case for donation.token_amount

* add test case for donation.token_amount_usdt

* add test case for donation.tx_id

* add test case for donation.network

* add test cases for donation_percentage and subscription

* add test case for relation to contribution

* remove some whitespace

* refactor and add docstrings

* refactor

* refactor
  • Loading branch information
Jeremy Schuurmans authored Sep 29, 2021
1 parent 65aea9b commit 5a5a5bf
Show file tree
Hide file tree
Showing 13 changed files with 187 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/grants/models/clr_match.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ class CLRMatch(SuperModel):

def __str__(self):
"""Return the string representation of a Grant."""
return f"id: {self.pk}, grant: {self.grant.pk}, round: {self.round_number}, amount: {self.amount}"
return f"id: {self.pk}, grant: {self.grant.pk}, round: {self.round_number}, amount: {self.amount}"
1 change: 0 additions & 1 deletion app/grants/models/grant.py
Original file line number Diff line number Diff line change
Expand Up @@ -885,7 +885,6 @@ def save(self, update=True, *args, **kwargs):

self.clr_prediction_curve = self.calc_clr_prediction_curve
self.clr_round_num = self.calc_clr_round_label

self.search_vector = (
SearchVector('title', weight='A') + SearchVector('description', weight='B')
)
Expand Down
2 changes: 1 addition & 1 deletion app/grants/models/match_pledge.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,4 @@ def data_json(self):

def __str__(self):
"""Return the string representation of this object."""
return f"{self.profile} <> {self.amount} DAI"
return f"{self.profile} <> {self.amount} DAI"
2 changes: 1 addition & 1 deletion app/grants/models/subscription.py
Original file line number Diff line number Diff line change
Expand Up @@ -554,4 +554,4 @@ def create_contribution(self, tx_id, is_successful_contribution=True):
successful_contribution(self.grant, self, contribution)

update_grant_metadata.delay(self.pk)
return contribution
return contribution
Empty file.
Empty file.
13 changes: 13 additions & 0 deletions app/grants/tests/models/factories/contribution_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import factory
from grants.models.contribution import Contribution

from .subscription_factory import SubscriptionFactory


class ContributionFactory(factory.django.DjangoModelFactory):
"""Create mock Contribution for testing."""

class Meta:
model = Contribution

subscription = factory.SubFactory(SubscriptionFactory)
19 changes: 19 additions & 0 deletions app/grants/tests/models/factories/donation_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
import factory
import pytest
from grants.models.donation import Donation

from .contribution_factory import ContributionFactory
from .profile_factory import ProfileFactory
from .subscription_factory import SubscriptionFactory


@pytest.mark.django_db
class DonationFactory(factory.django.DjangoModelFactory):
"""Create a mock Donation for testing."""

class Meta:
model = Donation

profile = factory.SubFactory(ProfileFactory)
subscription = factory.SubFactory(SubscriptionFactory)
contribution = factory.SubFactory(ContributionFactory)
9 changes: 9 additions & 0 deletions app/grants/tests/models/factories/grant_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
import factory
from grants.models.grant import Grant


class GrantFactory(factory.django.DjangoModelFactory):
"""Create mock Grant for testing."""

class Meta:
model = Grant
12 changes: 12 additions & 0 deletions app/grants/tests/models/factories/profile_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import factory
from dashboard.models import Profile


class ProfileFactory(factory.django.DjangoModelFactory):
"""Create mock Profile for testing."""

class Meta:
model = Profile

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

from .grant_factory import GrantFactory
from .profile_factory import ProfileFactory


class SubscriptionFactory(factory.django.DjangoModelFactory):
"""Create mock Subscription for testing."""

class Meta:
model = Subscription

grant = factory.SubFactory(GrantFactory)
contributor_profile = factory.SubFactory(ProfileFactory)
115 changes: 115 additions & 0 deletions app/grants/tests/models/test_donation.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
import pytest
from dashboard.models import Profile
from grants.models.contribution import Contribution
from grants.models.donation import Donation
from grants.models.subscription import Subscription

from .factories.donation_factory import DonationFactory


@pytest.mark.django_db
class TestDonation:
"""Test Donation model."""

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

donation = DonationFactory()

assert isinstance(donation, Donation)

def test_donation_has_a_from_address(self):
"""Test from_address attribute is present and defaults to '0x0'."""

donation = DonationFactory()

assert hasattr(donation, 'from_address')
assert donation.from_address == '0x0'

def test_donation_has_a_to_address(self):
"""Test to_address attribute is present and defaults to '0x0'."""

donation = DonationFactory()

assert hasattr(donation, 'to_address')
assert donation.to_address == '0x0'

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

donation = DonationFactory()

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

def test_donation_has_a_token_address(self):
"""Test token_address attribute is present and defaults to '0x0'."""

donation = DonationFactory()

assert hasattr(donation, 'token_address')
assert donation.token_address == '0x0'

def test_donation_has_a_token_symbol(self):
"""Test token_symbol attribute is present and defaults to empty string."""

donation = DonationFactory()

assert hasattr(donation, 'token_symbol')
assert donation.token_symbol == ''

def test_donation_has_a_token_amount(self):
"""Test token_amount attribute is present and defaults to 0."""

donation = DonationFactory()

assert hasattr(donation, 'token_amount')
assert donation.token_amount == 0

def test_donation_has_a_token_amount_usdt(self):
"""Test token_amount_usdt attribute is present and defaults to 0."""

donation = DonationFactory()

assert hasattr(donation, 'token_amount_usdt')
assert donation.token_amount_usdt == 0

def test_donation_has_a_tx_id(self):
"""Test tx_id attribute is present and defaults to '0x0'."""

donation = DonationFactory()

assert hasattr(donation, 'tx_id')
assert donation.tx_id == '0x0'

def test_donation_has_a_network(self):
"""Test network attribute is present and defaults to 'mainnet'."""

donation = DonationFactory()

assert hasattr(donation, 'network')
assert donation.network == 'mainnet'

def test_donation_has_a_donation_percentage(self):
"""Test donation_percentage attribute is present and defaults to 0."""

donation = DonationFactory()

assert hasattr(donation, 'donation_percentage')
assert donation.donation_percentage == 0

def test_donation_has_related_subscription(self):
"""Test subscription attribute is present and is an instance of Subscription."""

donation = DonationFactory()

assert hasattr(donation, 'subscription')
assert isinstance(donation.subscription, Subscription)

def test_donation_has_related_contribution(self):
"""Test contribution attribute is present and is an instance of Contribution."""

donation = DonationFactory()

assert hasattr(donation, 'contribution')
assert isinstance(donation.contribution, Contribution)
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
-r base.txt
pytest==6.2.4
pytest-cov==2.12.0
pytest-factoryboy==2.1.0
pytest-isort==2.0.0
pytest-django==4.3.0
pytest-sugar==0.9.4
Expand Down

0 comments on commit 5a5a5bf

Please sign in to comment.