1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196
|
import os
import unittest
from mcstasscript.interface.functions import Configurator
def setup_expected_file(test_name):
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
expected_file = os.path.join(THIS_DIR, "..", test_name + ".yaml")
if os.path.isfile(expected_file):
os.remove(expected_file)
return expected_file
def setup_configurator(test_name):
setup_expected_file(test_name)
return Configurator(test_name)
class TestConfigurator(unittest.TestCase):
"""
Tests for configurator class that handles yaml configuration file
"""
def test_simple_initialize(self):
"""
Tests that initialization happens, new configuration file should be
written.
"""
test_name = "test_configuration"
expected_file = setup_expected_file(test_name)
# check the file did not exist before testing
self.assertFalse(os.path.isfile(expected_file))
# initialize the configurator
Configurator(test_name)
# check a new configuration file was made
self.assertTrue(os.path.isfile(expected_file))
# remove the testing configuration file
if os.path.isfile(expected_file):
os.remove(expected_file)
def test_default_config(self):
"""
This tests confirms the content of the default configuration file
"""
test_name = "test_configuration"
expected_file = setup_expected_file(test_name)
# check the file did not exist before testing
self.assertFalse(os.path.isfile(expected_file))
my_configurator = Configurator(test_name)
default_config = my_configurator._read_yaml()
run = "/usr/bin"
mcstas = "/usr/share/mcstas/resources"
mxrun = "/usr/bin"
mcxtrace = "/usr/share/mcxtrace/resources"
self.assertEqual(default_config["paths"]["mcrun_path"], run)
self.assertEqual(default_config["paths"]["mcstas_path"], mcstas)
self.assertEqual(default_config["paths"]["mxrun_path"], mxrun)
self.assertEqual(default_config["paths"]["mcxtrace_path"], mcxtrace)
self.assertEqual(default_config["other"]["characters_per_line"], 85)
# remove the testing configuration file
if os.path.isfile(expected_file):
os.remove(expected_file)
def test_yaml_write(self):
"""
This test checks that writing to the configuration file works
"""
test_name = "test_configuration"
my_configurator = setup_configurator(test_name)
config = my_configurator._read_yaml()
config["new_field"] = 123
config["paths"]["new_path"] = "/test/path/"
my_configurator._write_yaml(config)
new_config = my_configurator._read_yaml()
self.assertEqual(new_config["other"]["characters_per_line"], 85)
self.assertEqual(new_config["new_field"], 123)
self.assertEqual(new_config["paths"]["new_path"], "/test/path/")
# remove the testing configuration file
setup_expected_file(test_name)
def test_set_mcrun_path(self):
"""
This test checks that setting the mcrun path works
"""
test_name = "test_configuration"
my_configurator = setup_configurator(test_name)
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
dummy_mcstas_path = os.path.join(THIS_DIR, "dummy_mcstas")
my_configurator.set_mcrun_path(dummy_mcstas_path)
new_config = my_configurator._read_yaml()
self.assertEqual(new_config["paths"]["mcrun_path"], dummy_mcstas_path)
# remove the testing configuration file
setup_expected_file(test_name)
def test_set_mcstas_path(self):
"""
This test checks that setting the mcstas path works
"""
test_name = "test_configuration"
my_configurator = setup_configurator(test_name)
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
dummy_mcstas_path = os.path.join(THIS_DIR, "dummy_mcstas")
my_configurator.set_mcstas_path(dummy_mcstas_path)
new_config = my_configurator._read_yaml()
self.assertEqual(new_config["paths"]["mcstas_path"], dummy_mcstas_path)
# remove the testing configuration file
setup_expected_file(test_name)
def test_set_mxrun_path(self):
"""
This test checks that setting the mxrun path works
"""
test_name = "test_configuration"
my_configurator = setup_configurator(test_name)
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
dummy_mcstas_path = os.path.join(THIS_DIR, "dummy_mcstas")
my_configurator.set_mxrun_path(dummy_mcstas_path)
new_config = my_configurator._read_yaml()
self.assertEqual(new_config["paths"]["mxrun_path"], dummy_mcstas_path)
# remove the testing configuration file
setup_expected_file(test_name)
def test_set_mcxtrace_path(self):
"""
This test checks that setting the mcxtrace path works
"""
test_name = "test_configuration"
my_configurator = setup_configurator(test_name)
THIS_DIR = os.path.dirname(os.path.abspath(__file__))
dummy_mcstas_path = os.path.join(THIS_DIR, "dummy_mcstas")
my_configurator.set_mcxtrace_path(dummy_mcstas_path)
new_config = my_configurator._read_yaml()
self.assertEqual(new_config["paths"]["mcxtrace_path"], dummy_mcstas_path)
# remove the testing configuration file
setup_expected_file(test_name)
def test_set_line_length(self):
"""
This test checks that setting the line length works
"""
test_name = "test_configuration"
my_configurator = setup_configurator(test_name)
my_configurator.set_line_length(123)
new_config = my_configurator._read_yaml()
self.assertEqual(new_config["other"]["characters_per_line"], 123)
# remove the testing configuration file
setup_expected_file(test_name)
if __name__ == "__main__":
unittest.main()
|