Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ticket 8211 unit test #190

Merged
merged 9 commits into from
Mar 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions .github/workflows/push_event_workflow.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
name: Push Event Workflow

on: push

jobs:
unit-testing:
runs-on: windows-latest

steps:
- name : Checkout code
uses : actions/checkout@v4

- name: Install dependencies
run: |
python -m pip install --upgrade pip
pip install -r ./installation_and_upgrade/requirements.txt

- name : Run tests
working-directory: ./installation_and_upgrade
run: |
pip install pytest
python -m pytest
4 changes: 1 addition & 3 deletions installation_and_upgrade/ibex_install_utils/ca_utils.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
import os

from genie_python.utilities import dehex_and_decompress


class CaWrapper:
"""
Wrapper around genie python's channel access class providing some useful abstractions.
Expand Down Expand Up @@ -52,6 +49,7 @@ def get_object_from_compressed_hexed_json(self, name):
if data is None:
return None
else:
from genie_python.utilities import dehex_and_decompress
return dehex_and_decompress(data)

def get_blocks(self):
Expand Down
1 change: 0 additions & 1 deletion installation_and_upgrade/ibex_install_utils/run_process.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@

import os
import subprocess
from types import NoneType
from typing import Union, List

from ibex_install_utils.exceptions import ErrorInRun
Expand Down
8 changes: 4 additions & 4 deletions installation_and_upgrade/ibex_install_utils/tasks/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,11 @@ def _get_backup_dir():
new_backup_dir = os.path.join(BACKUP_DIR, BaseTasks._generate_backup_dir_name())

if not os.path.exists(BACKUP_DATA_DIR):
# data dir is a linked directory on real instrument machines so can't just simply be created with mkdir
raise IOError(f"Base directory does not exist {BACKUP_DATA_DIR} should be a provided linked dir")
if not os.path.exists(BACKUP_DIR):
os.mkdir(BACKUP_DIR)
if not os.path.exists(new_backup_dir):
os.mkdir(new_backup_dir)

os.makedirs(new_backup_dir, exist_ok=True)

# cache backup dir name (useful when backup happens over multiple days)
# it will always refer to the date when backup was started
BaseTasks._backup_dir = new_backup_dir
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,11 +108,9 @@ def _backup_dir(self, src, copy=True, ignore=None):
if copy:
print(f"Copying {src} to {backup_dir}")
self.backup_files(src, backup_dir, copy=True, ignore=ignore, number_of_files=number_of_files)
# self.zip_file(src, backup_dir)
else:
print(f"Moving {src} to {backup_dir}")
self.backup_files(src, backup_dir, ignore=ignore, number_of_files=number_of_files)
# self.zip_file(src, backup_dir)

else: # if src can't be found on the machine
if src.lower() in self.FAILED_BACKUP_DIRS_TO_IGNORE:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import io
import os
from unittest.mock import patch, Mock
from ibex_install_utils.tasks.backup_tasks import BackupTasks
from ibex_install_utils.tasks import BaseTasks
from ibex_install_utils.user_prompt import UserPrompt

class TestBackupTasks:
@patch('sys.stdout', new_callable=io.StringIO)
def test_WHEN_updating_THEN_progress_bar_works(self, mockstdout):

BackupTasks('','','','','').update_progress_bar(10, 20)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Might be worth creating a variable empty_argument = '' or the like, just for clarity.

expected_output = '\rProgress: [========== ] 50% (10/20)'
assert mockstdout.getvalue() == expected_output

def test_WHEN_verifying_backup_THEN_all_directories_checked(self):
backup_path = 'C:\\Data\\old\\today\\'

def mock_get_backup_dir():
return backup_path

BaseTasks._get_backup_dir = Mock(side_effect=mock_get_backup_dir)
os.path.exists = Mock()
prompter = UserPrompt(True, False)

# For the purpose of testing, we don't need to properly set up the BackupTasks() constructor
# method, so '' is an empty argument to placehold
BackupTasks(prompter,'','','','').backup_checker()

for dir in [
f'{backup_path}EPICS\\VERSION.txt',
f'{backup_path}Python3\\VERSION.txt',
f'{backup_path}Client_E4\\VERSION.txt',
f'{backup_path}Settings',
f'{backup_path}Autosave',
f'{backup_path}EPICS_UTILS'
]:
os.path.exists.assert_any_call(dir)
17 changes: 17 additions & 0 deletions installation_and_upgrade/ibex_install_utils/tests/test_dummy.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#use the following command in an epic terminal
#python -m pytest
#to run the file as a temp fix

import io
from unittest.mock import patch, Mock

class TestDummyTest:
@patch("sys.stdout", new_callable=io.StringIO) #for patching mocked methods into tests
def test_WHEN_true_THEN_true(self, mocked_stdout):
mocked_func = Mock()

mocked_func("hello mr.mock")
mocked_func.assert_called_with("hello mr.mock")

print("hello")
assert mocked_stdout.getvalue() == "hello\n"
19 changes: 19 additions & 0 deletions installation_and_upgrade/ibex_install_utils/tests/test_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#use the following command in an epic terminal
#.\pythonpath.bat ../../ test.py
#to run the file as a temp fix

import io
from unittest.mock import patch
from ibex_install_utils.tasks.backup_tasks import BackupTasks

class TestStringMethods:
@patch('sys.stdout', new_callable=io.StringIO)
def test_update_progress_bar(self, mockstdout):

BackupTasks('','','','','').update_progress_bar(10, 20)
expected_output = '\rProgress: [========== ] 50% (10/20)'
assert mockstdout.getvalue() == expected_output

#def test_zip_file(self):

# BackupTasks('','','','','').zip_file('test','test')
9 changes: 9 additions & 0 deletions installation_and_upgrade/requirements.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
contextlib2==21.6.0
GitPython==3.1.41
kafka_python==2.0.2
lxml==4.9.3
psutil==5.9.5
pytest==8.0.0
pywin32==306
semantic_version==2.10.0
six==1.16.0
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Since your adding specific requirements other than the default system ones this should probably use a venv.

Copy link
Contributor

@zsoltkebel zsoltkebel Feb 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This txt contains the requirements of the ibex_utils repository (apart from pytest which is only for testing). We needed this file to set up the GitHub runner with the neccessary dependencies to be able to run ibex_util code

1 change: 1 addition & 0 deletions installation_and_upgrade/run_tests.bat
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
python -m pytest