def setUp(self): unittest.TestCase.setUp(self) self.h = InclusionXMLHandler() self.obj = Inclusion(Material.pure(29), Material.pure(30), 123.456, 1.1, 2.2) etree.register_namespace('mc', 'http://pymontecarlo.sf.net') source = BytesIO(b'<mc:inclusion xmlns:mc="http://pymontecarlo.sf.net" rotation="2.2" tilt="1.1"><materials><mc:material _index="1" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material><mc:material _index="2" density="8960.0" name="Copper"><composition><element weightFraction="1.0" z="29" /></composition></mc:material></materials><substrate material="2" /><inclusion diameter="123.456" material="1" /></mc:inclusion>') self.element = etree.parse(source).getroot()
class TestInclusionXMLHandler(unittest.TestCase): def setUp(self): unittest.TestCase.setUp(self) self.h = InclusionXMLHandler() self.obj = Inclusion(Material.pure(29), Material.pure(30), 123.456, 1.1, 2.2) etree.register_namespace('mc', 'http://pymontecarlo.sf.net') source = BytesIO(b'<mc:inclusion xmlns:mc="http://pymontecarlo.sf.net" rotation="2.2" tilt="1.1"><materials><mc:material _index="1" density="7140.0" name="Zinc"><composition><element weightFraction="1.0" z="30" /></composition></mc:material><mc:material _index="2" density="8960.0" name="Copper"><composition><element weightFraction="1.0" z="29" /></composition></mc:material></materials><substrate material="2" /><inclusion diameter="123.456" material="1" /></mc:inclusion>') self.element = etree.parse(source).getroot() def tearDown(self): unittest.TestCase.tearDown(self) def testcan_parse(self): self.assertTrue(self.h.can_parse(self.element)) def testparse(self): obj = self.h.parse(self.element) self.assertEqual('Copper', str(obj.substrate.material)) self.assertEqual('Zinc', str(obj.inclusion.material)) self.assertAlmostEqual(123.456, obj.inclusion.diameter_m, 4) self.assertAlmostEqual(1.1, obj.tilt_rad, 4) self.assertAlmostEqual(2.2, obj.rotation_rad, 4) def testcan_convert(self): self.assertTrue(self.h.can_convert(self.obj)) def testconvert(self): element = self.h.convert(self.obj) self.assertAlmostEqual(1.1, float(element.get('tilt')), 4) self.assertAlmostEqual(2.2, float(element.get('rotation')), 4) subelement = element.find('inclusion') self.assertAlmostEqual(123.456, float(subelement.get('diameter')), 4)