test_checks.py 3.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. # MODULE: Test of grass.grassdb.checks
  2. #
  3. # AUTHOR(S): Vaclav Petras <wenzeslaus gmail com>
  4. #
  5. # PURPOSE: Checks of GRASS database/location/mapset structure
  6. #
  7. # COPYRIGHT: (C) 2021 Vaclav Petras, and by the GRASS Development Team
  8. #
  9. # This program is free software under the GNU General Public
  10. # License (>=v2). Read the file COPYING that comes with GRASS
  11. # for details.
  12. """Tests of grass.grassdb.checks"""
  13. import os
  14. from pathlib import Path
  15. from grass.grassdb.checks import (
  16. dir_contains_location,
  17. get_list_of_locations,
  18. is_location_valid,
  19. is_mapset_valid,
  20. location_exists,
  21. mapset_exists,
  22. )
  23. from grass.gunittest.case import TestCase
  24. from grass.gunittest.gmodules import call_module
  25. from grass.gunittest.main import test
  26. class TestWithCurrent(TestCase):
  27. """Tests that functions return expected result for the current mapset"""
  28. def test_valid_location(self):
  29. """Test that different parameter combinations work and return true"""
  30. db_path = call_module("g.gisenv", get="GISDBASE").strip()
  31. loc_name = call_module("g.gisenv", get="LOCATION_NAME").strip()
  32. self.assertTrue(is_location_valid(db_path, loc_name))
  33. self.assertTrue(is_location_valid(os.path.join(db_path, loc_name)))
  34. self.assertTrue(is_location_valid(Path(db_path), loc_name))
  35. self.assertTrue(is_location_valid(Path(db_path) / loc_name))
  36. def test_valid_mapset(self):
  37. """Test that different parameter combinations work and return true"""
  38. db_path = call_module("g.gisenv", get="GISDBASE").strip()
  39. loc_name = call_module("g.gisenv", get="LOCATION_NAME").strip()
  40. mapset_name = call_module("g.gisenv", get="MAPSET").strip()
  41. self.assertTrue(is_mapset_valid(db_path, loc_name, mapset_name))
  42. self.assertTrue(is_mapset_valid(os.path.join(db_path, loc_name, mapset_name)))
  43. self.assertTrue(is_mapset_valid(Path(db_path), loc_name, mapset_name))
  44. self.assertTrue(is_mapset_valid(Path(db_path) / loc_name / mapset_name))
  45. def test_location_exists(self):
  46. """Test that different parameter combinations work and return true"""
  47. db_path = call_module("g.gisenv", get="GISDBASE").strip()
  48. loc_name = call_module("g.gisenv", get="LOCATION_NAME").strip()
  49. self.assertTrue(location_exists(db_path, loc_name))
  50. self.assertTrue(location_exists(os.path.join(db_path, loc_name)))
  51. self.assertTrue(location_exists(Path(db_path), loc_name))
  52. self.assertTrue(location_exists(Path(db_path) / loc_name))
  53. def test_mapset_exists(self):
  54. """Test that different parameter combinations work and return true"""
  55. db_path = call_module("g.gisenv", get="GISDBASE").strip()
  56. loc_name = call_module("g.gisenv", get="LOCATION_NAME").strip()
  57. mapset_name = call_module("g.gisenv", get="MAPSET").strip()
  58. self.assertTrue(mapset_exists(db_path, loc_name, mapset_name))
  59. self.assertTrue(mapset_exists(os.path.join(db_path, loc_name, mapset_name)))
  60. self.assertTrue(mapset_exists(Path(db_path), loc_name, mapset_name))
  61. self.assertTrue(mapset_exists(Path(db_path) / loc_name / mapset_name))
  62. def test_dir_contains_location(self):
  63. """Test that different parameter combinations work and return true"""
  64. db_path = call_module("g.gisenv", get="GISDBASE").strip()
  65. self.assertTrue(dir_contains_location(db_path))
  66. self.assertTrue(dir_contains_location(Path(db_path)))
  67. def test_get_list_of_locations(self):
  68. """Test that different parameter combinations work and return true"""
  69. db_path = call_module("g.gisenv", get="GISDBASE").strip()
  70. current_loc_name = call_module("g.gisenv", get="LOCATION_NAME").strip()
  71. list_of_locations = get_list_of_locations(db_path)
  72. self.assertTrue(list_of_locations, msg="No locations in the current db found")
  73. self.assertIn(current_loc_name, list_of_locations)
  74. if __name__ == "__main__":
  75. test()