import unittest
import uuid

from simphony.cuds.particles import (
    Particle, Particles)
from simphony.testing.abc_check_particles import (
    ContainerAddParticlesCheck, ContainerManipulatingParticlesCheck)
from simlammps.lammps_wrapper import LammpsWrapper
from simlammps.tests.example_configurator import ExampleConfigurator


class TestFileIoParticlesAddParticles(
        ContainerAddParticlesCheck, unittest.TestCase):

    def container_factory(self, name):
        return self.pc

    def setUp(self):
        self.wrapper = LammpsWrapper()
        ExampleConfigurator.configure_wrapper(self.wrapper)
        pcs = [pc for pc in self.wrapper.iter_particles()]
        self.pc = pcs[0]
        ContainerAddParticlesCheck.setUp(self)


class TestFileIoParticlesManipulatingParticles(
        ContainerManipulatingParticlesCheck, unittest.TestCase):

    def container_factory(self, name):
        pc = Particles(name=name)
        return self.wrapper.add_particles(pc)

    def setUp(self):
        self.wrapper = LammpsWrapper()
        ContainerManipulatingParticlesCheck.setUp(self)


def _get_particle(particles):
    for p in particles.iter_particles():
        return p
    else:
        raise RuntimeError("could not find a particle to test with")


class TestLammpsParticles(unittest.TestCase):

    def setUp(self):

        self.wrapper = LammpsWrapper()

        # configuration is being done by dummy class
        # the wrapper is properly configured with
        # CM/SP/BC and given particles
        ExampleConfigurator.configure_wrapper(self.wrapper)

        # keep track of first wrapper-based container of particles
        # and the particle ids that it contains
        pcs = [pc for pc in self.wrapper.iter_particles()]
        self.pc = pcs[0]
        self.particle_ids_in_pc = []
        for p in pcs[0].iter_particles():
            self.particle_ids_in_pc.append(p.uid)

    def test_update_non_existing_particle(self):
        # TODO we should test that this raises
        # ValueError but Particles raises KeyError
        # see #104 issue in simphony-common
        with self.assertRaises(Exception):
            p = Particle(
                uid=uuid.UUID(int=100000000), coordinates=(0.0, 0.0, 0.0))
            self.pc.update_particle(p)

    def test_get_non_existing_particle(self):
        with self.assertRaises(KeyError):
            self.pc.get_particle(100000000)

    def test_update_particle(self):
        particle = _get_particle(self.pc)
        particle.coordinates = (42.0, 42.0, 42.0)
        self.pc.update_particle(particle)
        updated_p = self.pc.get_particle(particle.uid)

        self.assertEqual(particle.coordinates, updated_p.coordinates)

    def test_add_particle_without_id(self):
        p = Particle(coordinates=(0.0, 2.5, 0.0))
        uid = self.pc.add_particle(p)
        added_p = self.pc.get_particle(uid)
        self.assertTrue(self.pc.has_particle(uid))
        self.assertEqual(p.coordinates, added_p.coordinates)

    def test_delete_particle(self):
        removed_particle = _get_particle(self.pc)
        self.pc.remove_particle(removed_particle.uid)

        # check that it was removed
        with self.assertRaises(KeyError):
            self.pc.get_particle(removed_particle.uid)

        self.wrapper.run()

        # check that it stayed removed
        with self.assertRaises(KeyError):
            self.pc.get_particle(removed_particle.uid)

    def test_iter_particles(self):
        iterated_ids = []
        for particle in self.pc.iter_particles():
            iterated_ids.append(particle.uid)

        self.assertEqual(set(self.particle_ids_in_pc), set(iterated_ids),
                         'Error: incorrect iteration!')

        # make different list of ids to test with
        ids = self.particle_ids_in_pc
        ids = ids[0:3]

        iterated_ids = []
        for particle in self.pc.iter_particles(ids):
            iterated_ids.append(particle.uid)

        self.assertEqual(set(ids), set(iterated_ids),
                         'Error: incorrect iteration! {0}---\n{1}'
                         .format(ids, iterated_ids))

if __name__ == '__main__':
    unittest.main()

@@ -12,20 +12,8 @@
 test
-%0Aimport uuid
 %0A%0Afr
@@ -49,24 +49,8 @@
 port
- (%0A    Particle,
  Par
@@ -55,17 +55,16 @@
 articles
-)
 %0Afrom si
@@ -1299,17 +1299,16 @@
 (self):%0A
-%0A
         
@@ -1888,2228 +1888,432 @@
 est_
-update_non_existing_particle(self):%0A        # TODO we should test that this raises%0A        # ValueError but Particles raises KeyError%0A        # see #104 issue in simphony-common%0A        with self.assertRaises(Exception):%0A            p = Particle(%0A                uid=uuid.UUID(int=100000000), coordinates=(0.0, 0.0, 0.0))%0A            self.pc.update_particle(p)%0A%0A    def test_get_non_existing_particle(self):%0A        with self.assertRaises(KeyError):%0A            self.pc.get_particle(100000000)%0A%0A    def test_update_particle(self):%0A        particle = _get_particle(self.pc)%0A        particle.coordinates = (42.0, 42.0, 42.0)%0A        self.pc.update_particle(particle)%0A        updated_p = self.pc.get_particle(particle.uid)%0A%0A        self.assertEqual(particle.coordinates, updated_p.coordinates)%0A%0A    def test_add_particle_without_id(self):%0A        p = Particle(coordinates=(0.0, 2.5, 0.0))%0A        uid = self.pc.add_particle(p)%0A        added_p = self.pc.get_particle(uid)%0A        self.assertTrue(self.pc.has_particle(uid))%0A        self.assertEqual(p.coordinates, added_p.coordinates)%0A%0A    def test_delete_particle(self):%0A        removed_particle = _get_particle(self.pc)%0A        self.pc.remove_particle(removed_particle.uid)%0A%0A        # check that it was removed%0A        with self.assertRaises(KeyError):%0A            self.pc.get_particle(removed_particle.uid)%0A%0A        self.wrapper.run()%0A%0A        # check that it stayed removed%0A        with self.assertRaises(KeyError):%0A            self.pc.get_particle(removed_particle.uid)%0A%0A    def test_iter_particles(self):%0A        iterated_ids = %5B%5D%0A        for particle in self.pc.iter_particles():%0A            iterated_ids.append(particle.uid)%0A%0A        self.assertEqual(set(self.particle_ids_in_pc), set(iterated_ids),%0A                         'Error: incorrect iteration!')%0A%0A        # make different list of ids to test with%0A        ids = self.particle_ids_in_pc%0A        ids = ids%5B0:3%5D%0A%0A        iterated_ids = %5B%5D%0A        for particle in self.pc.iter_particles(ids):%0A            iterated_ids.append(particle.uid)%0A%0A        self.assertEqual(set(ids), set(iterated_ids),%0A                         'Error: incorrect iteration! %7B0%7D---%5Cn%7B1%7D'%0A                         .format(ids, iterated_ids)
+delete_particle(self):%0A        removed_particle = _get_particle(self.pc)%0A        self.pc.remove_particle(removed_particle.uid)%0A%0A        # check that it was removed%0A        with self.assertRaises(KeyError):%0A            self.pc.get_particle(removed_particle.uid)%0A%0A        self.wrapper.run()%0A%0A        # check that it stayed removed%0A        with self.assertRaises(KeyError):%0A            self.pc.get_particle(removed_particle.uid
 )%0A%0Ai

remove repeated test