Browse Source

Added more tests

tags/0.1
Johann Schmitz 4 years ago
parent
commit
4a4a0e8ea0
4 changed files with 77 additions and 6 deletions
  1. +2
    -1
      requirements.txt
  2. +11
    -2
      src/pymax/cube.py
  3. +12
    -0
      src/pymax/messages.py
  4. +52
    -3
      tests/cube.py

+ 2
- 1
requirements.txt View File

@@ -1 +1,2 @@
coverage
coverage
mock

+ 11
- 2
src/pymax/cube.py View File

@@ -151,8 +151,11 @@ class Connection(Debugger):

class Cube(object):

def __init__(self, address, port=62910):
self.connection = Connection((address, port))
def __init__(self, **kwargs):
if 'address' in kwargs:
self.connection = Connection((kwargs.get('address'), kwargs.get('port', None)))
else:
self.connection = kwargs.get('connection')

def __enter__(self):
self.connection.connect()
@@ -161,6 +164,12 @@ class Cube(object):
def __exit__(self, exc_type, exc_val, exc_tb):
self.connection.disconnect()

def connect(self):
self.connection.connect()

def disconnect(self):
self.connection.disconnect()

@property
def rooms(self):
msg = self.connection.get_message(M_RESPONSE)


+ 12
- 0
src/pymax/messages.py View File

@@ -89,3 +89,15 @@ class SetTemperatureAndModeMessage(SetMessage):
payload += bytearray([(self.end.time().hour * 2) + (1 if self.end.time().minute >= 30 else 0)])

return payload

def __str__(self):
return "%s(mode=%s, temperature=%s, end=%s)" % (self.__class__.__name__, self.mode, self.temperature, self.end)

def __repr__(self):
return self.__str__()

def __eq__(self, other):
return isinstance(other, SetTemperatureAndModeMessage) and \
self.mode == other.mode and \
self.temperature == other.temperature and \
self.end == other.end

+ 52
- 3
tests/cube.py View File

@@ -1,10 +1,19 @@
# -*- coding: utf-8 -*-
import unittest
import sys

from pymax.cube import Connection
from pymax.response import HELLO_RESPONSE, HelloResponse, M_RESPONSE, MResponse
from response import HelloResponseBytes, MResponseBytes
import datetime

from pymax.messages import SetTemperatureAndModeMessage

if sys.version_info.major == 2 or (sys.version_info.major == 3 and sys.version_info.minor <= 2):
from mock import MagicMock, Mock
else:
from unittest.mock import MagicMock, Mock

from pymax.cube import Connection, Cube
from pymax.response import HELLO_RESPONSE, HelloResponse, M_RESPONSE, MResponse, SetResponse
from response import HelloResponseBytes, MResponseBytes

class ConnectionTest(unittest.TestCase):

@@ -22,3 +31,43 @@ class ConnectionTest(unittest.TestCase):
self.assertEqual(len(conn.received_messages), 1)
self.assertTrue(message_type in conn.received_messages.keys(), "Message of type %s not found in .received_messages" % message_type)
self.assertIsInstance(conn.received_messages[message_type], message_class)

def test_connect(self):
c = Cube(connection=Mock())
c.connect()
self.assertTrue(c.connection.connect.called)

def test_disconnect(self):
c = Cube(connection=Mock())
c.disconnect()
self.assertFalse(c.connection.connect.called)
self.assertTrue(c.connection.disconnect.called)

def _mocked_cube(self):
conn = Mock()
conn.get_message = Mock(return_value=SetResponse(bytearray("00,0,31", encoding='utf-8')))
return Cube(connection=conn)

def test_set_mode_auto(self):
c = self._mocked_cube()
response = c.set_mode_auto(123, '001122')
c.connection.send_message.assert_called_with(SetTemperatureAndModeMessage('001122', 123, 0x0))
self.assertIsInstance(response, SetResponse)

def test_set_mode_boost(self):
c = self._mocked_cube()
response = c.set_mode_boost(123, '001122')
c.connection.send_message.assert_called_with(SetTemperatureAndModeMessage('001122', 123, 0xc0))
self.assertIsInstance(response, SetResponse)

def test_set_mode_manual(self):
c = self._mocked_cube()
response = c.set_mode_manual(123, '001122', temperature=123)
c.connection.send_message.assert_called_with(SetTemperatureAndModeMessage('001122', 123, 0x40, temperature=123))
self.assertIsInstance(response, SetResponse)

def test_set_mode_vacation(self):
c = self._mocked_cube()
response = c.set_mode_vacation(123, '001122', temperature=123, end=datetime.datetime(2015, 12, 16, 12, 00, 00))
c.connection.send_message.assert_called_with(SetTemperatureAndModeMessage('001122', 123, 0x80, temperature=123, end=datetime.datetime(2015, 12, 16, 12, 00, 00)))
self.assertIsInstance(response, SetResponse)

Loading…
Cancel
Save