Skip to content

Commit 8f54d46

Browse files
ksoloKevin Soloriogdixon
authored
Testing workshop (#9668)
* updated to ignore tool versions for local python version management * remove whitespace * update factory locations for testing workshop * update module path for cy_create_grants * update factories and tests * prep for testing workshop * tests from workshop * updated location of factory in import * update test and factory for grant_clr * Update .gitignore Co-authored-by: Graham Dixon <[email protected]> Co-authored-by: Kevin Solorio <[email protected]> Co-authored-by: Graham Dixon <[email protected]>
1 parent 0cd2af9 commit 8f54d46

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

45 files changed

+119
-144
lines changed

.gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -61,3 +61,6 @@ _build/
6161
# cypress artifacts
6262
cypress/videos
6363
cypress/screenshots
64+
65+
# asdf-vm (https://asdf-vm.com/)
66+
.tool-versions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
from .bounty_factory import BountyFactory
2+
from .fulfillment_factory import FulfillmentFactory
3+
from .profile_factory import ProfileFactory

app/dashboard/tests/factories/bounty_factory.py

+5-5
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,8 @@ class BountyFactory(factory.django.DjangoModelFactory):
88
class Meta:
99
model = Bounty
1010

11-
web3_created = datetime.now()
12-
is_open = True
13-
expires_date = datetime.now() + timedelta(days=365)
14-
raw_data = {}
15-
bounty_owner_github_username = 'gitcoin'
11+
web3_created = factory.LazyFunction(datetime.now)
12+
is_open = factory.Faker('pybool')
13+
expires_date = factory.LazyFunction(lambda: datetime.now() + timedelta(days=365))
14+
raw_data = factory.LazyFunction(dict)
15+
bounty_owner_github_username = factory.LazyFunction(lambda: 'gitcoin')

app/dashboard/tests/factories/profile_factory.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,4 @@ class Meta:
99
model = Profile
1010

1111
handle = factory.Sequence(lambda n: "Contributor_%03d" % n)
12-
data = {}
12+
data = factory.LazyFunction(dict)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
from attr import has
2+
import pytest
3+
4+
from dashboard.models import BountyFulfillment
5+
from dashboard.tests.factories.fulfillment_factory import FulfillmentFactory
6+
from dashboard.tests.factories.bounty_factory import BountyFactory
7+
8+
@pytest.mark.django_db
9+
class TestBountyFulfillmentProperties:
10+
def test_fulfillment_has_bounty(self):
11+
fulfillment = FulfillmentFactory()
12+
assert hasattr(fulfillment, 'bounty')
13+
14+
def test_deleting_bounty_deletes_fulfillment(self):
15+
bounty = BountyFactory()
16+
fulfillment = FulfillmentFactory(bounty=bounty)
17+
18+
bounty.delete()
19+
20+
with pytest.raises(BountyFulfillment.DoesNotExist):
21+
fulfillment.refresh_from_db()

app/dashboard/tests/views/test_bounty_api.py

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
import pytest
2-
from dashboard.tests.factories.bounty_factory import BountyFactory
2+
from dashboard.tests.factories import BountyFactory
33
from rest_framework.test import APIClient
44

55

@@ -13,4 +13,4 @@ def test_retrieves_activities(self, django_user_model):
1313
client.force_login(user)
1414
response = client.get('/actions/api/v0.1/bounty/', github_url, format='json')
1515

16-
assert response.status_code == 200
16+
assert response.status_code == 200

app/dashboard/tests/views/test_bounty_payout.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
import pytest
2-
from dashboard.tests.factories.profile_factory import ProfileFactory
3-
from dashboard.tests.factories.bounty_factory import BountyFactory
4-
from dashboard.tests.factories.fulfillment_factory import FulfillmentFactory
2+
from dashboard.tests.factories import ProfileFactory, BountyFactory, FulfillmentFactory
53
from rest_framework.test import APIClient
64

75

@@ -31,4 +29,4 @@ def test_pays_out_bounty(self, django_user_model):
3129

3230
response = client.post(f'/api/v1/bounty/payout/{fulfillment.id}', payload)
3331

34-
assert response.status_code == 200
32+
assert response.status_code == 200

app/dashboard/tests/views/test_profile_projects.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
import pytest
44
from dashboard.models import PortfolioItem
5-
from dashboard.tests.factories.profile_factory import ProfileFactory
5+
from dashboard.tests.factories import ProfileFactory
66

77

88
class TestProfileTabProjectCreation:

app/grants/management/commands/cy_create_grants.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from django.core.management.base import BaseCommand
44

5-
from grants.tests.models.factories.grant_factory import GrantFactory
5+
from grants.tests.factories import GrantFactory
66

77

88
class Command(BaseCommand):
+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
from .cart_activity_factory import CartActivityFactory
2+
from .clr_match_factory import CLRMatchFactory
3+
from .contribution_factory import ContributionFactory
4+
from .donation_factory import DonationFactory
5+
from .flag_factory import FlagFactory
6+
from .grant_api_key_factory import GrantAPIKeyFactory
7+
from .grant_branding_routing_policy_factory import GrantBrandingRoutingPolicyFactory
8+
from .grant_category_factory import GrantCategoryFactory
9+
from .grant_clr_calculation_factory import GrantCLRCalculationFactory
10+
from .grant_clr_factory import GrantCLRFactory
11+
from .grant_collection_factory import GrantCollectionFactory
12+
from .grant_factory import GrantFactory
13+
from .grant_stat_factory import GrantStatFactory
14+
from .grant_type_factory import GrantTypeFactory
15+
from .match_pledge_factory import MatchPledgeFactory
16+
from .subscription_factory import SubscriptionFactory
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
1+
import random
2+
13
import factory
24
import pytest
3-
from dashboard.tests.factories.profile_factory import ProfileFactory
5+
from dashboard.tests.factories import ProfileFactory
46
from grants.models.cart_activity import CartActivity
57

68
from .grant_factory import GrantFactory
79

810

911
@pytest.mark.django_db
1012
class CartActivityFactory(factory.django.DjangoModelFactory):
11-
"""Create mock CartActivity for testing."""
12-
1313
class Meta:
1414
model = CartActivity
1515

1616
grant = factory.SubFactory(GrantFactory)
1717
profile = factory.SubFactory(ProfileFactory)
18-
action = ''
18+
action = factory.LazyFunction(lambda: random.choice(CartActivity.ACTIONS)[0])
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import factory
22
import pytest
3+
34
from grants.models.clr_match import CLRMatch
45

56
from .contribution_factory import ContributionFactory
@@ -8,12 +9,10 @@
89

910
@pytest.mark.django_db
1011
class CLRMatchFactory(factory.django.DjangoModelFactory):
11-
"""Create a mock CLRMatch for testing."""
12-
1312
class Meta:
14-
model = CLRMatch
13+
model = CLRMatch
1514

16-
amount = 0.0
15+
amount = factory.Faker('pyfloat')
1716
grant = factory.SubFactory(GrantFactory)
1817
test_payout_contribution = factory.SubFactory(ContributionFactory)
1918
payout_contribution = factory.SubFactory(ContributionFactory)

app/grants/tests/models/factories/contribution_factory.py renamed to app/grants/tests/factories/contribution_factory.py

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66

77
class ContributionFactory(factory.django.DjangoModelFactory):
8-
"""Create mock Contribution for testing."""
9-
108
class Meta:
119
model = Contribution
1210

app/grants/tests/models/factories/donation_factory.py renamed to app/grants/tests/factories/donation_factory.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,12 @@
33
from grants.models.donation import Donation
44

55
from .contribution_factory import ContributionFactory
6-
from .profile_factory import ProfileFactory
6+
from dashboard.tests.factories import ProfileFactory
77
from .subscription_factory import SubscriptionFactory
88

99

1010
@pytest.mark.django_db
1111
class DonationFactory(factory.django.DjangoModelFactory):
12-
"""Create a mock Donation for testing."""
13-
1412
class Meta:
1513
model = Donation
1614

app/grants/tests/models/factories/flag_factory.py renamed to app/grants/tests/factories/flag_factory.py

+1-4
Original file line numberDiff line numberDiff line change
@@ -3,16 +3,13 @@
33
from grants.models.flag import Flag
44

55
from .grant_factory import GrantFactory
6-
from .profile_factory import ProfileFactory
6+
from dashboard.tests.factories import ProfileFactory
77

88

99
@pytest.mark.django_db
1010
class FlagFactory(factory.django.DjangoModelFactory):
11-
"""Create a mock Flag for testing."""
12-
1311
class Meta:
1412
model = Flag
1513

1614
grant = factory.SubFactory(GrantFactory)
1715
profile = factory.SubFactory(ProfileFactory)
18-
comments = ''

app/grants/tests/models/factories/grant_api_key_factory.py renamed to app/grants/tests/factories/grant_api_key_factory.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import factory
22
from grants.models.grant_api_key import GrantAPIKey
33

4-
from .profile_factory import ProfileFactory
4+
from dashboard.tests.factories import ProfileFactory
55

66

77
class GrantAPIKeyFactory(factory.django.DjangoModelFactory):
8-
"""Create mock GrantAPIKey for testing."""
9-
108
class Meta:
119
model = GrantAPIKey
1210

Original file line numberDiff line numberDiff line change
@@ -1,11 +1,11 @@
1+
from random import randint
2+
13
import factory
24
from grants.models.grant_branding_routing_policy import GrantBrandingRoutingPolicy
35

46

57
class GrantBrandingRoutingPolicyFactory(factory.django.DjangoModelFactory):
6-
"""Create mock GrantBrandingRoutingPolicy for testing."""
7-
88
class Meta:
99
model = GrantBrandingRoutingPolicy
1010

11-
priority = 1
11+
priority = factory.LazyFunction(lambda: randint(1, 255))

app/grants/tests/factories/grant_category_factory.py

-4
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,5 @@
33

44

55
class GrantCategoryFactory(factory.django.DjangoModelFactory):
6-
"""Create mock GrantCategory for testing."""
7-
86
class Meta:
97
model = GrantCategory
10-
11-
category = factory.Sequence(lambda n: "Category #%s" % n)

app/grants/tests/models/factories/grant_clr_calculation_factory.py renamed to app/grants/tests/factories/grant_clr_calculation_factory.py

-2
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88

99
@pytest.mark.django_db
1010
class GrantCLRCalculationFactory(factory.django.DjangoModelFactory):
11-
"""Create mock GrantCLRCalculation for testing."""
12-
1311
class Meta:
1412
model = GrantCLRCalculation
1513

Original file line numberDiff line numberDiff line change
@@ -1,20 +1,21 @@
11
from datetime import datetime, timedelta
2+
from random import choice
23

34
import factory
45
import pytest
56
from grants.models.grant import GrantCLR
67

8+
from dashboard.tests.factories import ProfileFactory
9+
710

811
@pytest.mark.django_db
912
class GrantCLRFactory(factory.django.DjangoModelFactory):
10-
"""Create mock GrantCLR for testing."""
11-
1213
class Meta:
1314
model = GrantCLR
1415

15-
round_num = 2
16-
start_date = datetime.now()
17-
end_date = start_date + timedelta(weeks=2)
18-
is_active = True
19-
type='main'
20-
banner_text='text which appears below banner'
16+
round_num = factory.Faker('pyint')
17+
start_date = factory.LazyFunction(datetime.now)
18+
end_date = factory.LazyAttribute(lambda o: o.start_date + timedelta(weeks=2))
19+
type = factory.LazyFunction(lambda: choice(GrantCLR.CLR_TYPES)[0])
20+
banner_text = factory.Faker('catch_phrase')
21+
owner = factory.SubFactory(ProfileFactory)

app/grants/tests/models/factories/grant_collection_factory.py renamed to app/grants/tests/factories/grant_collection_factory.py

+1-3
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
import factory
22
from grants.models.grant_collection import GrantCollection
33

4-
from .profile_factory import ProfileFactory
4+
from dashboard.tests.factories import ProfileFactory
55

66

77
class GrantCollectionFactory(factory.django.DjangoModelFactory):
8-
"""Create mock GrantCollection for testing."""
9-
108
class Meta:
119
model = GrantCollection
1210

app/grants/tests/models/factories/grant_factory.py renamed to app/grants/tests/factories/grant_factory.py

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33

44

55
class GrantFactory(factory.django.DjangoModelFactory):
6-
"""Create mock Grant for testing."""
7-
86
class Meta:
97
model = Grant

app/grants/tests/models/factories/grant_stat_factory.py renamed to app/grants/tests/factories/grant_stat_factory.py

-2
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@
55

66

77
class GrantStatFactory(factory.django.DjangoModelFactory):
8-
"""Create mock GrantStat for testing."""
9-
108
class Meta:
119
model = GrantStat
1210

app/grants/tests/factories/grant_type_factory.py

-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,5 @@
33

44

55
class GrantTypeFactory(factory.django.DjangoModelFactory):
6-
"""Create mock GrantType for testing."""
7-
86
class Meta:
97
model = GrantType

app/grants/tests/models/factories/match_pledge_factory.py renamed to app/grants/tests/factories/match_pledge_factory.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -4,15 +4,13 @@
44
from grants.models.match_pledge import MatchPledge
55

66
from .grant_clr_factory import GrantCLRFactory
7-
from .profile_factory import ProfileFactory
7+
from dashboard.tests.factories import ProfileFactory
88

99

1010
class MatchPledgeFactory(factory.django.DjangoModelFactory):
11-
"""Create mock MatchPledge for testing."""
12-
1311
class Meta:
1412
model = MatchPledge
1513

1614
profile = factory.SubFactory(ProfileFactory)
17-
data = json.dumps('test string')
15+
data = factory.LazyFunction(lambda: json.dumps(dict()))
1816
clr_round_num = factory.SubFactory(GrantCLRFactory)

app/grants/tests/models/factories/subscription_factory.py renamed to app/grants/tests/factories/subscription_factory.py

+2-4
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22
from grants.models.subscription import Subscription
33

44
from .grant_factory import GrantFactory
5-
from .profile_factory import ProfileFactory
5+
from dashboard.tests.factories import ProfileFactory
66

77

88
class SubscriptionFactory(factory.django.DjangoModelFactory):
9-
"""Create mock Subscription for testing."""
10-
119
class Meta:
1210
model = Subscription
1311

1412
grant = factory.SubFactory(GrantFactory)
15-
contributor_profile = factory.SubFactory(ProfileFactory)
13+
contributor_profile = factory.SubFactory(ProfileFactory)

app/grants/tests/management/commands/test_grant_collections_shuffle.py

+7-13
Original file line numberDiff line numberDiff line change
@@ -6,31 +6,25 @@
66
import pytest
77
from grants.management.commands.grant_collections_shuffle import grant_collection_age_score, grant_meta_data_score
88

9-
from ...models.factories.grant_collection_factory import GrantCollectionFactory
10-
from ...models.factories.grant_factory import GrantFactory
9+
from grants.tests.factories import GrantFactory
1110

1211

1312
@pytest.mark.django_db
1413
def test_grant_collections_shuffle_grant_meta_data_score():
15-
grant_1 = GrantFactory()
16-
grant_2 = GrantFactory()
17-
18-
grant_1.twitter_verified = True
19-
grant_2.twitter_verified = True
20-
21-
grant_1.github_project_url = 'https://github.com/gitcoinco/web'
22-
grant_2.github_project_url = 'https://github.com/gitcoinco/web'
23-
14+
grant_1 = GrantFactory(twitter_verified=True, github_project_url='https://github.com/gitcoinco/web')
15+
grant_2 = GrantFactory(twitter_verified=True, github_project_url='https://github.com/gitcoinco/web')
2416
grants = (grant_1, grant_2)
2517

2618
score = grant_meta_data_score(grants)
27-
19+
2820
assert score == 12000
29-
21+
3022
@pytest.mark.django_db
3123
def test_grant_collections_shuffle_calc_age_score():
3224
today = datetime.now()
3325
last_month = today - (today - timedelta(days=28))
3426
last_week = today - timedelta(days=7)
27+
3528
score = grant_collection_age_score(last_month, last_week)
29+
3630
assert score == 3750

0 commit comments

Comments
 (0)