From 3058a048927f0f0a123d733282d8b4102c42c6be Mon Sep 17 00:00:00 2001 From: pinzia Date: Wed, 13 Oct 2021 10:57:38 +0200 Subject: [PATCH 1/7] added pytest-qt --- requirements-dev.txt | 1 + tests/main_with_gui/__init__.py | 0 tests/main_with_gui/test_gui.py | 0 3 files changed, 1 insertion(+) create mode 100644 tests/main_with_gui/__init__.py create mode 100644 tests/main_with_gui/test_gui.py diff --git a/requirements-dev.txt b/requirements-dev.txt index 0f30ad5..0133393 100644 --- a/requirements-dev.txt +++ b/requirements-dev.txt @@ -8,6 +8,7 @@ pluggy==0.13.1 py==1.10.0 pyparsing==2.4.6 pytest==5.3.2 +pytest-qt==4.0.2 six==1.13.0 wcwidth==0.1.7 zipp==3.3.1 diff --git a/tests/main_with_gui/__init__.py b/tests/main_with_gui/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/tests/main_with_gui/test_gui.py b/tests/main_with_gui/test_gui.py new file mode 100644 index 0000000..e69de29 From 540f014ef14bf0f41bb7b4bdbb0e98ac6480667a Mon Sep 17 00:00:00 2001 From: Pinzia Date: Tue, 26 Oct 2021 18:30:20 +0200 Subject: [PATCH 2/7] Test for one pc works --- tests/main_with_gui/test_gui.py | 108 ++++++++++++++++++++++++++++++++ 1 file changed, 108 insertions(+) diff --git a/tests/main_with_gui/test_gui.py b/tests/main_with_gui/test_gui.py index e69de29..de5ec67 100644 --- a/tests/main_with_gui/test_gui.py +++ b/tests/main_with_gui/test_gui.py @@ -0,0 +1,108 @@ +import pytest +import os +from PyQt5 import QtCore, QtTest, QtWidgets +from main import extract_and_collect_data_from_generated_files +from main_with_gui import Welcome, FilesGenerated, GPU, DataToTarallo, get_gpu_location + + +@pytest.fixture +def open_welcome(qtbot): + def callback(window): + widget = window(QtWidgets.QMainWindow) + qtbot.addWidget(widget) + widget.show() + qtbot.wait_for_window_shown(widget) + return widget + + return callback + +@pytest.fixture +def open_filesgen(qtbot): + def callback(window, gpu_loc: GPU): + widget = window(Welcome, gpu_loc) + qtbot.addWidget(widget) + widget.show() + qtbot.wait_for_window_shown(widget) + #QtTest.QTest.qWait(3 * 1000) + return widget + + return callback + + +class TestVisibleWindow: + def test_visible_welcome(self, open_welcome, qtbot): + widget = open_welcome(Welcome) + assert widget.isVisible() + + def test_visible_filesgen(self, open_filesgen, qtbot): + widget = open_filesgen(FilesGenerated, GPU.dec_gpu) + assert widget.isVisible() + + +class TestDataTarallo: + def show_window(self): + system_info = self.create_system_info() + widget = DataToTarallo(system_info) + widget.show() + return widget + + def press_upload(self, widget, qtbot): + qtbot.mouseClick(widget.btnupl, QtCore.Qt.LeftButton) + QtTest.QTest.qWait(1000) + w = widget.focusWidget() + assert isinstance(w, QtWidgets.QPushButton) + name = w.text() + assert name == "Upload" + + def check_result(self): + messagebox = QtWidgets.QApplication.activeWindow() + QtTest.QTest.qWait(1000) + text = messagebox.text() + assert text == "Everything went fine, what do you want to do?" + messagebox.close() + + def create_system_info(self): + gpu_loc = get_gpu_location(os.path.join(os.getcwd(), "tests/asdpc")) + if gpu_loc == GPU.int_mobo: + has_dedicated_gpu = False + gpu_in_cpu = False + elif gpu_loc == GPU.int_cpu: + has_dedicated_gpu = False + gpu_in_cpu = True + elif gpu_loc == GPU.dec_gpu: + has_dedicated_gpu = True + gpu_in_cpu = False + files_dir = os.path.join(os.getcwd(), "tests/asdpc") + return extract_and_collect_data_from_generated_files( + files_dir, has_dedicated_gpu, gpu_in_cpu + ) + + def test_no_pref(self,qtbot): + widget = self.show_window() + QtTest.QTest.qWait(1000) + self.press_upload(widget, qtbot) + self.check_result() + + def test_id(self, qtbot): + widget = self.show_window() + widget.txtid.setText("asdone3")#nome in base al pc + QtTest.QTest.qWait(000) + self.press_upload(widget, qtbot) + self.check_result() + + def test_overwrite(self, qtbot): + widget = self.show_window() + widget.chbov.setChecked(True) + QtTest.QTest.qWait(1000) + self.press_upload(widget, qtbot) + self.check_result() + + + def test_over_id(self, qtbot): + widget = self.show_window() + widget.chbov.setChecked(True) + widget.txtid.setText("asdone") # nome in base al pc + QtTest.QTest.qWait(1000) + self.press_upload(widget, qtbot) + self.check_result() + From 17fe92bd3f1d54ac2cb354a65569347502c9681e Mon Sep 17 00:00:00 2001 From: Pinzia Date: Mon, 1 Nov 2021 16:48:10 +0100 Subject: [PATCH 3/7] succesfull DataToTarallo tests --- tests/main_with_gui/test_gui.py | 87 +++++++++++++++++++++++---------- 1 file changed, 60 insertions(+), 27 deletions(-) diff --git a/tests/main_with_gui/test_gui.py b/tests/main_with_gui/test_gui.py index de5ec67..aed315d 100644 --- a/tests/main_with_gui/test_gui.py +++ b/tests/main_with_gui/test_gui.py @@ -4,6 +4,18 @@ from main import extract_and_collect_data_from_generated_files from main_with_gui import Welcome, FilesGenerated, GPU, DataToTarallo, get_gpu_location +test_folders = [ + entries for entries in os.listdir("tests/") if os.path.isdir(f"tests/{entries}") +] +for fold in set(test_folders): + if "baseboard.txt" not in os.listdir(f"tests/{fold}"): + test_folders.remove(fold) + + +@pytest.fixture(params=test_folders) +def folders(request): + return request.param + @pytest.fixture def open_welcome(qtbot): @@ -16,6 +28,7 @@ def callback(window): return callback + @pytest.fixture def open_filesgen(qtbot): def callback(window, gpu_loc: GPU): @@ -40,15 +53,8 @@ def test_visible_filesgen(self, open_filesgen, qtbot): class TestDataTarallo: - def show_window(self): - system_info = self.create_system_info() - widget = DataToTarallo(system_info) - widget.show() - return widget - def press_upload(self, widget, qtbot): qtbot.mouseClick(widget.btnupl, QtCore.Qt.LeftButton) - QtTest.QTest.qWait(1000) w = widget.focusWidget() assert isinstance(w, QtWidgets.QPushButton) name = w.text() @@ -56,13 +62,11 @@ def press_upload(self, widget, qtbot): def check_result(self): messagebox = QtWidgets.QApplication.activeWindow() - QtTest.QTest.qWait(1000) text = messagebox.text() assert text == "Everything went fine, what do you want to do?" messagebox.close() - def create_system_info(self): - gpu_loc = get_gpu_location(os.path.join(os.getcwd(), "tests/asdpc")) + def def_gpu_location(self, gpu_loc): if gpu_loc == GPU.int_mobo: has_dedicated_gpu = False gpu_in_cpu = False @@ -72,37 +76,66 @@ def create_system_info(self): elif gpu_loc == GPU.dec_gpu: has_dedicated_gpu = True gpu_in_cpu = False - files_dir = os.path.join(os.getcwd(), "tests/asdpc") - return extract_and_collect_data_from_generated_files( + return has_dedicated_gpu, gpu_in_cpu + + def test_no_pref(self,qtbot, folders): + gpu_loc = get_gpu_location(os.path.join(os.getcwd(), "tests", folders)) + has_dedicated_gpu, gpu_in_cpu = self.def_gpu_location(gpu_loc) + files_dir = os.path.join(os.getcwd(), "tests", folders) + system_info = extract_and_collect_data_from_generated_files( files_dir, has_dedicated_gpu, gpu_in_cpu ) - - def test_no_pref(self,qtbot): - widget = self.show_window() - QtTest.QTest.qWait(1000) + widget = DataToTarallo(system_info) + widget.show() + QtTest.QTest.qWait(100) self.press_upload(widget, qtbot) + QtTest.QTest.qWait(100) self.check_result() - def test_id(self, qtbot): - widget = self.show_window() - widget.txtid.setText("asdone3")#nome in base al pc - QtTest.QTest.qWait(000) + def test_id(self, qtbot, folders): + gpu_loc = get_gpu_location(os.path.join(os.getcwd(), "tests", folders)) + has_dedicated_gpu, gpu_in_cpu = self.def_gpu_location(gpu_loc) + files_dir = os.path.join(os.getcwd(), "tests", folders) + system_info = extract_and_collect_data_from_generated_files( + files_dir, has_dedicated_gpu, gpu_in_cpu + ) + widget = DataToTarallo(system_info) + widget.show() + widget.txtid.setText(folders) + QtTest.QTest.qWait(100) self.press_upload(widget, qtbot) + QtTest.QTest.qWait(100) self.check_result() - def test_overwrite(self, qtbot): - widget = self.show_window() + def test_overwrite(self, qtbot, folders): + gpu_loc = get_gpu_location(os.path.join(os.getcwd(), "tests", folders)) + has_dedicated_gpu, gpu_in_cpu = self.def_gpu_location(gpu_loc) + files_dir = os.path.join(os.getcwd(), "tests", folders) + system_info = extract_and_collect_data_from_generated_files( + files_dir, has_dedicated_gpu, gpu_in_cpu + ) + widget = DataToTarallo(system_info) + widget.show() widget.chbov.setChecked(True) - QtTest.QTest.qWait(1000) + QtTest.QTest.qWait(100) self.press_upload(widget, qtbot) + QtTest.QTest.qWait(100) self.check_result() - def test_over_id(self, qtbot): - widget = self.show_window() + def test_over_id(self, qtbot, folders): + gpu_loc = get_gpu_location(os.path.join(os.getcwd(), "tests", folders)) + has_dedicated_gpu, gpu_in_cpu = self.def_gpu_location(gpu_loc) + files_dir = os.path.join(os.getcwd(), "tests", folders) + system_info = extract_and_collect_data_from_generated_files( + files_dir, has_dedicated_gpu, gpu_in_cpu + ) + widget = DataToTarallo(system_info) + widget.show() widget.chbov.setChecked(True) - widget.txtid.setText("asdone") # nome in base al pc - QtTest.QTest.qWait(1000) + widget.txtid.setText(folders) + QtTest.QTest.qWait(100) self.press_upload(widget, qtbot) + QtTest.QTest.qWait(100) self.check_result() From e47bf9a59cc0ea3c94279b426affb2706465a85c Mon Sep 17 00:00:00 2001 From: Lint Action Date: Mon, 1 Nov 2021 16:07:59 +0000 Subject: [PATCH 4/7] Fix code style issues with Black --- tests/main_with_gui/test_gui.py | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/tests/main_with_gui/test_gui.py b/tests/main_with_gui/test_gui.py index aed315d..a93e19a 100644 --- a/tests/main_with_gui/test_gui.py +++ b/tests/main_with_gui/test_gui.py @@ -36,7 +36,7 @@ def callback(window, gpu_loc: GPU): qtbot.addWidget(widget) widget.show() qtbot.wait_for_window_shown(widget) - #QtTest.QTest.qWait(3 * 1000) + # QtTest.QTest.qWait(3 * 1000) return widget return callback @@ -78,7 +78,7 @@ def def_gpu_location(self, gpu_loc): gpu_in_cpu = False return has_dedicated_gpu, gpu_in_cpu - def test_no_pref(self,qtbot, folders): + def test_no_pref(self, qtbot, folders): gpu_loc = get_gpu_location(os.path.join(os.getcwd(), "tests", folders)) has_dedicated_gpu, gpu_in_cpu = self.def_gpu_location(gpu_loc) files_dir = os.path.join(os.getcwd(), "tests", folders) @@ -122,7 +122,6 @@ def test_overwrite(self, qtbot, folders): QtTest.QTest.qWait(100) self.check_result() - def test_over_id(self, qtbot, folders): gpu_loc = get_gpu_location(os.path.join(os.getcwd(), "tests", folders)) has_dedicated_gpu, gpu_in_cpu = self.def_gpu_location(gpu_loc) @@ -138,4 +137,3 @@ def test_over_id(self, qtbot, folders): self.press_upload(widget, qtbot) QtTest.QTest.qWait(100) self.check_result() - From 60b19264d3b739882905d5a5a654f61abc2df564 Mon Sep 17 00:00:00 2001 From: Pinzia Date: Mon, 1 Nov 2021 17:12:01 +0100 Subject: [PATCH 5/7] modified things to run tests --- main_with_gui.py | 26 +++++++++++++++----------- 1 file changed, 15 insertions(+), 11 deletions(-) diff --git a/main_with_gui.py b/main_with_gui.py index b22d280..f17d19a 100755 --- a/main_with_gui.py +++ b/main_with_gui.py @@ -82,6 +82,11 @@ def init_ui(self): self.show() +def get_gpu_location(directory): + with open(os.path.join(directory, gpu_loc_file)) as f: + return GPU(f.read()) + + class Welcome(QWidget): def __init__(self, window: QMainWindow): # noinspection PyArgumentList @@ -112,7 +117,7 @@ def __init__(self, window: QMainWindow): "Load previously generated files" ) self.load_previously_generated_files_button.clicked.connect( - lambda: self.load_previously_generated_files(self, window) + lambda: self.load_previously_generated_files(window) ) # by default it's enabled, if one of the expected files does not exist it's disabled style_disabled = "background-color:#666677; color:#444444" @@ -334,11 +339,10 @@ def generate_files(self, window: QMainWindow, gpu_loc: GPU): "Have a look at the extent of your huge fuck-up:\n" + str(e), ) - @staticmethod - def load_previously_generated_files(self, window: QMainWindow): - cwd = os.getcwd() - tmp_path = os.path.join(cwd, "tmp") - for existing_file in os.listdir(tmp_path): + def load_previously_generated_files(self, + window: QMainWindow, + directory: str = os.path.join(os.getcwd(), "tmp")): + for existing_file in os.listdir(directory): if existing_file == gpu_loc_file: break else: # there is no gpu_location.txt file @@ -346,8 +350,7 @@ def load_previously_generated_files(self, window: QMainWindow): gpu_loc = self.prompt_gpu_location(window, True) with open(os.path.join(folder_name, gpu_loc_file), "w") as f: f.write(gpu_loc.value) - with open(os.path.join(os.getcwd(), "tmp", gpu_loc_file)) as f: - gpu_loc = GPU(f.read()) + gpu_loc = get_gpu_location(directory) window.takeCentralWidget() new_widget = FilesGenerated(window, gpu_loc) window.setCentralWidget(new_widget) @@ -675,7 +678,7 @@ def upload(self, system_info, checked, bulkid): btntar = mb_wentok.addButton( "See this PC on T.A.R.A.L.L.O.", QMessageBox.NoRole ) - mb_wentok.exec_() + mb_wentok.show() if mb_wentok.clickedButton() == btncnt: self.close() elif mb_wentok.clickedButton() == btnclose: @@ -690,8 +693,9 @@ def upload(self, system_info, checked, bulkid): "It seems there have been some problems or this PC is a duplicate. Please retry." ) btnclose = mb_notok.addButton("Back to JSON", QMessageBox.YesRole) - mb_notok.exec_() - self.close() + mb_notok.show() + if mb_notok.clickedButton(): + self.close() except NoInternetConnectionError: mb_notok = QMessageBox(self) mb_notok.setWindowTitle("Send data to T.A.R.A.L.L.O.") From eb68290dcd836a67a74eefc5871aece7de2280b6 Mon Sep 17 00:00:00 2001 From: Pinzia Date: Wed, 3 Nov 2021 20:24:55 +0100 Subject: [PATCH 6/7] trying to solve github tests problem --- main_with_gui.py | 46 ++++++++++++++++----------------- tests/main_with_gui/test_gui.py | 7 ++++- 2 files changed, 29 insertions(+), 24 deletions(-) diff --git a/main_with_gui.py b/main_with_gui.py index f17d19a..191d8c0 100755 --- a/main_with_gui.py +++ b/main_with_gui.py @@ -187,6 +187,22 @@ def __init__(self, window: QMainWindow): self.setLayout(v_box) + def load_previously_generated_files(self, + window: QMainWindow, + directory: str = os.path.join(os.getcwd(), "tmp")): + for existing_file in os.listdir(directory): + if existing_file == gpu_loc_file: + break + else: # there is no gpu_location.txt file + folder_name = "tmp" + gpu_loc = self.prompt_gpu_location(window, True) + with open(os.path.join(folder_name, gpu_loc_file), "w") as f: + f.write(gpu_loc.value) + gpu_loc = get_gpu_location(directory) + window.takeCentralWidget() + new_widget = FilesGenerated(window, gpu_loc) + window.setCentralWidget(new_widget) + def check_install_dependencies(self, window: QMainWindow): working_directory = os.getcwd() cmd = os.path.join(working_directory, "scripts/check_dependencies.sh") @@ -205,13 +221,13 @@ def check_install_dependencies(self, window: QMainWindow): ) working_directory = os.getcwd() with sp.Popen( - [ - "pkexec", - os.path.join( - working_directory, "scripts/install_dependencies_all.sh" - ), - ], - shell=False, + [ + "pkexec", + os.path.join( + working_directory, "scripts/install_dependencies_all.sh" + ), + ], + shell=False, ) as process: process.wait(timeout=80) else: @@ -339,22 +355,6 @@ def generate_files(self, window: QMainWindow, gpu_loc: GPU): "Have a look at the extent of your huge fuck-up:\n" + str(e), ) - def load_previously_generated_files(self, - window: QMainWindow, - directory: str = os.path.join(os.getcwd(), "tmp")): - for existing_file in os.listdir(directory): - if existing_file == gpu_loc_file: - break - else: # there is no gpu_location.txt file - folder_name = "tmp" - gpu_loc = self.prompt_gpu_location(window, True) - with open(os.path.join(folder_name, gpu_loc_file), "w") as f: - f.write(gpu_loc.value) - gpu_loc = get_gpu_location(directory) - window.takeCentralWidget() - new_widget = FilesGenerated(window, gpu_loc) - window.setCentralWidget(new_widget) - class FilesGenerated(QWidget): def __init__(self, window: QMainWindow, gpu_loc: GPU): diff --git a/tests/main_with_gui/test_gui.py b/tests/main_with_gui/test_gui.py index aed315d..b514c2e 100644 --- a/tests/main_with_gui/test_gui.py +++ b/tests/main_with_gui/test_gui.py @@ -2,7 +2,9 @@ import os from PyQt5 import QtCore, QtTest, QtWidgets from main import extract_and_collect_data_from_generated_files -from main_with_gui import Welcome, FilesGenerated, GPU, DataToTarallo, get_gpu_location +from main_with_gui import Welcome, FilesGenerated, GPU, DataToTarallo + +gpu_loc_file = "gpu_location.txt" test_folders = [ entries for entries in os.listdir("tests/") if os.path.isdir(f"tests/{entries}") @@ -41,6 +43,9 @@ def callback(window, gpu_loc: GPU): return callback +def get_gpu_location(directory): + with open(os.path.join(directory, gpu_loc_file)) as f: + return GPU(f.read()) class TestVisibleWindow: def test_visible_welcome(self, open_welcome, qtbot): From 8c986874be609c811b673c4df16973b44362af10 Mon Sep 17 00:00:00 2001 From: Lint Action Date: Wed, 3 Nov 2021 19:31:52 +0000 Subject: [PATCH 7/7] Fix code style issues with Black --- main_with_gui.py | 16 +++++++--------- tests/main_with_gui/test_gui.py | 2 ++ 2 files changed, 9 insertions(+), 9 deletions(-) diff --git a/main_with_gui.py b/main_with_gui.py index 9a391b9..f88620f 100755 --- a/main_with_gui.py +++ b/main_with_gui.py @@ -187,7 +187,6 @@ def __init__(self, window: QMainWindow): self.setLayout(v_box) - def check_install_dependencies(self, window: QMainWindow): working_directory = os.getcwd() cmd = os.path.join(working_directory, "scripts/check_dependencies.sh") @@ -206,13 +205,13 @@ def check_install_dependencies(self, window: QMainWindow): ) working_directory = os.getcwd() with sp.Popen( - [ - "pkexec", - os.path.join( - working_directory, "scripts/install_dependencies_all.sh" - ), - ], - shell=False, + [ + "pkexec", + os.path.join( + working_directory, "scripts/install_dependencies_all.sh" + ), + ], + shell=False, ) as process: process.wait(timeout=80) else: @@ -340,7 +339,6 @@ def generate_files(self, window: QMainWindow, gpu_loc: GPU): "Have a look at the extent of your huge fuck-up:\n" + str(e), ) - def load_previously_generated_files(self, window: QMainWindow): cwd = os.getcwd() tmp_path = os.path.join(cwd, "tmp") diff --git a/tests/main_with_gui/test_gui.py b/tests/main_with_gui/test_gui.py index 7607cf9..a826dbf 100644 --- a/tests/main_with_gui/test_gui.py +++ b/tests/main_with_gui/test_gui.py @@ -43,10 +43,12 @@ def callback(window, gpu_loc: GPU): return callback + def get_gpu_location(directory): with open(os.path.join(directory, gpu_loc_file)) as f: return GPU(f.read()) + class TestVisibleWindow: def test_visible_welcome(self, open_welcome, qtbot): widget = open_welcome(Welcome)