Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "hatchling.build"

[project]
name = "soliscloud-api"
version = "1.1.0"
version = "1.2.0"
authors = [
{ name="Peter van Hulten", email="peter.vanhulten@gmx.net" },
]
Expand Down
17 changes: 10 additions & 7 deletions soliscloud_api/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import async_timeout

# VERSION
VERSION = '1.1.0'
VERSION = '1.2.0'
SUPPORTED_SPEC_VERSION = '2.0'
RESOURCE_PREFIX = '/v1/api/'

Expand Down Expand Up @@ -618,7 +618,7 @@ async def epm_detail(

params: dict[str, Any] = {'sn': epm_sn}

return await self._get_records(EPM_DETAIL, key_id, secret, params)
return await self._get_data(EPM_DETAIL, key_id, secret, params)

async def epm_day(
self, key_id: str, secret: bytes, /, *,
Expand All @@ -636,7 +636,7 @@ async def epm_day(
'time': time,
'timezone': time_zone}

return await self._get_records(EPM_DAY, key_id, secret, params)
return await self._get_data(EPM_DAY, key_id, secret, params)

async def epm_month(
self, key_id: str, secret: bytes, /, *,
Expand All @@ -648,7 +648,7 @@ async def epm_month(
SoliscloudAPI._verify_date(SoliscloudAPI.DateFormat.MONTH, month)
params: dict[str, Any] = {'sn': epm_sn, 'month': month}

return await self._get_records(EPM_MONTH, key_id, secret, params)
return await self._get_data(EPM_MONTH, key_id, secret, params)

async def epm_year(
self, key_id: str, secret: bytes, /, *,
Expand All @@ -660,7 +660,7 @@ async def epm_year(
SoliscloudAPI._verify_date(SoliscloudAPI.DateFormat.YEAR, year)
params: dict[str, Any] = {'sn': epm_sn, 'year': year}

return await self._get_records(EPM_YEAR, key_id, secret, params)
return await self._get_data(EPM_YEAR, key_id, secret, params)

async def epm_all(
self, key_id: str, secret: bytes, /, *,
Expand All @@ -670,7 +670,7 @@ async def epm_all(

params: dict[str, Any] = {'sn': epm_sn}

return await self._get_records(EPM_ALL, key_id, secret, params)
return await self._get_data(EPM_ALL, key_id, secret, params)

async def weather_list(
self, key_id: str, secret: bytes, /, *,
Expand Down Expand Up @@ -720,7 +720,10 @@ async def _get_records(
url = f"{self.domain}{canonicalized_resource}"
try:
result = await self._post_data_json(url, header, params)
return result['page']['records']
if 'page' in result.keys():
return result['page']['records']
else:
return result['records']
except KeyError as err:
raise SoliscloudAPI.ApiError("Malformed data", result) from err

Expand Down
24 changes: 23 additions & 1 deletion test/const.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,26 @@
KEY = 'key_id'
SECRET = b'000000'
NMI = 'nmi_code'
VALID_RESPONSE = {'success': True, 'code': '0', 'msg': 'success', 'data': {'page': {'records': {'success': 1}}}}
VALID_RESPONSE = {
'success': True,
'code': '0',
'msg': 'success',
'data': {'item': 1}}

VALID_RESPONSE_LIST = {
'success': True,
'code': '0',
'msg': 'success',
'data': [{'item': 1}, {'item': 2}]}

VALID_RESPONSE_RECORDS = {
'success': True,
'code': '0',
'msg': 'success',
'data': {'records': [{'item': 1}, {'item': 2}]}}

VALID_RESPONSE_PAGED_RECORDS = {
'success': True,
'code': '0',
'msg': 'success',
'data': {'page': {'records': [{'item': 1}, {'item': 2}]}}}
8 changes: 4 additions & 4 deletions test/test_private_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from datetime import timezone
from aiohttp import ClientError
from soliscloud_api import SoliscloudAPI
from .const import KEY, SECRET, VALID_RESPONSE
from .const import KEY, SECRET, VALID_RESPONSE, VALID_RESPONSE_PAGED_RECORDS

VALID_HEADER = {
'Content-MD5': 'U0Xj//qmRi3zoyapfAAuXw==',
Expand All @@ -15,7 +15,7 @@
}

INVALID_RESPONSE_KEYERROR = {
'succes': True,
'success': True,
'codes': '0',
'msg': 'success',
'data': {'page': {'records': {'success': 1}}}
Expand Down Expand Up @@ -150,7 +150,7 @@ async def test_get_records(api_instance, mocker):
mocker.patch.object(
api_instance,
'_post_data_json',
return_value=VALID_RESPONSE['data'])
return_value=VALID_RESPONSE_PAGED_RECORDS['data'])
mocker.patch(
'soliscloud_api.SoliscloudAPI._prepare_header',
return_value=VALID_HEADER)
Expand All @@ -162,4 +162,4 @@ async def test_get_records(api_instance, mocker):
'https://soliscloud_test.com:13333/TEST',
VALID_HEADER,
{'pageNo': 1, 'pageSize': 100})
assert result == VALID_RESPONSE['data']['page']['records']
assert result == VALID_RESPONSE_PAGED_RECORDS['data']['page']['records']
117 changes: 77 additions & 40 deletions test/test_public_methods.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,15 @@
import soliscloud_api as api

# from soliscloud_api import *
from .const import KEY, SECRET, NMI, VALID_RESPONSE
from .const import (
KEY,
SECRET,
NMI,
VALID_RESPONSE,
VALID_RESPONSE_LIST,
VALID_RESPONSE_PAGED_RECORDS,
VALID_RESPONSE_RECORDS
)


@pytest.fixture
Expand All @@ -15,31 +23,60 @@ def api_instance():
@pytest.fixture
def patched_api(api_instance, mocker):
mocked_class = mocker.create_autospec(api.SoliscloudAPI)
mocker.patch.object(mocked_class, '_get_records',
mocker.patch.object(mocked_class, '_get_data',
return_value=VALID_RESPONSE)
mocker.patch.object(mocked_class, '_get_data', return_value=VALID_RESPONSE)
mocker.patch.object(api_instance, '_get_data', mocked_class._get_data)

return mocked_class


@pytest.fixture
def patched_api_list(api_instance, mocker):
mocked_class = mocker.create_autospec(api.SoliscloudAPI)
mocker.patch.object(mocked_class, '_get_data',
return_value=VALID_RESPONSE_LIST)
mocker.patch.object(api_instance, '_get_data', mocked_class._get_data)

return mocked_class


@pytest.fixture
def patched_api_paged(api_instance, mocker):
mocked_class = mocker.create_autospec(api.SoliscloudAPI)
mocker.patch.object(mocked_class, '_get_records',
return_value=VALID_RESPONSE_PAGED_RECORDS)
mocker.patch.object(api_instance, '_get_records',
mocked_class._get_records)

return mocked_class


@pytest.fixture
def patched_api_records(api_instance, mocker):
mocked_class = mocker.create_autospec(api.SoliscloudAPI)
mocker.patch.object(mocked_class, '_get_records',
return_value=VALID_RESPONSE_RECORDS)
mocker.patch.object(api_instance, '_get_records',
mocked_class._get_records)
mocker.patch.object(api_instance, '_get_data', mocked_class._get_data)

return mocked_class


@pytest.mark.asyncio
async def test_collector_list_valid(api_instance, patched_api):
async def test_collector_list_valid(api_instance, patched_api_paged):
# Required arguments only
result = await api_instance.collector_list(KEY, SECRET)
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_PAGED_RECORDS
patched_api_paged._get_records.assert_called_with(
api.COLLECTOR_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20})

# All arguments filled
result = await api_instance.collector_list(
KEY,
SECRET,
page_no=4, page_size=100, station_id=1000, nmi_code=NMI)
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_PAGED_RECORDS
patched_api_paged._get_records.assert_called_with(
api.COLLECTOR_LIST,
KEY, SECRET,
{
Expand Down Expand Up @@ -82,13 +119,13 @@ async def test_collector_detail_invalid_params(api_instance):


@pytest.mark.asyncio
async def test_collector_day_valid(api_instance, patched_api):
async def test_collector_day_valid(api_instance, patched_api_list):
# Required arguments only
result = await api_instance.collector_day(
KEY, SECRET,
collector_sn=1000, time='2023-01-01', time_zone=1)
assert result == VALID_RESPONSE
patched_api._get_data.assert_called_with(
assert result == VALID_RESPONSE_LIST
patched_api_list._get_data.assert_called_with(
api.COLLECTOR_DAY,
KEY, SECRET,
{'sn': 1000, 'time': '2023-01-01', 'timeZone': 1})
Expand Down Expand Up @@ -118,13 +155,13 @@ async def test_collector_day_invalid_params(api_instance):


@pytest.mark.asyncio
async def test_alarm_list_valid(api_instance, patched_api):
async def test_alarm_list_valid(api_instance, patched_api_records):
# Required arguments only
result = await api_instance.alarm_list(
KEY, SECRET,
station_id='1000', begintime='2022-01-01', endtime='2023-01-01')
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_RECORDS
patched_api_records._get_records.assert_called_with(
api.ALARM_LIST,
KEY, SECRET,
{
Expand All @@ -137,8 +174,8 @@ async def test_alarm_list_valid(api_instance, patched_api):
result = await api_instance.alarm_list(
KEY, SECRET,
device_sn='1000', begintime='2022-01-01', endtime='2023-01-01')
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_RECORDS
patched_api_records._get_records.assert_called_with(
api.ALARM_LIST,
KEY, SECRET,
{
Expand All @@ -156,8 +193,8 @@ async def test_alarm_list_valid(api_instance, patched_api):
begintime='2022-01-01',
endtime='2023-01-01',
nmi_code=NMI)
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_RECORDS
patched_api_records._get_records.assert_called_with(
api.ALARM_LIST,
KEY, SECRET,
{
Expand Down Expand Up @@ -229,20 +266,20 @@ async def test_alarm_list_invalid_params(api_instance):


@pytest.mark.asyncio
async def test_epm_list_valid(api_instance, patched_api):
async def test_epm_list_valid(api_instance, patched_api_paged):
# Required arguments only
result = await api_instance.epm_list(KEY, SECRET)
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_PAGED_RECORDS
patched_api_paged._get_records.assert_called_with(
api.EPM_LIST,
KEY, SECRET,
{'pageNo': 1, 'pageSize': 20})

result = await api_instance.epm_list(
KEY, SECRET,
page_no=4, page_size=30, station_id='1000')
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_PAGED_RECORDS
patched_api_paged._get_records.assert_called_with(
api.EPM_LIST,
KEY, SECRET,
{'pageNo': 4, 'pageSize': 30, 'stationId': '1000'})
Expand All @@ -260,7 +297,7 @@ async def test_epm_detail(api_instance, patched_api):
# Required arguments only
result = await api_instance.epm_detail(KEY, SECRET, epm_sn='sn')
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
patched_api._get_data.assert_called_with(
api.EPM_DETAIL,
KEY, SECRET,
{'sn': 'sn'})
Expand All @@ -273,7 +310,7 @@ async def test_epm_day_valid(api_instance, patched_api):
KEY, SECRET,
searchinfo='info', epm_sn='sn', time='2023-01-01', time_zone=1)
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
patched_api._get_data.assert_called_with(
api.EPM_DAY,
KEY, SECRET,
{
Expand Down Expand Up @@ -304,12 +341,12 @@ async def test_epm_day_invalid_params(api_instance):


@pytest.mark.asyncio
async def test_epm_month_valid(api_instance, patched_api):
async def test_epm_month_valid(api_instance, patched_api_list):
# Required arguments only
result = await api_instance.epm_month(
KEY, SECRET, epm_sn='sn', month='2023-01')
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_LIST
patched_api_list._get_data.assert_called_with(
api.EPM_MONTH,
KEY, SECRET,
{'sn': 'sn', 'month': '2023-01'})
Expand All @@ -330,13 +367,13 @@ async def test_epm_month_invalid_params(api_instance):


@pytest.mark.asyncio
async def test_epm_year_valid(api_instance, patched_api):
async def test_epm_year_valid(api_instance, patched_api_list):
# Required arguments only
result = await api_instance.epm_year(
KEY, SECRET,
epm_sn='sn', year='2023')
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_LIST
patched_api_list._get_data.assert_called_with(
api.EPM_YEAR,
KEY, SECRET,
{'sn': 'sn', 'year': '2023'})
Expand All @@ -352,28 +389,28 @@ async def test_epm_year_invalid_params(api_instance):


@pytest.mark.asyncio
async def test_epm_all_valid(api_instance, patched_api):
async def test_epm_all_valid(api_instance, patched_api_list):
# Required arguments only
result = await api_instance.epm_all(KEY, SECRET, epm_sn='sn')
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_LIST
patched_api_list._get_data.assert_called_with(
api.EPM_ALL, KEY, SECRET, {'sn': 'sn'})


@pytest.mark.asyncio
async def test_weather_list_valid(api_instance, patched_api):
async def test_weather_list_valid(api_instance, patched_api_paged):
# Required arguments only
result = await api_instance.weather_list(KEY, SECRET)
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_PAGED_RECORDS
patched_api_paged._get_records.assert_called_with(
api.WEATHER_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20})

# All arguments filled
result = await api_instance.weather_list(
KEY, SECRET,
page_no=4, page_size=100, station_id=1000, nmi_code=NMI)
assert result == VALID_RESPONSE
patched_api._get_records.assert_called_with(
assert result == VALID_RESPONSE_PAGED_RECORDS
patched_api_paged._get_records.assert_called_with(
api.WEATHER_LIST,
KEY, SECRET,
{
Expand Down
Loading
Loading