Skip to content

Commit

Permalink
TST: finalized UI tests + added general test UI
Browse files Browse the repository at this point in the history
  • Loading branch information
rpartzsch committed Jan 18, 2025
1 parent a2ce475 commit 8834757
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 48 deletions.
18 changes: 14 additions & 4 deletions motor_stage_ui/motor_stage_gui.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
from motor_stage_ui.pi_stages_interface import PIStagesInterface
from motor_stage_ui.pi_stages_interface import SerialInterface
from motor_stage_ui.test.utils import SerialInterfaceMock
from motor_stage_ui import logger
import motor_stage_ui

import yaml
import logging
from PyQt5.QtCore import QSize, Qt, QTimer
from PyQt5.QtWidgets import QApplication, QMainWindow, QPushButton, QLineEdit, QLabel
import sys
import os
from pathlib import Path

"""
Expand Down Expand Up @@ -357,10 +358,19 @@ def get_position_clicked(


def main():
app = QApplication(sys.argv)
try:
if os.environ["TEST"]:
path = Path(__file__).parent / "test"
config_path = path / "test_configuration.yaml"
interface = SerialInterfaceMock

except KeyError:
path = Path(__file__).parent
config_path = path / "configuration.yaml"
interface = SerialInterface

path = os.path.dirname(motor_stage_ui.__file__)
window = MainWindow(path + "/configuration.yaml")
app = QApplication(sys.argv)
window = MainWindow(config_path, interface=interface)
window.show()

app.exec()
Expand Down
68 changes: 64 additions & 4 deletions motor_stage_ui/motor_stage_terminal.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
from motor_stage_ui.pi_stages_interface import PIStagesInterface as MC
import motor_stage_ui
from motor_stage_ui.pi_stages_interface import SerialInterface
from motor_stage_ui.test.utils import SerialInterfaceMock

from pathlib import Path
import os
import click
import yaml
Expand All @@ -23,12 +26,24 @@ def motor(conf):
Args:
conf (str): Needs path to configuration yaml. This path is converted into a click object and passed to the individual functions.
"""
try:
if os.environ["TEST"]:
path = Path(__file__).parent / "test"
config_path = path / "test_configuration.yaml"
test = True

except KeyError:
path = Path(__file__).parent
config_path = path / "configuration.yaml"
test = False

conf.ensure_object(dict)
path = os.path.dirname(motor_stage_ui.__file__)
config_path = path + "/configuration.yaml"
with open(config_path, "r") as file:
conf.obj["CONF"] = yaml.full_load(file)
if test:
conf.obj["MOCK"] = True
else:
conf.obj["MOCK"] = False


