test_manage.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. # MODULE: Test of grass.grassdb.manage
  2. #
  3. # AUTHOR(S): Vaclav Petras <wenzeslaus gmail com>
  4. #
  5. # PURPOSE: Test of managing the 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.manage"""
  13. from pathlib import Path
  14. from grass.grassdb.manage import MapsetPath, resolve_mapset_path, split_mapset_path
  15. from grass.gunittest.case import TestCase
  16. from grass.gunittest.gmodules import call_module
  17. from grass.gunittest.main import test
  18. class TestMapsetPath(TestCase):
  19. """Check that object can be constructed"""
  20. def test_mapset_from_path_object(self):
  21. """Check that path is correctly stored"""
  22. path = "does/not/exist/"
  23. location_name = "test_location_A"
  24. mapset_name = "test_mapset_1"
  25. full_path = Path(path) / location_name / mapset_name
  26. mapset_path = MapsetPath(
  27. path=full_path, directory=path, location=location_name, mapset=mapset_name
  28. )
  29. # Paths are currently stored as is (not resolved).
  30. self.assertEqual(mapset_path.directory, path)
  31. self.assertEqual(mapset_path.location, location_name)
  32. self.assertEqual(mapset_path.mapset, mapset_name)
  33. self.assertEqual(mapset_path.path, Path(path) / location_name / mapset_name)
  34. def test_mapset_from_str(self):
  35. """Check with path from str and database directory as Path"""
  36. path = "does/not/exist"
  37. location_name = "test_location_A"
  38. mapset_name = "test_mapset_1"
  39. full_path = Path(path) / location_name / mapset_name
  40. mapset_path = MapsetPath(
  41. path=str(full_path),
  42. directory=Path(path),
  43. location=location_name,
  44. mapset=mapset_name,
  45. )
  46. # Paths are currently stored as is (not resolved).
  47. self.assertEqual(mapset_path.directory, path)
  48. self.assertEqual(mapset_path.location, location_name)
  49. self.assertEqual(mapset_path.mapset, mapset_name)
  50. self.assertEqual(mapset_path.path, Path(path) / location_name / mapset_name)
  51. class TestSplitMapsetPath(TestCase):
  52. """Check that split works with different parameters"""
  53. def test_split_path(self):
  54. """Check that pathlib.Path is correctly split"""
  55. ref_db = "does/not/exist"
  56. ref_location = "test_location_A"
  57. ref_mapset = "test_mapset_1"
  58. path = Path(ref_db) / ref_location / ref_mapset
  59. new_db, new_location, new_mapset = split_mapset_path(path)
  60. self.assertEqual(new_db, ref_db)
  61. self.assertEqual(new_location, ref_location)
  62. self.assertEqual(new_mapset, ref_mapset)
  63. def test_split_str(self):
  64. """Check that path as str is correctly split"""
  65. ref_db = "does/not/exist"
  66. ref_location = "test_location_A"
  67. ref_mapset = "test_mapset_1"
  68. path = Path(ref_db) / ref_location / ref_mapset
  69. new_db, new_location, new_mapset = split_mapset_path(str(path))
  70. self.assertEqual(new_db, ref_db)
  71. self.assertEqual(new_location, ref_location)
  72. self.assertEqual(new_mapset, ref_mapset)
  73. def test_split_str_trailing_slash(self):
  74. """Check that path as str with a trailing slash is correctly split"""
  75. ref_db = "does/not/exist"
  76. ref_location = "test_location_A"
  77. ref_mapset = "test_mapset_1"
  78. path = Path(ref_db) / ref_location / ref_mapset
  79. new_db, new_location, new_mapset = split_mapset_path(str(path) + "/")
  80. self.assertEqual(new_db, ref_db)
  81. self.assertEqual(new_location, ref_location)
  82. self.assertEqual(new_mapset, ref_mapset)
  83. class TestResolveMapsetPath(TestCase):
  84. """Check expected results for current mapset and for a non-existent one"""
  85. def test_default_mapset_exists(self):
  86. """Check that default mapset is found for real path/location.
  87. The location (or mapset) may not exist, but exist in the test.
  88. """
  89. db_path = call_module("g.gisenv", get="GISDBASE").strip()
  90. loc_name = call_module("g.gisenv", get="LOCATION_NAME").strip()
  91. mapset_path = resolve_mapset_path(path=db_path, location=loc_name)
  92. self.assertEqual(mapset_path.mapset, "PERMANENT")
  93. def test_default_mapset_does_not_exist(self):
  94. """Check that default mapset is found for non-existent path/location.
  95. The location (or mapset) do not exist.
  96. """
  97. mapset_path = resolve_mapset_path(
  98. path="does/not/exist", location="does_not_exit"
  99. )
  100. self.assertEqual(mapset_path.mapset, "PERMANENT")
  101. def test_default_mapset_with_path(self):
  102. """Check that default mapset is found for path.
  103. This requires the location (with default mapset) to exists.
  104. """
  105. db_path = call_module("g.gisenv", get="GISDBASE").strip()
  106. loc_name = call_module("g.gisenv", get="LOCATION_NAME").strip()
  107. mapset_path = resolve_mapset_path(path=Path(db_path) / loc_name)
  108. self.assertEqual(mapset_path.mapset, "PERMANENT")
  109. def test_mapset_from_parts(self):
  110. """Check that a non-existing path is correctly constructed."""
  111. path = "does/not/exist"
  112. location_name = "test_location_A"
  113. mapset_name = "test_mapset_1"
  114. mapset_path = resolve_mapset_path(
  115. path=path, location=location_name, mapset=mapset_name
  116. )
  117. self.assertEqual(mapset_path.directory, str(Path(path).resolve()))
  118. self.assertEqual(mapset_path.location, location_name)
  119. self.assertEqual(mapset_path.mapset, mapset_name)
  120. self.assertEqual(
  121. mapset_path.path, Path(path).resolve() / location_name / mapset_name
  122. )
  123. def test_mapset_from_path(self):
  124. """Check that a non-existing path is correctly parsed."""
  125. path = "does/not/exist/"
  126. location_name = "test_location_A"
  127. mapset_name = "test_mapset_1"
  128. full_path = str(Path(path) / location_name / mapset_name)
  129. mapset_path = resolve_mapset_path(path=full_path)
  130. self.assertEqual(mapset_path.directory, str(Path(path).resolve()))
  131. self.assertEqual(mapset_path.location, location_name)
  132. self.assertEqual(mapset_path.mapset, mapset_name)
  133. self.assertEqual(
  134. mapset_path.path, Path(path).resolve() / location_name / mapset_name
  135. )
  136. if __name__ == "__main__":
  137. test()