db.test.py 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182
  1. #!/usr/bin/env python
  2. ############################################################################
  3. #
  4. # MODULE: db.test
  5. # AUTHOR(S): Radim Blazek
  6. # Converted to Python by Glynn Clements
  7. # PURPOSE: Test database driver
  8. # COPYRIGHT: (C) 2004 by the GRASS Development Team
  9. #
  10. # This program is free software under the GNU General Public
  11. # License (version 2). Read the file COPYING that comes with GRASS
  12. # for details.
  13. #
  14. #############################################################################
  15. #% Module
  16. #% description: Test database driver, database must exist and set by db.connect.
  17. #% keywords: database, attribute table
  18. #% End
  19. #% option
  20. #% key: test
  21. #% type: string
  22. #% description: Test name
  23. #% required : yes
  24. #% options : test1
  25. #% end
  26. import sys
  27. import os
  28. from grass.script import core as grass
  29. def main():
  30. test_file = options['test']
  31. expected = grass.tempfile()
  32. result = grass.tempfile()
  33. infile = os.path.join(os.environ['GISBASE'], 'etc', 'db.test', test_file)
  34. inf = file(infile)
  35. while True:
  36. type = inf.readline()
  37. if not type:
  38. break
  39. type = type.rstrip('\r\n')
  40. sql = inf.readline().rstrip('\r\n')
  41. sys.stdout.write(sql + '\n')
  42. # Copy expected result to temp file
  43. if type == 'X':
  44. r = grass.write_command('db.execute', stdin = sql + '\n')
  45. else:
  46. resf = file(result, 'w')
  47. r = grass.write_command('db.select', flags = 'c', stdin = sql + '\n', stdout = resf)
  48. resf.close()
  49. if r != 0:
  50. grass.error("EXECUTE: ******** ERROR ********")
  51. else:
  52. grass.message("EXECUTE: OK")
  53. expf = file(expected, 'w')
  54. while True:
  55. res = inf.readline().rstrip('\r\n')
  56. if not res:
  57. break
  58. expf.write(res + '\n')
  59. expf.close()
  60. if type == 'S':
  61. if grass.call(['diff', result, expected]) != 0:
  62. grass.error("RESULT: ******** ERROR ********")
  63. else:
  64. grass.message("RESULT: OK")
  65. if __name__ == "__main__":
  66. options, flags = grass.parser()
  67. main()