Files
seedsigner/tests/test_controller.py
biel 02f9b8c9ba Changing tests
Change tests

Correcting tests
2025-12-17 21:11:14 +01:00

82 lines
2.8 KiB
Python

import pytest
# Must import this before the Controller
from base import BaseTest
from seedsigner.controller import Controller
class TestController(BaseTest):
def test_reset_controller(self):
""" The reset_controller util should completely reset the Controller singleton """
controller = Controller.get_instance()
controller.address_explorer_data = "foo"
BaseTest.reset_controller()
controller = Controller.get_instance()
assert controller.address_explorer_data is None
def test_singleton_init_fails(self):
""" The Controller should not allow any code to instantiate it via Controller() """
with pytest.raises(Exception):
c = Controller()
def test_handle_exception(reset_controller):
""" Handle exceptions that get caught by the controller """
def process_exception_asserting_valid_error(exception_type, exception_msg=None):
"""
Exceptions caught by the controller are forwarded to the
UnhandledExceptionView with view_args["error"] being a list
of three strings, ie: [exception_type, line_info, exception_msg]
"""
try:
if exception_msg:
raise exception_type(exception_msg)
else:
raise exception_type()
except Exception as e:
error = controller.handle_exception(e).view_args["error"]
# assert that error structure is valid
assert len(error) == 3
assert error[0] in str(exception_type)
assert type(error[1]) == str
if exception_msg:
assert exception_msg in error[2]
else:
assert error[2] == ""
# Initialize the controller
controller = Controller.get_instance()
exception_tests = [
# exceptions with an exception_msg
(Exception, "foo"),
(KeyError, "key not found"),
# exceptions without an exception_msg
(Exception, ""),
(Exception, None),
]
for exception_type, exception_msg in exception_tests:
process_exception_asserting_valid_error(exception_type, exception_msg)
def test_singleton_get_instance_preserves_state(self):
""" Changes to the Controller singleton should be preserved across calls to get_instance() """
# Initialize the instance and verify that it read the config settings
controller = Controller.get_instance()
assert controller.unverified_address is None
# Change a value in the instance...
controller.unverified_address = "123abc"
# ...get a new copy of the instance and confirm change
controller = Controller.get_instance()
assert controller.unverified_address == "123abc"