diff --git a/pyproject.toml b/pyproject.toml index 0cdb14d..13509ea 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -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" }, ] diff --git a/soliscloud_api/__init__.py b/soliscloud_api/__init__.py index 5e30323..7f0b168 100644 --- a/soliscloud_api/__init__.py +++ b/soliscloud_api/__init__.py @@ -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/' @@ -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, /, *, @@ -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, /, *, @@ -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, /, *, @@ -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, /, *, @@ -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, /, *, @@ -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 diff --git a/test/const.py b/test/const.py index 4474b04..8c49b3c 100644 --- a/test/const.py +++ b/test/const.py @@ -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}]}}} diff --git a/test/test_private_methods.py b/test/test_private_methods.py index 449e123..b6c7b15 100644 --- a/test/test_private_methods.py +++ b/test/test_private_methods.py @@ -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==', @@ -15,7 +15,7 @@ } INVALID_RESPONSE_KEYERROR = { - 'succes': True, + 'success': True, 'codes': '0', 'msg': 'success', 'data': {'page': {'records': {'success': 1}}} @@ -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) @@ -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'] diff --git a/test/test_public_methods.py b/test/test_public_methods.py index f07a4c7..daea53f 100644 --- a/test/test_public_methods.py +++ b/test/test_public_methods.py @@ -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 @@ -15,22 +23,51 @@ 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 @@ -38,8 +75,8 @@ async def test_collector_list_valid(api_instance, patched_api): 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, { @@ -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}) @@ -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, { @@ -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, { @@ -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, { @@ -229,11 +266,11 @@ 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}) @@ -241,8 +278,8 @@ async def test_epm_list_valid(api_instance, patched_api): 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'}) @@ -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'}) @@ -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, { @@ -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'}) @@ -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'}) @@ -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, { diff --git a/test/test_public_methods_inverter.py b/test/test_public_methods_inverter.py index b2325b6..8ffc6e1 100644 --- a/test/test_public_methods_inverter.py +++ b/test/test_public_methods_inverter.py @@ -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 @@ -15,30 +23,59 @@ 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_inverter_list_valid(api_instance, patched_api): +async def test_inverter_list_valid(api_instance, patched_api_paged): # Required arguments only result = await api_instance.inverter_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.INVERTER_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20}) # All arguments filled result = await api_instance.inverter_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.INVERTER_LIST, KEY, SECRET, { @@ -81,13 +118,13 @@ async def test_inverter_detail_invalid_params(api_instance): @pytest.mark.asyncio -async def test_inverter_day_valid(api_instance, patched_api): +async def test_inverter_day_valid(api_instance, patched_api_list): # Required arguments only result = await api_instance.inverter_day( KEY, SECRET, currency='EUR', time='2023-01-01', time_zone=1, inverter_id='1000') - 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.INVERTER_DAY, KEY, SECRET, {'money': 'EUR', 'time': '2023-01-01', 'timeZone': 1, 'id': '1000'}) @@ -95,8 +132,8 @@ async def test_inverter_day_valid(api_instance, patched_api): result = await api_instance.inverter_day( KEY, SECRET, currency='EUR', time='2023-01-01', time_zone=1, inverter_sn='sn') - 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.INVERTER_DAY, KEY, SECRET, {'money': 'EUR', 'time': '2023-01-01', 'timeZone': 1, 'sn': 'sn'}) @@ -136,13 +173,13 @@ async def test_inverter_day_invalid_params(api_instance): @pytest.mark.asyncio -async def test_inverter_month_valid(api_instance, patched_api): +async def test_inverter_month_valid(api_instance, patched_api_list): # Required arguments only result = await api_instance.inverter_month( KEY, SECRET, currency='EUR', month='2023-01', inverter_id='1000') - 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.INVERTER_MONTH, KEY, SECRET, {'money': 'EUR', 'month': '2023-01', 'id': '1000'}) @@ -150,8 +187,8 @@ async def test_inverter_month_valid(api_instance, patched_api): result = await api_instance.inverter_month( KEY, SECRET, currency='EUR', month='2023-01', inverter_sn='sn') - 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.INVERTER_MONTH, KEY, SECRET, {'money': 'EUR', 'month': '2023-01', 'sn': 'sn'}) @@ -185,13 +222,13 @@ async def test_inverter_month_invalid_params(api_instance): @pytest.mark.asyncio -async def test_inverter_year_valid(api_instance, patched_api): +async def test_inverter_year_valid(api_instance, patched_api_list): # Required arguments only result = await api_instance.inverter_year( KEY, SECRET, currency='EUR', year='2023', inverter_id='1000') - 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.INVERTER_YEAR, KEY, SECRET, {'money': 'EUR', 'year': '2023', 'id': '1000'}) @@ -199,8 +236,8 @@ async def test_inverter_year_valid(api_instance, patched_api): result = await api_instance.inverter_year( KEY, SECRET, currency='EUR', year='2023', inverter_sn='sn') - 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.INVERTER_YEAR, KEY, SECRET, {'money': 'EUR', 'year': '2023', 'sn': 'sn'}) @@ -226,13 +263,13 @@ async def test_inverter_year_invalid_params(api_instance): @pytest.mark.asyncio -async def test_inverter_all_valid(api_instance, patched_api): +async def test_inverter_all_valid(api_instance, patched_api_list): # Required arguments only result = await api_instance.inverter_all( KEY, SECRET, currency='EUR', inverter_id='1000') - 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.INVERTER_ALL, KEY, SECRET, {'money': 'EUR', 'id': '1000'}) @@ -240,8 +277,8 @@ async def test_inverter_all_valid(api_instance, patched_api): result = await api_instance.inverter_all( KEY, SECRET, currency='EUR', inverter_sn='sn') - 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.INVERTER_ALL, KEY, SECRET, {'money': 'EUR', 'sn': 'sn'}) @@ -260,18 +297,18 @@ async def test_inverter_all_invalid_params(api_instance): @pytest.mark.asyncio -async def test_inverter_detail_list_valid(api_instance, patched_api): +async def test_inverter_detail_list_valid(api_instance, patched_api_records): # Required arguments only result = await api_instance.inverter_detail_list(KEY, SECRET) - 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.INVERTER_DETAIL_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20}) result = await api_instance.inverter_detail_list( KEY, SECRET, page_no=4, page_size=30) - 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.INVERTER_DETAIL_LIST, KEY, SECRET, {'pageNo': 4, 'pageSize': 30}) @@ -285,13 +322,13 @@ async def test_inverter_detail_list_invalid_params(api_instance): @pytest.mark.asyncio -async def test_inverter_shelf_time(api_instance, patched_api): +async def test_inverter_shelf_time(api_instance, patched_api_records): # Required arguments only result = await api_instance.inverter_shelf_time( KEY, SECRET, inverter_sn='sn') - 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.INVERTER_SHELF_TIME, KEY, SECRET, {'pageNo': 1, 'pageSize': 20, 'sn': 'sn'}) @@ -300,8 +337,8 @@ async def test_inverter_shelf_time(api_instance, patched_api): result = await api_instance.inverter_shelf_time( KEY, SECRET, page_no=50, page_size=50, inverter_sn='sn') - 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.INVERTER_SHELF_TIME, KEY, SECRET, {'pageNo': 50, 'pageSize': 50, 'sn': 'sn'}) diff --git a/test/test_public_methods_station.py b/test/test_public_methods_station.py index 0f443fc..2f4894c 100644 --- a/test/test_public_methods_station.py +++ b/test/test_public_methods_station.py @@ -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 @@ -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_user_station_list_valid(api_instance, patched_api): +async def test_user_station_list_valid(api_instance, patched_api_paged): # Required arguments only result = await api_instance.user_station_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.USER_STATION_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20}) - assert result == VALID_RESPONSE + assert result == VALID_RESPONSE_PAGED_RECORDS # All arguments filled result = await api_instance.user_station_list( KEY, SECRET, page_no=4, page_size=100, 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.USER_STATION_LIST, KEY, SECRET, {'pageNo': 4, 'pageSize': 100, 'nmiCode': 'nmi_code'}) @@ -71,13 +108,13 @@ async def test_station_detail_valid(api_instance, patched_api): @pytest.mark.asyncio -async def test_station_day_valid(api_instance, patched_api): +async def test_station_day_valid(api_instance, patched_api_list): # Required arguments only result = await api_instance.station_day( KEY, SECRET, currency='EUR', time='2023-01-01', time_zone=1, station_id='1000') - 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.STATION_DAY, KEY, SECRET, {'money': 'EUR', 'time': '2023-01-01', 'timeZone': 1, 'id': '1000'}) @@ -85,8 +122,8 @@ async def test_station_day_valid(api_instance, patched_api): result = await api_instance.station_day( KEY, SECRET, currency='EUR', time='2023-01-01', time_zone=1, nmi_code=NMI) - 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.STATION_DAY, KEY, SECRET, {'money': 'EUR', 'time': '2023-01-01', 'timeZone': 1, 'nmiCode': NMI}) @@ -126,13 +163,13 @@ async def test_station_day_invalid_params(api_instance): @pytest.mark.asyncio -async def test_station_month_valid(api_instance, patched_api): +async def test_station_month_valid(api_instance, patched_api_list): # Required arguments only result = await api_instance.station_month( KEY, SECRET, currency='EUR', month='2023-01', station_id='1000') - 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.STATION_MONTH, KEY, SECRET, {'money': 'EUR', 'month': '2023-01', 'id': '1000'}) @@ -140,8 +177,8 @@ async def test_station_month_valid(api_instance, patched_api): result = await api_instance.station_month( KEY, SECRET, currency='EUR', month='2023-01', nmi_code=NMI) - 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.STATION_MONTH, KEY, SECRET, {'money': 'EUR', 'month': '2023-01', 'nmiCode': NMI}) @@ -172,13 +209,13 @@ async def test_station_month_invalid_params(api_instance): @pytest.mark.asyncio -async def test_station_year_valid(api_instance, patched_api): +async def test_station_year_valid(api_instance, patched_api_list): # Required arguments only result = await api_instance.station_year( KEY, SECRET, currency='EUR', year='2023', station_id='1000') - 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.STATION_YEAR, KEY, SECRET, {'money': 'EUR', 'year': '2023', 'id': '1000'}) @@ -186,8 +223,8 @@ async def test_station_year_valid(api_instance, patched_api): result = await api_instance.station_year( KEY, SECRET, currency='EUR', year='2023', nmi_code=NMI) - 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.STATION_YEAR, KEY, SECRET, {'money': 'EUR', 'year': '2023', 'nmiCode': NMI}) @@ -213,20 +250,20 @@ async def test_station_year_invalid_params(api_instance): @pytest.mark.asyncio -async def test_station_all_valid(api_instance, patched_api): +async def test_station_all_valid(api_instance, patched_api_list): # Required arguments only result = await api_instance.station_all( KEY, SECRET, currency='EUR', station_id='1000') - 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.STATION_ALL, KEY, SECRET, {'money': 'EUR', 'id': '1000'}) result = await api_instance.station_all( KEY, SECRET, currency='EUR', nmi_code=NMI) - 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.STATION_ALL, KEY, SECRET, {'money': 'EUR', 'nmiCode': NMI}) @@ -243,11 +280,11 @@ async def test_station_all_invalid_params(api_instance): @pytest.mark.asyncio -async def test_station_detail_list_valid(api_instance, patched_api): +async def test_station_detail_list_valid(api_instance, patched_api_records): # Required arguments only result = await api_instance.station_detail_list(KEY, SECRET) - 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.STATION_DETAIL_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20}) @@ -255,8 +292,8 @@ async def test_station_detail_list_valid(api_instance, patched_api): result = await api_instance.station_detail_list( KEY, SECRET, page_no=4, page_size=30) - 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.STATION_DETAIL_LIST, KEY, SECRET, {'pageNo': 4, 'pageSize': 30}) @@ -270,13 +307,14 @@ async def test_station_detail_list_invalid_params(api_instance): @pytest.mark.asyncio -async def test_station_day_energy_list_valid(api_instance, patched_api): +async def test_station_day_energy_list_valid( + api_instance, patched_api_records): # Required arguments only result = await api_instance.station_day_energy_list( KEY, SECRET, time='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.STATION_DAY_ENERGY_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20, 'time': '2023-01-01'}) @@ -284,8 +322,8 @@ async def test_station_day_energy_list_valid(api_instance, patched_api): result = await api_instance.station_day_energy_list( KEY, SECRET, page_no=4, page_size=30, time='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.STATION_DAY_ENERGY_LIST, KEY, SECRET, {'pageNo': 4, 'pageSize': 30, 'time': '2023-01-01'}) @@ -316,13 +354,14 @@ async def test_station_day_energy_list_invalid_params(api_instance): @pytest.mark.asyncio -async def test_station_month_energy_list_valid(api_instance, patched_api): +async def test_station_month_energy_list_valid( + api_instance, patched_api_records): # Required arguments only result = await api_instance.station_month_energy_list( KEY, SECRET, month='2023-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.STATION_MONTH_ENERGY_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20, 'time': '2023-01'}) @@ -330,8 +369,8 @@ async def test_station_month_energy_list_valid(api_instance, patched_api): result = await api_instance.station_month_energy_list( KEY, SECRET, page_no=4, page_size=30, month='2023-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.STATION_MONTH_ENERGY_LIST, KEY, SECRET, {'pageNo': 4, 'pageSize': 30, 'time': '2023-01'}) @@ -357,13 +396,14 @@ async def test_station_month_energy_list_invalid_params(api_instance): @pytest.mark.asyncio -async def test_station_year_energy_list_valid(api_instance, patched_api): +async def test_station_year_energy_list_valid( + api_instance, patched_api_records): # Required arguments only result = await api_instance.station_year_energy_list( KEY, SECRET, year='2023') - 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.STATION_YEAR_ENERGY_LIST, KEY, SECRET, {'pageNo': 1, 'pageSize': 20, 'time': '2023'}) @@ -371,8 +411,8 @@ async def test_station_year_energy_list_valid(api_instance, patched_api): result = await api_instance.station_year_energy_list( KEY, SECRET, page_no=4, page_size=30, year='2023') - 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.STATION_YEAR_ENERGY_LIST, KEY, SECRET, {'pageNo': 4, 'pageSize': 30, 'time': '2023'})