From f45f1173ddda71b4d387f8ac36fbf9b315a61df0 Mon Sep 17 00:00:00 2001 From: Jeremy Schuurmans Date: Wed, 25 Aug 2021 13:29:46 -0500 Subject: [PATCH 01/12] 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 --- app/grants/models/grant.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app/grants/models/grant.py b/app/grants/models/grant.py index 03b5501eabb..ec8ab8fe42a 100644 --- a/app/grants/models/grant.py +++ b/app/grants/models/grant.py @@ -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)')) From b5d5ecbc32bc5d4dea4d9a0d2313377324179ccf Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 15:39:08 -0500 Subject: [PATCH 02/12] add initial test case --- .../tests/models/factories/flag_factory.py | 15 +++++++++++++++ .../tests/models/factories/grant_factory.py | 2 +- app/grants/tests/models/test_flag.py | 16 ++++++++++++++++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 app/grants/tests/models/factories/flag_factory.py create mode 100644 app/grants/tests/models/test_flag.py diff --git a/app/grants/tests/models/factories/flag_factory.py b/app/grants/tests/models/factories/flag_factory.py new file mode 100644 index 00000000000..43089f836ee --- /dev/null +++ b/app/grants/tests/models/factories/flag_factory.py @@ -0,0 +1,15 @@ +import factory +import pytest +from grants.models.flag import Flag + +from .grant_factory import GrantFactory + + +@pytest.mark.django_db +class FlagFactory(factory.django.DjangoModelFactory): + """Create a mock Flag for testing.""" + + class Meta: + model = Flag + + grant = factory.SubFactory(GrantFactory) diff --git a/app/grants/tests/models/factories/grant_factory.py b/app/grants/tests/models/factories/grant_factory.py index 2c05080e6ff..58b8f3e048a 100644 --- a/app/grants/tests/models/factories/grant_factory.py +++ b/app/grants/tests/models/factories/grant_factory.py @@ -6,4 +6,4 @@ class GrantFactory(factory.django.DjangoModelFactory): """Create mock Grant for testing.""" class Meta: - model = Grant \ No newline at end of file + model = Grant diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py new file mode 100644 index 00000000000..edac36d4389 --- /dev/null +++ b/app/grants/tests/models/test_flag.py @@ -0,0 +1,16 @@ +import pytest +from grants.models.flag import Flag + +from .factories.flag_factory import FlagFactory + + +@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) From b573a2b220d991af40b4041dc262fbe40ed50359 Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 15:44:23 -0500 Subject: [PATCH 03/12] add test case for associated Grant --- app/grants/tests/models/test_flag.py | 9 +++++++++ 1 file changed, 9 insertions(+) diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index edac36d4389..803c5eeba79 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -1,5 +1,6 @@ import pytest from grants.models.flag import Flag +from grants.models.grant import Grant from .factories.flag_factory import FlagFactory @@ -14,3 +15,11 @@ def test_creation(self): flag = FlagFactory() assert isinstance(flag, Flag) + + def test_flag_belongs_to_grant(self): + """Test relation of Flag to associated Grant.""" + + flag = FlagFactory() + + assert hasattr(flag, 'grant') + assert isinstance(flag.grant, Grant) From e182806641e1c8c93d34e169317922f34a433e3a Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 15:48:59 -0500 Subject: [PATCH 04/12] add test case for associated Profile --- app/grants/tests/models/test_flag.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index 803c5eeba79..f5e0aaa8f6b 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -23,3 +23,11 @@ def test_flag_belongs_to_grant(self): assert hasattr(flag, 'grant') assert isinstance(flag.grant, Grant) + + def test_flag_belongs_to_profile(self): + """Test relation of Flag to associated Profile and default value.""" + + flag = FlagFactory() + + assert hasattr(flag, 'profile') + assert flag.profile == None From 557946ec28811af4edaded3636c6ed4132126fb5 Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 15:50:06 -0500 Subject: [PATCH 05/12] add test case for comments --- app/grants/tests/models/test_flag.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index f5e0aaa8f6b..50e0bef4572 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -31,3 +31,11 @@ def test_flag_belongs_to_profile(self): assert hasattr(flag, 'profile') assert flag.profile == None + + def test_flag_has_comments(self): + """Test comments attribute and default value.""" + + flag = FlagFactory() + + assert hasattr(flag, 'comments') + assert flag.comments == '' From 7ce5c637879a1fc649065ae5d1f9efac002117f9 Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 15:51:29 -0500 Subject: [PATCH 06/12] add test case for processed attribute --- app/grants/tests/models/test_flag.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index 50e0bef4572..142eab6333c 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -39,3 +39,11 @@ def test_flag_has_comments(self): assert hasattr(flag, 'comments') assert flag.comments == '' + + def test_flag_has_processed_attribute(self): + """Test processed attribute and default value.""" + + flag = FlagFactory() + + assert hasattr(flag, 'processed') + assert flag.processed == False From 5ae41daea89e75a378084707b42fec796f2f7fcd Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 15:53:35 -0500 Subject: [PATCH 07/12] add test case for comments_admin --- app/grants/tests/models/test_flag.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index 142eab6333c..df16e368848 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -47,3 +47,11 @@ def test_flag_has_processed_attribute(self): assert hasattr(flag, 'processed') assert flag.processed == False + + def test_flag_has_comments_admin(self): + """Test comments_admin attribute and default.""" + + flag = FlagFactory() + + assert hasattr(flag, 'comments_admin') + assert flag.comments_admin == '' From 2535d690ece2eb11b3eb8cf010a3851b47c2c62a Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 15:59:38 -0500 Subject: [PATCH 08/12] add test case for tweet --- app/grants/tests/models/test_flag.py | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index df16e368848..2f55a3998d8 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -33,7 +33,7 @@ def test_flag_belongs_to_profile(self): assert flag.profile == None def test_flag_has_comments(self): - """Test comments attribute and default value.""" + """Test 'comments' attribute and default value.""" flag = FlagFactory() @@ -41,7 +41,7 @@ def test_flag_has_comments(self): assert flag.comments == '' def test_flag_has_processed_attribute(self): - """Test processed attribute and default value.""" + """Test 'processed' attribute and default value.""" flag = FlagFactory() @@ -49,9 +49,17 @@ def test_flag_has_processed_attribute(self): assert flag.processed == False def test_flag_has_comments_admin(self): - """Test comments_admin attribute and default.""" + """Test 'comments_admin' attribute and default.""" flag = FlagFactory() assert hasattr(flag, 'comments_admin') assert flag.comments_admin == '' + + def test_flag_has_tweet_attribute(self): + """Test 'tweet' attribute.""" + + flag = FlagFactory() + + assert hasattr(flag, 'tweet') + assert flag.tweet == '' From f876af6900d5dc7b6eb83a400c339f6f260bef0b Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 17:14:46 -0500 Subject: [PATCH 09/12] add test case for post_flag() method --- app/grants/tests/models/factories/flag_factory.py | 3 +++ .../tests/models/factories/profile_factory.py | 2 +- app/grants/tests/models/test_flag.py | 15 +++++++++++++-- 3 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/grants/tests/models/factories/flag_factory.py b/app/grants/tests/models/factories/flag_factory.py index 43089f836ee..e11e50bf33b 100644 --- a/app/grants/tests/models/factories/flag_factory.py +++ b/app/grants/tests/models/factories/flag_factory.py @@ -3,6 +3,7 @@ from grants.models.flag import Flag from .grant_factory import GrantFactory +from .profile_factory import ProfileFactory @pytest.mark.django_db @@ -13,3 +14,5 @@ class Meta: model = Flag grant = factory.SubFactory(GrantFactory) + profile = factory.SubFactory(ProfileFactory) + comments = 'Test comment' diff --git a/app/grants/tests/models/factories/profile_factory.py b/app/grants/tests/models/factories/profile_factory.py index 724dbc95bb0..4333c4830d5 100644 --- a/app/grants/tests/models/factories/profile_factory.py +++ b/app/grants/tests/models/factories/profile_factory.py @@ -9,4 +9,4 @@ class Meta: model = Profile handle = factory.Sequence(lambda n: "Contributor_%03d" % n) - data = {} \ No newline at end of file + data = {} diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index 2f55a3998d8..8b97484cd7e 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -1,6 +1,8 @@ import pytest +from dashboard.models import Profile from grants.models.flag import Flag from grants.models.grant import Grant +from townsquare.models import Comment from .factories.flag_factory import FlagFactory @@ -30,7 +32,7 @@ def test_flag_belongs_to_profile(self): flag = FlagFactory() assert hasattr(flag, 'profile') - assert flag.profile == None + assert isinstance(flag.profile, Profile) def test_flag_has_comments(self): """Test 'comments' attribute and default value.""" @@ -38,7 +40,7 @@ def test_flag_has_comments(self): flag = FlagFactory() assert hasattr(flag, 'comments') - assert flag.comments == '' + assert flag.comments == 'Test comment' def test_flag_has_processed_attribute(self): """Test 'processed' attribute and default value.""" @@ -63,3 +65,12 @@ def test_flag_has_tweet_attribute(self): assert hasattr(flag, 'tweet') assert flag.tweet == '' + + def test_flag_has_a_post_flag_method(self): + """Test post_flag method.""" + + flag = FlagFactory() + + flag.post_flag() + + assert Comment.objects.latest('id').comment == 'Comment from anonymous user: Test comment' From 60273e1ef342a099e99631b7e8b1103023d46d14 Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Thu, 26 Aug 2021 16:31:19 -0500 Subject: [PATCH 10/12] refactor test for post_flag() method --- app/grants/tests/models/test_flag.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index 8b97484cd7e..e2aeaa53075 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -1,10 +1,13 @@ +from unittest.mock import patch + import pytest -from dashboard.models import Profile +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 @@ -66,11 +69,17 @@ def test_flag_has_tweet_attribute(self): assert hasattr(flag, 'tweet') assert flag.tweet == '' - def test_flag_has_a_post_flag_method(self): - """Test post_flag method.""" + 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() - flag.post_flag() + 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() - assert Comment.objects.latest('id').comment == 'Comment from anonymous user: Test comment' + 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}") From 0f1bae8761213e7d3261611aa1b19daf5db999ed Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Wed, 15 Sep 2021 15:51:28 -0500 Subject: [PATCH 11/12] refactor --- .../tests/models/factories/flag_factory.py | 2 +- app/grants/tests/models/test_flag.py | 19 +++++++++---------- 2 files changed, 10 insertions(+), 11 deletions(-) diff --git a/app/grants/tests/models/factories/flag_factory.py b/app/grants/tests/models/factories/flag_factory.py index e11e50bf33b..c980aeb228b 100644 --- a/app/grants/tests/models/factories/flag_factory.py +++ b/app/grants/tests/models/factories/flag_factory.py @@ -15,4 +15,4 @@ class Meta: grant = factory.SubFactory(GrantFactory) profile = factory.SubFactory(ProfileFactory) - comments = 'Test comment' + comments = '' diff --git a/app/grants/tests/models/test_flag.py b/app/grants/tests/models/test_flag.py index e2aeaa53075..a6b1351d6d1 100644 --- a/app/grants/tests/models/test_flag.py +++ b/app/grants/tests/models/test_flag.py @@ -21,16 +21,16 @@ def test_creation(self): assert isinstance(flag, Flag) - def test_flag_belongs_to_grant(self): - """Test relation of Flag to associated Grant.""" + 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_belongs_to_profile(self): - """Test relation of Flag to associated Profile and default value.""" + def test_flag_has_associated_profile(self): + """Test 'profile' attribute is present and is an instance of Profile.""" flag = FlagFactory() @@ -38,15 +38,15 @@ def test_flag_belongs_to_profile(self): assert isinstance(flag.profile, Profile) def test_flag_has_comments(self): - """Test 'comments' attribute and default value.""" + """Test 'comments' attribute is present.""" flag = FlagFactory() assert hasattr(flag, 'comments') - assert flag.comments == 'Test comment' + assert flag.comments == '' def test_flag_has_processed_attribute(self): - """Test 'processed' attribute and default value.""" + """Test 'processed' attribute is present and defaults to False.""" flag = FlagFactory() @@ -54,7 +54,7 @@ def test_flag_has_processed_attribute(self): assert flag.processed == False def test_flag_has_comments_admin(self): - """Test 'comments_admin' attribute and default.""" + """Test 'comments_admin' attribute is present and defaults to empty string.""" flag = FlagFactory() @@ -62,12 +62,11 @@ def test_flag_has_comments_admin(self): assert flag.comments_admin == '' def test_flag_has_tweet_attribute(self): - """Test 'tweet' attribute.""" + """Test 'tweet' attribute is present.""" flag = FlagFactory() assert hasattr(flag, 'tweet') - assert 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.""" From ece68bf5f0aff02bf87291ff07754945d57a4c61 Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Tue, 28 Sep 2021 12:28:34 -0500 Subject: [PATCH 12/12] add missing checkout_type (present in migration but not in model) --- app/grants/models/contribution.py | 1 + 1 file changed, 1 insertion(+) diff --git a/app/grants/models/contribution.py b/app/grants/models/contribution.py index 49540e8737a..8315023f95c 100644 --- a/app/grants/models/contribution.py +++ b/app/grants/models/contribution.py @@ -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'),