From b1e691a046ffccb3b8c3243edb7241bebbe977f7 Mon Sep 17 00:00:00 2001 From: Jeremy Schuurmans Date: Wed, 25 Aug 2021 13:29:46 -0500 Subject: [PATCH 1/9] 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/clr_match.py | 2 +- app/grants/models/grant.py | 3 +-- app/grants/models/match_pledge.py | 2 +- app/grants/models/subscription.py | 2 +- 4 files changed, 4 insertions(+), 5 deletions(-) diff --git a/app/grants/models/clr_match.py b/app/grants/models/clr_match.py index 32835ac2449..5cd26afb999 100644 --- a/app/grants/models/clr_match.py +++ b/app/grants/models/clr_match.py @@ -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}" \ No newline at end of file + return f"id: {self.pk}, grant: {self.grant.pk}, round: {self.round_number}, amount: {self.amount}" diff --git a/app/grants/models/grant.py b/app/grants/models/grant.py index e7f44e801f6..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)')) @@ -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') ) diff --git a/app/grants/models/match_pledge.py b/app/grants/models/match_pledge.py index 4afa84c9a8b..fcd26445d2d 100644 --- a/app/grants/models/match_pledge.py +++ b/app/grants/models/match_pledge.py @@ -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" \ No newline at end of file + return f"{self.profile} <> {self.amount} DAI" diff --git a/app/grants/models/subscription.py b/app/grants/models/subscription.py index a4d8a9ad8d6..dbbe91a6192 100644 --- a/app/grants/models/subscription.py +++ b/app/grants/models/subscription.py @@ -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 \ No newline at end of file + return contribution From d8b6b10c7aedcc9c64ed781e36670489e37464bb Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 11:29:31 -0500 Subject: [PATCH 2/9] add pytest-factoryboy to requirements --- requirements/test.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/requirements/test.txt b/requirements/test.txt index a6f71ca364d..df604ebb7f2 100644 --- a/requirements/test.txt +++ b/requirements/test.txt @@ -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 From 95e5a62f4dfaea1a11acd0d0a4ba58e37da3a42f Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 11:31:54 -0500 Subject: [PATCH 3/9] add models and factories directories --- app/grants/tests/models/__init__.py | 0 app/grants/tests/models/factories/__init__.py | 0 2 files changed, 0 insertions(+), 0 deletions(-) create mode 100644 app/grants/tests/models/__init__.py create mode 100644 app/grants/tests/models/factories/__init__.py diff --git a/app/grants/tests/models/__init__.py b/app/grants/tests/models/__init__.py new file mode 100644 index 00000000000..e69de29bb2d diff --git a/app/grants/tests/models/factories/__init__.py b/app/grants/tests/models/factories/__init__.py new file mode 100644 index 00000000000..e69de29bb2d From 5f1567ea36821f50ac0834f88bed9cf30995274b Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 12:53:50 -0500 Subject: [PATCH 4/9] add initial test --- .../tests/models/factories/grant_factory.py | 9 +++++++++ .../models/factories/grant_stat_factory.py | 13 +++++++++++++ app/grants/tests/models/test_grant_stat.py | 17 +++++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 app/grants/tests/models/factories/grant_factory.py create mode 100644 app/grants/tests/models/factories/grant_stat_factory.py create mode 100644 app/grants/tests/models/test_grant_stat.py diff --git a/app/grants/tests/models/factories/grant_factory.py b/app/grants/tests/models/factories/grant_factory.py new file mode 100644 index 00000000000..2c05080e6ff --- /dev/null +++ b/app/grants/tests/models/factories/grant_factory.py @@ -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 \ No newline at end of file diff --git a/app/grants/tests/models/factories/grant_stat_factory.py b/app/grants/tests/models/factories/grant_stat_factory.py new file mode 100644 index 00000000000..1de8a5991a8 --- /dev/null +++ b/app/grants/tests/models/factories/grant_stat_factory.py @@ -0,0 +1,13 @@ +import factory +from grants.models.grant_stat import GrantStat + +from .grant_factory import GrantFactory + + +class GrantStatFactory(factory.django.DjangoModelFactory): + """Create mock GrantStat for testing.""" + + class Meta: + model = GrantStat + + grant = factory.SubFactory(GrantFactory) diff --git a/app/grants/tests/models/test_grant_stat.py b/app/grants/tests/models/test_grant_stat.py new file mode 100644 index 00000000000..acc203c1cae --- /dev/null +++ b/app/grants/tests/models/test_grant_stat.py @@ -0,0 +1,17 @@ +import pytest +from grants.models.grant_stat import GrantStat + +from .factories.grant_stat_factory import GrantStatFactory + + +@pytest.mark.django_db +class TestGrantStat: + """Test GrantStat model.""" + + def test_creation(self): + """Test GrantStat returned by factory is valid.""" + + grant_stat = GrantStatFactory() + + assert isinstance(grant_stat, GrantStat) + From 8d2d5e1e6cb3dc5b3b026eac9d679cf04cbfa78b Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 13:02:13 -0500 Subject: [PATCH 5/9] test association with Grant --- app/grants/tests/models/factories/grant_factory.py | 2 +- app/grants/tests/models/test_grant_stat.py | 10 ++++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) 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_grant_stat.py b/app/grants/tests/models/test_grant_stat.py index acc203c1cae..71be05bf8b0 100644 --- a/app/grants/tests/models/test_grant_stat.py +++ b/app/grants/tests/models/test_grant_stat.py @@ -1,4 +1,5 @@ import pytest +from grants.models.grant import Grant from grants.models.grant_stat import GrantStat from .factories.grant_stat_factory import GrantStatFactory @@ -15,3 +16,12 @@ def test_creation(self): assert isinstance(grant_stat, GrantStat) + def test_grant_stat_belongs_to_grant(self): + """Test association with Grant model.""" + + grant_stat = GrantStatFactory() + + assert hasattr(grant_stat, 'grant') + assert isinstance(grant_stat.grant, Grant) + + From 24d84c3a649845022367dc4f2a282992b2f27138 Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 13:02:48 -0500 Subject: [PATCH 6/9] test data attribute --- app/grants/tests/models/test_grant_stat.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/app/grants/tests/models/test_grant_stat.py b/app/grants/tests/models/test_grant_stat.py index 71be05bf8b0..14595231d06 100644 --- a/app/grants/tests/models/test_grant_stat.py +++ b/app/grants/tests/models/test_grant_stat.py @@ -24,4 +24,13 @@ def test_grant_stat_belongs_to_grant(self): assert hasattr(grant_stat, 'grant') assert isinstance(grant_stat.grant, Grant) - + def test_grant_stat_has_data_attribute(self): + """Test 'data' attribute.""" + + grant_stat = GrantStatFactory() + + assert hasattr(grant_stat, 'data') + assert grant_stat.data == {} + assert len(grant_stat.data) == 0 + + From 6e495b93f53abefe3f284c40e5c6471f9a6991bb Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Fri, 20 Aug 2021 13:08:28 -0500 Subject: [PATCH 7/9] add test case for snapshot_type --- app/grants/tests/models/test_grant_stat.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/app/grants/tests/models/test_grant_stat.py b/app/grants/tests/models/test_grant_stat.py index 14595231d06..2d1bd61b290 100644 --- a/app/grants/tests/models/test_grant_stat.py +++ b/app/grants/tests/models/test_grant_stat.py @@ -33,4 +33,12 @@ def test_grant_stat_has_data_attribute(self): assert grant_stat.data == {} assert len(grant_stat.data) == 0 + def test_grant_stat_has_snapshot_type(self): + """Test 'snapshot_type' attribute.""" + + grant_stat = GrantStatFactory() + + assert hasattr(grant_stat, 'snapshot_type') + assert grant_stat.snapshot_type == '' + From ee466543c0e7aad6c0347763886488fb108c08ee Mon Sep 17 00:00:00 2001 From: Jer-Sch Date: Wed, 15 Sep 2021 15:37:32 -0500 Subject: [PATCH 8/9] refactor --- app/grants/tests/models/test_grant_stat.py | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/app/grants/tests/models/test_grant_stat.py b/app/grants/tests/models/test_grant_stat.py index 2d1bd61b290..d8ab76e619d 100644 --- a/app/grants/tests/models/test_grant_stat.py +++ b/app/grants/tests/models/test_grant_stat.py @@ -16,8 +16,8 @@ def test_creation(self): assert isinstance(grant_stat, GrantStat) - def test_grant_stat_belongs_to_grant(self): - """Test association with Grant model.""" + def test_grant_stat_has_associated_grant(self): + """Test 'grant' attribute is present and is an instance of Grant.""" grant_stat = GrantStatFactory() @@ -25,7 +25,7 @@ def test_grant_stat_belongs_to_grant(self): assert isinstance(grant_stat.grant, Grant) def test_grant_stat_has_data_attribute(self): - """Test 'data' attribute.""" + """Test 'data' attribute is present and defaults to empty dictionary.""" grant_stat = GrantStatFactory() @@ -34,11 +34,10 @@ def test_grant_stat_has_data_attribute(self): assert len(grant_stat.data) == 0 def test_grant_stat_has_snapshot_type(self): - """Test 'snapshot_type' attribute.""" + """Test 'snapshot_type' attribute is present.""" grant_stat = GrantStatFactory() assert hasattr(grant_stat, 'snapshot_type') - assert grant_stat.snapshot_type == '' From 75dad027e822e41fd56ec3c84ff889301bc7067f Mon Sep 17 00:00:00 2001 From: Aditya Anand M C Date: Wed, 29 Sep 2021 17:22:29 +0530 Subject: [PATCH 9/9] Update grant.py --- 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 ec8ab8fe42a..03b5501eabb 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)'))