test_vector.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """
  2. Created on Wed Jun 18 17:21:42 2014
  3. @author: pietro
  4. """
  5. from grass.gunittest.case import TestCase
  6. from grass.gunittest.main import test
  7. from grass.script.core import run_command
  8. from grass.pygrass.vector import VectorTopo
  9. class VectorTopoTestCase(TestCase):
  10. tmpname = "VectorTopoTestCase_map"
  11. @classmethod
  12. def setUpClass(cls):
  13. from grass.pygrass import utils
  14. utils.create_test_vector_map(cls.tmpname)
  15. cls.vect = None
  16. cls.vect = VectorTopo(cls.tmpname)
  17. cls.vect.open("r")
  18. cls.vect.close()
  19. @classmethod
  20. def tearDownClass(cls):
  21. if cls.vect.is_open():
  22. cls.vect.close()
  23. """Remove the generated vector map, if exist"""
  24. cls.runModule("g.remove", flags="f", type="vector", name=cls.tmpname)
  25. def test_getitem_slice(self):
  26. """Test that getitem handle correctly the slice starting from 1"""
  27. vcoords = ((10.0, 6.0), (12.0, 6.0))
  28. with VectorTopo(self.tmpname, mode="r") as vect:
  29. coords = tuple([pnt.coords() for pnt in vect[:3]])
  30. self.assertTupleEqual(vcoords, coords)
  31. coords = tuple([pnt.coords() for pnt in vect[1:3]])
  32. self.assertTupleEqual(vcoords, coords)
  33. self.vect.close()
  34. def test_viter(self):
  35. """Test that getitem handle correctly the slice starting from 1"""
  36. with VectorTopo(self.tmpname, mode="r") as vect:
  37. for name in ["points", "lines", "areas", "islands", "nodes"]:
  38. count = 0
  39. for feature in vect.viter(name):
  40. count += 1
  41. self.assertEqual(count, vect.number_of(name))
  42. self.vect.close()
  43. def test_getitem_raise(self):
  44. """Test that getitem raise a value error if the key is not
  45. an integer or a slice"""
  46. with VectorTopo(self.tmpname, mode="r") as vect:
  47. with self.assertRaises(ValueError):
  48. vect["value"]
  49. self.vect.close()
  50. if __name__ == "__main__":
  51. test()