Skip to content

Commit

Permalink
Update tests, makefile, readme
Browse files Browse the repository at this point in the history
  • Loading branch information
Oli Girling committed Sep 28, 2023
1 parent 6b06597 commit ae04984
Show file tree
Hide file tree
Showing 20 changed files with 188 additions and 50 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
.DS_Store
.env

# Byte-compiled / optimized / DLL files
__pycache__/
Expand Down
4 changes: 3 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ FROM python:3-alpine
WORKDIR /app

RUN pip install requests \
coverage
coverage \
twine \
wheel

COPY . .
9 changes: 6 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ test: ## Run the tests
${DOCKER_COMMAND} python -m coverage run -m unittest discover

run: ## Run the sample testing file
${DOCKER_COMMAND} python index.py
${DOCKER_COMMAND} python run.py

test-coverage: ## Show test coverage
${DOCKER_COMMAND} python -m coverage report
Expand All @@ -24,8 +24,11 @@ setup: ## Setup
exec: ## Run test file
${DOCKER_COMMAND} sh

publish: ## Publish version
${DOCKER_COMMAND} npm publish
build-package: ## Build pip package
${DOCKER_COMMAND} python setup.py sdist bdist_wheel

upload-package: ## Upload pip package
${DOCKER_COMMAND} python -m twine upload dist/*

help:
@grep -h -E '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'
8 changes: 4 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ Python wrapper for <a href="https://currencyapi.net" title="CurrencyApi">Currenc

#### Prerequisites

- Minimum NodeJs v8 (npm v5 and above)
- Working on Python 3.x
- Minimum Python version 3.5
- Last tested and working on Python 3.11.5
- Free or Paid account with CurrencyApi.net

#### Test Coverage
Expand All @@ -41,15 +41,15 @@ pip install currencyapi
then import the package with:

```python
from currencyapi.client import Client
from currencyapi.currency import Currency
```

## Usage

### Instantiating

```python
currency = Client('API_KEY')
currency = Currency('API_KEY')
```

### Live rates:
Expand Down
2 changes: 1 addition & 1 deletion currencyapi/__init__.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
from currencyapi.client import Client
from currencyapi.currency import Currency
8 changes: 0 additions & 8 deletions currencyapi/client.py

This file was deleted.

24 changes: 24 additions & 0 deletions currencyapi/currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
from currencyapi.endpoints.rates import Rates
from currencyapi.endpoints.convert import Convert
from currencyapi.endpoints.history import History
from currencyapi.endpoints.timeframe import Timeframe
from currencyapi.endpoints.currencies import Currencies

class Currency(object):
def __init__(self, api_key: str):
self._api_key = api_key

def rates(self) -> Rates:
return Rates(self._api_key)

def convert(self) -> Convert:
return Convert(self._api_key)

def history(self) -> History:
return History(self._api_key)

def timeframe(self) -> Timeframe:
return Timeframe(self._api_key)

def currencies(self) -> Currencies:
return Currencies(self._api_key)
5 changes: 0 additions & 5 deletions currencyapi/endpoints/currencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,4 @@
class Currencies(Endpoint):
def __init__(self, api_key: str):
super().__init__(api_key, CURRENCIES_ENDPOINT)
self._base()

def base(self, currency: str):
self._base(currency)
return self

6 changes: 5 additions & 1 deletion currencyapi/endpoints/endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,14 @@ def _headers(self):
headers['X-Sdk'] = 'python'
headers['Content-Type'] = content_type
return headers

def _build_url_params(self):
url_params = {'key': self.api_key}
return url_params | self.param

def get(self):
url = BASE_URL + self.endpoint
r = requests.get(url, params=self.param, headers=self._headers())
r = requests.get(url, params=self._build_url_params(), headers=self._headers())

if self._isXml():
return r.text
Expand Down
4 changes: 0 additions & 4 deletions index.py

This file was deleted.

4 changes: 4 additions & 0 deletions run.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
from currencyapi.currency import Currency

currency = Currency('')
print(currency.history().output('JSON').date('2013-01-02').get())
4 changes: 2 additions & 2 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
name="currencyapi",
version="1.0.0",
packages=find_packages(exclude="tests"),
description="Python Client for CurrencyApi.net",
description="Python wrapper for CurrencyApi.net",
long_description=README_MD,
long_description_content_type="text/markdown",
url="https://github.com/houseofapis/currencyapi-python",
Expand All @@ -16,7 +16,7 @@
classifiers=[
"License :: OSI Approved :: MIT License",
"Intended Audience :: Developers",
"Programming Language :: Python :: 3 :: Only"
"Programming Language :: Python :: 3 :: Only",
"Topic :: Software Development :: Libraries :: Python Modules",
],
install_requires=[
Expand Down
20 changes: 19 additions & 1 deletion tests/endpoints/test_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,22 @@ def test_convert_amount(self):
self.assertIsInstance(self.class_under_test.amount(10), Convert)

def test_history_endpoint_set(self):
self.assertEqual('convert', self.class_under_test.endpoint)
self.assertEqual('convert', self.class_under_test.endpoint)

def test_convert__build_url_params(self):
self.assertDictEqual(
{'key': 'fakekey', 'output': 'JSON'},
self.class_under_test._build_url_params()
)
self.class_under_test.output('xMl')
self.assertDictEqual(
{'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)
self.class_under_test.from_currency('gbP')
self.class_under_test.to_currency('uSd')
self.class_under_test.amount(10)
self.assertDictEqual(
{'key': 'fakekey', 'output': 'XML', 'from': 'GBP', 'to': 'USD', 'amount': 10},
self.class_under_test._build_url_params()
)
18 changes: 12 additions & 6 deletions tests/endpoints/test_currencies.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,16 @@ class Test(TestCase):
def setUp(self):
self.class_under_test = Currencies('fakekey')

def test_currencies_base(self):
self.class_under_test.base('gBp')
self.assertEqual('GBP', self.class_under_test.param.get('base'))
self.assertIsInstance(self.class_under_test.base('uSD'), Currencies)
def test_currencies_endpoint_set(self):
self.assertEqual('currencies', self.class_under_test.endpoint)

def test_rates_endpoint_set(self):
self.assertEqual('currencies', self.class_under_test.endpoint)
def test_currencies__build_url_params(self):
self.assertDictEqual(
{'key': 'fakekey', 'output': 'JSON'},
self.class_under_test._build_url_params()
)
self.class_under_test.output('xMl')
self.assertDictEqual(
{'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)
16 changes: 16 additions & 0 deletions tests/endpoints/test_endpoint.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,4 +64,20 @@ def test_endpoint_get_xml(self, mock_requests_get):
mock_data,
self.class_under_test.get()
)

def test_endpoint__build_url_params(self):
self.assertDictEqual(
{'key': 'fakekey', 'output': 'JSON'},
self.class_under_test._build_url_params()
)
self.class_under_test.output('xMl')
self.assertDictEqual(
{'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)
self.class_under_test._base('gbP')
self.assertDictEqual(
{'base': 'GBP', 'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)

23 changes: 22 additions & 1 deletion tests/endpoints/test_history.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,4 +18,25 @@ def test_history_date(self):
self.assertIsInstance(self.class_under_test.date('2023-01-01'), History)

def test_history_endpoint_set(self):
self.assertEqual('history', self.class_under_test.endpoint)
self.assertEqual('history', self.class_under_test.endpoint)

def test_history__build_url_params(self):
self.assertDictEqual(
{'key': 'fakekey', 'base': 'USD', 'output': 'JSON'},
self.class_under_test._build_url_params()
)
self.class_under_test.output('xMl')
self.assertDictEqual(
{'key': 'fakekey', 'base': 'USD', 'output': 'XML'},
self.class_under_test._build_url_params()
)
self.class_under_test.base('gbP')
self.assertDictEqual(
{'key': 'fakekey', 'base': 'GBP', 'output': 'XML'},
self.class_under_test._build_url_params()
)
self.class_under_test.date('2023-01-01')
self.assertDictEqual(
{'key': 'fakekey', 'base': 'GBP', 'date': '2023-01-01', 'output': 'XML'},
self.class_under_test._build_url_params()
)
20 changes: 18 additions & 2 deletions tests/endpoints/test_rates.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ def test_rates_endpoint_set(self):
self.assertEqual('rates', self.class_under_test.endpoint)

@patch.object(requests, 'get')
def test_endpoint_get(self, mock_requests_get):
def test_rates_get(self, mock_requests_get):
mock_data = [{"id": 1}, {"id": 2}]
response_mock = Mock(return_value=mock_data)
mock_requests_get.return_value.json = response_mock
Expand All @@ -27,11 +27,27 @@ def test_endpoint_get(self, mock_requests_get):
)

@patch.object(requests, 'get')
def test_endpoint_get_xml(self, mock_requests_get):
def test_rates_get_xml(self, mock_requests_get):
self.class_under_test.output('xMl')
mock_data = [{"id": 1}, {"id": 2}]
mock_requests_get.return_value.text = mock_data
self.assertEqual(
mock_data,
self.class_under_test.get()
)

def test_rates__build_url_params(self):
self.assertDictEqual(
{'base': 'USD', 'key': 'fakekey', 'output': 'JSON'},
self.class_under_test._build_url_params()
)
self.class_under_test.output('xMl')
self.assertDictEqual(
{'base': 'USD', 'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)
self.class_under_test.base('gbP')
self.assertDictEqual(
{'base': 'GBP', 'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)
24 changes: 23 additions & 1 deletion tests/endpoints/test_timeframe.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,4 +23,26 @@ def test_timeframe_end_date(self):
self.assertIsInstance(self.class_under_test.end_date('2023-01-02'), Timeframe)

def test_history_endpoint_set(self):
self.assertEqual('timeframe', self.class_under_test.endpoint)
self.assertEqual('timeframe', self.class_under_test.endpoint)

def test_timeframe__build_url_params(self):
self.assertDictEqual(
{'base': 'USD', 'key': 'fakekey', 'output': 'JSON'},
self.class_under_test._build_url_params()
)
self.class_under_test.output('xMl')
self.assertDictEqual(
{'base': 'USD', 'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)
self.class_under_test.base('gbP')
self.assertDictEqual(
{'base': 'GBP', 'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)
self.class_under_test.start_date('2023-01-01')
self.class_under_test.end_date('2023-01-02')
self.assertDictEqual(
{'base': 'GBP', 'start_date': '2023-01-01', 'end_date': '2023-01-02', 'key': 'fakekey', 'output': 'XML'},
self.class_under_test._build_url_params()
)
10 changes: 0 additions & 10 deletions tests/test_client.py

This file was deleted.

28 changes: 28 additions & 0 deletions tests/test_currency.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
from currencyapi import Currency
from currencyapi.endpoints.rates import Rates
from currencyapi.endpoints.history import History
from currencyapi.endpoints.timeframe import Timeframe
from currencyapi.endpoints.convert import Convert
from currencyapi.endpoints.currencies import Currencies
from unittest import TestCase

class Test(TestCase):
def test_rates_method(self):
class_under_test = Currency('fakeKey')
self.assertIsInstance(class_under_test.rates(), Rates)

def test_history_method(self):
class_under_test = Currency('fakeKey')
self.assertIsInstance(class_under_test.history(), History)

def test_timeframe_method(self):
class_under_test = Currency('fakeKey')
self.assertIsInstance(class_under_test.timeframe(), Timeframe)

def test_convert_method(self):
class_under_test = Currency('fakeKey')
self.assertIsInstance(class_under_test.convert(), Convert)

def test_currencies_method(self):
class_under_test = Currency('fakeKey')
self.assertIsInstance(class_under_test.currencies(), Currencies)

0 comments on commit ae04984

Please sign in to comment.