|
| 1 | +from dataclasses import asdict |
| 2 | + |
1 | 3 | import pytest
|
2 | 4 | from ocpp.v20 import ChargePoint as cp
|
3 | 5 | from ocpp.routing import on, create_route_map
|
| 6 | +from ocpp.v16.call import BootNotificationPayload, MeterValuesPayload |
4 | 7 | from ocpp.v16.enums import Action
|
5 |
| -from ocpp.charge_point import camel_to_snake_case, snake_to_camel_case |
| 8 | +from ocpp.v16.datatypes import MeterValue, SampledValue |
| 9 | +from ocpp.v201.call import SetNetworkProfilePayload |
| 10 | +from ocpp.v201.enums import (OCPPVersionType, OCPPTransportType, |
| 11 | + OCPPInterfaceType) |
| 12 | +from ocpp.v201.datatypes import NetworkConnectionProfileType |
| 13 | +from ocpp.charge_point import ( |
| 14 | + camel_to_snake_case, snake_to_camel_case, remove_nones) |
6 | 15 |
|
7 | 16 |
|
8 | 17 | def test_getters_should_not_be_called_during_routemap_setup():
|
@@ -58,3 +67,84 @@ def test_camel_to_snake_case(test_input, expected):
|
58 | 67 | def test_snake_to_camel_case(test_input, expected):
|
59 | 68 | result = snake_to_camel_case(test_input)
|
60 | 69 | assert result == expected
|
| 70 | + |
| 71 | + |
| 72 | +def test_remove_nones(): |
| 73 | + expected_payload = {'charge_point_model': 'foo', |
| 74 | + 'charge_point_vendor': 'bar'} |
| 75 | + |
| 76 | + payload = BootNotificationPayload( |
| 77 | + charge_point_model='foo', charge_point_vendor='bar', |
| 78 | + charge_box_serial_number=None) |
| 79 | + payload = asdict(payload) |
| 80 | + |
| 81 | + assert expected_payload == remove_nones(payload) |
| 82 | + |
| 83 | + |
| 84 | +def test_nested_remove_nones(): |
| 85 | + expected_payload = {'configuration_slot': 1, |
| 86 | + 'connection_data': { |
| 87 | + 'ocpp_version': 'OCPP20', 'ocpp_transport': 'JSON', |
| 88 | + 'ocpp_csms_url': 'wss://localhost:9000', |
| 89 | + 'message_timeout': 60, 'security_profile': 1, |
| 90 | + 'ocpp_interface': 'Wired0'}} |
| 91 | + |
| 92 | + connection_data = NetworkConnectionProfileType( |
| 93 | + ocpp_version=OCPPVersionType.ocpp20, |
| 94 | + ocpp_transport=OCPPTransportType.json, |
| 95 | + ocpp_csms_url='wss://localhost:9000', message_timeout=60, |
| 96 | + security_profile=1, ocpp_interface=OCPPInterfaceType.wired0, |
| 97 | + vpn=None, apn=None) |
| 98 | + |
| 99 | + payload = SetNetworkProfilePayload(configuration_slot=1, |
| 100 | + connection_data=connection_data) |
| 101 | + payload = asdict(payload) |
| 102 | + |
| 103 | + assert expected_payload == remove_nones(payload) |
| 104 | + |
| 105 | + |
| 106 | +def test_nested_list_remove_nones(): |
| 107 | + expected_payload = { |
| 108 | + 'connector_id': 3, |
| 109 | + 'meter_value': [{ |
| 110 | + 'timestamp': '2017-08-17T07:08:06.186748+00:00', |
| 111 | + 'sampled_value': [ |
| 112 | + {'value': '10', 'context': 'Sample.Periodic', |
| 113 | + 'measurand': 'Power.Active.Import', 'unit': 'W'}, |
| 114 | + {'value': '50000', 'context': 'Sample.Periodic', |
| 115 | + 'measurand': 'Power.Active.Import', 'phase': 'L1', |
| 116 | + 'unit': 'W'}]}, |
| 117 | + {'timestamp': '2017-08-17T07:07:07.186748+00:00', |
| 118 | + 'sampled_value': [ |
| 119 | + {'value': '10', 'context': 'Sample.Periodic', |
| 120 | + 'measurand': 'Power.Active.Import', 'unit': 'W'}, |
| 121 | + {'value': '50000', 'context': 'Sample.Periodic', |
| 122 | + 'measurand': 'Power.Active.Import', 'phase': 'L1', |
| 123 | + 'unit': 'W'}]}], |
| 124 | + 'transaction_id': 5} |
| 125 | + |
| 126 | + payload = MeterValuesPayload(connector_id=3, meter_value=[ |
| 127 | + MeterValue(timestamp='2017-08-17T07:08:06.186748+00:00', |
| 128 | + sampled_value=[ |
| 129 | + SampledValue(value='10', context='Sample.Periodic', |
| 130 | + format=None, |
| 131 | + measurand='Power.Active.Import', |
| 132 | + phase=None, location=None, unit='W'), |
| 133 | + SampledValue(value='50000', context='Sample.Periodic', |
| 134 | + format=None, |
| 135 | + measurand='Power.Active.Import', |
| 136 | + phase='L1', location=None, unit='W')]), |
| 137 | + MeterValue(timestamp='2017-08-17T07:07:07.186748+00:00', |
| 138 | + sampled_value=[ |
| 139 | + SampledValue(value='10', context='Sample.Periodic', |
| 140 | + format=None, |
| 141 | + measurand='Power.Active.Import', |
| 142 | + phase=None, location=None, unit='W'), |
| 143 | + SampledValue(value='50000', context='Sample.Periodic', |
| 144 | + format=None, |
| 145 | + measurand='Power.Active.Import', |
| 146 | + phase='L1', location=None, unit='W')])], |
| 147 | + transaction_id=5) |
| 148 | + |
| 149 | + payload = asdict(payload) |
| 150 | + assert expected_payload == remove_nones(payload) |
0 commit comments