test_parser_json.py 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. """Test the JSON extension of the GRASS parser
  2. (C) 2014 by the GRASS Development Team
  3. This program is free software under the GNU General Public
  4. License (>=v2). Read the file COPYING that comes with GRASS
  5. for details.
  6. @author Soeren Gebbert
  7. """
  8. import subprocess
  9. from grass.gunittest.case import TestCase
  10. import json
  11. class TestParserJson(TestCase):
  12. def test_r_slope_aspect_json(self):
  13. args = ["r.slope.aspect",
  14. "elevation=elevation+https://storage.googleapis.com/graas-geodata/elev_ned_30m.tif",
  15. "slope=slope+GTiff",
  16. "aspect=aspect+GTiff", "--json"]
  17. inputs = [
  18. {"import_descr": {"source": "https://storage.googleapis.com/graas-geodata/elev_ned_30m.tif",
  19. "type": "raster"},
  20. "param": "elevation", "value": "elevation"},
  21. {"param": "format", "value": "degrees"},
  22. {"param": "precision", "value": "FCELL"},
  23. {"param": "zscale", "value": "1.0"},
  24. {"param": "min_slope", "value": "0.0"}
  25. ]
  26. outputs = [
  27. {"export": {"format": "GTiff", "type": "raster"},
  28. "param": "slope", "value": "slope"},
  29. {"export": {"format": "GTiff", "type": "raster"},
  30. "param": "aspect", "value": "aspect"}
  31. ]
  32. stdout, stderr = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()
  33. print(stdout)
  34. json_code = json.loads(stdout)
  35. self.assertEqual(json_code["module"], "r.slope.aspect")
  36. self.assertEqual(len(json_code["inputs"]), 5)
  37. self.assertEqual(json_code["inputs"], inputs)
  38. self.assertEqual(json_code["outputs"], outputs)
  39. def test_v_out_ascii(self):
  40. args = ["v.out.ascii",
  41. "input=hospitals@PERMANENT",
  42. "output=myfile+TXT",
  43. "--json"]
  44. inputs = [
  45. {"param": "input", "value": "hospitals@PERMANENT"},
  46. {"param": "layer", "value": "1"},
  47. {"param": "type", "value": "point,line,boundary,centroid,area,face,kernel"},
  48. {"param": "format", "value": "point"},
  49. {"param": "separator", "value": "pipe"},
  50. {"param": "precision", "value": "8"}
  51. ]
  52. outputs = [
  53. {"export": {"format": "TXT", "type": "file"},
  54. "param": "output", "value": "$file::myfile"}
  55. ]
  56. stdout, stderr = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()
  57. print(stdout)
  58. json_code = json.loads(stdout)
  59. self.assertEqual(json_code["module"], "v.out.ascii")
  60. self.assertEqual(len(json_code["inputs"]), 6)
  61. self.assertEqual(json_code["inputs"], inputs)
  62. self.assertEqual(json_code["outputs"], outputs)
  63. def test_v_info(self):
  64. args = ["v.info",
  65. "map=hospitals@PERMANENT",
  66. "-c",
  67. "--json"]
  68. inputs = [
  69. {"param": "map", "value": "hospitals@PERMANENT"},
  70. {"param": "layer", "value": "1"}
  71. ]
  72. stdout, stderr = subprocess.Popen(args, stdout=subprocess.PIPE).communicate()
  73. print(stdout)
  74. json_code = json.loads(stdout)
  75. self.assertEqual(json_code["module"], "v.info")
  76. self.assertEqual(len(json_code["inputs"]), 2)
  77. self.assertEqual(json_code["inputs"], inputs)
  78. if __name__ == '__main__':
  79. from grass.gunittest.main import test
  80. test()