diff --git a/qim3d/gui/iso3d.py b/qim3d/gui/iso3d.py index 728902f25479e55a59546aa4f62ae97cf8c9d40a..64e760d5c260f549f6d73932b3519520a95336f0 100644 --- a/qim3d/gui/iso3d.py +++ b/qim3d/gui/iso3d.py @@ -13,7 +13,7 @@ class Interface: self.show_header = False self.verbose = False self.title = "Isosurfaces for 3D visualization" - + self.interface = None self.plot_height = 768 self.height = 1024 self.width = 960 @@ -381,7 +381,7 @@ class Interface: internal_tools.gradio_header(self.title, self.port) # Create gradio interface - interface = self.create_interface() + self.interface = self.create_interface() # Set gradio verbose level if self.verbose: @@ -389,7 +389,7 @@ class Interface: else: quiet = True - interface.launch( + self.interface.launch( quiet=quiet, height=self.height, width=self.width, diff --git a/qim3d/tests/gui/test_local_thickness.py b/qim3d/tests/gui/test_local_thickness.py new file mode 100644 index 0000000000000000000000000000000000000000..4e10123db7506ea97cb7da4668d33117ad6906fe --- /dev/null +++ b/qim3d/tests/gui/test_local_thickness.py @@ -0,0 +1,36 @@ +import qim3d +import multiprocessing +import time + + +def test_starting_class(): + app = qim3d.gui.iso3d.Interface() + + assert app.title == "Isosurfaces for 3D visualization" + + +def test_app_launch(): + ip = "0.0.0.0" + port = 65432 + + def start_server(ip, port): + app = qim3d.gui.iso3d.Interface() + app.launch(server_name=ip, server_port=port) + + proc = multiprocessing.Process(target=start_server, args=(ip, port)) + proc.start() + + # App is running in a separate process + # So we try to get a response for a while + max_checks = 5 + check = 0 + server_running = False + while check < max_checks and not server_running: + server_running = qim3d.utils.internal_tools.is_server_running(ip, port) + time.sleep(1) + check += 1 + + # Terminate tre process before assertions + proc.terminate() + + assert server_running is True diff --git a/qim3d/tests/utils/test_internal_tools.py b/qim3d/tests/utils/test_internal_tools.py new file mode 100644 index 0000000000000000000000000000000000000000..c41f2aab0ae0f92bfe2f47fafc31e355f93d7312 --- /dev/null +++ b/qim3d/tests/utils/test_internal_tools.py @@ -0,0 +1,37 @@ +import qim3d +import os +import re + + +def test_mock_plot(): + fig = qim3d.utils.internal_tools.mock_plot() + + assert fig.get_figwidth() == 5.0 + + +def test_mock_write_file(): + filename = "test.txt" + content = "test file" + qim3d.utils.internal_tools.mock_write_file(filename, content=content) + + # Check contents + with open(filename, "r", encoding="utf-8") as f: + file_content = f.read() + + # Remove temp file + os.remove(filename) + + assert content == file_content + + +def test_get_local_ip(): + def validate_ip(ip_str): + reg = r"^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$" + if re.match(reg, ip_str): + return True + else: + return False + + local_ip = qim3d.utils.internal_tools.get_local_ip() + + assert validate_ip(local_ip) == True diff --git a/qim3d/utils/internal_tools.py b/qim3d/utils/internal_tools.py index d93e866b5d04684b8988de5471740f363cbc349d..838ed93e1dc502dad883ead1a5103fed37cbf009 100644 --- a/qim3d/utils/internal_tools.py +++ b/qim3d/utils/internal_tools.py @@ -6,6 +6,7 @@ import outputformat as ouf import matplotlib.pyplot as plt import matplotlib import numpy as np +import socket @@ -34,7 +35,7 @@ def mock_plot(): return fig -def mock_write_file(path): +def mock_write_file(path, content="File created by qim3d"): """ Creates a file at the specified path and writes a predefined text into it. @@ -45,7 +46,7 @@ def mock_write_file(path): >>> mock_write_file("example.txt") """ _file = open(path, "w", encoding="utf-8") - _file.write("File created by apptools") + _file.write(content) _file.close() @@ -166,3 +167,12 @@ def sizeof(num, suffix="B"): return f"{num:3.1f} {unit}{suffix}" num /= 1024.0 return f"{num:.1f} Y{suffix}" + +def is_server_running(ip, port): + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + try: + s.connect((ip, int(port))) + s.shutdown(2) + return True + except: + return False \ No newline at end of file