def test_ref_assumption_val_type(self):
        ds = DataStore()
        ds.put("item", 20)
        _ = ds.get("item")
        _ = 40

        self.assertEqual(ds.get("item"), 20)
class ParticlePrinterTest(unittest.TestCase):
    """
    Test class for the ParticlePrinter class
    """
    def setUp(self):
        self.datastore = DataStore()
        self.module = ParticlePrinter()
        self.datastore.put('Particles', ParticleContainer(),
                           ObjectLifetime.Application)
        particle = Particle(
            0.1, 'electron', 5, 0, 0.001, 0.005
        )  #Create Particle with determined specification for read out with the printer
        self.datastore.get('Particles').add_particle(particle, 'electron')

    def test_begin(self):
        with LogCapture() as l:
            self.module.begin(self.datastore)

            l.check(('root', 'INFO', "Begin of module 'ParticlePrinter'"))

    def test_event(self):
        with LogCapture() as l:
            self.module.event(self.datastore)

            l.check(
                ('root', 'INFO', "Particle: electron"),
                ('root', 'INFO', "Position\nx=5\ny=0"),
                ('root', 'INFO', "Momentum\np_x=0.001\np_y=0.005"),
            )

    def test_end(self):
        with LogCapture() as l:
            self.module.end(self.datastore)
            l.check(('root', 'INFO', "End of module 'ParticlePrinter'"))
 def test_objectLifeTime(self):
     data_store = DataStore()
     self_wrapper = self
     test_module = TestObjectLifeTimeModule(lambda: self_wrapper.assertRaises(NotFoundInDataStore, data_store.get, 'EventLifetime'))
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module], data_store)
     run_engine.run()
     data_store.get('ApplicationLifetime')
Example #4
0
 def test_objectLifeTime(self):
     data_store = DataStore()
     self_wrapper = self
     test_module = TestObjectLifeTimeModule(
         lambda: self_wrapper.assertRaises(NotFoundInDataStore, data_store.
                                           get, 'EventLifetime'))
     data_store.put('nEvent', 2)
     run_engine = RunEngine([test_module], data_store)
     run_engine.run()
     data_store.get('ApplicationLifetime')
    def test_ref_assumption_ref_type(self):
        ds = DataStore()

        class WrappedInt(object):
            def __int__(self):
                self.i = 20

        ds.put("item", WrappedInt())
        item = ds.get("item")
        item.i = 40

        self.assertEqual(ds.get("item").i, 40)
    def test_put_get_scalar(self):
        ds = DataStore()
        ds.put("int1", 10)
        ds.put("int2", 20)

        self.assertEqual(ds.get("int1"), 10)
        self.assertEqual(ds.get("int2"), 20)
 def test_run(self):
     configuration = Configuration(self.path_to_default_test_config_file, configuration_specification)
     dataStore = DataStore()
     module = DetectorInitializer()
     dataStore.put(module, configuration, ObjectLifetime.Application)
     module.begin(dataStore)
     # now, there must be a detector present
     detector = dataStore.get("Detector")
     self.assertEqual(detector.width, 100)
     self.assertEqual(detector.nSuperLayers, 11)
     self.assertEqual(detector.nLayersList, [1, 5, 5, 2, 6, 6, 2, 7, 5, 7, 8])
 def test_run(self):
     configuration = Configuration(self.path_to_default_test_config_file, configuration_specification)
     dataStore = DataStore()
     module = DetectorInitializer()
     dataStore.put(module, configuration, ObjectLifetime.Application)
     module.begin(dataStore)
     # now, there must be a detector present
     detector = dataStore.get('Detector')
     self.assertEqual(detector.width, 100)
     self.assertEqual(detector.nSuperLayers, 11)
     self.assertEqual(detector.nLayersList, [1, 5, 5, 2, 6, 6, 2, 7, 5, 7, 8])
     
class ParticlePrinterTest(unittest.TestCase):
    """
    Test class for the ParticlePrinter class
    """
    def setUp(self):
        self.datastore = DataStore()
        self.module = ParticlePrinter()
        self.datastore.put('Particles', ParticleContainer(), ObjectLifetime.Application)
        particle = Particle(0.1, 'electron', 5, 0, 0.001, 0.005) #Create Particle with determined specification for read out with the printer
        self.datastore.get('Particles').add_particle(particle, 'electron')


    def test_begin(self):
        with LogCapture() as l:
            self.module.begin(self.datastore)

            l.check(
                    ('root', 'INFO', "Begin of module 'ParticlePrinter'")
            )

    def test_event(self):
        with LogCapture() as l:
            self.module.event(self.datastore)

            l.check(
                    ('root', 'INFO', "Particle: electron"),
                    ('root', 'INFO', "Position\nx=5\ny=0"),
                    ('root', 'INFO', "Momentum\np_x=0.001\np_y=0.005"),

            )

    def test_end(self):
        with LogCapture() as l:
            self.module.end(self.datastore)
            l.check(
                    ('root', 'INFO', "End of module 'ParticlePrinter'")
            )
class ParticleGunTest(unittest.TestCase):
    """
    Test class for the ParticleGun class
    """
    def setUp(self):
        self.pathToConfgiFiles = os.path.dirname(os.path.abspath(__file__))
        self.path_to_default_test_config_file = self.pathToConfgiFiles + '/particleGun_Electron.cfg'
        configuration = Configuration(self.path_to_default_test_config_file, configuration_specification) #Read configuration from config file
        self.datastore = DataStore()
        self.module = ParticleGun()
        self.datastore.put(self.module, configuration, ObjectLifetime.Application)


    def test_begin(self):
        with LogCapture() as l:
            self.module.begin(self.datastore)

            l.check(
                    ('root', 'INFO', "Begin of module 'ParticleGun'"),
                    ('root', 'WARNING', "ParticleGun hasn't found detector, set cells to default."),
                    ('root', 'INFO', "Particle gun initialized that shoots a 'electron'.")
            )

    def test_event(self):
        self.module.begin(self.datastore) #Execute begin method to set variables needed to execute event method
        self.module.event(self.datastore)
        for particle in self.datastore.get('Particles').get_all_particles(): #Test outputs to be in expected range
            self.assertLessEqual(particle.position().pos()[0], self.module.cells)
            self.assertEqual(particle.position().pos()[1], 0)
            self.assertLessEqual(particle.momentum().mom()[0], self.module.particle_max_mom)
            self.assertLessEqual(particle.momentum().mom()[1], self.module.particle_max_mom)


    def test_end(self):
        with LogCapture() as l:
            self.module.end(self.datastore)

            l.check(
                    ('root', 'INFO', "End of module 'ParticleGun'")
            )
    def test_lifetime(self):
        ds = DataStore()
        # implicit event lifetime
        ds.put("item1", 20)
        # explicit event lifetime
        ds.put("item2", 30, ObjectLifetime.Event)
        # explicit application lifetime
        ds.put("item3", 40, ObjectLifetime.Application)

        ds.clear(ObjectLifetime.Event)

        with self.assertRaises(NotFoundInDataStore):
            ds.get("item1")
        with self.assertRaises(NotFoundInDataStore):
            ds.get("item2")

        self.assertIsNotNone(ds.get("item3"))

        ds.clear(ObjectLifetime.Application)

        with self.assertRaises(NotFoundInDataStore):
            ds.get("item3")
    def test_get_invalid(self):
        ds = DataStore()

        with self.assertRaises(NotFoundInDataStore):
            ds.get("not_set")