Skip to content
23 changes: 23 additions & 0 deletions news/gui-tests.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
**Added:**

* added pytest for gui files

**Changed:**

* <news item>

**Deprecated:**

* <news item>

**Removed:**

* <news item>

**Fixed:**

* <news item>

**Security:**

* <news item>
1 change: 1 addition & 0 deletions src/GUI/code/GUI_CA_exp_creator.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,6 +203,7 @@ def config_writer_CA(

Parameters
------
stp_num
exp_type: string, experiment type

rt: int, rest time
Expand Down
2 changes: 1 addition & 1 deletion src/GUI/code/GUI_config_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,6 @@ def check_config_inputs(arg):

if __name__ == "__main__":
# used for debugging. Does the function load the configs?
data = parse_config_file("pytentiostat/tests/static")
data = parse_config_file("C:/Users/user/PycharmProjects/pytentiostat/tests/static/config.yml")
data_out_name = data["general_parameters"]["data_output_filename"]
print(data_out_name)
26 changes: 8 additions & 18 deletions src/GUI/designer_files/buffer.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
## CA
from Adv_params_GUI import Ui_Adv_Params
from Load_config_GUI import Ui_Load
from PyQt6 import QtWidgets

from src.GUI.code.Adv_params_GUI import Ui_Adv_Params
from src.GUI.code.CA_GUI import Ui_CA
from src.GUI.code.CV_GUI import Ui_CV
from src.GUI.code.Exp_type_GUI import Ui_Experiment
from src.GUI.code.GUI_load_config import Ui_Load
from src.GUI.code.LSV_GUI import Ui_LSV


class Ui_CA(object):
Expand Down Expand Up @@ -31,12 +37,6 @@ def AP_window(self):
return self.AP, self.window


from Adv_params_GUI import Ui_Adv_Params

## CV
from Load_config_GUI import Ui_Load # more


class Ui_CV(object):
"""Initializes the 'Load config file' window.

Expand Down Expand Up @@ -94,16 +94,6 @@ def AP_window(self):
return self.AP, self.window


from CA_GUI import Ui_CA
from CV_GUI import Ui_CV
from Exp_type_GUI import Ui_Experiment
from Load_config_GUI import Ui_Load

## main
## main
from LSV_GUI import Ui_LSV


class Ui_MainWindow(object):
def show_exp(self):
"""Initializes the 'Experiment Type' window.
Expand Down
165 changes: 99 additions & 66 deletions src/pytentiostat/main.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
# Pytentiostat function files
import argparse
import sys

import matplotlib.pyplot as plt
Expand All @@ -7,6 +8,24 @@
from pytentiostat.operator import experiment
from pytentiostat.reporter import save_data_to_file
from pytentiostat.routines import closing_routine, startup_routine
from pytentiostat.version import __version__


def parser():
parser = argparse.ArgumentParser(
description="Pytentiostat Command Line Interface. To use, first plug "
"in the potentiostat, then run the software by typing"
"pytentiostat at the command line and follow the prompts."
"For more information, see the documentation at "
"juami.github.io/pytentiostat"
)
parser.add_argument(
"--version",
action="store_true",
help="Show the version of pytentiostat and exit",
)
args = parser.parse_args()
return args


class BoardCom:
Expand All @@ -16,77 +35,91 @@ def __init__(self):
)


try:
# Initialize Experiment
board_instance = BoardCom()
# com, board, a0, a2, d9 = startup_routine()
except KeyboardInterrupt:
sys.exit(
"Connection interrupted by user. Make sure the potentiostat "
"is plugged in then restart the software.\n"
"Exiting..."
)
def main():
args = parser()
if args.version:
print(f"{__version__}")
sys.exit(0)

