test_g_search_modules.py 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879
  1. """
  2. TEST: test_g.search.modules.py
  3. AUTHOR(S): Jachym Cepicky <jachym.cepicky gmail com>
  4. PURPOSE: Test g.search.modules script outputs
  5. COPYRIGHT: (C) 2015 Jachym Cepicky, and by the GRASS Development Team
  6. This program is free software under the GNU General Public
  7. License (>=v2). Read the file COPYING that comes with GRASS
  8. for details.
  9. """
  10. from grass.gunittest.case import TestCase
  11. from grass.gunittest.main import test
  12. from grass.gunittest.gmodules import SimpleModule
  13. from grass.script.utils import decode
  14. import unittest
  15. try:
  16. has_termcolor = True
  17. import termcolor
  18. except ImportError:
  19. has_termcolor = False
  20. class TestSearchModule(TestCase):
  21. def test_all_output(self):
  22. module = SimpleModule('g.search.modules', flags="g")
  23. self.assertModule(module)
  24. stdout = decode(module.outputs.stdout).split()
  25. # we expect at least 100 hits since we search for all modules
  26. assert(len(stdout) > 100)
  27. def test_terminal_output(self):
  28. """ """
  29. module = SimpleModule('g.search.modules', keyword="water")
  30. self.assertModule(module)
  31. stdout = decode(module.outputs.stdout)
  32. self.assertEqual(stdout.split()[0], 'r.basins.fill')
  33. def test_json_output(self):
  34. import json
  35. module = SimpleModule('g.search.modules', keyword="water", flags="j")
  36. self.assertModule(module)
  37. stdout = json.loads(decode(module.outputs.stdout))
  38. self.assertEqual(len(stdout), 6, 'Six modules found')
  39. self.assertEqual(stdout[3]['name'], 'r.water.outlet', 'r.water.outlet')
  40. self.assertTrue('keywords' in stdout[3]['attributes'])
  41. def test_shell_output(self):
  42. module = SimpleModule('g.search.modules', keyword="water", flags="g")
  43. self.assertModule(module)
  44. stdout = decode(module.outputs.stdout).split()
  45. self.assertEqual(len(stdout), 6)
  46. self.assertEqual(stdout[3], 'r.water.outlet')
  47. @unittest.skipUnless(has_termcolor,
  48. "not supported in this library version")
  49. def test_colored_terminal(self):
  50. module = SimpleModule('g.search.modules', keyword="water", flags="c")
  51. self.assertModule(module)
  52. stdout = decode(module.outputs.stdout).split()
  53. self.assertEqual(stdout[0],
  54. termcolor.colored('r.basins.fill',
  55. attrs=['bold']))
  56. def test_manual_pages(self):
  57. module = SimpleModule('g.search.modules', keyword="kapri", flags="gm")
  58. self.assertModule(module)
  59. stdout = decode(module.outputs.stdout).split()
  60. self.assertEqual(len(stdout), 2)
  61. if __name__ == '__main__':
  62. test()