test_vector.py 2.1 KB

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