board_objects = (
board_instance.pin_a0,
board_instance.pin_a2,
board_instance.pin_d9,
)
parse = input("Press enter to load the config file.")
config_data = parse_config_file()
while True:
while True:
# Run the experiment and get the config_data
start = input("Press enter to start the experiment.")
times, voltages, currents, interrupt = experiment(
config_data, *board_objects
try:
# Initialize Experiment
board_instance = BoardCom()
# com, board, a0, a2, d9 = startup_routine()
except KeyboardInterrupt:
sys.exit(
"Connection interrupted by user. Make sure the potentiostat "
"is plugged in then restart the software.\n"
"Exiting..."
)
if interrupt:
save = input(
"Experiment interrupted. Would you "
"like to save the data? [y/n]: "
)
if save.lower() == "y":
temp_data = zip(times, voltages, currents)
save_data_to_file(config_data, temp_data)
print("Saved.")
reconfig = input(
"\nIf you want to do a different experiment, edit and "
"save the config file then type"
' "new" and press enter.\n'
'If you need to reconnect the potentiostat, type "reconnect" '
"then press enter.\n"
"To close, just press enter. \n"

board_objects = (
board_instance.pin_a0,
board_instance.pin_a2,
board_instance.pin_d9,
)
input("Press enter to load the config file.")
config_data = parse_config_file()
while True:
while True:
# Run the experiment and get the config_data
input("Press enter to start the experiment.")
times, voltages, currents, interrupt = experiment(
config_data, *board_objects
)
if reconfig.lower() == "new":
plt.close()
config_data = parse_config_file()
elif reconfig.lower() == "reconnect":
plt.close()
closing_routine(board_instance.board, board_instance.pin_d9)
board_instance.__init__()
board_objects = (
board_instance.pin_a0,
board_instance.pin_a2,
board_instance.pin_d9,
if interrupt:
save = input(
"Experiment interrupted. Would you "
"like to save the data? [y/n]: "
)
if save.lower() == "y":
temp_data = zip(times, voltages, currents)
save_data_to_file(config_data, temp_data)
print("Saved.")
reconfig = input(
"\nIf you want to do a different experiment, edit and "
"save the config file then type"
' "new" and press enter.\n'
"If you need to reconnect the potentiostat, type "
'"reconnect" then press enter.\n'
"To close, just press enter. \n"
)
if reconfig.lower() == "new":
plt.close()
config_data = parse_config_file()
elif reconfig.lower() == "reconnect":
plt.close()
closing_routine(
board_instance.board, board_instance.pin_d9
)
board_instance.__init__()
board_objects = (
board_instance.pin_a0,
board_instance.pin_a2,
board_instance.pin_d9,
)
else:
closing_routine(
board_instance.board, board_instance.pin_d9
)
sys.exit(0)
else:
closing_routine(board_instance.board, board_instance.pin_d9)
sys.exit(0)
else:
break

# Generate a config_data report
if not interrupt:
collected_data = zip(times, voltages, currents)
save_data_to_file(config_data, collected_data)
print("Saved.")
stop = input("\nWould you like to repeat the last experiment? [y/n]: ")
if stop.lower() != "y":
break
else:
interrupt = False
plt.close()

# Wrap things up
closing_routine(board_instance.board, board_instance.pin_d9)

# Generate a config_data report
if not interrupt:
collected_data = zip(times, voltages, currents)
save_data_to_file(config_data, collected_data)
print("Saved.")
stop = input("\nWould you like to repeat the last experiment? [y/n]: ")
if stop.lower() != "y":
break
else:
interrupt = False
plt.close()

# Wrap things up
closing_routine(board_instance.board, board_instance.pin_d9)
if __name__ == "__main__":
main()
116 changes: 116 additions & 0 deletions tests/test_adv_params_gui.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
import sys
from unittest.mock import MagicMock

import pytest
from PySide6.QtWidgets import QApplication

from src.GUI.code.Adv_params_GUI import Ui_Adv_Params


@pytest.fixture
def app(qtbot):
"""Create a QApplication instance for testing."""
app = QApplication(sys.argv)
yield app
app.quit()


@pytest.fixture
def ui(app):
"""Create an instance of the UI for testing."""
main_window = MagicMock()
ui = Ui_Adv_Params()
ui.setupUi(main_window)
return ui, main_window


def test_ui_initialization(ui):
"""Test the initialization of the UI components."""
ui, main_window = ui

# Check if the main window is set up correctly
assert main_window.objectName() == "Adv_Params"
assert main_window.windowTitle() == "Adv Params"


def test_labels(ui):
"""Test the labels in the UI."""
ui, main_window = ui

# Check the text of the labels
assert ui.experiment_conversion_factor_label.text() == "Conversion Factor"
assert ui.experiment_averag_number_label.text() == "Average Number"
assert ui.experiment_shunt_resistor_label.text() == "Shunt Resistor /mohm"
assert ui.advanced_parameters_label.text() == "Advanced Parameters"
assert ui.experiment_time_step_label.text() == "Time Step /s"
assert ui.experiment_setpoint_gain_label.text() == "Setpoint Gain"
assert ui.experiment_setpoint_offset_label.text() == "Setpoint Offset"


def test_line_edit_initialization(ui):
"""Test the initialization of line edit fields."""
ui, main_window = ui

# Check if the line edits are initialized correctly
assert (
ui.experiment_conversion_factor.objectName()
== "experiment_conversion_factor"
)
assert (
ui.experiment_setpoint_gain.objectName() == "experiment_setpoint_gain"
)
assert (
ui.experiment_setpoint_offset.objectName()
== "experiment_setpoint_offset"
)
assert (
ui.experiment_shunt_resistor.objectName()
== "experiment_shunt_resistor"
)
assert ui.experiment_time_step.objectName() == "experiment_time_step"
assert (
ui.experiment_averag_number.objectName() == "experiment_average_number"
)


def test_button_initialization(ui):
"""Test the initialization of buttons."""
ui, main_window = ui

# Check if the save button is initialized correctly
assert (
ui.save_experiment_file_button.objectName()
== "save_experiment_file_button"
)
assert ui.save_experiment_file_button.text() == "Save "


def test_line_edit_properties(ui):
"""Test the properties of line edit fields."""
ui, main_window = ui

assert ui.experiment_conversion_factor.isAcceptDrops() is False
assert ui.experiment_conversion_factor.alignment() == 0x0004 # AlignCenter
assert ui.experiment_setpoint_gain.isAcceptDrops() is False
assert ui.experiment_setpoint_offset.isAcceptDrops() is False
assert ui.experiment_shunt_resistor.isAcceptDrops() is False
assert ui.experiment_time_step.isAcceptDrops() is False
assert ui.experiment_averag_number.isAcceptDrops() is False


def test_label_font_properties(ui):
"""Test the font properties of labels."""
ui, main_window = ui

# Check if the labels have the correct font properties
assert ui.experiment_conversion_factor_label.font().bold() is True
assert ui.experiment_averag_number_label.font().bold() is True
assert ui.experiment_shunt_resistor_label.font().bold() is True
assert ui.advanced_parameters_label.font().bold() is True
assert ui.experiment_time_step_label.font().bold() is True
assert ui.experiment_setpoint_gain_label.font().bold() is True
assert ui.experiment_setpoint_offset_label.font().bold() is True


if __name__ == "__main__":
pytest.main()
Loading