Skip to content
Snippets Groups Projects
Commit 1696901c authored by dh00601's avatar dh00601
Browse files

working on the tests

parent caa2bc58
No related branches found
No related tags found
No related merge requests found
......@@ -47,7 +47,15 @@ from binarycpython.tests.test_functions import (
test_write_binary_c_parameter_descriptions_to_rst_file,
)
# from binarycpython.tests.test_grid import *
from binarycpython.tests.test_grid import (
test__setup,
test_set,
test_cmdline,
test__return_argline,
test_return_population_settings,
test_return_binary_c_defaults,
test_return_all_info
)
from binarycpython.tests.test_plot_functions import (
test_color_by_index,
test_plot_system,
......@@ -69,6 +77,11 @@ from binarycpython.tests.tests_population_extensions.test__version_info import (
test_return_binary_c_version_info,
test_parse_binary_c_version_info
)
from binarycpython.tests.tests_population_extensions.test__gridcode import (
test_add_grid_variable,
)
from binarycpython.tests.test_stellar_types import *
from binarycpython.tests.test_useful_funcs import (
......
......@@ -3,12 +3,6 @@ Unit tests for the grid module
TODO: jobID
TODO: exit
TODO: set
TODO: parse_cmdline
TODO: _return_argline
TODO: return_population_settings
TODO: return_binary_c_defaults
TODO: return_all_info
TODO: export_all_info
TODO: _set_nprocesses
TODO: _pre_run_setup
......@@ -22,7 +16,6 @@ TODO: _parent_signal_handler
TODO: _child_signal_handler
TODO: _process_run_population_grid
TODO: evolve_single
TODO: _setup
TODO: _cleanup
TODO: _dry_run
TODO: _dry_run_source_file
......@@ -34,3 +27,233 @@ TODO: _increment_count
TODO: was_killed
TODO: _check_binary_c_error
"""
import unittest
from binarycpython.utils.functions import (
temp_dir,
Capturing,
)
from binarycpython.utils.grid import Population
TMP_DIR = temp_dir("tests", "test_grid")
TEST_VERBOSITY = 1
class test__setup(unittest.TestCase):
"""
Unittests for _setup function
"""
def test_setup(self):
with Capturing() as output:
self._test_setup()
def _test_setup(self):
"""
Unittests for function _setup
"""
test_pop = Population()
self.assertTrue("orbital_period" in test_pop.defaults)
self.assertTrue("metallicity" in test_pop.defaults)
self.assertNotIn("help_all", test_pop.cleaned_up_defaults)
self.assertEqual(test_pop.bse_options, {})
self.assertEqual(test_pop.custom_options, {})
self.assertEqual(test_pop.argline_dict, {})
self.assertEqual(test_pop.persistent_data_memory_dict, {})
self.assertTrue(test_pop.grid_options["parse_function"] == None)
self.assertTrue(isinstance(test_pop.grid_options["_main_pid"], int))
class test_set(unittest.TestCase):
"""
Unittests for _setup function
"""
def test_set(self):
with Capturing() as output:
self._test_set()
def _test_set(self):
"""
Unittests for function set
"""
test_pop = Population()
test_pop.set(num_cores=2, verbosity=TEST_VERBOSITY)
test_pop.set(M_1=10)
test_pop.set(data_dir="/tmp/binary_c_python")
test_pop.set(ensemble_filter_SUPERNOVAE=1, ensemble_dt=1000)
self.assertIn("data_dir", test_pop.custom_options)
self.assertEqual(test_pop.custom_options["data_dir"], "/tmp/binary_c_python")
#
self.assertTrue(test_pop.bse_options["M_1"] == 10)
self.assertTrue(test_pop.bse_options["ensemble_filter_SUPERNOVAE"] == 1)
#
self.assertTrue(test_pop.grid_options["num_cores"] == 2)
class test_cmdline(unittest.TestCase):
"""
Unittests for cmdline function
"""
def test_cmdline(self):
with Capturing() as output:
self._test_cmdline()
def _test_cmdline(self):
"""
Unittests for function parse_cmdline
"""
# copy old sys.argv values
prev_sysargv = sys.argv.copy()
# make a dummy cmdline arg input
sys.argv = [
"script",
"metallicity=0.0002",
"num_cores=2",
"data_dir=/tmp/binary_c_python",
]
# Set up population
test_pop = Population()
test_pop.set(data_dir="/tmp", verbosity=TEST_VERBOSITY)
# parse arguments
test_pop.parse_cmdline()
# metallicity
self.assertTrue(isinstance(test_pop.bse_options["metallicity"], str))
self.assertTrue(test_pop.bse_options["metallicity"] == "0.0002")
# Amt cores
self.assertTrue(isinstance(test_pop.grid_options["num_cores"], int))
self.assertTrue(test_pop.grid_options["num_cores"] == 2)
# datadir
self.assertTrue(isinstance(test_pop.custom_options["data_dir"], str))
self.assertTrue(test_pop.custom_options["data_dir"] == "/tmp/binary_c_python")
# put back the other args if they exist
sys.argv = prev_sysargv.copy()
class test__return_argline(unittest.TestCase):
"""
Unittests for _return_argline function
"""
def test__return_argline(self):
with Capturing() as output:
self._test__return_argline()
def _test__return_argline(self):
"""
Unittests for the function _return_argline
"""
# Set up population
test_pop = Population()
test_pop.set(metallicity=0.02, verbosity=TEST_VERBOSITY)
test_pop.set(M_1=10)
argline = test_pop._return_argline()
self.assertTrue(argline == "binary_c M_1 10 metallicity 0.02")
# custom dict
argline2 = test_pop._return_argline(
{"example_parameter1": 10, "example_parameter2": "hello"}
)
self.assertTrue(
argline2 == "binary_c example_parameter1 10 example_parameter2 hello"
)
class test_return_population_settings(unittest.TestCase):
"""
Unittests for return_population_settings function
"""
def test_return_population_settings(self):
with Capturing() as output:
self._test_return_population_settings()
def _test_return_population_settings(self):
"""
Unittests for the function return_population_settings
"""
test_pop = Population()
test_pop.set(metallicity=0.02, verbosity=TEST_VERBOSITY)
test_pop.set(M_1=10)
test_pop.set(num_cores=2)
test_pop.set(data_dir="/tmp")
population_settings = test_pop.return_population_settings()
self.assertIn("bse_options", population_settings)
self.assertTrue(population_settings["bse_options"]["metallicity"] == 0.02)
self.assertTrue(population_settings["bse_options"]["M_1"] == 10)
self.assertIn("grid_options", population_settings)
self.assertTrue(population_settings["grid_options"]["num_cores"] == 2)
self.assertIn("custom_options", population_settings)
self.assertTrue(population_settings["custom_options"]["data_dir"] == "/tmp")
class test_return_binary_c_defaults(unittest.TestCase):
"""
Unittests for return_binary_c_defaults function
"""
def test_return_binary_c_defaults(self):
with Capturing() as output:
self._test_return_binary_c_defaults()
def _test_return_binary_c_defaults(self):
"""
Unittests for the function return_binary_c_defaults
"""
test_pop = Population()
binary_c_defaults = test_pop.return_binary_c_defaults()
self.assertIn("probability", binary_c_defaults)
self.assertIn("phasevol", binary_c_defaults)
self.assertIn("metallicity", binary_c_defaults)
class test_return_all_info(unittest.TestCase):
"""
Unittests for return_all_info function
"""
def test_return_all_info(self):
with Capturing() as output:
self._test_return_all_info()
def _test_return_all_info(self):
"""
Unittests for the function return_all_info
Not going to do too much tests here, just check if they are not empty
"""
test_pop = Population()
all_info = test_pop.return_all_info()
self.assertIn("population_settings", all_info)
self.assertIn("binary_c_defaults", all_info)
self.assertIn("binary_c_version_info", all_info)
self.assertIn("binary_c_help_all", all_info)
self.assertNotEqual(all_info["population_settings"], {})
self.assertNotEqual(all_info["binary_c_defaults"], {})
self.assertNotEqual(all_info["binary_c_version_info"], {})
self.assertNotEqual(all_info["binary_c_help_all"], {})
\ No newline at end of file
......@@ -12,5 +12,63 @@ TODO: _last_grid_variable
TODO: update_grid_variable
TODO: delete_grid_variable
TODO: rename_grid_variable
TODO: add_grid_variable
"""
\ No newline at end of file
"""
import unittest
from binarycpython.utils.functions import (
temp_dir,
Capturing,
)
from binarycpython.utils.grid import Population
class test_add_grid_variable(unittest.TestCase):
"""
Unittests for add_grid_variable function
"""
def test_add_grid_variable(self):
with Capturing() as output:
self._test_add_grid_variable()
def _test_add_grid_variable(self):
"""
Unittests for the function add_grid_variable
TODO: Should I test more here?
"""
test_pop = Population()
resolution = {"M_1": 10, "q": 10}
test_pop.add_grid_variable(
name="lnm1",
longname="Primary mass",
valuerange=[1, 100],
samplerfunc="const(math.log(1), math.log(100), {})".format(
resolution["M_1"]
),
precode="M_1=math.exp(lnm1)",
probdist="three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 100, -1.3, -2.3, -2.3)*M_1",
dphasevol="dlnm1",
parameter_name="M_1",
condition="", # Impose a condition on this grid variable. Mostly for a check for yourself
)
test_pop.add_grid_variable(
name="q",
longname="Mass ratio",
valuerange=["0.1/M_1", 1],
samplerfunc="const(0.1/M_1, 1, {})".format(resolution["q"]),
probdist="flatsections(q, [{'min': 0.1/M_1, 'max': 1.0, 'height': 1}])",
dphasevol="dq",
precode="M_2 = q * M_1",
parameter_name="M_2",
condition="", # Impose a condition on this grid variable. Mostly for a check for yourself
)
self.assertIn("q", test_pop.grid_options["_grid_variables"])
self.assertIn("lnm1", test_pop.grid_options["_grid_variables"])
self.assertEqual(len(test_pop.grid_options["_grid_variables"]), 2)
"""
Unit tests for the _version_info Population extension module
TODO: return_binary_c_version_info
TODO: parse_binary_c_version_info
TODO: minimum_stellar_mass
"""
......@@ -30,6 +28,39 @@ class test_return_binary_c_version_info(unittest.TestCase):
super(test_return_binary_c_version_info, self).__init__(*args, **kwargs)
self._version_info_pop = Population()
def test_return_binary_c_version_info(self):
with Capturing() as output:
self._test_return_binary_c_version_info()
def _test_return_binary_c_version_info(self):
"""
Unittests for the function return_binary_c_version_info
"""
test_pop = Population()
binary_c_version_info = test_pop.return_binary_c_version_info(parsed=True)
self.assertTrue(isinstance(binary_c_version_info, dict))
self.assertIn("isotopes", binary_c_version_info)
self.assertIn("argpairs", binary_c_version_info)
self.assertIn("ensembles", binary_c_version_info)
self.assertIn("macros", binary_c_version_info)
self.assertIn("dt_limits", binary_c_version_info)
self.assertIn("nucleosynthesis_sources", binary_c_version_info)
self.assertIn("miscellaneous", binary_c_version_info)
self.assertIsNotNone(binary_c_version_info["argpairs"])
self.assertIsNotNone(binary_c_version_info["ensembles"])
self.assertIsNotNone(binary_c_version_info["macros"])
self.assertIsNotNone(binary_c_version_info["dt_limits"])
self.assertIsNotNone(binary_c_version_info["miscellaneous"])
if binary_c_version_info["macros"]["NUCSYN"] == "on":
self.assertIsNotNone(binary_c_version_info["isotopes"])
if binary_c_version_info["macros"]["NUCSYN_ID_SOURCES"] == "on":
self.assertIsNotNone(binary_c_version_info["nucleosynthesis_sources"])
def test_not_parsed(self):
with Capturing() as output:
self._test_not_parsed()
......
......@@ -29,9 +29,6 @@ from binarycpython.utils.dicts import (
from binarycpython.utils.custom_logging_functions import binary_c_log_code
TMP_DIR = temp_dir("tests", "test_grid")
TEST_VERBOSITY = 1
def parse_function_test_grid_evolve_2_threads_with_custom_logging(self, output):
"""
......@@ -60,280 +57,11 @@ def parse_function_test_grid_evolve_2_threads_with_custom_logging(self, output):
first_f.write(output + "\n")
# class test_(unittest.TestCase):
# """
# Unittests for function
# """
# def test_1(self):
# pass
# def test_(self):
# """
# Unittests for the function
# """
class test_Population(unittest.TestCase):
"""
Unittests for function
"""
def test_setup(self):
with Capturing() as output:
self._test_setup()
def _test_setup(self):
"""
Unittests for function _setup
"""
test_pop = Population()
self.assertTrue("orbital_period" in test_pop.defaults)
self.assertTrue("metallicity" in test_pop.defaults)
self.assertNotIn("help_all", test_pop.cleaned_up_defaults)
self.assertEqual(test_pop.bse_options, {})
self.assertEqual(test_pop.custom_options, {})
self.assertEqual(test_pop.argline_dict, {})
self.assertEqual(test_pop.persistent_data_memory_dict, {})
self.assertTrue(test_pop.grid_options["parse_function"] == None)
self.assertTrue(isinstance(test_pop.grid_options["_main_pid"], int))
def test_set(self):
with Capturing() as output:
self._test_set()
def _test_set(self):
"""
Unittests for function set
"""
test_pop = Population()
test_pop.set(num_cores=2, verbosity=TEST_VERBOSITY)
test_pop.set(M_1=10)
test_pop.set(data_dir="/tmp/binary_c_python")
test_pop.set(ensemble_filter_SUPERNOVAE=1, ensemble_dt=1000)
self.assertIn("data_dir", test_pop.custom_options)
self.assertEqual(test_pop.custom_options["data_dir"], "/tmp/binary_c_python")
#
self.assertTrue(test_pop.bse_options["M_1"] == 10)
self.assertTrue(test_pop.bse_options["ensemble_filter_SUPERNOVAE"] == 1)
#
self.assertTrue(test_pop.grid_options["num_cores"] == 2)
def test_cmdline(self):
with Capturing() as output:
self._test_cmdline()
def _test_cmdline(self):
"""
Unittests for function parse_cmdline
"""
# copy old sys.argv values
prev_sysargv = sys.argv.copy()
# make a dummy cmdline arg input
sys.argv = [
"script",
"metallicity=0.0002",
"num_cores=2",
"data_dir=/tmp/binary_c_python",
]
# Set up population
test_pop = Population()
test_pop.set(data_dir="/tmp", verbosity=TEST_VERBOSITY)
# parse arguments
test_pop.parse_cmdline()
# metallicity
self.assertTrue(isinstance(test_pop.bse_options["metallicity"], str))
self.assertTrue(test_pop.bse_options["metallicity"] == "0.0002")
# Amt cores
self.assertTrue(isinstance(test_pop.grid_options["num_cores"], int))
self.assertTrue(test_pop.grid_options["num_cores"] == 2)
# datadir
self.assertTrue(isinstance(test_pop.custom_options["data_dir"], str))
self.assertTrue(test_pop.custom_options["data_dir"] == "/tmp/binary_c_python")
# put back the other args if they exist
sys.argv = prev_sysargv.copy()
def test__return_argline(self):
with Capturing() as output:
self._test__return_argline()
def _test__return_argline(self):
"""
Unittests for the function _return_argline
"""
# Set up population
test_pop = Population()
test_pop.set(metallicity=0.02, verbosity=TEST_VERBOSITY)
test_pop.set(M_1=10)
argline = test_pop._return_argline()
self.assertTrue(argline == "binary_c M_1 10 metallicity 0.02")
# custom dict
argline2 = test_pop._return_argline(
{"example_parameter1": 10, "example_parameter2": "hello"}
)
self.assertTrue(
argline2 == "binary_c example_parameter1 10 example_parameter2 hello"
)
def test_add_grid_variable(self):
with Capturing() as output:
self._test_add_grid_variable()
def _test_add_grid_variable(self):
"""
Unittests for the function add_grid_variable
TODO: Should I test more here?
"""
test_pop = Population()
resolution = {"M_1": 10, "q": 10}
test_pop.add_grid_variable(
name="lnm1",
longname="Primary mass",
valuerange=[1, 100],
samplerfunc="const(math.log(1), math.log(100), {})".format(
resolution["M_1"]
),
precode="M_1=math.exp(lnm1)",
probdist="three_part_powerlaw(M_1, 0.1, 0.5, 1.0, 100, -1.3, -2.3, -2.3)*M_1",
dphasevol="dlnm1",
parameter_name="M_1",
condition="", # Impose a condition on this grid variable. Mostly for a check for yourself
)
test_pop.add_grid_variable(
name="q",
longname="Mass ratio",
valuerange=["0.1/M_1", 1],
samplerfunc="const(0.1/M_1, 1, {})".format(resolution["q"]),
probdist="flatsections(q, [{'min': 0.1/M_1, 'max': 1.0, 'height': 1}])",
dphasevol="dq",
precode="M_2 = q * M_1",
parameter_name="M_2",
condition="", # Impose a condition on this grid variable. Mostly for a check for yourself
)
self.assertIn("q", test_pop.grid_options["_grid_variables"])
self.assertIn("lnm1", test_pop.grid_options["_grid_variables"])
self.assertEqual(len(test_pop.grid_options["_grid_variables"]), 2)
def test_return_population_settings(self):
with Capturing() as output:
self._test_return_population_settings()
def _test_return_population_settings(self):
"""
Unittests for the function return_population_settings
"""
test_pop = Population()
test_pop.set(metallicity=0.02, verbosity=TEST_VERBOSITY)
test_pop.set(M_1=10)
test_pop.set(num_cores=2)
test_pop.set(data_dir="/tmp")
population_settings = test_pop.return_population_settings()
self.assertIn("bse_options", population_settings)
self.assertTrue(population_settings["bse_options"]["metallicity"] == 0.02)
self.assertTrue(population_settings["bse_options"]["M_1"] == 10)
self.assertIn("grid_options", population_settings)
self.assertTrue(population_settings["grid_options"]["num_cores"] == 2)
self.assertIn("custom_options", population_settings)
self.assertTrue(population_settings["custom_options"]["data_dir"] == "/tmp")
def test_return_binary_c_version_info(self):
with Capturing() as output:
self._test_return_binary_c_version_info()
def _test_return_binary_c_version_info(self):
"""
Unittests for the function return_binary_c_version_info
"""
test_pop = Population()
binary_c_version_info = test_pop.return_binary_c_version_info(parsed=True)
self.assertTrue(isinstance(binary_c_version_info, dict))
self.assertIn("isotopes", binary_c_version_info)
self.assertIn("argpairs", binary_c_version_info)
self.assertIn("ensembles", binary_c_version_info)
self.assertIn("macros", binary_c_version_info)
self.assertIn("dt_limits", binary_c_version_info)
self.assertIn("nucleosynthesis_sources", binary_c_version_info)
self.assertIn("miscellaneous", binary_c_version_info)
self.assertIsNotNone(binary_c_version_info["argpairs"])
self.assertIsNotNone(binary_c_version_info["ensembles"])
self.assertIsNotNone(binary_c_version_info["macros"])
self.assertIsNotNone(binary_c_version_info["dt_limits"])
self.assertIsNotNone(binary_c_version_info["miscellaneous"])
if binary_c_version_info["macros"]["NUCSYN"] == "on":
self.assertIsNotNone(binary_c_version_info["isotopes"])
if binary_c_version_info["macros"]["NUCSYN_ID_SOURCES"] == "on":
self.assertIsNotNone(binary_c_version_info["nucleosynthesis_sources"])
def test_return_binary_c_defaults(self):
with Capturing() as output:
self._test_return_binary_c_defaults()
def _test_return_binary_c_defaults(self):
"""
Unittests for the function return_binary_c_defaults
"""
test_pop = Population()
binary_c_defaults = test_pop.return_binary_c_defaults()
self.assertIn("probability", binary_c_defaults)
self.assertIn("phasevol", binary_c_defaults)
self.assertIn("metallicity", binary_c_defaults)
def test_return_all_info(self):
with Capturing() as output:
self._test_return_all_info()
def _test_return_all_info(self):
"""
Unittests for the function return_all_info
Not going to do too much tests here, just check if they are not empty
"""
test_pop = Population()
all_info = test_pop.return_all_info()
self.assertIn("population_settings", all_info)
self.assertIn("binary_c_defaults", all_info)
self.assertIn("binary_c_version_info", all_info)
self.assertIn("binary_c_help_all", all_info)
self.assertNotEqual(all_info["population_settings"], {})
self.assertNotEqual(all_info["binary_c_defaults"], {})
self.assertNotEqual(all_info["binary_c_version_info"], {})
self.assertNotEqual(all_info["binary_c_help_all"], {})
def test_export_all_info(self):
with Capturing() as output:
self._test_export_all_info()
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment