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

7/Test Flag model #9398

Merged
merged 12 commits into from
Sep 29, 2021
1 change: 1 addition & 0 deletions app/grants/models/contribution.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ class Contribution(SuperModel):
CHECKOUT_TYPES = [
('eth_std', 'eth_std'),
('eth_zksync', 'eth_zksync'),
('eth_polygon', 'eth_polygon'),
('zcash_std', 'zcash_std'),
('celo_std', 'celo_std'),
('zil_std', 'zil_std'),
Expand Down
2 changes: 1 addition & 1 deletion app/grants/models/grant.py
Original file line number Diff line number Diff line change
Expand Up @@ -488,7 +488,7 @@ class Meta:
)

in_active_clrs = models.ManyToManyField(
GrantCLR,
"GrantCLR",
help_text="Active Grants CLR Round"
)
is_clr_active = models.BooleanField(default=False, help_text=_('CLR Round active or not? (auto computed)'))
Expand Down
18 changes: 18 additions & 0 deletions app/grants/tests/models/factories/flag_factory.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
import factory
import pytest
from grants.models.flag import Flag

from .grant_factory import GrantFactory
from .profile_factory import ProfileFactory


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

class Meta:
model = Flag

grant = factory.SubFactory(GrantFactory)
profile = factory.SubFactory(ProfileFactory)
comments = ''
2 changes: 1 addition & 1 deletion app/grants/tests/models/factories/grant_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,4 @@ class GrantFactory(factory.django.DjangoModelFactory):
"""Create mock Grant for testing."""

class Meta:
model = Grant
model = Grant
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 = {}
84 changes: 84 additions & 0 deletions app/grants/tests/models/test_flag.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
from unittest.mock import patch

import pytest
from dashboard.models import Activity, Profile
from grants.models.flag import Flag
from grants.models.grant import Grant
from townsquare.models import Comment

from .factories.flag_factory import FlagFactory
from .factories.profile_factory import ProfileFactory


@pytest.mark.django_db
class TestFlag:
"""Test Flag model."""

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

flag = FlagFactory()

assert isinstance(flag, Flag)

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

flag = FlagFactory()

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

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

flag = FlagFactory()

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

def test_flag_has_comments(self):
"""Test 'comments' attribute is present."""

flag = FlagFactory()

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

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

flag = FlagFactory()

assert hasattr(flag, 'processed')
assert flag.processed == False

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

flag = FlagFactory()

assert hasattr(flag, 'comments_admin')
assert flag.comments_admin == ''

def test_flag_has_tweet_attribute(self):
"""Test 'tweet' attribute is present."""

flag = FlagFactory()

assert hasattr(flag, 'tweet')

def test_post_flag_method_calls_collaborators_with_appropriate_attributes(self):
"""Test post_flag() method calls filter() on Profile.objects, create() on Activity.objects, and create() on Comment.objects."""

flag = FlagFactory()

with patch.object(Profile.objects, 'filter') as filter:
with patch.object(Activity.objects, 'create') as activity:
with patch.object(Comment.objects, 'create') as comment:

flag.post_flag()

filter.assert_called_with(handle='gitcoinbot')
activity.assert_called_with(profile=filter().first(), activity_type='flagged_grant', grant=flag.grant)
comment.assert_called_with(profile=filter().first(), activity=activity(), comment=f"Comment from anonymous user: {flag.comments}")