def test_Component_write_to_file_simple(self, mock_f): """ Testing that a Component can be written to file with the expected output. Here with simple input. """ comp = Component("test_component", "Arm") comp._unfreeze() # Need to set up attribute parameters # Also need to categorize them as when created comp.parameter_names = [] comp.parameter_defaults = {} comp.parameter_types = {} comp._freeze() with mock_f('test.txt', 'w') as m_fo: comp.write_component(m_fo) my_call = unittest.mock.call expected_writes = [ my_call("COMPONENT test_component = Arm("), my_call(")\n"), my_call("AT (0,0,0)"), my_call(" ABSOLUTE\n") ] mock_f.assert_called_with('test.txt', 'w') handle = mock_f() handle.write.assert_has_calls(expected_writes, any_order=False)
def test_component_basic_init_set_parameters(self): """ Testing set_parameters method. Need to set some attribute parameters manually to test this. """ comp = Component("test_component", "Arm") # Need to add some parameters to this bare component # Parameters are usually added by McStas_Instr comp._unfreeze() comp.new_par1 = 1 comp.new_par2 = 3 comp.this_par = 1492.2 comp.set_parameters({ "new_par1": 37.0, "new_par2": 12.0, "this_par": 1 }) self.assertEqual(comp.name, "test_component") self.assertEqual(comp.component_name, "Arm") self.assertEqual(comp.new_par1, 37.0) self.assertEqual(comp.new_par2, 12.0) self.assertEqual(comp.this_par, 1) with self.assertRaises(NameError): comp.set_parameters({"new_par3": 37.0})
def test_Component_basic_new_attribute_error(self): """ The Component class is frozen after initialize in order to prevent the user accidentilly misspelling an attribute name, or at least be able to report an error when they do so. """ comp = Component("test_component", "Arm") with self.assertRaises(AttributeError): comp.new_attribute = 1 # If unfreeze does not work, this would cause an error comp._unfreeze() comp.new_attribute = 1
def test_Component_freeze(self): """ Testing frozen Component cant have new attributes, and that _unfreeze / _freeze works correctly. """ comp = Component("test_component", "Arm") with self.assertRaises(AttributeError): comp.new_parameter = 5 self.assertEqual(comp.name, "test_component") self.assertEqual(comp.component_name, "Arm") comp._unfreeze() comp.new_parameter = 5 self.assertEqual(comp.new_parameter, 5) comp._freeze() with self.assertRaises(AttributeError): comp.another_parameter = 5