Skip to content

Commit

Permalink
Adding testing and code coverage reporting (#1)
Browse files Browse the repository at this point in the history
* Fix tests to run within pipenv

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Fix code coverage

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* pipenv understands it's already in a venv

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Add coverage testing for branches

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Add coveralls and try to see if 3.3 fixes work

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Try another fix for python 3.3 error

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* pytest doesn't support python 3.3 anymore, so neither will I

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Potential fix for 3.3 not needed

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Remove support for python 2.7

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Add more tests for coverage, remove unneeded code

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Add code coverage badges

Signed-off-by: Edwin Hermans <edwin@madtech.cx>

* Version bump, cleanup dependencies, add setup.py test stuff

Signed-off-by: Edwin Hermans <edwin@madtech.cx>
  • Loading branch information
madeddie committed Mar 15, 2018
1 parent 8f75f15 commit cecf513
Show file tree
Hide file tree
Showing 10 changed files with 141 additions and 26 deletions.
6 changes: 4 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
language: python
python:
- '3.3'
# - '2.7' # module krakenex only support python 3+
- '3.4'
- '3.5'
- 3.5-dev
Expand All @@ -11,10 +11,12 @@ python:
install:
- pip install -U pipenv
- pipenv install --dev --skip-lock
- pip install -e .
script:
- pytest --cov=./
- pytest --cov=madcc --cov-report xml --cov-report=term tests
after_success:
- codecov
- coveralls
deploy:
provider: pypi
user: madeddie
Expand Down
4 changes: 3 additions & 1 deletion Pipfile
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ name = "pypi"
krakenex = "*"
coinmarketcap = "*"
tabulate = "*"
blockchain = "*"
clint = "*"
requests = "*"


[dev-packages]
Expand All @@ -22,6 +22,8 @@ mock = "*"
pytest-mock = "*"
pytest-cov = "*"
codecov = "*"
coveralls = "*"
requests-mock = "*"


[requires]
Expand Down
4 changes: 4 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,10 @@ madcc: Useful cryptocurrency tools
:target: https://pypi.python.org/pypi/madcc
.. image:: https://travis-ci.org/madeddie/madcc.svg?branch=master
:target: https://travis-ci.org/madeddie/madcc
.. image:: https://coveralls.io/repos/github/madeddie/madcc/badge.svg?branch=testing
:target: https://coveralls.io/github/madeddie/madcc?branch=testing
.. image:: https://codecov.io/gh/madeddie/madcc/branch/master/graph/badge.svg
:target: https://codecov.io/gh/madeddie/madcc

A collection of useful cryptocurrency utils and functions. Makes use of:

Expand Down
2 changes: 1 addition & 1 deletion madcc/kraken/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from . import kraken
from .kraken import KrakenUtils
9 changes: 3 additions & 6 deletions madcc/utils/crypto_assets.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,14 +95,11 @@ def demo():
decimals = 2

# Just for demo, not actual data
# crypto_data = parse_crypto_file(config['crypto_assets']['crypto_file'])
crypto_data = [['bitcoin', '10.5'], ['ethereum', '109.25']]
if not crypto_data:
sys.exit(1)

headers, crypto_table = generate_crypto_table(currency, crypto_data)
print(tabulate(crypto_table, headers=headers, floatfmt='.{}f'.format(decimals)))
return tabulate(crypto_table, headers=headers, floatfmt='.{}f'.format(decimals))


if __name__ == "__main__":
demo()
if __name__ == "__main__": # pragma: no cover
print(demo())
2 changes: 2 additions & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[aliases]
test=pytest
16 changes: 12 additions & 4 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@

setup(
name='madcc',
version='0.0.2-alpha.1',
version='0.0.2-alpha.2',
description='madtech cryptocurrency functions',
long_description=long_description,
url='https://github.com/madeddie/madcc',
Expand All @@ -23,13 +23,12 @@
'Topic :: Utilities',
'License :: OSI Approved :: MIT License',
'Programming Language :: Python :: 3',
'Programming Language :: Python :: 3.3',
'Programming Language :: Python :: 3.4',
'Programming Language :: Python :: 3.5',
'Programming Language :: Python :: 3.6',
],

keywords='personal project cryptocurrency kraken gdax',
keywords='personal project cryptocurrency kraken gdax coinmarketcap',
packages=find_packages(exclude=['contrib', 'docs', 'tests']),

install_requires=[
Expand All @@ -39,12 +38,21 @@
'requests',
'tabulate',
],
python_requires='~=3.3',
python_requires='~=3.4',

entry_points={
'console_scripts': [
'kraken_limits=madcc.entrypoints.kraken_limits:main',
'crypto_assets=madcc.entrypoints.crypto_assets:main',
],
},
setup_requires=[
'pytest-runner',
],
tests_require=[
'pytest',
'mock',
'pytest-mock',
'pytest-cov',
],
)
12 changes: 0 additions & 12 deletions tests/test_convert.py

