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