Select Git revision
run_render_depth.py
-
Brian Wang authoredBrian Wang authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
test_publish.py 4.88 KiB
# -*- coding: utf-8 -*-
"""
Unittests for publish
"""
import unittest
import os
import sys
import shutil
from unittest.mock import patch
import matplotlib.pyplot as plt
from src.plotid.publish import publish
SRC_DIR = 'test_src_folder'
PIC_NAME = 'test_picture'
DST_DIR = 'test_dst_folder'
DST_PARENT_DIR = 'test_parent'
DST_PATH = os.path.join(DST_PARENT_DIR, DST_DIR)
INVISIBLE_PATH = os.path.join(DST_PARENT_DIR, '.' + DST_DIR)
fig = plt.figure()
class TestPublish(unittest.TestCase):
"""
Class for all unittests of the publish module.
"""
def setUp(self):
""" Generate source and destination directory and test image. """
os.makedirs(SRC_DIR, exist_ok=True)
os.makedirs(DST_PARENT_DIR, exist_ok=True)
# Skip test if tests are run from command line.
@unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called '
'from a Python script. Therefore, the script cannot be '
'copied.')
def test_publish(self):
""" Test publish and check if an exported picture file exists. """
publish(SRC_DIR, DST_PATH, fig, PIC_NAME, 'individual')
assert os.path.isfile(os.path.join(DST_PATH, PIC_NAME + '.png'))
def test_src_directory(self):
""" Test if Error is raised when source directory does not exist."""
with self.assertRaises(FileNotFoundError):
publish('not_existing_folder', DST_PATH, fig,
PIC_NAME, 'individual')
def test_dst_directory(self):
""" Test if Error is raised when destination dir does not exist."""
with self.assertRaises(FileNotFoundError):
publish(SRC_DIR, 'not_existing_folder',
fig, PIC_NAME, 'individual')
# Skip test if tests are run from command line.
@unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called '
'from a Python script. Therefore, the script cannot be '
'copied.')
def test_dst_already_exists_yes(self):
"""
Test if publish succeeds if the user wants to overwrite an existing
destination directory.
"""
os.mkdir(DST_PATH)
# Mock user input as 'yes'
with patch('builtins.input', return_value='yes'):
publish(SRC_DIR, DST_PATH, fig, PIC_NAME, 'individual')
# Skip test if tests are run from command line.
@unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called '
'from a Python script. Therefore, the script cannot be '
'copied.')
def test_dst_already_exists_no(self):
"""
Test if publish exits with error if the user does not want to overwrite
an existing destination directory by user input 'no'.
"""
os.mkdir(DST_PATH)
# Mock user input as 'no'
with patch('builtins.input', return_value='no'):
with self.assertRaises(RuntimeError):
publish(SRC_DIR, DST_PATH, fig, PIC_NAME, 'individual')
# Skip test if tests are run from command line.
@unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called '
'from a Python script. Therefore, the script cannot be '
'copied.')
def test_dst_already_exists_empty(self):
"""
Test if publish exits with error if the user does not want to overwrite
an existing destination directory by missing user input.
"""
os.mkdir(DST_PATH)
# Mock user input as empty (no should be default).
with patch('builtins.input', return_value=''):
with self.assertRaises(RuntimeError):
publish(SRC_DIR, DST_PATH, fig, PIC_NAME, 'individual')
# Skip test if tests are run from command line.
@unittest.skipIf(not os.path.isfile(sys.argv[0]), 'Publish is not called '
'from a Python script. Therefore, the script cannot be '
'copied.')
def test_dst__invisible_already_exists(self):
"""
Test if publish succeeds when there is already an invisible
directory from a previous run (delete the folder and proceed).
"""
os.mkdir(INVISIBLE_PATH)
publish(SRC_DIR, DST_PATH, fig, PIC_NAME, 'individual')
def test_picture(self):
""" Test if Error is raised if fig is not a valid figure object. """
with self.assertRaises(TypeError):
publish(SRC_DIR, DST_PATH, 'fig', PIC_NAME, 'individual')
def test_data_storage(self):
"""
Test if Error is raised when unsupported storage method was chosen.
"""
with self.assertRaises(ValueError):
publish(SRC_DIR, DST_PATH, fig, PIC_NAME, 'none_existing_method')
def tearDown(self):
""" Delete all files created in setUp. """
shutil.rmtree(SRC_DIR)
shutil.rmtree(DST_PARENT_DIR)
if __name__ == '__main__':
unittest.main()