From e802c5360dcede58649f14883cd3662995c83d5f Mon Sep 17 00:00:00 2001 From: enexety Date: Sat, 30 Sep 2023 12:45:36 +0300 Subject: [PATCH] Addition of additional tests. Additions of waiting for commands in "PushManager". Bug fixes in "ConsoleManager". Small changes. --- Tests/Unit Testing/test_center_window.py | 22 ++++++++ .../test_create_and_configure_window.py | 48 +++++++++++++++++ Tests/Unit Testing/test_overwriting_file.py | 2 +- Tests/Unit Testing/test_set_settings.py | 51 +++++++++++++++++++ .../Overwatch Rank Tracker/ButtonManager.py | 4 +- sources/Overwatch Rank Tracker/MainWindow.py | 9 ++-- sources/Project Manager/ConsoleManager.py | 2 +- sources/Project Manager/ProjectManager.py | 9 ++-- sources/Project Manager/PushManager.py | 15 +++--- 9 files changed, 140 insertions(+), 22 deletions(-) create mode 100644 Tests/Unit Testing/test_center_window.py create mode 100644 Tests/Unit Testing/test_create_and_configure_window.py create mode 100644 Tests/Unit Testing/test_set_settings.py diff --git a/Tests/Unit Testing/test_center_window.py b/Tests/Unit Testing/test_center_window.py new file mode 100644 index 0000000..09a51d5 --- /dev/null +++ b/Tests/Unit Testing/test_center_window.py @@ -0,0 +1,22 @@ +import os +import sys +import tkinter + +sys.path.append(os.path.abspath(os.path.join(os.path.abspath(__file__), '../../../sources/Overwatch Rank Tracker'))) +import MainWindow # noqa: E402 + + +def test_center_window(): + """Check that the window is created in the center of the screen.""" + + # create window + test_window = tkinter.Tk() + + # calling window centering function + MainWindow.MainWindow.center_window(window=test_window, window_height=123, window_width=456) + + # getting window coordinates after centering + x_coord = test_window.winfo_x() + y_coord = test_window.winfo_y() + + assert (x_coord, y_coord) == (0, 0) diff --git a/Tests/Unit Testing/test_create_and_configure_window.py b/Tests/Unit Testing/test_create_and_configure_window.py new file mode 100644 index 0000000..dd4bf22 --- /dev/null +++ b/Tests/Unit Testing/test_create_and_configure_window.py @@ -0,0 +1,48 @@ +import os +import sys +import tkinter +from unittest.mock import patch +from tkinter import ttk + +sys.path.append(os.path.abspath(os.path.join(os.path.abspath(__file__), '../../../sources/Overwatch Rank Tracker'))) +import MainWindow # noqa: E402 + + +def setup_main_window_with_mock(): + + # creating an instance of a class + mainWindow = MainWindow.MainWindow() + + # since there is no actual window in the tests, the "iconphoto" method cannot be applied, so it is mocked here + with patch.object(tkinter.Tk, 'iconphoto'): + mainWindow.create_and_configure_window() + + return mainWindow + + +def test_window_created(): + """""" + + mainWindow = setup_main_window_with_mock() + assert isinstance(mainWindow.main_window, tkinter.Tk) + + +def test_window_title(): + """""" + + mainWindow = setup_main_window_with_mock() + assert mainWindow.main_window.title() == 'Overwatch Rank Tracker' + + +def test_window_style(): + """Checking that the styles have been configured correctly.""" + + mainWindow = setup_main_window_with_mock() + + # get styles + style = tkinter.ttk.Style(mainWindow.main_window) + actual_styles = {'background': style.lookup('Treeview', 'background'), 'foreground': style.lookup('Treeview', 'foreground'), 'fieldbackground': style.lookup('Treeview', 'fieldbackground')} + expected_styles = {'background': '#2B2B2B', 'foreground': 'white', 'fieldbackground': '#2B2B2B'} + + assert actual_styles == expected_styles + diff --git a/Tests/Unit Testing/test_overwriting_file.py b/Tests/Unit Testing/test_overwriting_file.py index 0bb538b..dc304b2 100644 --- a/Tests/Unit Testing/test_overwriting_file.py +++ b/Tests/Unit Testing/test_overwriting_file.py @@ -2,7 +2,7 @@ import sys sys.path.append(os.path.abspath(os.path.join(os.path.abspath(__file__), '../../../sources/Overwatch Rank Tracker'))) -import FileManager # noqa: E402. It imports it and recognizes it, I don't know why the Python interpreter is flagging an error +import FileManager # noqa: E402 """ diff --git a/Tests/Unit Testing/test_set_settings.py b/Tests/Unit Testing/test_set_settings.py new file mode 100644 index 0000000..d2535ad --- /dev/null +++ b/Tests/Unit Testing/test_set_settings.py @@ -0,0 +1,51 @@ +import os +import sys + +sys.path.append(os.path.abspath(os.path.join(os.path.abspath(__file__), '../../../sources/Overwatch Rank Tracker'))) +import FileManager # noqa: E402 + + +path_to_file_test = os.path.abspath(os.path.join(__file__, '../../test_settings_and_battle_tags.json')) + + +def test_set_settings_value_change(): + """Checks for changes to settings variables after using the "set_settings" function.""" + + # define a class + filemanager = FileManager.FileManager() + + # value change + filemanager.path_to_file = path_to_file_test + filemanager.max_workers = 3 + + # create file + filemanager.overwriting_file(full_rewrite=True) + + # value change, max_workers in file = 3, in class = 6 + filemanager.max_workers = 6 + + # set settings, max_workers should now be set to 2 + filemanager.set_settings() + + # delete file + os.remove(path_to_file_test) + + assert filemanager.max_workers == 3 + + +def test_set_settings_no_file(): + """Checks that after using the "set_settings" function a file with settings is created, if it was not there before.""" + + # define a class + filemanager = FileManager.FileManager() + + # value change + filemanager.path_to_file = path_to_file_test + + # set settings, max_workers should now be set to 2 + filemanager.set_settings() + + assert os.path.exists(filemanager.path_to_file) + + # delete file + os.remove(path_to_file_test) diff --git a/sources/Overwatch Rank Tracker/ButtonManager.py b/sources/Overwatch Rank Tracker/ButtonManager.py index 97ea999..7b323ea 100644 --- a/sources/Overwatch Rank Tracker/ButtonManager.py +++ b/sources/Overwatch Rank Tracker/ButtonManager.py @@ -19,10 +19,10 @@ def __init__(self, mainWindow): self.private_profiles = [] self.error_battle_tags = [] - def set_button_frame(self, main_window): + def set_button_frame(self): """Creates a frame for the buttons.""" - self.button_frame = tkinter.Frame(main_window, bg='#2B2B2B') + self.button_frame = tkinter.Frame(self.mainWindow.main_window, bg='#2B2B2B') self.button_frame.pack(side='bottom', fill='x') def set_buttons(self): diff --git a/sources/Overwatch Rank Tracker/MainWindow.py b/sources/Overwatch Rank Tracker/MainWindow.py index 3b2a573..ec4e5b3 100644 --- a/sources/Overwatch Rank Tracker/MainWindow.py +++ b/sources/Overwatch Rank Tracker/MainWindow.py @@ -9,7 +9,7 @@ class MainWindow: - def __init__(self, logManager): + def __init__(self, logManager=None): self.logManager = logManager self.main_window = None self.widgetManager = None @@ -19,7 +19,7 @@ def run(self): """Start the main window and create everything you need on it: frames, buttons, widgets, custom closing.""" # configure window - self.__create_and_configure_window() + self.create_and_configure_window() # set settings self.fileManager = FileManager.FileManager() @@ -33,7 +33,7 @@ def run(self): # set frame and buttons buttonManager = ButtonManager.ButtonManager(mainWindow=self) - buttonManager.set_button_frame(main_window=self.main_window) + buttonManager.set_button_frame() buttonManager.set_buttons() # custom close @@ -42,7 +42,7 @@ def run(self): # launch window self.main_window.mainloop() - def __create_and_configure_window(self): + def create_and_configure_window(self): """Create: main window, title, style, centering, logo.""" # create @@ -50,7 +50,6 @@ def __create_and_configure_window(self): self.main_window.title('Overwatch Rank Tracker') # set style - self.main_window.configure(bg='#2B2B2B') style = ttk.Style(self.main_window) style.theme_use("clam") style.configure('Treeview', background='#2B2B2B', foreground="white", fieldbackground="#2B2B2B") diff --git a/sources/Project Manager/ConsoleManager.py b/sources/Project Manager/ConsoleManager.py index 33aa6c6..b33a9d9 100644 --- a/sources/Project Manager/ConsoleManager.py +++ b/sources/Project Manager/ConsoleManager.py @@ -151,7 +151,7 @@ def push_replacing_last_commit(self): elif userChoice.lower() == 'y': replacing_commit_message = input('\nCommands:' "\n1. Back\n" - '\nAre you want to change last commit message? (y/n): ') + '\nCommit message: ') # user wants to finish if replacing_commit_message == '1': diff --git a/sources/Project Manager/ProjectManager.py b/sources/Project Manager/ProjectManager.py index 111cafc..e3f9298 100644 --- a/sources/Project Manager/ProjectManager.py +++ b/sources/Project Manager/ProjectManager.py @@ -1,16 +1,17 @@ import configparser import os +import sys import ConsoleManager import TokenManager -class ReleaseCreator: +class ProjectManager: def __init__(self): # get paths - self.project_directory_path = os.path.abspath(os.path.join(os.path.abspath(__file__), '../../..')) + self.project_directory_path = os.path.abspath(os.path.join(sys.argv[0], '../../..')) self.zip_file_path = os.path.join(self.project_directory_path, "Overwatch Rank Tracker.zip") config_file_path = os.path.join(self.project_directory_path, 'resources', 'config.ini') @@ -37,5 +38,5 @@ def run(self): if __name__ == "__main__": - releaseCreator = ReleaseCreator() - releaseCreator.run() + projectManager = ProjectManager() + projectManager.run() diff --git a/sources/Project Manager/PushManager.py b/sources/Project Manager/PushManager.py index e08907e..46dd6d2 100644 --- a/sources/Project Manager/PushManager.py +++ b/sources/Project Manager/PushManager.py @@ -1,5 +1,4 @@ import subprocess -import time class PushManager: @@ -8,15 +7,14 @@ class PushManager: def __git_add(): """Executing terminal command - git add .""" - subprocess.run(["git", "add", "."]) + subprocess.run(["git", "add", "."], check=True) def push_new_commit(self, commit_message: str): """Commands to create a new commit.""" self.__git_add() - subprocess.run(["git", "commit", "-m", commit_message]) - subprocess.run(["git", "push", "origin", "master"]) - time.sleep(3) + subprocess.run(["git", "commit", "-m", commit_message], check=True) + subprocess.run(["git", "push", "origin", "master"], check=True) def push_replacing_last_commit(self, commit_message: str = None): """Commands to replace last commit and possibly its message.""" @@ -25,11 +23,10 @@ def push_replacing_last_commit(self, commit_message: str = None): # without replacing commit message if commit_message is None: - subprocess.run(["git", "commit", "--amend", "-C", "HEAD"]) + subprocess.run(["git", "commit", "--amend", "-C", "HEAD"], check=True) # replacing commit message else: - subprocess.run(["git", "commit", "--amend", "-m", commit_message]) + subprocess.run(["git", "commit", "--amend", "-m", commit_message], check=True) - subprocess.run(["git", "push", "--force"]) - time.sleep(3) + subprocess.run(["git", "push", "--force"], check=True)