Skip to content

Commit

Permalink
chore: increase coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
Sébastian Clerson committed Jan 5, 2023
1 parent 16dba2c commit fb63896
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 10 deletions.
6 changes: 4 additions & 2 deletions src/pykiso/lib/connectors/cc_mp_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,17 @@ def __getattr__(self, name: str) -> Any:
this proxy channel yet.
:return: the found attribute value.
"""
# using getattr or dot access causes infinite recursion when process is unpickled
if self._physical_channel is not None:
with self._proxy.lock:
return getattr(self._physical_channel, name)
raise AttributeError(
f"{self.__class__.__name__} object has no attribute {name}"
f"{self.__class__.__name__} object has no attribute '{name}'"
)

def __getstate__(self):
"""Avoid getattr to be called on pickling before instanciation,
which would cause infinite recursion.
"""
return self.__dict__

def __setstate__(self, state):
Expand Down
2 changes: 1 addition & 1 deletion src/pykiso/lib/connectors/cc_proxy.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ def __getattr__(self, name: str) -> Any:
with self._proxy.lock:
return getattr(self._physical_channel, name)
raise AttributeError(
f"{self.__class__.__name__} object has no attribute {name}"
f"{self.__class__.__name__} object has no attribute '{name}'"
)

def detach_tx_callback(self) -> None:
Expand Down
37 changes: 37 additions & 0 deletions tests/test_auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,43 @@ def __init__(self, **kwargs):
return MockSimpleAux()


def test_add_internal_log_levels():

# does the opposite of add_logging_level, so log levels addition can be tested
def del_logging_level(level_name):
method_name = level_name.lower()

delattr(logging, level_name)
delattr(logging.getLoggerClass(), method_name)
delattr(logging, method_name)

del_logging_level("INTERNAL_WARNING")
del_logging_level("INTERNAL_INFO")
del_logging_level("INTERNAL_DEBUG")

assert not hasattr(logging, "INTERNAL_INFO")
assert not hasattr(logging, "INTERNAL_DEBUG")
assert not hasattr(logging, "INTERNAL_WARNING")

class DummyAux(AuxiliaryCommon):
def __init__(self):
super().__init__()

def create_instance(self):
pass

def delete_instance(self):
pass

def run(self):
pass

my_dummy_aux = DummyAux()
assert hasattr(logging, "INTERNAL_INFO")
assert hasattr(logging, "INTERNAL_DEBUG")
assert hasattr(logging, "INTERNAL_WARNING")


def test_thread_aux_raise_creation_error(mock_thread_aux):
mock_thread_aux.queue_out.put(False)
with pytest.raises(AuxiliaryCreationError):
Expand Down
14 changes: 11 additions & 3 deletions tests/test_mp_proxy_auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -289,25 +289,33 @@ def test_getattr_physical_cchannel(

assert mock_aux1.channel._physical_channel is cchannel_inst

# attribute exists in the physical channel instance
# attribute exists in the physical channel
assert mock_aux1.channel.some_attribute is cchannel_inst.some_attribute
proxy_inst.lock.__enter__.assert_called_once()
proxy_inst.lock.__exit__.assert_called_once()
proxy_inst.lock.reset_mock()

# attribute exists in the proxy channel instance
# attribute exists in the proxy channel
assert mock_aux1.channel.cc_send is not cchannel_inst.cc_send
proxy_inst.lock.__enter__.assert_not_called()
proxy_inst.lock.__exit__.assert_not_called()

# attribute does not exist
# attribute does not exist in physical channel
with pytest.raises(
AttributeError, match="object has no attribute 'does_not_exist'"
):
mock_aux1.channel.does_not_exist
proxy_inst.lock.__enter__.assert_called_once()
proxy_inst.lock.__exit__.assert_called_once()

# attribute does not exist in proxy channel (no physical channel attached)
proxy_inst.lock.reset_mock()
mock_aux1.channel._physical_channel = None
with pytest.raises(AttributeError, match="has no attribute 'does_not_exist'"):
mock_aux1.channel.does_not_exist
proxy_inst.lock.__enter__.assert_not_called()
proxy_inst.lock.__exit__.assert_not_called()


def test_create_auxiliary_instance(mp_proxy_auxiliary_inst, caplog):

Expand Down
14 changes: 10 additions & 4 deletions tests/test_proxy_auxiliary.py
Original file line number Diff line number Diff line change
Expand Up @@ -183,14 +183,20 @@ def test_getattr_physical_cchannel(
proxy_inst.lock.__enter__.assert_not_called()
proxy_inst.lock.__exit__.assert_not_called()

# attribute does not exist
with pytest.raises(
AttributeError, match="object has no attribute 'does_not_exist'"
):
# attribute does not exist in physical channel
with pytest.raises(AttributeError, match="has no attribute 'does_not_exist'"):
mock_aux1.channel.does_not_exist
proxy_inst.lock.__enter__.assert_called_once()
proxy_inst.lock.__exit__.assert_called_once()

# attribute does not exist in proxy channel (no physical channel attached)
proxy_inst.lock.reset_mock()
mock_aux1.channel._physical_channel = None
with pytest.raises(AttributeError, match="has no attribute 'does_not_exist'"):
mock_aux1.channel.does_not_exist
proxy_inst.lock.__enter__.assert_not_called()
proxy_inst.lock.__exit__.assert_not_called()


def test_get_proxy_con_pre_load(mocker, cchannel_inst):
mocker.patch.object(ConfigRegistry, "get_auxes_alias", return_value="later_aux")
Expand Down

0 comments on commit fb63896

Please sign in to comment.