From 4da4c27ea44953d51c50e5bd84a28555049feecb Mon Sep 17 00:00:00 2001 From: antohaUa Date: Thu, 29 Aug 2019 22:54:01 +0300 Subject: [PATCH] preparations for 0.2.5 version release --- LICENSE => LICENSE.txt | 0 blynklib.py | 3 ++- setup.py | 2 +- test/test_blynk_connection.py | 25 +++++++++++++++++-------- test/test_blynk_main.py | 15 ++++++++++++++- test/test_blynk_protocol.py | 2 +- 6 files changed, 35 insertions(+), 12 deletions(-) rename LICENSE => LICENSE.txt (100%) diff --git a/LICENSE b/LICENSE.txt similarity index 100% rename from LICENSE rename to LICENSE.txt diff --git a/blynklib.py b/blynklib.py index f9de30a..942a1c8 100644 --- a/blynklib.py +++ b/blynklib.py @@ -2,7 +2,7 @@ # Copyright (c) 2015-2019 Volodymyr Shymanskyy. # See the file LICENSE for copying permission. -__version__ = '0.2.4' +__version__ = '0.2.5' try: import usocket as socket @@ -40,6 +40,7 @@ def stub_log(*args): class BlynkError(Exception): pass + class RedirectError(Exception): def __init__(self, server, port): self.server = server diff --git a/setup.py b/setup.py index ffa7c20..0421be7 100644 --- a/setup.py +++ b/setup.py @@ -5,7 +5,7 @@ setup( name='blynklib', - version='0.2.4', + version='0.2.5', description='Blynk Python/Micropython library', long_description=long_description, long_description_content_type="text/markdown", diff --git a/test/test_blynk_connection.py b/test/test_blynk_connection.py index 02e2003..74e4325 100644 --- a/test/test_blynk_connection.py +++ b/test/test_blynk_connection.py @@ -3,7 +3,7 @@ import time import pytest import socket -from blynklib import Connection, BlynkError +from blynklib import Connection, BlynkError, RedirectError class TestBlynkConnection: @@ -96,7 +96,7 @@ def test_receive_raise_other_oserror(self, cb, mocker): with mocker.patch('socket.socket.recv', side_effect=OSError('[Errno 13]')): with pytest.raises(OSError) as os_err: cb.receive(10, 1) - assert '[Errno 13]' in str(os_err) + assert '[Errno 13]' in str(os_err.value) def test_is_server_alive_negative(self, cb): result = cb.is_server_alive() @@ -131,7 +131,7 @@ def test_get_socket_exception(self, cb, mocker): with mocker.patch('socket.getaddrinfo', side_effect=BlynkError('BE')): with pytest.raises(BlynkError) as b_err: cb._get_socket() - assert 'Connection with the Blynk server failed: BE' in str(b_err) + assert 'Connection with the Blynk server failed: BE' in str(b_err.value) def test_authenticate(self, cb, mocker): with mocker.patch.object(cb, 'send', return_value=None): @@ -144,35 +144,44 @@ def test_authenticate_invalid_auth_token(self, cb, mocker): with mocker.patch.object(cb, 'receive', return_value=b'\x00\x00\x02\x00\x09'): with pytest.raises(BlynkError) as b_err: cb._authenticate() - assert 'Invalid Auth Token' in str(b_err) + assert 'Invalid Auth Token' in str(b_err.value) + + def test_authenticate_redirect_message(self, cb, mocker): + with mocker.patch.object(cb, 'send', return_value=None): + with mocker.patch.object(cb, 'receive', return_value=b'\x29\x00\x02\x00\x11127.0.0.1\x004444'): + with pytest.raises(RedirectError) as r_err: + cb._authenticate() + # pytest exception wraps real exception - so we need access value field first + assert '127.0.0.1' in r_err.value.server + assert '4444' in r_err.value.port def test_authenticate_not_ok_status(self, cb, mocker): with mocker.patch.object(cb, 'send', return_value=None): with mocker.patch.object(cb, 'receive', return_value=b'\x00\x00\x02\x00\x19'): with pytest.raises(BlynkError) as b_err: cb._authenticate() - assert 'Auth stage failed. Status=25' in str(b_err) + assert 'Auth stage failed. Status=25' in str(b_err.value) def test_authenticate_timeout(self, cb, mocker): with mocker.patch.object(cb, 'send', return_value=None): with mocker.patch.object(cb, 'receive', return_value=None): with pytest.raises(BlynkError) as b_err: cb._authenticate() - assert 'Auth stage timeout' in str(b_err) + assert 'Auth stage timeout' in str(b_err.value) def test_set_heartbeat_timeout(self, cb, mocker): with mocker.patch.object(cb, 'send', return_value=None): with mocker.patch.object(cb, 'receive', return_value=None): with pytest.raises(BlynkError) as b_err: cb._set_heartbeat() - assert 'Heartbeat stage timeout' in str(b_err) + assert 'Heartbeat stage timeout' in str(b_err.value) def test_set_heartbeat_error_status(self, cb, mocker): with mocker.patch.object(cb, 'send', return_value=None): with mocker.patch.object(cb, 'receive', return_value=b'\x00\x00\x02\x00\x0e'): with pytest.raises(BlynkError) as b_err: cb._set_heartbeat() - assert 'Set heartbeat returned code=14' in str(b_err) + assert 'Set heartbeat returned code=14' in str(b_err.value) def test_set_heartbeat_positive(self, cb, mocker): with mocker.patch.object(cb, 'send', return_value=None): diff --git a/test/test_blynk_main.py b/test/test_blynk_main.py index e2d6c74..7913105 100644 --- a/test/test_blynk_main.py +++ b/test/test_blynk_main.py @@ -2,7 +2,7 @@ from __future__ import print_function import socket import pytest -from blynklib import Blynk, BlynkError +from blynklib import Blynk, BlynkError, RedirectError class TestBlynk: @@ -31,6 +31,19 @@ def test_connect_exception(self, bl, mocker): assert result is False assert bl.disconnect.call_count > 1 + def test_connect_redirect_exception(self, bl, mocker): + with mocker.patch.object(bl, 'connected', return_value=False): + with mocker.patch.object(bl, '_get_socket', return_value=None): + with mocker.patch.object(bl, '_authenticate', side_effect=RedirectError('127.0.0.1', 4444)): + with mocker.patch.object(bl, 'disconnect', return_value=None): + with mocker.patch('time.sleep', return_value=None): + mocker.spy(bl, 'disconnect') + result = bl.connect(0.001) + assert result is False + assert bl.disconnect.call_count > 1 + assert bl.server == '127.0.0.1' + assert bl.port == 4444 + def test_connect_timeout(self, bl, mocker): bl._state = bl.CONNECTING with mocker.patch.object(bl, 'connected', return_value=False): diff --git a/test/test_blynk_protocol.py b/test/test_blynk_protocol.py index 7853a8a..ceee9ae 100644 --- a/test/test_blynk_protocol.py +++ b/test/test_blynk_protocol.py @@ -99,7 +99,7 @@ def test_parse_response_msg_hw_unicode(self, pb): def test_heartbeat_msg(self, pb): result = pb.heartbeat_msg(20, 2048) - assert result == b'\x11\x00\x02\x00+ver\x000.2.4\x00buff-in\x002048\x00h-beat\x0020\x00dev\x00python' + assert result == b'\x11\x00\x02\x00+ver\x000.2.5\x00buff-in\x002048\x00h-beat\x0020\x00dev\x00python' def test_login_msg(self, pb): result = pb.login_msg('1234')