This file was deleted.

108 changes: 108 additions & 0 deletions tests/test_crypto_assets.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
from unittest.mock import patch

import pytest

from madcc.utils import crypto_assets
from madcc.entrypoints import crypto_assets as crypto_assets_cli


# Testing data
raw_crypto_file = """
# cryptocurrency
- bitcoin 12.05
- some comment
- maybe mentioning ledger contains 12.05
- ethereum 80.2
- litecoin 250
- some comment
- eur 500
- also accepts usd
# closing hash"""

parsed_crypto_file = [
['bitcoin', '12.05'],
['ethereum', '80.2'],
['litecoin', '250'],
['eur', '500']
]

full_ticker_data = [
{'id': 'bitcoin', 'price_eur': '6615.31631639'},
{'id': 'ethereum', 'price_eur': '491.385586985'},
{'id': 'litecoin', 'price_eur': '130.550739603'}
]

generated_crypto_table = (
['symbol', 'amount', '%', 'eur price', 'eur total'],
[
['bitcoin', 12.05, 52.35, 6615.31631639, 79714.5616124995],
['ethereum', 80.2, 25.88, 491.385586985, 39409.124076197],
['litecoin', 250.0, 21.44, 130.550739603, 32637.684900750002],
['eur', 500.0, 0.33, 1, 500.0],
['total', None, None, None, 152261.3705894465]
]
)

demo_output = """symbol amount % eur price eur total
-------- -------- ----- ----------- -----------
bitcoin 12.05 52.35 6615.32 79714.56
ethereum 80.20 25.88 491.39 39409.12
litecoin 250.00 21.44 130.55 32637.68
eur 500.00 0.33 1.00 500.00
total 152261.37"""


def test_convert(mocker):
mocker.patch('requests.get')
crypto_assets.convert('eur', 10, 'usd')
crypto_assets.requests.get.assert_called_with(
crypto_assets.currency_api,
params={'base': 'EUR', 'symbols': 'USD'}
)


def test_convert_same():
assert crypto_assets.convert('eur', 10, 'eur') == ['eur', 10, 1, 10]


def test_parse_crypto_file(mocker):
with patch('builtins.open', mocker.mock_open(read_data=raw_crypto_file)) as m:
result = crypto_assets.parse_crypto_file('crypto_file')

m.assert_called_once_with('crypto_file')
assert result == parsed_crypto_file


def test_parse_crypto_file_fail_open(mocker):

with patch('builtins.open', mocker.mock_open()) as m:
m.side_effect = IOError
result = crypto_assets.parse_crypto_file('crypto_file')

m.assert_called_once_with('crypto_file')
assert result is False


def test_retrieve_ticker_data(mocker):
mocker.patch('coinmarketcap.Market.ticker')
result = crypto_assets.retrieve_ticker_data('eur')

crypto_assets.Market.ticker.assert_called_with(start=0, limit=2000, convert='eur')


def test_generate_crypto_table(mocker):
mocker.patch.object(crypto_assets, 'retrieve_ticker_data', return_value=full_ticker_data)

result = crypto_assets.generate_crypto_table('eur', parsed_crypto_file)
assert result == generated_crypto_table


def test_generate_crypto_table_missing_data():
assert crypto_assets.generate_crypto_table('eur', None) is False


def test_demo(mocker):
mocker.patch.object(crypto_assets, 'generate_crypto_table', return_value=generated_crypto_table)
assert crypto_assets.demo() == demo_output
4 changes: 4 additions & 0 deletions tests/test_kraken.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import pytest

from madcc.kraken import KrakenUtils
from madcc.entrypoints import kraken_limits

0 comments on commit cecf513

Please sign in to comment.