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

Testing workshop #9668

Merged
merged 10 commits into from
Nov 23, 2021
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -61,3 +61,6 @@ _build/
# cypress artifacts
cypress/videos
cypress/screenshots

#asdf vm
ksolo marked this conversation as resolved.
Show resolved Hide resolved
.tool-versions
3 changes: 3 additions & 0 deletions app/dashboard/tests/factories/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from .bounty_factory import BountyFactory
from .fulfillment_factory import FulfillmentFactory
from .profile_factory import ProfileFactory
10 changes: 5 additions & 5 deletions app/dashboard/tests/factories/bounty_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class BountyFactory(factory.django.DjangoModelFactory):
class Meta:
model = Bounty

web3_created = datetime.now()
is_open = True
expires_date = datetime.now() + timedelta(days=365)
raw_data = {}
bounty_owner_github_username = 'gitcoin'
web3_created = factory.LazyFunction(datetime.now)
is_open = factory.Faker('pybool')
expires_date = factory.LazyFunction(lambda: datetime.now() + timedelta(days=365))
raw_data = factory.LazyFunction(dict)
bounty_owner_github_username = factory.LazyFunction(lambda: 'gitcoin')
2 changes: 1 addition & 1 deletion app/dashboard/tests/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 = factory.LazyFunction(dict)
21 changes: 21 additions & 0 deletions app/dashboard/tests/models/test_bounty_fulfillment.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from attr import has
import pytest

from dashboard.models import BountyFulfillment
from dashboard.tests.factories.fulfillment_factory import FulfillmentFactory
from dashboard.tests.factories.bounty_factory import BountyFactory

@pytest.mark.django_db
class TestBountyFulfillmentProperties:
def test_fulfillment_has_bounty(self):
fulfillment = FulfillmentFactory()
assert hasattr(fulfillment, 'bounty')

def test_deleting_bounty_deletes_fulfillment(self):
bounty = BountyFactory()
fulfillment = FulfillmentFactory(bounty=bounty)

bounty.delete()

with pytest.raises(BountyFulfillment.DoesNotExist):
fulfillment.refresh_from_db()
4 changes: 2 additions & 2 deletions app/dashboard/tests/views/test_bounty_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import pytest
from dashboard.tests.factories.bounty_factory import BountyFactory
from dashboard.tests.factories import BountyFactory
from rest_framework.test import APIClient


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

assert response.status_code == 200
assert response.status_code == 200
6 changes: 2 additions & 4 deletions app/dashboard/tests/views/test_bounty_payout.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
import pytest
from dashboard.tests.factories.profile_factory import ProfileFactory
from dashboard.tests.factories.bounty_factory import BountyFactory
from dashboard.tests.factories.fulfillment_factory import FulfillmentFactory
from dashboard.tests.factories import ProfileFactory, BountyFactory, FulfillmentFactory
from rest_framework.test import APIClient


Expand Down Expand Up @@ -31,4 +29,4 @@ def test_pays_out_bounty(self, django_user_model):

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

assert response.status_code == 200
assert response.status_code == 200
2 changes: 1 addition & 1 deletion app/dashboard/tests/views/test_profile_projects.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import pytest
from dashboard.models import PortfolioItem
from dashboard.tests.factories.profile_factory import ProfileFactory
from dashboard.tests.factories import ProfileFactory


class TestProfileTabProjectCreation:
Expand Down
2 changes: 1 addition & 1 deletion app/grants/management/commands/cy_create_grants.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

from django.core.management.base import BaseCommand

from grants.tests.models.factories.grant_factory import GrantFactory
from grants.tests.factories import GrantFactory


