98 lines
2.2 KiB
Python
98 lines
2.2 KiB
Python
# -*- coding: utf-8 -*-
|
|
"""
|
|
This program has been developed by students from the bachelor Computer Science at Utrecht
|
|
University within the Software Project course.
|
|
© Copyright Utrecht University (Department of Information and Computing Sciences)
|
|
"""
|
|
|
|
import sys
|
|
|
|
# Import module under test
|
|
import robot_interface.utils.qi_utils as qi_utils
|
|
|
|
|
|
def reload_qi_utils_with(qi_module):
|
|
"""
|
|
Helper: reload qi_utils after injecting a fake qi module.
|
|
Python 2 uses built-in reload().
|
|
Just changing sys.modules[qi] won't affect the already imported module.
|
|
"""
|
|
if qi_module is None:
|
|
if "qi" in sys.modules:
|
|
del sys.modules["qi"]
|
|
else:
|
|
sys.modules["qi"] = qi_module
|
|
|
|
# Python 2 reload
|
|
global qi_utils
|
|
qi_utils = reload(qi_utils)
|
|
|
|
|
|
def test_get_qi_session_no_qi_module():
|
|
"""
|
|
Tests the 'qi is None' path.
|
|
"""
|
|
reload_qi_utils_with(None)
|
|
|
|
session = qi_utils.get_qi_session()
|
|
assert session is None
|
|
|
|
|
|
def test_get_qi_session_no_qi_url_argument(monkeypatch):
|
|
"""
|
|
Tests the '--qi-url not in sys.argv' path.
|
|
"""
|
|
class FakeQi:
|
|
pass
|
|
|
|
reload_qi_utils_with(FakeQi())
|
|
|
|
monkeypatch.setattr(sys, "argv", ["pytest"])
|
|
|
|
session = qi_utils.get_qi_session()
|
|
assert session is None
|
|
|
|
|
|
def test_get_qi_session_runtime_error(monkeypatch):
|
|
"""
|
|
Tests the 'exept RuntineError' path.
|
|
"""
|
|
class FakeApp:
|
|
def start(self):
|
|
raise RuntimeError("boom")
|
|
|
|
class FakeQi:
|
|
Application = lambda self=None: FakeApp()
|
|
|
|
reload_qi_utils_with(FakeQi())
|
|
|
|
monkeypatch.setattr(sys, "argv", ["pytest", "--qi-url", "tcp://localhost"])
|
|
|
|
session = qi_utils.get_qi_session()
|
|
assert session is None
|
|
|
|
|
|
def test_get_qi_session_success(monkeypatch):
|
|
"""
|
|
Tests a valid path.
|
|
"""
|
|
class FakeSession:
|
|
pass
|
|
|
|
class FakeApp:
|
|
def __init__(self):
|
|
self.session = FakeSession()
|
|
|
|
def start(self):
|
|
return True
|
|
|
|
class FakeQi:
|
|
Application = lambda self=None: FakeApp()
|
|
|
|
reload_qi_utils_with(FakeQi())
|
|
|
|
monkeypatch.setattr(sys, "argv", ["pytest", "--qi-url", "tcp://localhost"])
|
|
|
|
session = qi_utils.get_qi_session()
|
|
assert isinstance(session, FakeSession)
|