#!/usr/bin/env python # This file is part of MAUS: http://micewww.pp.rl.ac.uk:8080/projects/maus # # MAUS is free software: you can redistribute it and/or modify # it under the terms of the GNU General Public License as published by # the Free Software Foundation, either version 3 of the License, or # (at your option) any later version. # # MAUS is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the # GNU General Public License for more details. # # You should have received a copy of the GNU General Public License # along with MAUS. If not, see . """Script to test the BeamlineSimulation Application.""" import unittest import json from MapPyBeamlineSimulation import MapPyBeamlineSimulation """ Test Process function by taking all particles in spill generated by g4bl and adjusting number of protons from target. """ #pylint: disable = W0105 TEST_1 = { "g4bl":{"q_1":1.16, "q_2":-1.45, "q_3":1.006, "d_1":-1.41,\ "proton_absorber_thickness":11,'d_s':200,\ "proton_number":500000000,'particles_per_spill':0,\ "particle_charge":"all", 'rotation_angle':0, 'translation_z':20000.0,\ "random_seed":0,"run_number":0,"get_magnet_currents_pa_cdb":False,\ "proton_weight":1 } } """ Test Process function by taking specified number of particles per spill generated by g4bl and require that the buffer be refilled twice """ #pylint: disable = W0105 TEST_2 = { "g4bl":{"q_1":1.16, "q_2":-1.45, "q_3":1.006, "d_1":-1.41,\ "proton_absorber_thickness":11,'d_s':200,\ "proton_number":500000000,'particles_per_spill':200,\ "particle_charge":"all", 'rotation_angle':0, 'translation_z':20000.0,\ "random_seed":0,"run_number":0,"get_magnet_currents_pa_cdb":False,\ "proton_weight":1 } } """ Test Process function by taking one spill with more particles than generated by g4bl and requiring that the buffer be refilled twice """ #pylint: disable = W0105 TEST_3 = { "g4bl":{"q_1":1.16, "q_2":-1.45, "q_3":1.006, "d_1":-1.41,\ "proton_absorber_thickness":11,'d_s':200,\ "proton_number":500000000,'particles_per_spill':500,\ "particle_charge":"all", 'rotation_angle':0, 'translation_z':20000.0,\ "random_seed":0,"run_number":0,"get_magnet_currents_pa_cdb":False,\ "proton_weight":1 } } """ Test error messages in Birth function """ #pylint: disable = W0105 TEST_4 = { "g4bl":{"q_1":1.16, "q_2":-1.45, "q_3":1.006, "d_1":-1.41,\ "proton_absorber_thickness":-11,'d_s':200, "proton_number":10000,'particles_per_spill':200,\ "particle_charge":"all", 'rotation_angle':0, 'translation_z':20000.0,\ "random_seed":0,"run_number":0,"get_magnet_currents_pa_cdb":False,\ "proton_weight":1 } } """ Test error messages in Birth function """ #pylint: disable = W0105 TEST_5 = { "g4bl":{"q_1":1.16, "q_2":-1.45, "q_3":1.006, "d_1":-1.41,\ "proton_absorber_thickness":11,'d_s':200,\ "proton_number":-10000,'particles_per_spill':200,\ "particle_charge":"all", 'rotation_angle':0, 'translation_z':20000.0,\ "random_seed":0,"run_number":0,"get_magnet_currents_pa_cdb":False,\ "proton_weight":1 } } """ Test error messages in Birth function """ #pylint: disable = W0105 TEST_6 = { "g4bl":{"q_1":1.16, "q_2":-1.45, "q_3":1.006, "d_1":-1.41,\ "proton_absorber_thickness":11,'d_s':200,\ "proton_number":10000,'particles_per_spill':200,\ "particle_charge":"1.21 jiggawatts", 'rotation_angle':0,\ 'translation_z':20000.0,\ "random_seed":0,"run_number":0,"get_magnet_currents_pa_cdb":False,\ "proton_weight":1 } } """ Test that process function stops if g4bl file is empty. """ #pylint: disable = W0105 TEST_7 = { "g4bl":{"q_1":0, "q_2":0, "q_3":0, "d_1":0,\ "proton_absorber_thickness":11,'d_s':0,\ "proton_number":100,'particles_per_spill':25,\ "particle_charge":"negative", 'rotation_angle':0,\ 'translation_z':20000.0,\ "random_seed":0,"run_number":0,"get_magnet_currents_pa_cdb":False,\ "proton_weight":1 } } class TestMapPyBeamlineSimulation(unittest.TestCase): #pylint: disable = R0904 """ set of tests for MapPyBeamlineSimulation """ @classmethod def setUp(cls): #pylint: disable = C0103 """Initialise beam_maker object""" cls.g4bl = MapPyBeamlineSimulation() def teardown(cls): #pylint: disable = E0213 """Close beam_maker object""" cls.assertTrue(cls.g4bl.death()) def test_birth(self): """ Test error messages """ self.assertTrue(self.g4bl.birth(json.dumps(TEST_1))) self.assertFalse(self.g4bl.birth(json.dumps(TEST_4))) self.assertFalse(self.g4bl.birth(json.dumps(TEST_5))) self.assertFalse(self.g4bl.birth(json.dumps(TEST_6))) self.assertFalse(self.g4bl.birth("")) self.assertFalse(self.g4bl.birth(json.dumps({}))) def test_birth_values(self): """ Test birth assigns correct values """ self.g4bl.birth(json.dumps(TEST_1)) self.assertEqual(self.g4bl.q_1, 1.16) self.assertEqual(self.g4bl.q_2, -1.45) self.assertEqual(self.g4bl.q_3, 1.006) self.assertEqual(self.g4bl.d_1, -1.41) self.assertEqual(self.g4bl.d_s, 200) self.assertEqual(self.g4bl.proton_absorber_thickness, 11) self.assertEqual(self.g4bl.proton_weight, 1) self.assertEqual(self.g4bl.particles_per_spill, 0) self.assertEqual(self.g4bl.particle_charge, 'all') def test_process_test_1(self): """ Test that process returns 'good' spill """ print json.dumps(TEST_1) self.g4bl.birth(json.dumps(TEST_1)) print self.g4bl.birth(json.dumps(TEST_1)) spill_one = self.g4bl.process(json.dumps({})) spill = json.loads(spill_one) self.assertTrue(spill["mc_events"]!=[]) # spill_two = self.g4bl.process(json.dumps({})) # spill = json.loads(spill_two) # self.assertTrue(spill["mc_events"]!=[]) def test_process_test_2(self): """ Test that process returns 'good' spill """ self.g4bl.birth(json.dumps(TEST_2)) spill_one = self.g4bl.process(json.dumps({})) spill = json.loads(spill_one) self.assertEqual(len(spill["mc_events"]), 200) spill_two = self.g4bl.process(json.dumps({})) spill = json.loads(spill_two) self.assertEqual(len(spill["mc_events"]), 200) def test_process_test_3(self): """ Test that process returns 'good' spill """ self.g4bl.birth(json.dumps(TEST_3)) spill_one = self.g4bl.process(json.dumps({})) spill = json.loads(spill_one) self.assertEqual(len(spill["mc_events"]), 500) def test_process_test_7(self): """ Test that process returns 'bad' spill """ with self.assertRaises(SystemExit): self.g4bl.birth(json.dumps(TEST_7)) self.g4bl.process(json.dumps({})) if __name__ == "__main__": unittest.main()