Skip to content

Commit

Permalink
Merge pull request #375 from tyleryasaka/increase-code-coverage
Browse files Browse the repository at this point in the history
Increase code coverage
  • Loading branch information
mbeacom authored Feb 5, 2018
2 parents 8baca40 + f109a88 commit 57068e0
Show file tree
Hide file tree
Showing 7 changed files with 357 additions and 4 deletions.
6 changes: 3 additions & 3 deletions app/dashboard/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ def amount(request):

try:
amount = request.GET.get('amount')
deonomination = request.GET.get('denomination', 'ETH')
if deonomination == 'ETH':
denomination = request.GET.get('denomination', 'ETH')
if denomination == 'ETH':
amount_in_eth = float(amount)
else:
amount_in_eth = convert_amount(amount, deonomination, 'ETH')
amount_in_eth = convert_amount(amount, denomination, 'ETH')
amount_in_usdt = convert_amount(amount_in_eth, 'ETH', 'USDT')
response = {
'eth': amount_in_eth,
Expand Down
2 changes: 1 addition & 1 deletion app/dashboard/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ def get_github_api_url(self):
if self.github_url.lower()[:19] != 'https://github.com/':
return ''
url_path = urlparse(self.github_url).path
return 'https://api.github.com/repos/' + url_path
return 'https://api.github.com/repos' + url_path

def fetch_issue_item(self, item_type='body'):
"""Fetch the item type of an issue.
Expand Down
77 changes: 77 additions & 0 deletions app/dashboard/tests/test_dashboard_helpers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
# -*- coding: utf-8 -*-
"""Handle dashboard helper related tests.
Copyright (C) 2018 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from django.test import TestCase
from django.test.client import RequestFactory

import requests_mock
from dashboard.helpers import amount, description, keywords, normalizeURL, title
from economy.models import ConversionRate


class DashboardHelpersTest(TestCase):
"""Define tests for dashboard helpers."""

def setUp(self):
"""Perform setup for the testcase."""
self.factory = RequestFactory()
ConversionRate.objects.create(
from_amount=1,
to_amount=2,
source='etherdelta',
from_currency='ETH',
to_currency='USDT',
)

def test_amount(self):
"""Test the dashboard helper amount method."""
params = { 'amount': '5', 'denomination': 'ETH' }
request = self.factory.get('/sync/get_amount', params)
assert amount(request).content == b'{"eth": 5.0, "usdt": 10.0}'

def test_title(self):
"""Test the dashboard helper title method."""
sample_url = 'https://github.com/gitcoinco/web/issues/353'
params = { 'url': sample_url, 'denomination': 'ETH' }
with requests_mock.Mocker() as m:
m.get(sample_url, text='<span class="js-issue-title refined-linkified-title">Increase Code Coverage by 4%</span>')
request = self.factory.get('/sync/get_issue_title', params)
assert title(request).content == b'{"title": "Increase Code Coverage by 4%"}'

def test_description(self):
"""Test the dashboard helper description method."""
sample_url = 'https://github.com/gitcoinco/web/issues/353'
params = { 'url': sample_url }
with requests_mock.Mocker() as m:
m.get('https://api.github.com/repos/gitcoinco/web/issues/353', text='{"body": "This bounty will be paid out to anyone who meaningfully increases the code coverage of the repository by 4%."}')
request = self.factory.get('/sync/get_issue_description', params)
assert description(request).content == b'{"description": "This bounty will be paid out to anyone who meaningfully increases the code coverage of the repository by 4%."}'

def test_keywords(self):
"""Test the dashboard helper keywords method."""
sample_url = 'https://github.com/gitcoinco/web/issues/353'
params = { 'url': sample_url }
with requests_mock.Mocker() as m:
m.get('https://github.com/gitcoinco/web', text='<span class="lang">hello</span><span class="lang">world</span>')
request = self.factory.get('/sync/get_issue_keywords', params)
assert keywords(request).content == b'{"keywords": ["web", "gitcoinco", "hello", "world"]}'

def test_normalize_url(self):
"""Test the dashboard helper normalizeURL method."""
assert normalizeURL('https://gitcoin.co/') == 'https://gitcoin.co'
140 changes: 140 additions & 0 deletions app/dashboard/tests/test_dashboard_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,140 @@
# -*- coding: utf-8 -*-
"""Handle dashboard model related tests.
Copyright (C) 2018 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from datetime import date, datetime, timedelta

from django.test import TestCase

from dashboard.models import Bounty, Interest, Profile, Tip
from economy.models import ConversionRate


class DashboardModelsTest(TestCase):
"""Define tests for dashboard models."""

def setUp(self):
"""Perform setup for the testcase."""
ConversionRate.objects.create(
from_amount=1,
to_amount=2,
source='etherdelta',
from_currency='ETH',
to_currency='USDT',
)

def test_bounty(self):
"""Test the dashboard Bounty model."""
bounty = Bounty(
title='foo',
value_in_token=3,
token_name='ETH',
web3_created=datetime(2008, 10, 31),
github_url='https://github.com/gitcoinco/web',
token_address='0x0',
issue_description='hello world',
fulfiller_github_username='fred',
bounty_owner_github_username='flintstone',
is_open=False,
accepted=True,
expires_date=datetime(2008, 11, 30),
fulfiller_address="0x0000000000000000000000000000000000000000",
idx_project_length = 5,
bounty_type='Feature',
experience_level='Intermediate',
)
assert str(bounty) == 'foo 3 ETH 2008-10-31 00:00:00'
assert bounty.url == '/funding/details?url=https://github.com/gitcoinco/web'
assert bounty.title_or_desc == 'foo'
assert bounty.issue_description_text == 'hello world'
assert bounty.org_name == 'gitcoinco'
assert bounty.is_hunter('fred') == True
assert bounty.is_hunter('flintstone') == False
assert bounty.is_funder('fred') == False
assert bounty.is_funder('flintstone') == True
assert bounty.get_avatar_url
assert bounty.status == 'expired'
assert bounty.value_true == 3e-18
assert bounty.value_in_eth == 3
assert bounty.value_in_usdt == 0
assert 'ago 5 Feature Intermediate' in bounty.desc
assert bounty.is_legacy == False
assert bounty.get_github_api_url() == 'https://api.github.com/repos/gitcoinco/web'

def test_tip(self):
"""Test the dashboard Tip model."""
tip = Tip(
emails=['[email protected]'],
tokenName='ETH',
amount=7,
username='fred',
network='net',
expires_date=date.today() + timedelta(days=1),
tokenAddress='0x0000000000000000000000000000000000000000',
)
assert str(tip) == '(net) - PENDING 7 ETH to fred, created: today, expires: tomorrow'
assert tip.get_natural_value() == 7e-18
assert tip.value_in_eth == 7
assert tip.value_in_usdt == 14
assert tip.status == 'PENDING'

def test_interest(self):
"""Test the dashboard Interest model."""
profile = Profile(
handle='foo',
)
interest = Interest(
profile=profile,
)
assert str(interest) == 'foo'

def test_profile(self):
"""Test the dashboard Profile model."""
bounty = Bounty.objects.create(
github_url='https://github.com/gitcoinco/web',
web3_created=date.today(),
expires_date=date.today() + timedelta(days=1),
is_open=True,
raw_data={},
current_bounty=True,
bounty_owner_github_username='gitcoinco',
)
tip = Tip.objects.create(
emails=['[email protected]'],
github_url='https://github.com/gitcoinco/web',
expires_date=date.today() + timedelta(days=1),
)
profile = Profile(
handle='gitcoinco',
data={'type': 'Organization'},
repos_data=[{'contributors': [{'contributions': 50, 'login': 'foo'}]}],
)
assert str(profile) == 'gitcoinco'
assert profile.is_org == True
assert profile.bounties.first() == bounty
assert profile.tips.first() == tip
assert profile.authors == ['foo', 'gitcoinco']
assert profile.desc == '@gitcoinco is a newbie who has participated in 1 funded issue on Gitcoin'
assert profile.stats == [
('newbie', 'Status'),
(1, 'Total Funded Issues'),
(0, 'Open Funded Issues'),
('0x', 'Loyalty Rate'),
]
assert profile.github_url == 'https://github.com/gitcoinco'
assert profile.get_relative_url() == '/profile/gitcoinco'
56 changes: 56 additions & 0 deletions app/economy/tests/test_economy_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
# -*- coding: utf-8 -*-
"""Handle economy util related tests.
Copyright (C) 2018 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from django.test import TestCase
from django.test.client import RequestFactory

from economy.models import ConversionRate
from economy.utils import convert_amount, etherscan_link


class EconomyUtilsTest(TestCase):
"""Define tests for economy utils."""

def setUp(self):
"""Perform setup for the testcase."""
self.factory = RequestFactory()
ConversionRate.objects.create(
from_amount=1,
to_amount=2,
source='etherdelta',
from_currency='ETH',
to_currency='USDT',
)
ConversionRate.objects.create(
from_amount=1,
to_amount=3,
source='etherdelta',
from_currency='ETH',
to_currency='USDT',
)

def test_convert_amount(self):
"""Test the economy util convert_amount method."""
result = convert_amount(2, 'ETH', 'USDT')
assert round(result, 1) == 6

def test_etherscan_link(self):
"""Test the economy util etherscan_link method."""
txid = '0xcb39900d98fa00de2936d2770ef3bfef2cc289328b068e580dc68b7ac1e2055b'
assert etherscan_link(txid) == 'https://etherscan.io/tx/0xcb39900d98fa00de2936d2770ef3bfef2cc289328b068e580dc68b7ac1e2055b'
79 changes: 79 additions & 0 deletions app/gas/tests/test_gas_utils.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
# -*- coding: utf-8 -*-
"""Handle gas util related tests.
Copyright (C) 2018 Gitcoin Core
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License as published
by the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>.
"""
from django.test import TestCase
from django.test.client import RequestFactory

from economy.models import ConversionRate
from gas.models import GasProfile
from gas.utils import (
conf_time_spread, eth_usd_conv_rate, gas_price_to_confirm_time_minutes, recommend_min_gas_price_to_confirm_in_time,
)


class GasUtilsTest(TestCase):
"""Define tests for gas utils."""

def setUp(self):
"""Perform setup for the testcase."""
self.factory = RequestFactory()
GasProfile.objects.create(
gas_price=1,
mean_time_to_confirm_blocks=11,
mean_time_to_confirm_minutes=10,
_99confident_confirm_time_blocks=300,
_99confident_confirm_time_mins=100,
)
GasProfile.objects.create(
gas_price=2,
mean_time_to_confirm_blocks=5,
mean_time_to_confirm_minutes=4,
_99confident_confirm_time_blocks=150,
_99confident_confirm_time_mins=40,
)
GasProfile.objects.create(
gas_price=3,
mean_time_to_confirm_blocks=3,
mean_time_to_confirm_minutes=1,
_99confident_confirm_time_blocks=100,
_99confident_confirm_time_mins=10,
)
ConversionRate.objects.create(
from_amount=1,
to_amount=3,
source='etherdelta',
from_currency='ETH',
to_currency='USDT',
)

def test_recommend_min_gas_price_to_confirm_in_time(self):
"""Test the gas util recommend_min_gas_price_to_confirm_in_time method."""
assert recommend_min_gas_price_to_confirm_in_time(5) == 2

def test_gas_price_to_confirm_time_minutes(self):
"""Test the gas util gas_price_to_confirm_time_minutes method."""
assert gas_price_to_confirm_time_minutes(2) == 4

def test_eth_usd_conv_rate(self):
"""Test the gas util eth_usd_conv_rate method."""
assert round(eth_usd_conv_rate()) == 3

def test_conf_time_spread(self):
"""Test the gas util conf_time_spread method."""
assert conf_time_spread() == '[["1.00", "10.00"], ["2.00", "4.00"], ["3.00", "1.00"]]'
1 change: 1 addition & 0 deletions requirements/test.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,4 @@ coverage
django-coverage-plugin
isort
responses
requests_mock

0 comments on commit 57068e0

Please sign in to comment.