Skip to content

Commit

Permalink
#35 | Added test for DSMR v4.2
Browse files Browse the repository at this point in the history
  • Loading branch information
dennissiemensma committed Feb 10, 2016
1 parent f5d7e29 commit fbb0631
Showing 1 changed file with 101 additions and 9 deletions.
110 changes: 101 additions & 9 deletions dsmr_datalogger/tests/management_commands/test_datalogger.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,12 +9,12 @@
from dsmr_datalogger.models.reading import DsmrReading


class TestDsmrStatsDatalogger(CallCommandStdoutMixin, TestCase):
""" Test 'dsmr_stats_datalogger' management command. """
def _dummy_data(self):
class TestDsmrV40Datalogger(CallCommandStdoutMixin, TestCase):
""" Test 'dsmr_stats_datalogger' management command, DSMR v4.0. """
def _dsmr_dummy_data(self):
""" Returns DSMR v4.0 data. """
return [
"/XMX5LGBBFFB231117727\n",
"\n",
"/XMX5LGBBFFB123456789\n",
"\n",
"1-3:0.2.8(40)\n",
"0-0:1.0.0(151110192959W)\n",
Expand Down Expand Up @@ -57,21 +57,22 @@ def _dummy_data(self):

@mock.patch('serial.Serial.open')
@mock.patch('serial.Serial.readline')
def _poll_reading(self, serial_readline_mock, serial_open_mock):
def _fake_dsmr_reading(self, serial_readline_mock, serial_open_mock):
""" Fake & process an DSMR vX telegram reading. """
serial_open_mock.return_value = None
serial_readline_mock.side_effect = self._dummy_data()
serial_readline_mock.side_effect = self._dsmr_dummy_data()

self._call_command_stdout('dsmr_datalogger')
self.assertTrue(DsmrReading.objects.exists())

def test_reading_creation(self):
""" Test whether dsmr_datalogger insert a reading. """
self._poll_reading()
self._fake_dsmr_reading()
self.assertTrue(DsmrReading.objects.exists())

def test_reading_values(self):
""" Test whether dsmr_datalogger reads the correct values. """
self._poll_reading()
self._fake_dsmr_reading()
self.assertTrue(DsmrReading.objects.exists())
reading = DsmrReading.objects.get()
self.assertEqual(
Expand Down Expand Up @@ -99,3 +100,94 @@ def test_reading_values(self):
datetime(2015, 11, 10, 18, 0, 0, tzinfo=pytz.UTC)
)
self.assertEqual(reading.extra_device_delivered, Decimal('845.206'))


class TestDsmrV42Datalogger(CallCommandStdoutMixin, TestCase):
""" Test 'dsmr_stats_datalogger' management command, DSMR v4.2. """

def _dsmr_dummy_data(self):
""" Returns DSMR v4.2 data. """
return [
"/XMX5LGBBFFB123456789\n",
"\n",
"1-3:0.2.8(42)\n",
"0-0:1.0.0(160210203034W)\n",
"0-0:96.1.1(xxxxxxxxxxxxx)\n",
"1-0:1.8.1(000756.849*kWh)\n",
"1-0:2.8.1(000000.000*kWh)\n",
"1-0:1.8.2(000714.405*kWh)\n",
"1-0:2.8.2(000000.000*kWh)\n",
"0-0:96.14.0(0002)\n",
"1-0:1.7.0(00.111*kW)\n",
"1-0:2.7.0(00.000*kW)\n",
"0-0:96.7.21(00003)\n",
"0-0:96.7.9(00000)\n",
"1-0:99.97.0(0)(0-0:96.7.19)\n",
"1-0:32.32.0(00002)\n",
"1-0:52.32.0(00002)\n",
"1-0:72.32.0(00000)\n",
"1-0:32.36.0(00000)\n",
"1-0:52.36.0(00000)\n",
"1-0:72.36.0(00000)\n",
"0-0:96.13.1()\n",
"0-0:96.13.0()\n",
"1-0:31.7.0(000*A)\n",
"1-0:51.7.0(000*A)\n",
"1-0:71.7.0(001*A)\n",
"1-0:21.7.0(00.000*kW)\n",
"1-0:41.7.0(00.000*kW)\n",
"1-0:61.7.0(00.110*kW)\n",
"1-0:22.7.0(00.000*kW)\n",
"1-0:42.7.0(00.000*kW)\n",
"1-0:62.7.0(00.000*kW)\n",
"0-1:24.1.0(003)\n",
"0-1:96.1.0(xxxxxxxxxxxxx)\n",
"0-1:24.2.1(160210200000W)(01197.484*m3)\n",
"!AD8D\n",
]

@mock.patch('serial.Serial.open')
@mock.patch('serial.Serial.readline')
def _fake_dsmr_reading(self, serial_readline_mock, serial_open_mock):
""" Fake & process an DSMR vX telegram reading. """
serial_open_mock.return_value = None
serial_readline_mock.side_effect = self._dsmr_dummy_data()

self._call_command_stdout('dsmr_datalogger')
self.assertTrue(DsmrReading.objects.exists())

def test_reading_creation(self):
""" Test whether dsmr_datalogger insert a reading. """
self._fake_dsmr_reading()
self.assertTrue(DsmrReading.objects.exists())

def test_reading_values(self):
""" Test whether dsmr_datalogger reads the correct values. """
self._fake_dsmr_reading()
self.assertTrue(DsmrReading.objects.exists())
reading = DsmrReading.objects.get()
self.assertEqual(
reading.timestamp,
datetime(2016, 2, 10, 19, 30, 34, tzinfo=pytz.UTC)
)
self.assertEqual(reading.electricity_delivered_1, Decimal('756.849'))
self.assertEqual(reading.electricity_returned_1, Decimal('0'))
self.assertEqual(reading.electricity_delivered_2, Decimal('714.405'))
self.assertEqual(reading.electricity_returned_2, Decimal('0'))
self.assertEqual(reading.electricity_tariff, 2)
self.assertEqual(reading.electricity_currently_delivered, Decimal('0.111'))
self.assertEqual(reading.electricity_currently_returned, Decimal('0'))
self.assertEqual(reading.power_failure_count, 3)
self.assertEqual(reading.long_power_failure_count, 0)
self.assertEqual(reading.voltage_sag_count_l1, 2)
self.assertEqual(reading.voltage_sag_count_l2, 2)
self.assertEqual(reading.voltage_sag_count_l3, 0)
self.assertEqual(reading.voltage_swell_count_l1, 0)
self.assertEqual(reading.voltage_swell_count_l2, 0)
self.assertEqual(reading.voltage_swell_count_l3, 0)

self.assertEqual(
reading.extra_device_timestamp,
datetime(2016, 2, 10, 19, 0, 0, tzinfo=pytz.UTC)
)
self.assertEqual(reading.extra_device_delivered, Decimal('1197.484'))

0 comments on commit fbb0631

Please sign in to comment.