Skip to content
Snippets Groups Projects
Commit 2e4ba1b2 authored by fima's avatar fima :beers:
Browse files

Merge branch 'pytest_structure' into 'main'

First unit tests

See merge request !16
parents 6b47f9e0 d3695660
No related branches found
No related tags found
1 merge request!16First unit tests
......@@ -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,
......
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
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
......@@ -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
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment