Skip to content

Commit

Permalink
test: test async library
Browse files Browse the repository at this point in the history
  • Loading branch information
MrMarble committed Sep 20, 2021
1 parent 861ec51 commit 30cf82c
Show file tree
Hide file tree
Showing 3 changed files with 123 additions and 75 deletions.
20 changes: 19 additions & 1 deletion poetry.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ pytest = "^5.2"
flake8 = "^3.9.2"
pylint = "^2.11.1"
black = "^21.9b0"
pytest-asyncio = "^0.15.1"

[build-system]
requires = ["poetry-core>=1.0.0"]
Expand Down
177 changes: 103 additions & 74 deletions tests/test_datadis.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,13 @@
from datadis import (get_consumption_data, get_max_power, get_token,
get_supplies, get_contract_detail, _ENDPOINTS)
from datadis import (
get_consumption_data,
get_max_power,
get_token,
get_supplies,
get_contract_detail,
_ENDPOINTS,
)
from unittest import mock
import pytest


def mock_requests(*args, **kwargs):
Expand All @@ -16,92 +23,114 @@ def json(self):
@property
def text(self):
return self.text_data

print(args[0])
if args[0].startswith(_ENDPOINTS['get_supplies']):
return MockResponse([{
"address": "home",
"cups": "1234ABC",
"postalCode": "1024",
"province": "madrid",
"municipality": "madrid",
"distributor": "Energy",
"validDateFrom": "2020/09",
"validDateTo": "2021/09",
"pointType": 0,
"distributorCode": "2"
}], status_code=200)
elif args[0].startswith(_ENDPOINTS['get_contract_detail']):
return MockResponse([{
"address": "home",
"cups": "c",
"postalCode": "1024",
"province": "madrid",
"municipality": "madrid",
"distributor": "Energy",
"validDateFrom": "2020/09",
"validDateTo": "2021/09",
"pointType": 0,
"marketer": "idk",
"tension": "10",
"accesFare": "10",
"contractedPowerkW": ["10"],
"timeDiscrimination": "4",
"modePowerControl": "1",
"startDate": "2020/09",
"endDate": "2020/09",
}], status_code=200)
elif args[0].startswith(_ENDPOINTS['get_consumption_data']):
return MockResponse([{
"cups": "1234ABC",
"date": "2021/08/01",
"time": "01:00",
"consumptionKWh": 0.194,
"obtainMethod": "Real"
}])
elif args[0].startswith(_ENDPOINTS['get_max_power']):
return MockResponse([{
"cups": "1234ABC",
"date": "2021/08/21",
"time": "13:30",
"maxPower": 3.788
}])
if args[0].startswith(_ENDPOINTS["get_supplies"]):
return MockResponse(
[
{
"address": "home",
"cups": "1234ABC",
"postalCode": "1024",
"province": "madrid",
"municipality": "madrid",
"distributor": "Energy",
"validDateFrom": "2020/09",
"validDateTo": "2021/09",
"pointType": 0,
"distributorCode": "2",
}
],
status_code=200,
)
elif args[0].startswith(_ENDPOINTS["get_contract_detail"]):
return MockResponse(
[
{
"address": "home",
"cups": "c",
"postalCode": "1024",
"province": "madrid",
"municipality": "madrid",
"distributor": "Energy",
"validDateFrom": "2020/09",
"validDateTo": "2021/09",
"pointType": 0,
"marketer": "idk",
"tension": "10",
"accesFare": "10",
"contractedPowerkW": ["10"],
"timeDiscrimination": "4",
"modePowerControl": "1",
"startDate": "2020/09",
"endDate": "2020/09",
}
],
status_code=200,
)
elif args[0].startswith(_ENDPOINTS["get_consumption_data"]):
return MockResponse(
[
{
"cups": "1234ABC",
"date": "2021/08/01",
"time": "01:00",
"consumptionKWh": 0.194,
"obtainMethod": "Real",
}
]
)
elif args[0].startswith(_ENDPOINTS["get_max_power"]):
return MockResponse(
[
{
"cups": "1234ABC",
"date": "2021/08/21",
"time": "13:30",
"maxPower": 3.788,
}
]
)
else:
return MockResponse([{"cups": "of rice"}], 'token_ok', 200)
return MockResponse([{"cups": "of rice"}], "token_ok", 200)


@mock.patch('httpx.post', side_effect=mock_requests)
def test_get_token(mock_post: mock.MagicMock):
token = get_token("username", "password")
@pytest.mark.asyncio
@mock.patch("httpx.AsyncClient.post", side_effect=mock_requests)
async def test_get_token(mock_post: mock.MagicMock):
token = await get_token("username", "password")
assert token is not None
assert token == 'token_ok'

assert token == "token_ok"

@mock.patch('httpx.get', side_effect=mock_requests)
def test_get_supplies(mock_get: mock.MagicMock):
supplies = get_supplies("token")
@pytest.mark.asyncio
@mock.patch("httpx.AsyncClient.get", side_effect=mock_requests)
async def test_get_supplies(mock_get: mock.MagicMock):
supplies = await get_supplies("token")
assert supplies is not None
assert len(supplies) == 1
assert supplies[0]['address'] == 'home'

assert supplies[0]["address"] == "home"

@mock.patch('httpx.get', side_effect=mock_requests)
def test_get_contract_detail(mock_get: mock.MagicMock):
contract_detail = get_contract_detail("token", "cupaso", 2)
@pytest.mark.asyncio
@mock.patch("httpx.AsyncClient.get", side_effect=mock_requests)
async def test_get_contract_detail(mock_get: mock.MagicMock):
contract_detail = await get_contract_detail("token", "cupaso", 2)

assert contract_detail is not None


@mock.patch('httpx.get', side_effect=mock_requests)
def test_get_consumption_data(mock_get: mock.MagicMock):
contract_detail = get_consumption_data(
"token", "cupaso", 2, '2021/08/01', '2021/08/31', 0, 5)
@pytest.mark.asyncio
@mock.patch("httpx.AsyncClient.get", side_effect=mock_requests)
async def test_get_consumption_data(mock_get: mock.MagicMock):
contract_detail = await get_consumption_data(
"token", "cupaso", 2, "2021/08/01", "2021/08/31", 0, 5
)

assert contract_detail is not None


@mock.patch('httpx.get', side_effect=mock_requests)
def test_get_max_power(mock_get: mock.MagicMock):
contract_detail = get_max_power(
"token", "cupaso", 2, '2021/08/01', '2021/08/31')
@pytest.mark.asyncio
@mock.patch("httpx.AsyncClient.get", side_effect=mock_requests)
async def test_get_max_power(mock_get: mock.MagicMock):
contract_detail = await get_max_power(
"token", "cupaso", 2, "2021/08/01", "2021/08/31"
)

assert contract_detail is not None

0 comments on commit 30cf82c

Please sign in to comment.