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

Add more unit tests for routines in common.py #465

Merged
merged 2 commits into from
Apr 10, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
52 changes: 52 additions & 0 deletions test/unit_tests/test_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@
import unittest

TEST_DIR = os.path.dirname(os.path.abspath(__file__))
TEST_FILE = os.path.basename(os.path.abspath(__file__))
SCRIPTS_DIR = os.path.abspath(os.path.join(TEST_DIR, os.pardir, os.pardir, "scripts"))
SAMPLE_FILES_DIR = os.path.join(TEST_DIR, "sample_files")

if not os.path.exists(SCRIPTS_DIR):
raise ImportError("Cannot find scripts directory")

sys.path.append(SCRIPTS_DIR)
logging.disable(logging.CRITICAL)
mkavulich marked this conversation as resolved.
Show resolved Hide resolved

# pylint: disable=wrong-import-position
import common
Expand All @@ -31,6 +34,55 @@ class CommonTestCase(unittest.TestCase):

"""Tests functionality of functions in common.py"""

def test_execute(self):
"""Test execute() function"""

# Input for successful test: ls command on this file
self.assertEqual(common.execute(f"ls {TEST_FILE}"),(0,f"{TEST_FILE}",""))

# Input for failing test (no exception): exit 1 from a subshell
self.assertEqual(common.execute(f"(exit 1)",abort=False),(1,"",f""))

# Input for failing test (raise exception): exit 1 from a subshell
self.assertRaises(Exception,common.execute,f"(exit 1)",abort=True)

def test_split_var_name_and_array_reference(self):
"""Test split_var_name_and_array_reference() function"""

self.assertEqual(common.split_var_name_and_array_reference("foo(:,a,1:ddt%ngas)"),
("foo","(:,a,1:ddt%ngas)"))

def test_encode_container(self):
"""Test encode_container() function"""

modulename = "ABCD1234"
typename = "COMPLEX"
schemename = "testscheme"
subroutinename = "testsubroutine"
self.assertEqual(common.encode_container(modulename),f"MODULE_{modulename}")
self.assertEqual(common.encode_container(modulename,typename),f"MODULE_{modulename} TYPE_{typename}")
self.assertEqual(common.encode_container(modulename,schemename,subroutinename),
f"MODULE_{modulename} SCHEME_{schemename} SUBROUTINE_{subroutinename}")
self.assertRaises(Exception,common.encode_container,modulename,typename,schemename,subroutinename)
self.assertRaises(Exception,common.encode_container)

def test_decode_container(self):
"""Test decode_container() function"""

modulename = "ABCD1234"
typename = "COMPLEX"
schemename = "testscheme"
subroutinename = "testsubroutine"
self.assertEqual(common.decode_container(f"MODULE_{modulename}"),f"MODULE {modulename}")
self.assertEqual(common.decode_container(f"MODULE_{modulename} TYPE_{typename}"),
f"MODULE {modulename} TYPE {typename}")
self.assertEqual(common.decode_container(f"MODULE_{modulename} SCHEME_{schemename} SUBROUTINE_{subroutinename}"),
f"MODULE {modulename} SCHEME {schemename} SUBROUTINE {subroutinename}")
self.assertRaises(Exception,common.decode_container,
f"MODULE_{modulename} TYPE_{typename} SCHEME_{schemename} SUBROUTINE_{subroutinename}")
self.assertRaises(Exception,common.decode_container,"That dog won't hunt, Monsignor")
self.assertRaises(Exception,common.decode_container)

def test_string_to_python_identifier(self):
"""Test string_to_python_identifier() function"""

Expand Down
2 changes: 1 addition & 1 deletion test/unit_tests/test_var_transforms.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@

Command line arguments: none

Usage: python test_var_transform.py # run the unit tests
Usage: python test_var_transforms.py # run the unit tests
-----------------------------------------------------------------------
"""
import sys
Expand Down