class Command(BaseCommand):
Expand Down
16 changes: 16 additions & 0 deletions app/grants/tests/factories/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
from .cart_activity_factory import CartActivityFactory
from .clr_match_factory import CLRMatchFactory
from .contribution_factory import ContributionFactory
from .donation_factory import DonationFactory
from .flag_factory import FlagFactory
from .grant_api_key_factory import GrantAPIKeyFactory
from .grant_branding_routing_policy_factory import GrantBrandingRoutingPolicyFactory
from .grant_category_factory import GrantCategoryFactory
from .grant_clr_calculation_factory import GrantCLRCalculationFactory
from .grant_clr_factory import GrantCLRFactory
from .grant_collection_factory import GrantCollectionFactory
from .grant_factory import GrantFactory
from .grant_stat_factory import GrantStatFactory
from .grant_type_factory import GrantTypeFactory
from .match_pledge_factory import MatchPledgeFactory
from .subscription_factory import SubscriptionFactory
Original file line number Diff line number Diff line change
@@ -1,18 +1,18 @@
import random

import factory
import pytest
from dashboard.tests.factories.profile_factory import ProfileFactory
from dashboard.tests.factories import ProfileFactory
from grants.models.cart_activity import CartActivity

from .grant_factory import GrantFactory


@pytest.mark.django_db
class CartActivityFactory(factory.django.DjangoModelFactory):
"""Create mock CartActivity for testing."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come were removing all of these descriptions?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed them because they were technically not correct, these are factories not mocks. I also didn't think they added context. I am happy to put them back in though with the correction that they are factories that return instances of the Django Model.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thats cool with me, I think they're well enough named that we don't need the descriptions!


class Meta:
model = CartActivity

grant = factory.SubFactory(GrantFactory)
profile = factory.SubFactory(ProfileFactory)
action = ''
action = factory.LazyFunction(lambda: random.choice(CartActivity.ACTIONS)[0])
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import factory
import pytest

from grants.models.clr_match import CLRMatch

from .contribution_factory import ContributionFactory
Expand All @@ -8,12 +9,10 @@

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

class Meta:
model = CLRMatch
model = CLRMatch

amount = 0.0
amount = factory.Faker('pyfloat')
grant = factory.SubFactory(GrantFactory)
test_payout_contribution = factory.SubFactory(ContributionFactory)
payout_contribution = factory.SubFactory(ContributionFactory)
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@


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

class Meta:
model = Contribution

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,12 @@
from grants.models.donation import Donation

from .contribution_factory import ContributionFactory
from .profile_factory import ProfileFactory
from dashboard.tests.factories 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,13 @@
from grants.models.flag import Flag

from .grant_factory import GrantFactory
from .profile_factory import ProfileFactory
from dashboard.tests.factories 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 = ''
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import factory
from grants.models.grant_api_key import GrantAPIKey

from .profile_factory import ProfileFactory
from dashboard.tests.factories import ProfileFactory


class GrantAPIKeyFactory(factory.django.DjangoModelFactory):
"""Create mock GrantAPIKey for testing."""

class Meta:
model = GrantAPIKey

Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
from random import randint

import factory
from grants.models.grant_branding_routing_policy import GrantBrandingRoutingPolicy


class GrantBrandingRoutingPolicyFactory(factory.django.DjangoModelFactory):
"""Create mock GrantBrandingRoutingPolicy for testing."""

class Meta:
model = GrantBrandingRoutingPolicy

priority = 1
priority = factory.LazyFunction(lambda: randint(1, 255))
4 changes: 0 additions & 4 deletions app/grants/tests/factories/grant_category_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,5 @@


class GrantCategoryFactory(factory.django.DjangoModelFactory):
"""Create mock GrantCategory for testing."""

class Meta:
model = GrantCategory

category = factory.Sequence(lambda n: "Category #%s" % n)
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@

@pytest.mark.django_db
class GrantCLRCalculationFactory(factory.django.DjangoModelFactory):
"""Create mock GrantCLRCalculation for testing."""

class Meta:
model = GrantCLRCalculation

Expand Down
17 changes: 9 additions & 8 deletions app/grants/tests/factories/grant_clr_factory.py
Original file line number Diff line number Diff line change
@@ -1,20 +1,21 @@
from datetime import datetime, timedelta
from random import choice

import factory
import pytest
from grants.models.grant import GrantCLR

from dashboard.tests.factories import ProfileFactory


@pytest.mark.django_db
class GrantCLRFactory(factory.django.DjangoModelFactory):
"""Create mock GrantCLR for testing."""

class Meta:
model = GrantCLR

round_num = 2
start_date = datetime.now()
end_date = start_date + timedelta(weeks=2)
is_active = True
type='main'
banner_text='text which appears below banner'
round_num = factory.Faker('pyint')
start_date = factory.LazyFunction(datetime.now)
end_date = factory.LazyAttribute(lambda o: o.start_date + timedelta(weeks=2))
type = factory.LazyFunction(lambda: choice(GrantCLR.CLR_TYPES)[0])
banner_text = factory.Faker('catch_phrase')
owner = factory.SubFactory(ProfileFactory)
Original file line number Diff line number Diff line change
@@ -1,12 +1,10 @@
import factory
from grants.models.grant_collection import GrantCollection

from .profile_factory import ProfileFactory
from dashboard.tests.factories import ProfileFactory


class GrantCollectionFactory(factory.django.DjangoModelFactory):
"""Create mock GrantCollection for testing."""

class Meta:
model = GrantCollection

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@


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

class Meta:
model = Grant
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@


class GrantStatFactory(factory.django.DjangoModelFactory):
"""Create mock GrantStat for testing."""

class Meta:
model = GrantStat

Expand Down
2 changes: 0 additions & 2 deletions app/grants/tests/factories/grant_type_factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,5 @@


class GrantTypeFactory(factory.django.DjangoModelFactory):
"""Create mock GrantType for testing."""

class Meta:
model = GrantType
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,13 @@
from grants.models.match_pledge import MatchPledge

from .grant_clr_factory import GrantCLRFactory
from .profile_factory import ProfileFactory
from dashboard.tests.factories import ProfileFactory


class MatchPledgeFactory(factory.django.DjangoModelFactory):
"""Create mock MatchPledge for testing."""

class Meta:
model = MatchPledge

profile = factory.SubFactory(ProfileFactory)
data = json.dumps('test string')
data = factory.LazyFunction(lambda: json.dumps(dict()))
clr_round_num = factory.SubFactory(GrantCLRFactory)
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,12 @@
from grants.models.subscription import Subscription

from .grant_factory import GrantFactory
from .profile_factory import ProfileFactory
from dashboard.tests.factories 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)
contributor_profile = factory.SubFactory(ProfileFactory)
Original file line number Diff line number Diff line change
Expand Up @@ -6,31 +6,25 @@
import pytest
from grants.management.commands.grant_collections_shuffle import grant_collection_age_score, grant_meta_data_score

from ...models.factories.grant_collection_factory import GrantCollectionFactory
from ...models.factories.grant_factory import GrantFactory
from grants.tests.factories import GrantFactory


@pytest.mark.django_db
def test_grant_collections_shuffle_grant_meta_data_score():
grant_1 = GrantFactory()
grant_2 = GrantFactory()

grant_1.twitter_verified = True
grant_2.twitter_verified = True

grant_1.github_project_url = 'https://github.com/gitcoinco/web'
grant_2.github_project_url = 'https://github.com/gitcoinco/web'

grant_1 = GrantFactory(twitter_verified=True, github_project_url='https://github.com/gitcoinco/web')
grant_2 = GrantFactory(twitter_verified=True, github_project_url='https://github.com/gitcoinco/web')
grants = (grant_1, grant_2)

score = grant_meta_data_score(grants)

assert score == 12000

@pytest.mark.django_db
def test_grant_collections_shuffle_calc_age_score():
today = datetime.now()
last_month = today - (today - timedelta(days=28))
last_week = today - timedelta(days=7)

score = grant_collection_age_score(last_month, last_week)

assert score == 3750
Loading