Skip to content
Snippets Groups Projects
Commit 32f9bcda authored by nugget's avatar nugget
Browse files

Add first unittests for Publish function.

parent 428ad4ce
Branches
Tags
2 merge requests!12v.0.1 for alpha release,!6Implement basic functionality of publish.
......@@ -4,9 +4,10 @@
import os
import shutil
import sys
import warnings
def Publish(src_datapath, dst_path, data_storage):
def Publish(src_datapath, dst_path, src_plot_path, data_storage):
"""
Save plot, data and measuring script.
......@@ -16,6 +17,8 @@ def Publish(src_datapath, dst_path, data_storage):
Path to data that should be published.
dst_dirname : string
Path to the destination directory.
src_plot_path : string
Current path to the plot (.jpg/.png/...) that was tagged.
data_storage: string
Method how the data should be stored. Available options:
centralized: The raw data will copied only once. All other plots
......@@ -30,16 +33,21 @@ def Publish(src_datapath, dst_path, data_storage):
"""
# Check if source directory exists
if not os.path.exists(src_datapath):
raise ValueError('The specified source directory does not exist.')
raise FileNotFoundError('The specified source directory does not exist.')
# Check if destination directory is allowed path
dst_path_head, dst_dirname = os.path.split(dst_path)
if not os.path.exists(dst_path_head):
raise ValueError('The specified destination directory does not exist.')
raise FileNotFoundError('The specified destination directory does not exist.')
# Check if plot file exists
if not os.path.isfile(src_plot_path):
raise TypeError('The specified plot is not a file.')
# If directory already exists ask user if it should be overwritten or not.
if os.path.isdir(dst_path):
print(f'Folder {dst_dirname} already exists – plot has already been '
'published.')
warnings.warn(f'Folder {dst_dirname} already exists – '
'plot has already been published.')
overwrite_dir = input('Do you want to overwrite the existing folder?'
' (yes/no[default])\n')
......@@ -68,6 +76,8 @@ def Publish(src_datapath, dst_path, data_storage):
# Copy script that calls this function to folder
shutil.copy2(sys.argv[0], dst_path_invisible)
# Copy plot file to folder
shutil.copy2(src_plot_path, dst_path_invisible)
case _:
raise ValueError('The data storage method {data_storage} '
'is not available.')
......
Image diff could not be displayed: it is too large. Options to address this: view the blob.
Image diff could not be displayed: it is too large. Options to address this: view the blob.
......@@ -65,4 +65,5 @@ for i, figure in enumerate(figs):
figure.savefig(name)
# %% Publish
Publish('fcn_help', '/home/chief/Dokumente/fst/plotid_python/data', 'individual')
Publish('fcn_help', '/home/chief/Dokumente/fst/plotid_python/data',
'/home/chief/Dokumente/fst/plotid_python/Test1.tiff', 'individual')
File deleted
# -*- coding: utf-8 -*-
'''
Unittests for Publish
'''
import unittest
import os
import shutil
import base64
from Publish import Publish
SRC_DIR = 'test_src_folder'
IMG_DATA = b'iVBORw0KGgoAAAANSUhEUgAAAUAAAAFAAgMAAACw/k05AAAACXBIWXMAAA7DAAAOwwHHb6hkAAAAGXRFWHRTb2Z0d2FyZQB3d3cuaW5rc2NhcGUub3Jnm+48GgAAAAxQTFRFAAAAHBwcVFRU////6irJIAAAAIVJREFUeNrt3TERACAQBLHTgQlMU6GQDkz8MF9kBcTCJmrY2IWtJPMWdoBAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAoFAIBAIBAKBQCAQCAQCgUAgEAgEAv+A5RMHtesBZRvjTSF8ofkAAAAASUVORK5CYII='
PIC_NAME = 'test_picture.png'
DST_DIR = 'test_dst_folder'
DST_PARENT_DIR = 'test_parent'
DST_PATH = os.path.join(DST_PARENT_DIR, DST_DIR)
class TestPublish(unittest.TestCase):
def setUp(self):
os.makedirs(SRC_DIR, exist_ok=True)
os.makedirs(DST_PARENT_DIR, exist_ok=True)
with open(PIC_NAME, "wb") as f:
f.write(base64.decodebytes(IMG_DATA))
def test_publish(self):
Publish(SRC_DIR, DST_PATH, PIC_NAME, 'individual')
def test_src_directory(self):
with self.assertRaises(FileNotFoundError):
Publish(SRC_DIR, 'not_existing_folder', PIC_NAME, 'individual')
def tearDown(self):
shutil.rmtree(SRC_DIR)
shutil.rmtree(DST_PARENT_DIR)
os.remove(PIC_NAME)
if __name__ == '__main__':
unittest.main()
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment