Skip to content

Commit

Permalink
preparations for 0.2.5 version release
Browse files Browse the repository at this point in the history
  • Loading branch information
antohaUa committed Aug 29, 2019
1 parent 2ebcf31 commit 4da4c27
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 12 deletions.
File renamed without changes.
3 changes: 2 additions & 1 deletion blynklib.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -40,6 +40,7 @@ def stub_log(*args):
class BlynkError(Exception):
pass


class RedirectError(Exception):
def __init__(self, server, port):
self.server = server
Expand Down
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
25 changes: 17 additions & 8 deletions test/test_blynk_connection.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import time
import pytest
import socket
from blynklib import Connection, BlynkError
from blynklib import Connection, BlynkError, RedirectError


class TestBlynkConnection:
Expand Down Expand Up @@ -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()
Expand Down Expand Up @@ -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):
Expand All @@ -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):
Expand Down
15 changes: 14 additions & 1 deletion test/test_blynk_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -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:
Expand Down Expand Up @@ -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):
Expand Down
2 changes: 1 addition & 1 deletion test/test_blynk_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down

0 comments on commit 4da4c27

Please sign in to comment.