test_vector.py 2.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  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. from grass.pygrass.utils import get_mapset_vector
  26. mset = get_mapset_vector(cls.tmpname, mapset='')
  27. if mset:
  28. run_command("g.remove", flags='f', type='vector', name=cls.tmpname)
  29. def test_getitem_slice(self):
  30. """Test that getitem handle correctly the slice starting from 1"""
  31. vcoords = ((10.0, 6.0), (12.0, 6.0))
  32. with VectorTopo(self.tmpname, mode="r") as vect:
  33. coords = tuple([pnt.coords() for pnt in vect[:3]])
  34. self.assertTupleEqual(vcoords, coords)
  35. coords = tuple([pnt.coords() for pnt in vect[1:3]])
  36. self.assertTupleEqual(vcoords, coords)
  37. self.vect.close()
  38. def test_viter(self):
  39. """Test that getitem handle correctly the slice starting from 1"""
  40. with VectorTopo(self.tmpname, mode="r") as vect:
  41. for name in ["points", "lines", "areas", "islands", "nodes"]:
  42. count = 0
  43. for feature in vect.viter(name):
  44. count += 1
  45. self.assertEqual(count, vect.number_of(name))
  46. self.vect.close()
  47. def test_getitem_raise(self):
  48. """Test that getitem raise a value error if the key is not
  49. an integer or a slice"""
  50. with VectorTopo(self.tmpname, mode="r") as vect:
  51. with self.assertRaises(ValueError):
  52. vect['value']
  53. self.vect.close()
  54. if __name__ == '__main__':
  55. test()