@click.command()
Expand All @@ -40,9 +55,14 @@ def init(conf, motor_name: str):
Args:
motor_name (str): name of the motorstage
"""
if conf.obj["MOCK"]:
interface = SerialInterfaceMock
else:
interface = SerialInterface
mc = MC(
port=conf.obj["CONF"][motor_name]["port"],
baud_rate=conf.obj["CONF"][motor_name]["baud_rate"],
interface=interface,
)
mc.init_motor(conf.obj["CONF"][motor_name]["address"])

Expand All @@ -59,9 +79,14 @@ def move(conf, motor_name: str, a: str):
motor_name (str): name of the motorstage
a (str): Move amount
"""
if conf.obj["MOCK"]:
interface = SerialInterfaceMock
else:
interface = SerialInterface
mc = MC(
port=conf.obj["CONF"][motor_name]["port"],
baud_rate=conf.obj["CONF"][motor_name]["baud_rate"],
interface=interface,
)
mc.move_relative(
conf.obj["CONF"][motor_name]["address"],
Expand All @@ -84,9 +109,14 @@ def moveto(conf, motor_name: str, a: str):
motor_name (str): name of the motorstage
a (str): Move to position
"""
if conf.obj["MOCK"]:
interface = SerialInterfaceMock
else:
interface = SerialInterface
mc = MC(
port=conf.obj["CONF"][motor_name]["port"],
baud_rate=conf.obj["CONF"][motor_name]["baud_rate"],
interface=interface,
)
mc.move_to_position(
conf.obj["CONF"][motor_name]["address"],
Expand All @@ -106,9 +136,14 @@ def pos(conf, motor_name: str):
Args:
motor_name (str): name of the motorstage
"""
if conf.obj["MOCK"]:
interface = SerialInterfaceMock
else:
interface = SerialInterface
mc = MC(
port=conf.obj["CONF"][motor_name]["port"],
baud_rate=conf.obj["CONF"][motor_name]["baud_rate"],
interface=interface,
)
click.echo(
"Position of: "
Expand All @@ -119,7 +154,7 @@ def pos(conf, motor_name: str):
conf.obj["CONF"][motor_name]["address"],
conf.obj["CONF"][motor_name]["unit"],
conf.obj["CONF"][motor_name]["stage_type"],
conf.obj["CONF"][motor_name]["step_size"],
float(conf.obj["CONF"][motor_name]["step_size"]),
)
)
+ " "
Expand All @@ -136,9 +171,14 @@ def stop(conf, motor_name: str):
Args:
motor_name (str): name of the motorstage
"""
if conf.obj["MOCK"]:
interface = SerialInterfaceMock
else:
interface = SerialInterface
mc = MC(
port=conf.obj["CONF"][motor_name]["port"],
baud_rate=conf.obj["CONF"][motor_name]["baud_rate"],
interface=interface,
)
mc.abort(conf.obj["CONF"][motor_name]["address"])

Expand All @@ -152,9 +192,14 @@ def sethome(conf, motor_name: str):
Args:
motor_name (str): name of the motorstage
"""
if conf.obj["MOCK"]:
interface = SerialInterfaceMock
else:
interface = SerialInterface
mc = MC(
port=conf.obj["CONF"][motor_name]["port"],
baud_rate=conf.obj["CONF"][motor_name]["baud_rate"],
interface=interface,
)
mc.set_home(conf.obj["CONF"][motor_name]["address"])

Expand All @@ -168,9 +213,14 @@ def gohome(conf, motor_name: str):
Args:
motor_name (str): name of the motorstage
"""
if conf.obj["MOCK"]:
interface = SerialInterfaceMock
else:
interface = SerialInterface
mc = MC(
port=conf.obj["CONF"][motor_name]["port"],
baud_rate=conf.obj["CONF"][motor_name]["baud_rate"],
interface=interface,
)
mc.go_home(conf.obj["CONF"][motor_name]["address"])

Expand All @@ -184,11 +234,21 @@ def status(conf, motor_name: str):
Args:
motor_name (str): name of the motorstage
"""
if conf.obj["MOCK"]:
interface = SerialInterfaceMock
else:
interface = SerialInterface
mc = MC(
port=conf.obj["CONF"][motor_name]["port"],
baud_rate=conf.obj["CONF"][motor_name]["baud_rate"],
interface=interface,
)
click.echo(
"Status of: "
+ motor_name
+ " "
+ str(mc.get_stat(conf.obj["CONF"][motor_name]["address"]))
)
mc.get_stat(conf.obj["CONF"][motor_name]["address"])


motor.add_command(init)
Expand Down
7 changes: 3 additions & 4 deletions motor_stage_ui/pi_stages_interface.py
Original file line number Diff line number Diff line change
Expand Up @@ -199,9 +199,8 @@ def get_stat(self, address: int) -> None:
address (int): Address of the motorstage
"""
err_msg = self._write_read("TS", address)
self.log.warning(
"Status of motor stage with address %i: %s" % (address, err_msg)
)
self.log.debug("Status of motor stage with address %i: %s" % (address, err_msg))
return err_msg

def abort(self, address: int) -> None:
"""Stops all movement of the motorstage.
Expand Down Expand Up @@ -269,7 +268,7 @@ def get_position(
address (int): Address of the motorstage
unit (str): output unit
stage (int): stage type either 'rotation' or translation
step_size (int): step size of the motorstage given in deg or um
step_size (float): step size of the motorstage given in deg or um
Returns:
str: current position of motorstage in unit 3 digits precision respectively
Expand Down
Loading

0 comments on commit 8834757

Please sign in to comment.