interactivemap_test.py 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # NAME: interactivemap_test.py
  5. #
  6. # AUTHOR: Caitlin Haedrich (caitlin dot haedrich gmail com)
  7. #
  8. # PURPOSE: This is a test script for grass.jupyter's InteractiveMap
  9. #
  10. # COPYRIGHT: (C) 2021 by Caitlin Haedrich and the GRASS Development Team
  11. #
  12. # This program is free software under the GNU General Public
  13. # License (>=v2). Read the file COPYING that comes with GRASS
  14. # for details.
  15. #
  16. #############################################################################
  17. import os
  18. import unittest
  19. import sys
  20. from pathlib import Path
  21. import grass.jupyter as gj
  22. from grass.gunittest.case import TestCase
  23. from grass.gunittest.main import test
  24. def can_import_folium():
  25. """Test folium import to see if test can be run."""
  26. try:
  27. import folium # noqa
  28. return True
  29. except ImportError:
  30. return False
  31. class TestDisplay(TestCase):
  32. # Setup variables
  33. files = []
  34. @classmethod
  35. def setUpClass(cls):
  36. """Ensures expected computational region"""
  37. # to not override mapset's region (which might be used by other tests)
  38. cls.use_temp_region()
  39. # cls.runModule or self.runModule is used for general module calls
  40. # we'll use the elevation raster as a test display
  41. cls.runModule("g.region", raster="elevation")
  42. @classmethod
  43. def tearDownClass(cls):
  44. """Remove temporary region"""
  45. cls.del_temp_region()
  46. def tearDown(self):
  47. """
  48. Remove the PNG file created after testing with "filename =" option.
  49. This is executed after each test run.
  50. """
  51. for f in self.files:
  52. f = Path(f)
  53. if sys.version_info < (3, 8):
  54. try:
  55. os.remove(f)
  56. except FileNotFoundError:
  57. pass
  58. else:
  59. f.unlink(missing_ok=True)
  60. @unittest.skipIf(not can_import_folium(), "Cannot import folium")
  61. def test_basic(self):
  62. # Create InteractiveMap
  63. interactive_map = gj.InteractiveMap()
  64. interactive_map.add_raster("elevation")
  65. interactive_map.add_vector("roadsmajor")
  66. interactive_map.show()
  67. @unittest.skipIf(not can_import_folium(), "Cannot import folium")
  68. def test_save_as_html(self):
  69. # Create InteractiveMap
  70. interactive_map = gj.InteractiveMap()
  71. interactive_map.add_vector("roadsmajor")
  72. filename = "InteractiveMap_test.html"
  73. self.files.append(filename)
  74. interactive_map.save(filename)
  75. self.assertFileExists(filename)
  76. if __name__ == "__main__":
  77. test()